Skip to content

fix(extract-tf): match end tags in and around svg and math as Chromium does - #151

Closed
77777R7 wants to merge 1 commit into
claude/parse5-head-colfrom
claude/parse5-foreign-end
Closed

77777R7 wants to merge 1 commit into
claude/parse5-head-colfrom
claude/parse5-foreign-end

Conversation

@77777R7

@77777R7 77777R7 commented Oct 3, 2026

Copy link
Copy Markdown
Owner

Stacked on #150 (claude/parse5-head-col); it retargets as the stack merges. Fixes the Known gap in #137 about Chromium's </foreignObject> past an HTML <foreignObject>. It also fixes two related namespace bugs that the same oracle turned up.

Problem

1. Foreign end tags compared by lowercased name.

  • The standard and parse5 match an end tag in svg or math to an open element by its lowercased name.
  • Chromium matches by exact name. In svg it first respells the end tag the svg way (</foreignObject>, </clipPath>, </linearGradient>, </textPath>, …). In math it keeps the tag's own spelling.
  • An end tag that first meets an HTML element goes to the HTML rules. There a respelled name matches nothing, because HTML element names are lowercase.
  • Effect: </foreignObject> in an svg closed an HTML <foreignobject> (or <clippath>, or math's <clippath>) around the svg, and what followed left the svg. In <p>a<lineargradient><svg></lineargradient>b</p>, Chromium keeps b inside the svg, which displays no text, so the Markdown is a, not ab.

2. "Any other end tag" ignored namespaces. parse5's genericEndTagInBody matched by tag ID alone. A </desc> or </mi> written in HTML inside svg or math therefore closed the svg <desc> or math <mi> around it.

3. Resetting the insertion mode ignored namespaces. parse5 treated an svg <tfoot> as a row group, so a second <table> in a <foreignObject> under it was dropped.

The standard and Chromium read HTML elements only in both 2 and 3.

Change

packages/extract-tf/src/dom.ts, in StandardParser:

  • onEndTag handles end tags in foreign content, except </p> and </br>, which stay with parse5. When the current node is svg, the name is respelled by foreignContent.SVG_TAG_NAMES_ADJUSTMENT_MAP. The stack is then walked comparing exact names; element names are read through the budgeted adapter.
    • At the first HTML element, an unchanged name goes to the HTML rules.
    • A respelled name is dropped, except that after the body it still switches back to the body's rules.
  • endTagAsAnyOther matches HTML elements only. It is used for every end tag that parse5's body and table handlers do not handle by a rule of their own (OWN_END_RULES). It also covers formatting end tags with no entry in the list of active formatting elements, and end tags after the body.
  • _resetInsertionMode is reimplemented to skip svg and math elements, including in its walk over a <select>'s ancestors.
  • The insertion-mode numbers are now gathered in one MODE table. They are parse5 8.0.1's, which is pinned.

Because the Markdown changes, EXTRACTOR_VERSION is now extract-tf/17. The version test's digest is unchanged.

Tests

  • New test in packages/extract-tf/test/tables.test.ts; every expected value is Chromium's. 8 of its assertions fail on fix(extract-tf): read a <title> or <base> at a template's top level as Chromium does #150's code:

    • respelled end tags in svg and math;
    • the a Markdown;
    • </desc> and </mi> in HTML;
    • the second <table>.

    Two guards pass on both versions: an svg element of the tag's name still closes, and a comment after </body> stays in the body.

  • npm run typecheck: exit 0.

  • npm test: 145 files, 1841 tests passed.

Chromium oracle

Random inputs of table, template, svg and math tags, with foreignObject, clipPath and linearGradient written in either case; #150's code compared with this PR's:

Run #150's code (matching) This PR (matching) Regressions
pages, seeds 1, 2, 3, 5, 6 4,951–4,965 of 5,001 5,001 of 5,001 each 0
fragments, same seeds 4,958–4,968 of 5,000 5,000 except one 0
broad tag set without the case variants, pages 4,991 of 5,001 5,001 of 5,001 0
  • The one fragment that still differs is written as <body>…</body>. parse reads that as a selection on purpose.
  • 120 captured pages: the Markdown (main content and whole page), tables, links and images are identical.

Review

The fresh-context reviewer found two blocking problems. Both are fixed and the reviewer re-checked them:

  1. Budget bypass. The new walk read element names without charging the element budget. Deep svg with many unmatched end tags went quadratic: 13.5 s at N=40,000. It now reads through the budgeted adapter: 975 ms, the same as before, by the same fallback.
  2. After-body mode. After </body>, a respelled end tag was dropped without switching back to the body's rules, so a later comment went under <html>. This is fixed, with a test.

The reviewer also checked:

  • OWN_END_RULES against parse5 8.0.1's six handlers;
  • every mode in BODY_RULE_MODES;
  • the reset in fragment parsing;
  • adoption-agency cases.

Known gaps

  • Chromium's newer <select> parsing. The standard dropped the "in select" modes. This is being worked on next.
  • MathML namespace. linkedom loses the MathML namespace on copied math elements. The tree shape matches; namespaceURI does not. This was the same before.
  • Comments after </html>. Chromium puts them on the Document; our copy keeps only <html>. This was the same before.

🤖 Generated with Claude Code

…m does

An end tag in svg or math is now matched to an open element by its exact
name, as in Chromium: in svg it first takes svg's spelling
(</foreignObject>, </clipPath>, </linearGradient>), in math it keeps its
own. One that meets an HTML element first is read by the HTML rules,
where a name svg respelled matches nothing (after the body it still
returns to the body's rules). The standard (and parse5) compares names
lowercased, so </foreignObject> in an svg closed an HTML <foreignobject>
or math's around it, and what followed left the svg or math: in
<p>a<lineargradient><svg></lineargradient>b</p> the "b" is in the svg,
which shows no text, so the Markdown is "a", not "ab".

Two more places where parse5 also took an svg or math element for an HTML
one now read HTML elements only, as the standard and Chromium do: "any
other end tag" in the body's rules (a </desc> or </mi> written in HTML
inside svg or math closed the element around it), and resetting the
insertion mode (an svg <tfoot> made the rest be read as a row group's).

Random pages of table, template, svg and math tags now match Chromium in
every case (36 to 50 of 5,001 per run differed before); the 120 captured
pages give the same output. EXTRACTOR_VERSION is now extract-tf/17.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: 77777R7 <lunhoward306@gmail.com>
@77777R7

77777R7 commented Oct 4, 2026

Copy link
Copy Markdown
Owner Author

Landed on main through #167 (merge commit 82c925a), which brought the whole stack #113 → #167 in at once after merging main into its top branch. This PR's commits are all in main.

@77777R7 77777R7 closed this Oct 4, 2026
@77777R7
77777R7 deleted the claude/parse5-foreign-end branch October 4, 2026 04:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant