Skip to content

Commit 7d0c0e4

Browse files
gnpricePIG208
authored andcommitted
content [nfc]: Move static _parseMath out from parser class
Because this is used for parsing both inline and block nodes, moving it out will help us separate the rest of the parser code between the handling of inline content and block content.
1 parent d0260b8 commit 7d0c0e4

File tree

1 file changed

+46
-46
lines changed

1 file changed

+46
-46
lines changed

lib/model/content.dart

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,52 @@ class GlobalTimeNode extends InlineContentNode {
806806

807807
////////////////////////////////////////////////////////////////
808808
809+
String? _parseMath(dom.Element element, {required bool block}) {
810+
final dom.Element katexElement;
811+
if (!block) {
812+
assert(element.localName == 'span' && element.className == 'katex');
813+
814+
katexElement = element;
815+
} else {
816+
assert(element.localName == 'span' && element.className == 'katex-display');
817+
818+
if (element.nodes.length != 1) return null;
819+
final child = element.nodes.single;
820+
if (child is! dom.Element) return null;
821+
if (child.localName != 'span') return null;
822+
if (child.className != 'katex') return null;
823+
katexElement = child;
824+
}
825+
826+
// Expect two children span.katex-mathml, span.katex-html .
827+
// For now we only care about the .katex-mathml .
828+
if (katexElement.nodes.isEmpty) return null;
829+
final child = katexElement.nodes.first;
830+
if (child is! dom.Element) return null;
831+
if (child.localName != 'span') return null;
832+
if (child.className != 'katex-mathml') return null;
833+
834+
if (child.nodes.length != 1) return null;
835+
final grandchild = child.nodes.single;
836+
if (grandchild is! dom.Element) return null;
837+
if (grandchild.localName != 'math') return null;
838+
if (grandchild.attributes['display'] != (block ? 'block' : null)) return null;
839+
if (grandchild.namespaceUri != 'http://www.w3.org/1998/Math/MathML') return null;
840+
841+
if (grandchild.nodes.length != 1) return null;
842+
final greatgrand = grandchild.nodes.single;
843+
if (greatgrand is! dom.Element) return null;
844+
if (greatgrand.localName != 'semantics') return null;
845+
846+
if (greatgrand.nodes.isEmpty) return null;
847+
final descendant4 = greatgrand.nodes.last;
848+
if (descendant4 is! dom.Element) return null;
849+
if (descendant4.localName != 'annotation') return null;
850+
if (descendant4.attributes['encoding'] != 'application/x-tex') return null;
851+
852+
return descendant4.text.trim();
853+
}
854+
809855
/// What sort of nodes a [_ZulipContentParser] is currently expecting to find.
810856
enum _ParserContext {
811857
/// The parser is currently looking for block nodes.
@@ -825,52 +871,6 @@ class _ZulipContentParser {
825871
/// and should be read or updated only inside an assertion.
826872
_ParserContext _debugParserContext = _ParserContext.block;
827873

828-
static String? _parseMath(dom.Element element, {required bool block}) {
829-
final dom.Element katexElement;
830-
if (!block) {
831-
assert(element.localName == 'span' && element.className == 'katex');
832-
833-
katexElement = element;
834-
} else {
835-
assert(element.localName == 'span' && element.className == 'katex-display');
836-
837-
if (element.nodes.length != 1) return null;
838-
final child = element.nodes.single;
839-
if (child is! dom.Element) return null;
840-
if (child.localName != 'span') return null;
841-
if (child.className != 'katex') return null;
842-
katexElement = child;
843-
}
844-
845-
// Expect two children span.katex-mathml, span.katex-html .
846-
// For now we only care about the .katex-mathml .
847-
if (katexElement.nodes.isEmpty) return null;
848-
final child = katexElement.nodes.first;
849-
if (child is! dom.Element) return null;
850-
if (child.localName != 'span') return null;
851-
if (child.className != 'katex-mathml') return null;
852-
853-
if (child.nodes.length != 1) return null;
854-
final grandchild = child.nodes.single;
855-
if (grandchild is! dom.Element) return null;
856-
if (grandchild.localName != 'math') return null;
857-
if (grandchild.attributes['display'] != (block ? 'block' : null)) return null;
858-
if (grandchild.namespaceUri != 'http://www.w3.org/1998/Math/MathML') return null;
859-
860-
if (grandchild.nodes.length != 1) return null;
861-
final greatgrand = grandchild.nodes.single;
862-
if (greatgrand is! dom.Element) return null;
863-
if (greatgrand.localName != 'semantics') return null;
864-
865-
if (greatgrand.nodes.isEmpty) return null;
866-
final descendant4 = greatgrand.nodes.last;
867-
if (descendant4 is! dom.Element) return null;
868-
if (descendant4.localName != 'annotation') return null;
869-
if (descendant4.attributes['encoding'] != 'application/x-tex') return null;
870-
871-
return descendant4.text.trim();
872-
}
873-
874874
String? parseInlineMath(dom.Element element) {
875875
assert(_debugParserContext == _ParserContext.inline);
876876
return _parseMath(element, block: false);

0 commit comments

Comments
 (0)