@@ -28,6 +28,10 @@ typedef OnMathError = Widget Function(
28
28
String exception,
29
29
String exceptionWithType,
30
30
);
31
+ typedef OnCSSParseError = String ? Function (
32
+ String css,
33
+ List <cssparser.Message > errors,
34
+ );
31
35
typedef CustomRender = dynamic Function (
32
36
RenderContext context,
33
37
Widget parsedChild,
@@ -38,6 +42,7 @@ class HtmlParser extends StatelessWidget {
38
42
final dom.Document htmlData;
39
43
final OnTap ? onLinkTap;
40
44
final OnTap ? onImageTap;
45
+ final OnCSSParseError ? onCSSParseError;
41
46
final ImageErrorListener ? onImageError;
42
47
final OnMathError ? onMathError;
43
48
final bool shrinkWrap;
@@ -54,6 +59,7 @@ class HtmlParser extends StatelessWidget {
54
59
required this .htmlData,
55
60
required this .onLinkTap,
56
61
required this .onImageTap,
62
+ required this .onCSSParseError,
57
63
required this .onImageError,
58
64
required this .onMathError,
59
65
required this .shrinkWrap,
@@ -66,7 +72,7 @@ class HtmlParser extends StatelessWidget {
66
72
67
73
@override
68
74
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 );
70
76
StyledElement lexedTree = lexDomTree (
71
77
htmlData,
72
78
customRender.keys.toList (),
@@ -77,7 +83,7 @@ class HtmlParser extends StatelessWidget {
77
83
if (declarations.isNotEmpty) {
78
84
externalCSSStyledTree = _applyExternalCSS (declarations, lexedTree);
79
85
}
80
- StyledElement inlineStyledTree = _applyInlineStyles (externalCSSStyledTree ?? lexedTree);
86
+ StyledElement inlineStyledTree = _applyInlineStyles (externalCSSStyledTree ?? lexedTree, onCSSParseError );
81
87
StyledElement customStyledTree = _applyCustomStyles (style, inlineStyledTree);
82
88
StyledElement cascadedStyledTree = _cascadeStyles (style, customStyledTree);
83
89
StyledElement cleanedTree = cleanTree (cascadedStyledTree);
@@ -194,13 +200,13 @@ class HtmlParser extends StatelessWidget {
194
200
}
195
201
}
196
202
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 ) {
198
204
String fullCSS = "" ;
199
205
for (final e in styles) {
200
206
fullCSS = fullCSS + e.innerHtml;
201
207
}
202
208
if (fullCSS.isNotEmpty) {
203
- final declarations = parseExternalCSS (fullCSS);
209
+ final declarations = parseExternalCSS (fullCSS, errorHandler );
204
210
return declarations;
205
211
} else {
206
212
return {};
@@ -219,12 +225,15 @@ class HtmlParser extends StatelessWidget {
219
225
return tree;
220
226
}
221
227
222
- static StyledElement _applyInlineStyles (StyledElement tree) {
228
+ static StyledElement _applyInlineStyles (StyledElement tree, OnCSSParseError ? errorHandler ) {
223
229
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
+ }
225
234
}
226
235
227
- tree.children.forEach (_applyInlineStyles);
236
+ tree.children.forEach ((e) => _applyInlineStyles (e, errorHandler) );
228
237
return tree;
229
238
}
230
239
0 commit comments