@@ -66,15 +66,20 @@ class HtmlParser extends StatelessWidget {
6666
6767 @override
6868 Widget build (BuildContext context) {
69+ Map <String , Map <String , List <css.Expression >>> declarations = _getExternalCSSDeclarations (htmlData.getElementsByTagName ("style" ));
6970 StyledElement lexedTree = lexDomTree (
7071 htmlData,
7172 customRender.keys.toList (),
7273 tagsList,
7374 navigationDelegateForIframe,
7475 );
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);
7883 StyledElement cleanedTree = cleanTree (cascadedStyledTree);
7984 InlineSpan parsedTree = parseTree (
8085 RenderContext (
@@ -189,34 +194,59 @@ class HtmlParser extends StatelessWidget {
189194 }
190195 }
191196
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) {
193223 if (tree.attributes.containsKey ("style" )) {
194224 tree.style = tree.style.merge (inlineCSSToStyle (tree.attributes['style' ]));
195225 }
196226
197- tree.children.forEach (applyInlineStyles );
227+ tree.children.forEach (_applyInlineStyles );
198228 return tree;
199229 }
200230
201231 /// [applyCustomStyles] applies the [Style] objects passed into the [Html]
202232 /// 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) {
204234 style.forEach ((key, style) {
205235 if (tree.matchesSelector (key)) {
206236 tree.style = tree.style.merge (style);
207237 }
208238 });
209- tree.children.forEach (_applyCustomStyles);
239+ tree.children.forEach ((e) => _applyCustomStyles (style, e) );
210240
211241 return tree;
212242 }
213243
214244 /// [_cascadeStyles] cascades all of the inherited styles down the tree, applying them to each
215245 /// child that doesn't specify a different style.
216- StyledElement _cascadeStyles (StyledElement tree) {
246+ static StyledElement _cascadeStyles (Map < String , Style > style, StyledElement tree) {
217247 tree.children.forEach ((child) {
218248 child.style = tree.style.copyOnlyInherited (child.style);
219- _cascadeStyles (child);
249+ _cascadeStyles (style, child);
220250 });
221251
222252 return tree;
0 commit comments