Skip to content

Commit cfbc4f6

Browse files
committed
Add support for the font-size inline style
1 parent e5e24d0 commit cfbc4f6

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

lib/src/css_parser.dart

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ Style declarationsToStyle(Map<String, List<css.Expression>> declarations) {
1818
case 'text-align':
1919
style.textAlign = ExpressionMapping.expressionToTextAlign(value.first);
2020
break;
21-
21+
case 'font-size':
22+
style.fontSize = ExpressionMapping.expressionToFontSize(value.first);
23+
break;
2224
}
2325
});
2426
return style;
@@ -70,6 +72,37 @@ class ExpressionMapping {
7072
return null;
7173
}
7274

75+
static FontSize expressionToFontSize(css.Expression value) {
76+
if (value is css.NumberTerm) {
77+
return FontSize(double.tryParse(value.text));
78+
} else if (value is css.PercentageTerm) {
79+
return FontSize.percent(int.tryParse(value.text.replaceAll(new RegExp(r'[^0-9]'),'')));
80+
} else if (value is css.EmTerm) {
81+
return FontSize(double.tryParse(value.text.replaceAll(new RegExp(r'[^0-9]'), '')));
82+
} else if (value is css.RemTerm) {
83+
return FontSize(double.tryParse(value.text.replaceAll(new RegExp(r'[^0-9]'), '')));
84+
} else if (value is css.LiteralTerm) {
85+
switch (value.text) {
86+
case "xx-small":
87+
return FontSize.xxSmall;
88+
case "x-small":
89+
return FontSize.xSmall;
90+
case "small":
91+
return FontSize.small;
92+
case "medium":
93+
return FontSize.medium;
94+
case "large":
95+
return FontSize.large;
96+
case "x-large":
97+
return FontSize.xLarge;
98+
case "xx-large":
99+
return FontSize.xxLarge;
100+
}
101+
return FontSize(double.tryParse(value.text.replaceAll(new RegExp(r'[^0-9]'), '')));
102+
}
103+
return null;
104+
}
105+
73106
static Color stringToColor(String _text) {
74107
var text = _text.replaceFirst('#', '');
75108
if (text.length == 3)

lib/style.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,9 @@ class FontSize {
409409
return FontSize(percent.toDouble() / -100.0);
410410
}
411411

412+
factory FontSize.em(int em) {
413+
return FontSize(em.toDouble() * 16 - 2);
414+
}
412415
// These values are calculated based off of the default (`medium`)
413416
// being 14px.
414417
//

0 commit comments

Comments
 (0)