Skip to content

Commit f98fa6d

Browse files
committed
Fix % font size transferring to child rather than the font size itself, implement em support relative to parent font size
1 parent 7c1644f commit f98fa6d

File tree

4 files changed

+34
-24
lines changed

4 files changed

+34
-24
lines changed

lib/html_parser.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ class HtmlParser extends StatelessWidget {
683683
tree.children?.forEach((child) {
684684
if ((child.style.fontSize?.size ?? parentFontSize) < 0) {
685685
child.style.fontSize =
686-
FontSize(parentFontSize * -child.style.fontSize.size);
686+
FontSize(parentFontSize * -child.style.fontSize.size, "");
687687
}
688688

689689
_processFontSize(child);

lib/src/css_parser.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ class ExpressionMapping {
7474

7575
static FontSize expressionToFontSize(css.Expression value) {
7676
if (value is css.NumberTerm) {
77-
return FontSize(double.tryParse(value.text));
77+
return FontSize(double.tryParse(value.text), "");
7878
} else if (value is css.PercentageTerm) {
79-
return FontSize.percent(int.tryParse(value.text.replaceAll(new RegExp(r'[^0-9]'),'')));
79+
return FontSize.percent(int.tryParse(value.text));
8080
} else if (value is css.EmTerm) {
81-
return FontSize.em(int.tryParse(value.text.replaceAll(new RegExp(r'[^0-9]'), '')));
81+
return FontSize.em(double.tryParse(value.text));
8282
} else if (value is css.RemTerm) {
83-
return FontSize.em(int.tryParse(value.text.replaceAll(new RegExp(r'[^0-9]'), '')));
83+
return FontSize.rem(double.tryParse(value.text));
8484
} else if (value is css.LiteralTerm) {
8585
switch (value.text) {
8686
case "xx-small":
@@ -98,7 +98,7 @@ class ExpressionMapping {
9898
case "xx-large":
9999
return FontSize.xxLarge;
100100
}
101-
return FontSize(double.tryParse(value.text.replaceAll(new RegExp(r'[^0-9]'), '')));
101+
return FontSize(double.tryParse(value.text.replaceAll(new RegExp(r'\s+(\d+\.\d+)\s+'), '')), "");
102102
}
103103
return null;
104104
}

lib/src/styled_element.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ StyledElement parseStyledElement(
196196
break;
197197
case "h3":
198198
styledElement.style = Style(
199-
fontSize: FontSize(16.38),
199+
fontSize: FontSize(16.38, ""),
200200
fontWeight: FontWeight.bold,
201201
margin: EdgeInsets.symmetric(vertical: 16.5),
202202
display: Display.BLOCK,
@@ -212,15 +212,15 @@ StyledElement parseStyledElement(
212212
break;
213213
case "h5":
214214
styledElement.style = Style(
215-
fontSize: FontSize(11.62),
215+
fontSize: FontSize(11.62, ""),
216216
fontWeight: FontWeight.bold,
217217
margin: EdgeInsets.symmetric(vertical: 19.25),
218218
display: Display.BLOCK,
219219
);
220220
break;
221221
case "h6":
222222
styledElement.style = Style(
223-
fontSize: FontSize(9.38),
223+
fontSize: FontSize(9.38, ""),
224224
fontWeight: FontWeight.bold,
225225
margin: EdgeInsets.symmetric(vertical: 22),
226226
display: Display.BLOCK,

lib/style.dart

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,11 @@ class Style {
291291
direction: child.direction ?? direction,
292292
fontFamily: child.fontFamily ?? fontFamily,
293293
fontFeatureSettings: child.fontFeatureSettings ?? fontFeatureSettings,
294-
fontSize: child.fontSize ?? fontSize,
294+
fontSize: child.fontSize != null ?
295+
fontSize != null && child.fontSize?.units == "em" ?
296+
FontSize(child.fontSize.size * fontSize.size, "") : child.fontSize
297+
: fontSize != null && fontSize.size < 0 ?
298+
FontSize.percent(100) : fontSize,
295299
fontStyle: child.fontStyle ?? fontStyle,
296300
fontWeight: child.fontWeight ?? fontWeight,
297301
letterSpacing: child.letterSpacing ?? letterSpacing,
@@ -382,7 +386,7 @@ class Style {
382386
this.textDecorationThickness = textStyle.decorationThickness;
383387
this.fontFamily = textStyle.fontFamily;
384388
this.fontFeatureSettings = textStyle.fontFeatures;
385-
this.fontSize = FontSize(textStyle.fontSize);
389+
this.fontSize = FontSize(textStyle.fontSize, "");
386390
this.fontStyle = textStyle.fontStyle;
387391
this.fontWeight = textStyle.fontWeight;
388392
this.letterSpacing = textStyle.letterSpacing;
@@ -401,16 +405,22 @@ enum Display {
401405

402406
class FontSize {
403407
final double size;
408+
final String units;
404409

405-
const FontSize(this.size);
410+
const FontSize(this.size, this.units);
406411

407412
/// A percentage of the parent style's font size.
408413
factory FontSize.percent(int percent) {
409-
return FontSize(percent.toDouble() / -100.0);
414+
return FontSize(percent.toDouble() / -100.0, "%");
410415
}
411416

412-
factory FontSize.em(int em) {
413-
return FontSize(em.toDouble() * 16 - 2);
417+
factory FontSize.em(double em) {
418+
return FontSize(em, "em");
419+
}
420+
421+
factory FontSize.rem(double rem) {
422+
print(rem * 16 - 2);
423+
return FontSize(rem * 16 - 2, "rem");
414424
}
415425
// These values are calculated based off of the default (`medium`)
416426
// being 14px.
@@ -419,15 +429,15 @@ class FontSize {
419429
//
420430
// Negative values are computed during parsing to be a percentage of
421431
// the parent style's font size.
422-
static const xxSmall = FontSize(7.875);
423-
static const xSmall = FontSize(8.75);
424-
static const small = FontSize(11.375);
425-
static const medium = FontSize(14.0);
426-
static const large = FontSize(15.75);
427-
static const xLarge = FontSize(21.0);
428-
static const xxLarge = FontSize(28.0);
429-
static const smaller = FontSize(-0.83);
430-
static const larger = FontSize(-1.2);
432+
static const xxSmall = FontSize(7.875, "");
433+
static const xSmall = FontSize(8.75, "");
434+
static const small = FontSize(11.375, "");
435+
static const medium = FontSize(14.0, "");
436+
static const large = FontSize(15.75, "");
437+
static const xLarge = FontSize(21.0, "");
438+
static const xxLarge = FontSize(28.0, "");
439+
static const smaller = FontSize(-0.83, "");
440+
static const larger = FontSize(-1.2, "");
431441
}
432442

433443
enum ListStyleType {

0 commit comments

Comments
 (0)