Skip to content

Commit c3f17cb

Browse files
committed
Fix inheritance of relative font-sizes.
In particular, get the nexw 'late inheritance' test passing in inherit.t
1 parent 8a6e701 commit c3f17cb

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

Changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{{$NEXT}}
2+
- Fix inheritance relative font-size calculations
23

34
0.10.0 2025-04-29T08:40:14+12:00
45
- Handle Pairs, List, Hash methods

lib/CSS/Properties.rakumod

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ Options:
178178

179179
my @decls = self!build-declarations(@style);
180180
with $inherit -> CSS::Properties() $_ {
181-
$!calc.em = .em;
182181
self.inherit: $_;
183182
}
184183

@@ -613,6 +612,12 @@ multi sub to-ast($v, :$get = True) is default {
613612

614613
#| CSS conformant inheritance from the given parent declaration list.
615614
method inherit(CSS::Properties:D() $css) {
615+
616+
$!calc.em = $css.measure: :font-size;
617+
# font-size may be relative to parent, e.g. '.5em' or '85%'
618+
$!calc.em = self.measure: :font-size($_)
619+
with %!values<font-size>;
620+
616621
for $css.properties -> \name {
617622
# skip unknown extension properties
618623
next if name.starts-with('-') && !self.prop-num(name).defined;
@@ -628,10 +633,7 @@ method inherit(CSS::Properties:D() $css) {
628633
$inherit = True without %!values{name};
629634
}
630635
if $inherit {
631-
my $val := $css.computed(name);
632-
$!calc.em = $val
633-
if name eq 'font-size' && !%!values{name}.defined;
634-
%!values{name} //= $val;
636+
%!values{name} = $css.computed(name);
635637
}
636638
}
637639
}

lib/CSS/Properties/Calculator.rakumod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ class CSS::Properties::Calculator {
128128
when Bool { CSS::Units.value($!em, $!units) }
129129
default { %Compute<font-size>(self, $_) }
130130
}
131+
131132
multi method measure(:$ref = 0, *%misc where .elems == 1) {
132133
my ($prop, $value) = %misc.kv;
133134
given $value {

t/css-fonts.t

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use v6;
22
use Test;
3-
plan 3;
3+
plan 4;
44
use CSS::Properties;
55

66
my $style = 'font:italic bold 10pt/12pt times-roman;';
@@ -41,4 +41,10 @@ subtest 'issue#23', {
4141
is $css.clone.measure(:font-size), $css.measure(:font-size);
4242
}
4343

44+
subtest 'change em', {
45+
my CSS::Properties $css .= new: :style("font-size:.75em");
46+
is $css.em, 9;
47+
is $css.measure(:font-size), 9;
48+
}
49+
4450
done-testing;

t/inherit.t

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use v6;
22
use Test;
3-
plan 21;
3+
plan 23;
44
use CSS::Properties;
55

66
my $inherit = CSS::Properties.new: :style("margin-top:5pt; margin-right: 10pt; margin-left: 15pt; margin-bottom: 20pt; color:rgb(0,0,255)!important");
@@ -103,4 +103,22 @@ subtest 'issue#11 inheritence', {
103103
is $child, $style;
104104
}
105105

106+
subtest 'early inheritence', {
107+
my CSS::Properties() $inherit = 'font-size:20pt';
108+
my CSS::Properties $css .= new: :style('border: .5em solid gray; font-size:.5em; margin:.5em'), :$inherit;
109+
is $css.measure(:font-size), 10;
110+
is $css.measure(:border-top-width), 5;
111+
is $css.measure(:margin-top), 5;
112+
}
113+
114+
subtest 'late inheritance', {
115+
my CSS::Properties() $inherit = 'font-size:20pt';
116+
my CSS::Properties $css .= new: :style('border: .5em solid gray; font-size:.5em; margin:.5em');
117+
$css.inherit($inherit);
118+
is $css.em, 10;
119+
is $css.measure(:font-size), 10;
120+
is $css.measure(:border-top-width), 5;
121+
is $css.measure(:margin-top), 5;
122+
}
123+
106124
done-testing;

0 commit comments

Comments
 (0)