Repository navigation
🐛 fix(dom): parse and sanitize annotation-xml children as HTML - #2229
Merged
gaborbernat merged 6 commits intoNov 16, 2026
Merged
Conversation
set_inner_html() and insert_adjacent_html() parse the new markup in the context of the target element. For a MathML annotation-xml with an HTML encoding, that context did not carry the HTML integration point status the document parser gives the element, so the new children were parsed as MathML. They then serialized and sanitized as foreign elements, and a reparse of the output built HTML elements instead. The fragment context root now takes the integration point flag from the same predicate the tree builder applies to annotation-xml start tags. The predicate reads the element's encoding attribute when the fragment is parsed, so an encoding set or removed after parsing is honored the way a reparse of the start tag would honor it.
Merging this PR will not alter performance
Comparing Footnotes
|
setAttribute can rewrite the encoding of an annotation-xml while another thread parses a fragment in its context. The fragment context read the attribute before the tree lock was taken, so the flag could come from a half-updated attribute list. The read now runs under the lock, and the flag reaches the fragment root through th_tree_parse_fragment_ex. The other callers keep th_tree_parse_fragment with its original signature, since they have no element context. The new DOM tests splice content under SVG foreignObject, desc and title and under the MathML text points, and check that a non-first, misspelled or valueless encoding leaves the children in the MathML namespace.
A DOM move can place a MathML or SVG child under an HTML integration point. A reparse reads such a child as HTML, so the serialized output does not match the tree. namespace_reachable_under accepted every same-namespace child, so the sanitizer kept the child as a live foreign element, and the output parsed differently from the sanitized tree. Same-namespace children are now checked against what the parser keeps foreign under each point: only an svg under foreignObject, desc or title; only math, mglyph and malignmark under a MathML text point; only math under an HTML annotation-xml. Other children are escaped, as other namespace-confused nodes are. XML output has no integration points, so it keeps every same-namespace child.
…ion-point' into fix/annotation-xml-integration-point # Conflicts: # docs/changelog/2228.bugfix.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Code that edits MathML with embedded HTML through the DOM got the wrong tree (#2228). 🐛 Calling
set_inner_html(), orinsert_adjacent_html()withafterbeginorbeforeend, on an<annotation-xml encoding="text/html">built the new children as MathML, while parsing the same markup as a document builds them as HTML. The result serialized and sanitized as foreign content and read back differently after a round trip. Once this merges, an edited tree matches what the parser builds from the same markup.The tree builder already decides when an
annotation-xmlis an HTML integration point, and fragment parsing now asks it the same question about the target element, as the fragment parsing algorithm requires.The sanitizer covers trees assembled by moving nodes through the DOM, which can place an element where the parser never would. Under every integration point (
annotation-xmlwith an HTML encoding, SVGforeignObject,descandtitle, and the MathML text points such asmi), it now keeps a same-namespace child as live markup only when a reparse would keep it foreign: an<svg>under the SVG points, a<math>underannotation-xml, and<math>,<mglyph>or<malignmark>under a text point. Any other child is written out as escaped text, as the sanitizer already does for other namespace-confused nodes.Behavior changes
annotation-xmlwhoseencodingistext/htmlorapplication/xhtml+xml(case-insensitive) becomes HTML elements. Other encodings, or none, still give MathML.encodingattribute's current value counts, so changing it after parsing affects later splices.<g>moved underforeignObject. Sanitizer output for parsed documents and for XML serialization is unchanged.