Skip to content

🐛 fix(dom): parse and sanitize annotation-xml children as HTML - #2229

Merged
gaborbernat merged 6 commits into
tox-dev:mainfrom
gaborbernat:fix/annotation-xml-integration-point
Nov 16, 2026
Merged

gaborbernat merged 6 commits into
tox-dev:mainfrom
gaborbernat:fix/annotation-xml-integration-point

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Code that edits MathML with embedded HTML through the DOM got the wrong tree (#2228). 🐛 Calling set_inner_html(), or insert_adjacent_html() with afterbegin or beforeend, 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-xml is 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-xml with an HTML encoding, SVG foreignObject, desc and title, and the MathML text points such as mi), 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> under annotation-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

  • HTML spliced into an annotation-xml whose encoding is text/html or application/xhtml+xml (case-insensitive) becomes HTML elements. Other encodings, or none, still give MathML.
  • The encoding attribute's current value counts, so changing it after parsing affects later splices.
  • Sanitizing a DOM-built tree escapes a child such as an SVG <g> moved under foreignObject. Sanitizer output for parsed documents and for XML serialization is unchanged.

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.
@gaborbernat gaborbernat added the bug Something isn't working label Nov 16, 2026
@codspeed

codspeed Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 582 untouched benchmarks
⏩ 89 skipped benchmarks1


Comparing gaborbernat:fix/annotation-xml-integration-point (56f3387) with main (6c6e490)

Open in CodSpeed

Footnotes

  1. 89 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

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
@gaborbernat gaborbernat changed the title 🐛 fix(dom): parse annotation-xml fragments as HTML 🐛 fix(dom): parse and sanitize annotation-xml children as HTML Nov 16, 2026
@gaborbernat
gaborbernat merged commit 605c952 into tox-dev:main Nov 16, 2026
57 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant