Skip to content

fix(extract-tf): end a <col> fragment's pending table text as Chromium does - #148

Closed
77777R7 wants to merge 1 commit into
claude/parse5-template-formfrom
claude/parse5-fragment-text
Closed

77777R7 wants to merge 1 commit into
claude/parse5-template-formfrom
claude/parse5-fragment-text

Conversation

@77777R7

@77777R7 77777R7 commented Oct 3, 2026 •

Copy link
Copy Markdown
Owner

Stacked on #145 (claude/parse5-template-form); it retargets as the stack merges. This fixes the fragment-only Known gap carried in #143 and #145: Chromium drops trailing text in a template's table at the end of a fragment.

Problem

The cause turned out to be narrower than the gap's description. Chromium drops the text only when both of these hold:

  1. The fragment's top level starts with a <col>. That puts its template's insertion mode in the column-group mode.
  2. The fragment ends while a table's text is still pending. The parser is then in the "in table text" mode.

The standard and parse5 write that text out. Chromium's template.innerHTML drops it. If the text is not all whitespace, Chromium still re-opens the formatting elements before the table; only the text itself is lost.

Fragment Chromium template.innerHTML Before
<col><template><table>abc <col><template><table></table></template> <col><template>abc<table></table></template>
<col><template><table><tr><b><i></tr>abc <col><template><b><i></i></b><b><i></i></b><table>…</table></template> the second <b><i> holds abc
<template><table>abc (no <col>) text kept text kept
whole page, or an outer template holding the <col> text kept text kept

Change

packages/extract-tf/src/dom.ts, StandardParser.onEof. The rule applies only when all of these hold:

  • it is a fragment parse;
  • the parser is in IN_TABLE_TEXT;
  • the fragment's own template mode is IN_COLUMN_GROUP (the bottom of tmplInsertionModeStack).

When it applies:

  • if the pending text is not all whitespace, the active formatting elements are re-opened with foster parenting;
  • the pending text is then discarded, before parse5's own EOF steps run.

No output changes. The fragment's column-group mode ignores tables and text at its top level, so the dropped text is always inside a nested <template>, which nothing reads. EXTRACTOR_VERSION stays extract-tf/15.

Tests

  • New test in packages/extract-tf/test/tables.test.ts. Every expected value is Chromium's template.innerHTML. On fix(extract-tf): read a <form> in a <template> as Chromium does #145's code the five "dropped" assertions fail; the four "kept" assertions pass on both versions and guard the rule's boundaries.
  • npm run typecheck: exit 0.
  • npm test: 145 files, 1839 tests passed.

Chromium oracle

  • Random fragments of table and template tags (template.innerHTML): 50,000 of 50,000 match over seeds 1–10. Before, seeds 1–3 had 1, 6 and 3 mismatches in 5,000.
  • Random fragments with svg, math, option and frameset as well: the remaining mismatches are the same before and after. They are the known </foreignObject> and <select> differences, plus <body>…</body> input, which parse deliberately reads as a selection.
  • The page oracles from fix(extract-tf): read a <form> in a <template> as Chromium does #145 are unchanged; the template, table and form groups are still 0.

Review

A fresh-context reviewer found nothing blocking. It checked:

  • the parse5 internals: mode numbers, tmplInsertionModeStack order, fragmentContext being null for pages, and how the foster-parenting flag is restored;
  • about 100 hand-picked cases and seven fuzz seeds of <col>-led fragments, with no regressions against the old code;
  • the selection (<body>…</body>) and <noscript> paths;
  • that no text outside a template can be dropped. It could not construct a case.

Known gaps (unchanged)

🤖 Generated with Claude Code

…m does

A fragment whose top level starts with a <col> has the column group's mode
as its template's mode. Chromium's template.innerHTML then drops a table's
text still pending at the fragment's end, where the standard (and parse5)
writes it: <col><template><table>abc gives <col><template><table></table>
</template>. When the text is not all whitespace, Chromium still re-opens
the formatting elements before the table; only the text is lost.

StandardParser.onEof now does the same. Such text is always in a nested
<template>, as the fragment's own column group mode ignores tables and
text, so no output reads it and EXTRACTOR_VERSION stays extract-tf/15.
Random fragments of table and template tags now match Chromium in all
50,000 of ten runs (1 to 6 of 5,000 per run differed before).

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-fragment-text 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