@@ -66,15 +66,20 @@ class HtmlParser extends StatelessWidget {
66
66
67
67
@override
68
68
Widget build (BuildContext context) {
69
+ Map <String , Map <String , List <css.Expression >>> declarations = _getExternalCSSDeclarations (htmlData.getElementsByTagName ("style" ));
69
70
StyledElement lexedTree = lexDomTree (
70
71
htmlData,
71
72
customRender.keys.toList (),
72
73
tagsList,
73
74
navigationDelegateForIframe,
74
75
);
75
- StyledElement inlineStyledTree = applyInlineStyles (lexedTree);
76
- StyledElement customStyledTree = _applyCustomStyles (inlineStyledTree);
77
- StyledElement cascadedStyledTree = _cascadeStyles (customStyledTree);
76
+ StyledElement ? externalCSSStyledTree;
77
+ if (declarations.isNotEmpty) {
78
+ externalCSSStyledTree = _applyExternalCSS (declarations, lexedTree);
79
+ }
80
+ StyledElement inlineStyledTree = _applyInlineStyles (externalCSSStyledTree ?? lexedTree);
81
+ StyledElement customStyledTree = _applyCustomStyles (style, inlineStyledTree);
82
+ StyledElement cascadedStyledTree = _cascadeStyles (style, customStyledTree);
78
83
StyledElement cleanedTree = cleanTree (cascadedStyledTree);
79
84
InlineSpan parsedTree = parseTree (
80
85
RenderContext (
@@ -189,34 +194,59 @@ class HtmlParser extends StatelessWidget {
189
194
}
190
195
}
191
196
192
- static StyledElement applyInlineStyles (StyledElement tree) {
197
+ static Map <String , Map <String , List <css.Expression >>> _getExternalCSSDeclarations (List <dom.Element > styles) {
198
+ String fullCSS = "" ;
199
+ for (final e in styles) {
200
+ fullCSS = fullCSS + e.innerHtml;
201
+ }
202
+ if (fullCSS.isNotEmpty) {
203
+ final declarations = parseExternalCSS (fullCSS);
204
+ return declarations;
205
+ } else {
206
+ return {};
207
+ }
208
+ }
209
+
210
+ static StyledElement _applyExternalCSS (Map <String , Map <String , List <css.Expression >>> declarations, StyledElement tree) {
211
+ declarations.forEach ((key, style) {
212
+ if (tree.matchesSelector (key)) {
213
+ tree.style = tree.style.merge (declarationsToStyle (style));
214
+ }
215
+ });
216
+
217
+ tree.children.forEach ((e) => _applyExternalCSS (declarations, e));
218
+
219
+ return tree;
220
+ }
221
+
222
+ static StyledElement _applyInlineStyles (StyledElement tree) {
193
223
if (tree.attributes.containsKey ("style" )) {
194
224
tree.style = tree.style.merge (inlineCSSToStyle (tree.attributes['style' ]));
195
225
}
196
226
197
- tree.children.forEach (applyInlineStyles );
227
+ tree.children.forEach (_applyInlineStyles );
198
228
return tree;
199
229
}
200
230
201
231
/// [applyCustomStyles] applies the [Style] objects passed into the [Html]
202
232
/// widget onto the [StyledElement] tree, no cascading of styles is done at this point.
203
- StyledElement _applyCustomStyles (StyledElement tree) {
233
+ static StyledElement _applyCustomStyles (Map < String , Style > style, StyledElement tree) {
204
234
style.forEach ((key, style) {
205
235
if (tree.matchesSelector (key)) {
206
236
tree.style = tree.style.merge (style);
207
237
}
208
238
});
209
- tree.children.forEach (_applyCustomStyles);
239
+ tree.children.forEach ((e) => _applyCustomStyles (style, e) );
210
240
211
241
return tree;
212
242
}
213
243
214
244
/// [_cascadeStyles] cascades all of the inherited styles down the tree, applying them to each
215
245
/// child that doesn't specify a different style.
216
- StyledElement _cascadeStyles (StyledElement tree) {
246
+ static StyledElement _cascadeStyles (Map < String , Style > style, StyledElement tree) {
217
247
tree.children.forEach ((child) {
218
248
child.style = tree.style.copyOnlyInherited (child.style);
219
- _cascadeStyles (child);
249
+ _cascadeStyles (style, child);
220
250
});
221
251
222
252
return tree;
0 commit comments