Skip to content

fix(extract-tf): read a <title> or <base> at a template's top level as Chromium does - #150

Closed
77777R7 wants to merge 1 commit into
claude/parse5-fragment-textfrom
claude/parse5-head-col
Closed

77777R7 wants to merge 1 commit into
claude/parse5-fragment-textfrom
claude/parse5-head-col

Conversation

@77777R7

@77777R7 77777R7 commented Oct 3, 2026

Copy link
Copy Markdown
Owner

Stacked on #148 (claude/parse5-fragment-text); it retargets as the stack merges. Fixes the Known gap in #148: Chromium drops the <col> itself in a fragment that starts with a <title>.

Problem

The gap was wider than <col>. In a template's own mode, Chromium sends only five tags to the head's rules: <link>, <meta>, <script>, <style> and <template>. Every other start tag switches the template to the body's rules, including <title>, <base>, <basefont>, <bgsound> and <noframes>.

The standard (and parse5) sends those five to the head's rules as well. That leaves the template's mode as it was, so rows, cells and columns after them were kept. Chromium drops those tags and keeps their text.

Template content / fragment Chromium template.innerHTML Before
<title>t</title><tr><td>x</td></tr> <title>t</title>x <title>t</title><tr><td>x</td></tr>
<base><col> <base> <base><col>
<noframes></noframes><caption>x <noframes></noframes>x the caption kept
<style>x</style><tr><td>y</td></tr> unchanged (head's rules) same

Whole pages behave the same inside a <template>.

Change

In packages/extract-tf/src/dom.ts, StandardParser._startTagOutsideForeignContent handles these five tags in IN_TEMPLATE. It first sets the current template mode and the insertion mode to IN_BODY, as parse5's "any other start tag" in a template does. The body's rules then insert the tag by the head's rules, so the tag itself is created as before.

Output changes. A fragment such as the main content or a selection is read as a template's content. Its Markdown therefore changes when its top level has such a tag before rows: <title>Rows</title><tr><td>a</td><td>b</td></tr> gives ab, not a\n\nb. EXTRACTOR_VERSION is now extract-tf/16. The version test's digest is unchanged, because its fixed pages have no such fragment.

Tests

Chromium oracle

Random fragments (template.innerHTML) and pages built from table and template tags plus <title>, <base>, <noframes>, <meta>, <link> and <style>. Old means #148's code, new means this PR; seeds 1–3:

Old mismatches New mismatches
fragments (5,000 per seed) 844 / 788 / 826 0 / 0 / 0
pages (5,001 per seed) 274 / 236 / 303 0 / 0 / 0
  • Regressions: no input in any run matched Chromium before and differs now.
  • Without head tags: the table and template fragments still match in 15,000 of 15,000.
  • Broad tag set (svg, math, option and frameset as well): 1 and 5 of 5,000 fragments still differ. These are the known </foreignObject> and <select> cases, which differed before too.
  • 120 captured pages: the Markdown (main content and whole page), tables, links and images are identical.

Review

A fresh-context reviewer found nothing blocking.

  • parse5 internals: it checked the mode numbers and that tmplInsertionModeStack[0] is the current template's mode. Every start tag, including re-processed ones and those breaking out of svg or math, goes through the override.
  • The Chromium rule: it checked the rule tag by tag. <noscript>, <head>, <body>, <html>, <frameset>, <frame> and <hr> also switch to the body's rules, and parse5 already did that.
  • Targeted cases: about 40 hand-picked cases, including nested templates, a <title> after a row or column set the mode, a <title> in svg or math, and templates in <head> or tables.
  • Random oracles: four more seeds, with 0 regressions.

Known gaps (unchanged)

🤖 Generated with Claude Code

…s Chromium does

In a template's own mode, Chromium reads only <link>, <meta>, <script>,
<style> and <template> by the head's rules; every other start tag,
<title>, <base>, <basefont>, <bgsound> and <noframes> included, switches
the template to the body's rules. The standard (and parse5) reads those
five by the head's rules too, which left the template's mode as it was,
so rows, cells and columns after them were kept where Chromium drops them
and keeps their text.

StandardParser now switches the template to the body's rules first. A
fragment is read as a template's content, so its Markdown changes when its
top level has such a tag before rows: <title>Rows</title><tr><td>a</td>
<td>b</td></tr> is "ab", not "a" and "b" as paragraphs. Random fragments
and pages of table, template and head tags now match Chromium in every
case (788 to 850 of 5,000 fragments and 236 to 303 of 5,001 pages per run
differed before); the 120 captured pages give the same output.

EXTRACTOR_VERSION is now extract-tf/16.

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-head-col 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