Skip to content

🐛 fix(parser): tree-construction whitespace handling - #442

Merged
gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:fix/parser-whitespace
Jul 3, 2026
Merged

gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:fix/parser-whitespace

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

Two WHATWG §13 tree-construction whitespace rules produced wrong trees, and both cost content or structure in ordinary documents.

The parser dropped a leading newline in <pre>, <listing>, and <textarea> whenever a newline reached the first text child, even when a comment or element sat between the start tag and that newline. HTML ignores the newline only when it is the token immediately following the start tag, so an interposed token has to cancel the skip. 📄 The parser now clears the drop flag on any token other than the text that directly follows the start tag: <pre>\nX still drops the newline, while <pre><!--c-->\nX and <pre><span></span>\nX keep it, matching what issue #402 reported against html5lib and Lexbor.

A carriage return decoded from a character reference (&#13; or &#xD;) reached tree construction as a real U+000D that the parser never counted as whitespace. Input preprocessing folds a literal CR to LF, so the tokenizer's raw scan never sees one, but the reference decodes after preprocessing and slips past that fold. U+000D is whitespace in every insertion mode, so the early modes should ignore a leading &#13;, and it must not set frameset-ok to "not ok". 🔧 Adding U+000D to the shared whitespace set fixes both symptoms from issue #439: &#13;a no longer leaks a stray text node, and &#13;<frameset> keeps the frameset instead of dropping it. The tokenizer only classifies preprocessed input, so its tag and attribute delimiting stays the same.

closes #402
closes #439

Two WHATWG §13 whitespace bugs in tree construction.

The pre/listing/textarea leading-newline skip fired whenever a newline
later reached the first text token, even when a comment or element came
between the start tag and the newline. HTML ignores the newline only when
it is the token immediately following the start tag, so clear the drop
flag on any intervening token. `<pre>\nX` still drops the newline;
`<pre><!--c-->\nX` now keeps it.

A carriage return decoded from a character reference (&tox-dev#13; / &#xD;)
reached tree construction as a real U+000D but was not classified as
whitespace, so a leading one leaked a stray text node and flipped
frameset-ok. Input preprocessing folds a literal CR to LF, but a CR from
a reference bypasses it; U+000D is whitespace in every insertion mode, so
add it to the whitespace set. The tokenizer's raw scan never sees a CR,
so its delimiting is unaffected.

closes tox-dev#402
closes tox-dev#439
@gaborbernat gaborbernat added the bug Something isn't working label Jul 3, 2026
@codspeed

codspeed Bot commented Jul 3, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 9.27%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 57 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_feature[boilerplate] 406.1 µs 371.7 µs +9.27%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing gaborbernat:fix/parser-whitespace (7d5a977) with main (fd56ab7)

Open in CodSpeed

@gaborbernat
gaborbernat merged commit b7e1305 into tox-dev:main Jul 3, 2026
46 checks passed
@gaborbernat
gaborbernat deleted the fix/parser-whitespace branch July 10, 2026 18:44
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

1 participant