@@ -873,41 +873,48 @@ String? _parseMath(dom.Element element, {required bool block}) {
873873 } else {
874874 assert (element.localName == 'span' && element.className == 'katex-display' );
875875
876- if (element.nodes.length != 1 ) return null ;
877- final child = element.nodes.single;
878- if (child is ! dom.Element ) return null ;
879- if (child.localName != 'span' ) return null ;
880- if (child.className != 'katex' ) return null ;
881- katexElement = child;
876+ if (element.nodes case [
877+ dom.Element (localName: 'span' , className: 'katex' ) && final child,
878+ ]) {
879+ katexElement = child;
880+ } else {
881+ return null ;
882+ }
882883 }
883884
884885 // Expect two children span.katex-mathml, span.katex-html .
885886 // For now we only care about the .katex-mathml .
886- if (katexElement.nodes.isEmpty) return null ;
887- final child = katexElement.nodes.first;
888- if (child is ! dom.Element ) return null ;
889- if (child.localName != 'span' ) return null ;
890- if (child.className != 'katex-mathml' ) return null ;
891-
892- if (child.nodes.length != 1 ) return null ;
893- final grandchild = child.nodes.single;
894- if (grandchild is ! dom.Element ) return null ;
895- if (grandchild.localName != 'math' ) return null ;
896- if (grandchild.attributes['display' ] != (block ? 'block' : null )) return null ;
897- if (grandchild.namespaceUri != 'http://www.w3.org/1998/Math/MathML' ) return null ;
898-
899- if (grandchild.nodes.length != 1 ) return null ;
900- final greatgrand = grandchild.nodes.single;
901- if (greatgrand is ! dom.Element ) return null ;
902- if (greatgrand.localName != 'semantics' ) return null ;
903-
904- if (greatgrand.nodes.isEmpty) return null ;
905- final descendant4 = greatgrand.nodes.last;
906- if (descendant4 is ! dom.Element ) return null ;
907- if (descendant4.localName != 'annotation' ) return null ;
908- if (descendant4.attributes['encoding' ] != 'application/x-tex' ) return null ;
909-
910- return descendant4.text.trim ();
887+ if (katexElement.nodes case [
888+ dom.Element (localName: 'span' , className: 'katex-mathml' , nodes: [
889+ dom.Element (
890+ localName: 'math' ,
891+ namespaceUri: 'http://www.w3.org/1998/Math/MathML' )
892+ && final mathElement,
893+ ]),
894+ ...
895+ ]) {
896+ if (mathElement.attributes['display' ] != (block ? 'block' : null )) {
897+ return null ;
898+ }
899+
900+ final String texSource;
901+ if (mathElement.nodes case [
902+ dom.Element (localName: 'semantics' , nodes: [
903+ ...,
904+ dom.Element (
905+ localName: 'annotation' ,
906+ attributes: {'encoding' : 'application/x-tex' },
907+ : final text),
908+ ]),
909+ ]) {
910+ texSource = text.trim ();
911+ } else {
912+ return null ;
913+ }
914+ return texSource;
915+ } else {
916+ return null ;
917+ }
911918}
912919
913920/// Parser for the inline-content subtrees within Zulip content HTML.
0 commit comments