Skip to content

Commit 4cc782d

Browse files
committed
Add support for inline direction, display, font-family, font-style, font-weight
1 parent f98fa6d commit 4cc782d

File tree

3 files changed

+105
-4
lines changed

3 files changed

+105
-4
lines changed

lib/html_parser.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,8 @@ class HtmlParser extends StatelessWidget {
661661
child.text.trim().isEmpty &&
662662
lastChildBlock) {
663663
toRemove.add(child);
664+
} else if (child.style.display == Display.NONE) {
665+
toRemove.add(child);
664666
} else {
665667
_removeEmptyElements(child);
666668
}

lib/src/css_parser.dart

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,32 @@ Style declarationsToStyle(Map<String, List<css.Expression>> declarations) {
99
declarations.forEach((property, value) {
1010
switch (property) {
1111
case 'background-color':
12-
style.backgroundColor =
13-
ExpressionMapping.expressionToColor(value.first);
12+
style.backgroundColor = ExpressionMapping.expressionToColor(value.first);
1413
break;
1514
case 'color':
1615
style.color = ExpressionMapping.expressionToColor(value.first);
1716
break;
18-
case 'text-align':
19-
style.textAlign = ExpressionMapping.expressionToTextAlign(value.first);
17+
case 'direction':
18+
style.direction = ExpressionMapping.expressionToDirection(value.first);
19+
break;
20+
case 'display':
21+
style.display = ExpressionMapping.expressionToDisplay(value.first);
22+
break;
23+
case 'font-family':
24+
style.fontFamily = ExpressionMapping.expressionToFontFamily(value.first);
2025
break;
2126
case 'font-size':
2227
style.fontSize = ExpressionMapping.expressionToFontSize(value.first);
2328
break;
29+
case 'font-style':
30+
style.fontStyle = ExpressionMapping.expressionToFontStyle(value.first);
31+
break;
32+
case 'font-weight':
33+
style.fontWeight = ExpressionMapping.expressionToFontWeight(value.first);
34+
break;
35+
case 'text-align':
36+
style.textAlign = ExpressionMapping.expressionToTextAlign(value.first);
37+
break;
2438
}
2539
});
2640
return style;
@@ -72,6 +86,36 @@ class ExpressionMapping {
7286
return null;
7387
}
7488

89+
static TextDirection expressionToDirection(css.Expression value) {
90+
if (value is css.LiteralTerm) {
91+
switch(value.text) {
92+
case "ltr":
93+
return TextDirection.ltr;
94+
case "rtl":
95+
return TextDirection.rtl;
96+
}
97+
}
98+
return TextDirection.ltr;
99+
}
100+
101+
static Display expressionToDisplay(css.Expression value) {
102+
if (value is css.LiteralTerm) {
103+
switch(value.text) {
104+
case 'block':
105+
return Display.BLOCK;
106+
case 'inline-block':
107+
return Display.INLINE_BLOCK;
108+
case 'inline':
109+
return Display.INLINE;
110+
case 'list-item':
111+
return Display.LIST_ITEM;
112+
case 'none':
113+
return Display.NONE;
114+
}
115+
}
116+
return Display.INLINE;
117+
}
118+
75119
static FontSize expressionToFontSize(css.Expression value) {
76120
if (value is css.NumberTerm) {
77121
return FontSize(double.tryParse(value.text), "");
@@ -103,6 +147,59 @@ class ExpressionMapping {
103147
return null;
104148
}
105149

150+
static FontStyle expressionToFontStyle(css.Expression value) {
151+
if (value is css.LiteralTerm) {
152+
switch(value.text) {
153+
case "italic":
154+
case "oblique":
155+
return FontStyle.italic;
156+
}
157+
return FontStyle.normal;
158+
}
159+
return FontStyle.normal;
160+
}
161+
162+
static FontWeight expressionToFontWeight(css.Expression value) {
163+
if (value is css.NumberTerm) {
164+
switch (value.text) {
165+
case "100":
166+
return FontWeight.w100;
167+
case "200":
168+
return FontWeight.w200;
169+
case "300":
170+
return FontWeight.w300;
171+
case "400":
172+
return FontWeight.w400;
173+
case "500":
174+
return FontWeight.w500;
175+
case "600":
176+
return FontWeight.w600;
177+
case "700":
178+
return FontWeight.w700;
179+
case "800":
180+
return FontWeight.w800;
181+
case "900":
182+
return FontWeight.w900;
183+
}
184+
} else if (value is css.LiteralTerm) {
185+
switch(value.text) {
186+
case "bold":
187+
return FontWeight.bold;
188+
case "bolder":
189+
return FontWeight.w900;
190+
case "lighter":
191+
return FontWeight.w200;
192+
}
193+
return FontWeight.normal;
194+
}
195+
return FontWeight.normal;
196+
}
197+
198+
static String expressionToFontFamily(css.Expression value) {
199+
if (value is css.LiteralTerm) return value.text;
200+
return null;
201+
}
202+
106203
static Color stringToColor(String _text) {
107204
var text = _text.replaceFirst('#', '');
108205
if (text.length == 3)

lib/style.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ class Style {
289289
return child.copyWith(
290290
color: child.color ?? color,
291291
direction: child.direction ?? direction,
292+
display: display == Display.NONE ? display : child.display,
292293
fontFamily: child.fontFamily ?? fontFamily,
293294
fontFeatureSettings: child.fontFeatureSettings ?? fontFeatureSettings,
294295
fontSize: child.fontSize != null ?
@@ -401,6 +402,7 @@ enum Display {
401402
INLINE,
402403
INLINE_BLOCK,
403404
LIST_ITEM,
405+
NONE,
404406
}
405407

406408
class FontSize {

0 commit comments

Comments
 (0)