@@ -8,10 +8,40 @@ import 'binding.dart';
8
8
import 'content.dart' ;
9
9
import 'settings.dart' ;
10
10
11
+ /// The failure reason in case the KaTeX parser encountered a
12
+ /// `_KatexHtmlParseError` exception.
13
+ ///
14
+ /// Generally this means that parser encountered an unexpected HTML structure,
15
+ /// an unsupported HTML node, or an unexpected inline CSS style or CSS class on
16
+ /// a specific node.
17
+ class KatexParserHardFailReason {
18
+ const KatexParserHardFailReason ({
19
+ required this .error,
20
+ required this .stackTrace,
21
+ });
22
+
23
+ final String error;
24
+ final StackTrace stackTrace;
25
+ }
26
+
27
+ /// The failure reason in case the KaTeX parser found an unsupported
28
+ /// CSS class or unsupported inline CSS style property.
29
+ class KatexParserSoftFailReason {
30
+ const KatexParserSoftFailReason ({
31
+ this .unsupportedCssClasses = const [],
32
+ this .unsupportedInlineCssProperties = const [],
33
+ });
34
+
35
+ final List <String > unsupportedCssClasses;
36
+ final List <String > unsupportedInlineCssProperties;
37
+ }
38
+
11
39
class MathParserResult {
12
40
const MathParserResult ({
13
41
required this .texSource,
14
42
required this .nodes,
43
+ this .hardFailReason,
44
+ this .softFailReason,
15
45
});
16
46
17
47
final String texSource;
@@ -22,6 +52,9 @@ class MathParserResult {
22
52
/// CSS style, indicating that the widget should render the [texSource] as a
23
53
/// fallback instead.
24
54
final List <KatexNode >? nodes;
55
+
56
+ final KatexParserHardFailReason ? hardFailReason;
57
+ final KatexParserSoftFailReason ? softFailReason;
25
58
}
26
59
27
60
/// Parses the HTML spans containing KaTeX HTML tree.
@@ -87,21 +120,33 @@ MathParserResult? parseMath(dom.Element element, { required bool block }) {
87
120
final flagForceRenderKatex =
88
121
globalSettings.getBool (BoolGlobalSetting .forceRenderKatex);
89
122
123
+ KatexParserHardFailReason ? hardFailReason;
124
+ KatexParserSoftFailReason ? softFailReason;
90
125
List <KatexNode >? nodes;
91
126
if (flagRenderKatex) {
92
127
final parser = _KatexParser ();
93
128
try {
94
129
nodes = parser.parseKatexHtml (katexHtmlElement);
95
130
} on _KatexHtmlParseError catch (e, st) {
96
131
assert (debugLog ('$e \n $st ' ));
132
+ hardFailReason = KatexParserHardFailReason (
133
+ error: e.message ?? 'unknown' ,
134
+ stackTrace: st);
97
135
}
98
136
99
137
if (parser.hasError && ! flagForceRenderKatex) {
100
138
nodes = null ;
139
+ softFailReason = KatexParserSoftFailReason (
140
+ unsupportedCssClasses: parser.unsupportedCssClasses,
141
+ unsupportedInlineCssProperties: parser.unsupportedInlineCssProperties);
101
142
}
102
143
}
103
144
104
- return MathParserResult (nodes: nodes, texSource: texSource);
145
+ return MathParserResult (
146
+ nodes: nodes,
147
+ texSource: texSource,
148
+ hardFailReason: hardFailReason,
149
+ softFailReason: softFailReason);
105
150
} else {
106
151
return null ;
107
152
}
@@ -111,6 +156,9 @@ class _KatexParser {
111
156
bool get hasError => _hasError;
112
157
bool _hasError = false ;
113
158
159
+ final unsupportedCssClasses = < String > [];
160
+ final unsupportedInlineCssProperties = < String > [];
161
+
114
162
List <KatexNode > parseKatexHtml (dom.Element element) {
115
163
assert (element.localName == 'span' );
116
164
assert (element.className == 'katex-html' );
@@ -122,7 +170,10 @@ class _KatexParser {
122
170
if (node case dom.Element (localName: 'span' )) {
123
171
return _parseSpan (node);
124
172
} else {
125
- throw _KatexHtmlParseError ();
173
+ throw _KatexHtmlParseError (
174
+ node is dom.Element
175
+ ? 'unsupported html node: ${node .localName }'
176
+ : 'unsupported html node' );
126
177
}
127
178
}));
128
179
}
@@ -357,6 +408,7 @@ class _KatexParser {
357
408
358
409
default :
359
410
assert (debugLog ('KaTeX: Unsupported CSS class: $spanClass ' ));
411
+ unsupportedCssClasses.add (spanClass);
360
412
_hasError = true ;
361
413
}
362
414
}
@@ -410,6 +462,7 @@ class _KatexParser {
410
462
// TODO handle more CSS properties
411
463
assert (debugLog ('KaTeX: Unsupported CSS expression:'
412
464
' ${expression .toDebugString ()}' ));
465
+ unsupportedInlineCssProperties.add (property);
413
466
_hasError = true ;
414
467
} else {
415
468
throw _KatexHtmlParseError ();
0 commit comments