@@ -28,6 +28,10 @@ typedef OnMathError = Widget Function(
2828 String exception,
2929 String exceptionWithType,
3030);
31+ typedef OnCSSParseError = String ? Function (
32+ String css,
33+ List <cssparser.Message > errors,
34+ );
3135typedef CustomRender = dynamic Function (
3236 RenderContext context,
3337 Widget parsedChild,
@@ -38,6 +42,7 @@ class HtmlParser extends StatelessWidget {
3842 final dom.Document htmlData;
3943 final OnTap ? onLinkTap;
4044 final OnTap ? onImageTap;
45+ final OnCSSParseError ? onCSSParseError;
4146 final ImageErrorListener ? onImageError;
4247 final OnMathError ? onMathError;
4348 final bool shrinkWrap;
@@ -54,6 +59,7 @@ class HtmlParser extends StatelessWidget {
5459 required this .htmlData,
5560 required this .onLinkTap,
5661 required this .onImageTap,
62+ required this .onCSSParseError,
5763 required this .onImageError,
5864 required this .onMathError,
5965 required this .shrinkWrap,
@@ -66,7 +72,7 @@ class HtmlParser extends StatelessWidget {
6672
6773 @override
6874 Widget build (BuildContext context) {
69- Map <String , Map <String , List <css.Expression >>> declarations = _getExternalCSSDeclarations (htmlData.getElementsByTagName ("style" ));
75+ Map <String , Map <String , List <css.Expression >>> declarations = _getExternalCSSDeclarations (htmlData.getElementsByTagName ("style" ), onCSSParseError );
7076 StyledElement lexedTree = lexDomTree (
7177 htmlData,
7278 customRender.keys.toList (),
@@ -77,7 +83,7 @@ class HtmlParser extends StatelessWidget {
7783 if (declarations.isNotEmpty) {
7884 externalCSSStyledTree = _applyExternalCSS (declarations, lexedTree);
7985 }
80- StyledElement inlineStyledTree = _applyInlineStyles (externalCSSStyledTree ?? lexedTree);
86+ StyledElement inlineStyledTree = _applyInlineStyles (externalCSSStyledTree ?? lexedTree, onCSSParseError );
8187 StyledElement customStyledTree = _applyCustomStyles (style, inlineStyledTree);
8288 StyledElement cascadedStyledTree = _cascadeStyles (style, customStyledTree);
8389 StyledElement cleanedTree = cleanTree (cascadedStyledTree);
@@ -194,13 +200,13 @@ class HtmlParser extends StatelessWidget {
194200 }
195201 }
196202
197- static Map <String , Map <String , List <css.Expression >>> _getExternalCSSDeclarations (List <dom.Element > styles) {
203+ static Map <String , Map <String , List <css.Expression >>> _getExternalCSSDeclarations (List <dom.Element > styles, OnCSSParseError ? errorHandler ) {
198204 String fullCSS = "" ;
199205 for (final e in styles) {
200206 fullCSS = fullCSS + e.innerHtml;
201207 }
202208 if (fullCSS.isNotEmpty) {
203- final declarations = parseExternalCSS (fullCSS);
209+ final declarations = parseExternalCSS (fullCSS, errorHandler );
204210 return declarations;
205211 } else {
206212 return {};
@@ -219,12 +225,15 @@ class HtmlParser extends StatelessWidget {
219225 return tree;
220226 }
221227
222- static StyledElement _applyInlineStyles (StyledElement tree) {
228+ static StyledElement _applyInlineStyles (StyledElement tree, OnCSSParseError ? errorHandler ) {
223229 if (tree.attributes.containsKey ("style" )) {
224- tree.style = tree.style.merge (inlineCSSToStyle (tree.attributes['style' ]));
230+ final newStyle = inlineCSSToStyle (tree.attributes['style' ], errorHandler);
231+ if (newStyle != null ) {
232+ tree.style = tree.style.merge (newStyle);
233+ }
225234 }
226235
227- tree.children.forEach (_applyInlineStyles);
236+ tree.children.forEach ((e) => _applyInlineStyles (e, errorHandler) );
228237 return tree;
229238 }
230239
0 commit comments