Skip to content

Commit 8c63812

Browse files
committed
Add Pairs, List, Hash methods
1 parent e5b40d8 commit 8c63812

File tree

6 files changed

+67
-8
lines changed

6 files changed

+67
-8
lines changed

Changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{{$NEXT}}
2+
- Handle Pairs, List, Hash methods
23

34
0.9.7 2024-09-04T10:56:52+12:00
45
- Fix setting of 'em' in cloning and copying #23

docs/CSS/Properties.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,17 @@ Options:
138138
### measure
139139

140140
```raku
141-
# Converts a value to a numeric quantity;
141+
# Converts values to a normalized numeric quantities;
142142
my Numeric $font-size = $css.measure: :font-size; # get current font size
143143
$font-size = $css.measure: :font-size<smaller>; # compute a smaller font
144144
$font-size = $css.measure: :font-size(120%); # compute a larger font
145145
my $weight = $css.measure: :font-weight; # get current font weight 100..900
146+
my $height = $css.measure: :height<10px>; # get height in base units
146147
$weight = $css.measure: :font-weight<bold>; # compute bold font weight
147148
```
148149

150+
All length values are converted to the current base units (default `pt`).
151+
149152
This function is implemented for `font-size`, `font-weight`, `letter-spacing`, `line-height`, and `word-spacing`.
150153

151154
It also works for box related properties: `width`, `height`, `{min|max}-{width|height}`, `border-{top|right|bottom|left}-width`, and `{padding|margin}-{top|right|bottom|left}`. The `reference-width` attribute represents the width of a containing element; which needs to set for correct calculation of percentage box related quantities:
@@ -235,7 +238,7 @@ method clone
235238
------------
236239

237240
```raku
238-
method clone(@decls, *%opts) returns CSS::Properties
241+
method clone(@decls, *%opts) returns CSS::Properties
239242
```
240243

241244
Creates a deep copy of a CSS declarations object
@@ -307,6 +310,16 @@ method delete(
307310

308311
delete property values from the list of populated properties
309312

313+
### Pairs, List, Array, Hash, Slip
314+
315+
Pairs, Lists and Hashes are all supported as pairs of leaf properties and measured values.
316+
317+
```raku
318+
use CSS::Properties;
319+
my CSS::Properties() $css = "color:red; padding: 1pt";
320+
dd $css.Pairs;
321+
```
322+
310323
### method property
311324

312325
```raku

docs/index.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,21 @@ use CSS::Units :pt;
1212
use CSS::Properties;
1313
1414
my CSS::Properties() $css = "color:red !important; padding: 1pt";
15+
16+
say $css.color.hex; # (FF 00 00)
1517
say $css.important("color"); # True
16-
$css.border-color = 'red';
1718
19+
$css.border-color = 'blue';
20+
21+
# set all four margins individually (top, left, bottom, right)
1822
$css.margin = [5pt, 2pt, 5pt, 2pt];
19-
$css.margin = 5pt; # set margin on all 4 sides
23+
# set a common margin on all four sides
24+
$css.margin = 5pt;
2025
2126
# set text alignment
2227
$css.text-align = 'right';
2328
24-
say ~$css; # border-color:red; color:red!important; margin:5pt; padding:1pt; text-align:right;
29+
say ~$css; # border-color:blue; color:red!important; margin:5pt; padding:1pt; text-align:right;
2530
```
2631

2732
Classes in this module

lib/CSS/Properties.rakumod

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,17 @@ Options:
190190
=begin pod
191191
=head3 measure
192192
=begin code :lang<raku>
193-
# Converts a value to a numeric quantity;
193+
# Converts values to a normalized numeric quantities;
194194
my Numeric $font-size = $css.measure: :font-size; # get current font size
195195
$font-size = $css.measure: :font-size<smaller>; # compute a smaller font
196196
$font-size = $css.measure: :font-size(120%); # compute a larger font
197197
my $weight = $css.measure: :font-weight; # get current font weight 100..900
198+
my $height = $css.measure: :height<10px>; # get height in base units
198199
$weight = $css.measure: :font-weight<bold>; # compute bold font weight
199200
=end code
200201
202+
All length values are converted to the current base units (default C<pt>).
203+
201204
This function is implemented for `font-size`, `font-weight`, `letter-spacing`, `line-height`, and `word-spacing`.
202205
203206
It also works for box related properties: `width`, `height`, `{min|max}-{width|height}`, `border-{top|right|bottom|left}-width`, and `{padding|margin}-{top|right|bottom|left}`.
@@ -803,6 +806,20 @@ method delete(*@props) {
803806
self;
804807
}
805808

809+
method Pairs is also<Seq> handles<List Array Hash> {
810+
%!values.pairs.sort.map: { .key => $!calc.measure: |$_ }
811+
}
812+
=head3 Pairs, List, Array, Hash, Slip
813+
814+
=para Pairs, Lists and Hashes are all supported as pairs of
815+
leaf properties and measured values.
816+
817+
=begin code :lang<raku>
818+
use CSS::Properties;
819+
my CSS::Properties() $css = "color:red; padding: 1pt";
820+
dd $css.Pairs;
821+
=end code
822+
806823
method dispatch:<.?>(\name, |c) is raw {
807824
self.can(name)
808825
?? self."{name}"(|c)

t/01-property-basic.t

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use v6;
22
use Test;
3-
plan 16;
3+
plan 18;
44

55
use CSS::Properties::PropertyInfo;
66
use CSS::Properties;
77
use CSS::Units :pt, :px;
8+
use CSS::Grammar::Test :&json-eqv;
89

910
my CSS::Properties::PropertyInfo $sample-prop .= new( :name<background-image> );
1011

@@ -36,4 +37,22 @@ is $css2.write, 'height:5px; margin:7pt;', 'write';
3637
$css2.copy($css);
3738
is $css2.write, 'height:5px; margin:5pt; width:4px;', 'copy/write';
3839

40+
cmp-ok $css2.List, &json-eqv, (
41+
:height(15/4),
42+
:margin-bottom(5.0),
43+
:margin-left(5.0),
44+
:margin-right(5.0),
45+
:margin-top(5.0),
46+
:width(3.0)
47+
);
48+
49+
cmp-ok $css2.Hash, &json-eqv, {
50+
:height(15/4),
51+
:margin-bottom(5.0),
52+
:margin-left(5.0),
53+
:margin-right(5.0),
54+
:margin-top(5.0),
55+
:width(3.0)
56+
};
57+
3958
done-testing;

t/measure.t

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use v6;
22
use Test;
3-
plan 28;
3+
plan 30;
44

55
use CSS::Units :pt, :px, :pc, :in, :vw, :vh, :em, :ex, :percent;
66
use CSS::Properties;
@@ -30,6 +30,10 @@ is '%0.2f'.sprintf($css.measure: :font-size<smaller>), '10.00', '$css.measure("s
3030
$css .= new: :style("border-spacing: 3pt .75em");
3131
is $css.measure(:border-spacing), [3.0, 9.0];
3232

33+
$css .= new: :style("height:5px");
34+
is $css.measure(:height), 15/4;
35+
is $css.measure(:height($css.height)), 15/4;
36+
3337
# change base units
3438
$css .= new: :units<pc>;
3539
is $css.units, 'pc', 'changed units';

0 commit comments

Comments
 (0)