Repository navigation
🐛 fix(parser): tree-construction whitespace handling - #442
Merged
Merged
Conversation
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; / 
) 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
Merging this PR will improve performance by 9.27%
|
| 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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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>\nXstill drops the newline, while<pre><!--c-->\nXand<pre><span></span>\nXkeep it, matching what issue #402 reported against html5lib and Lexbor.A carriage return decoded from a character reference (
or
) 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 , and it must not set frameset-ok to "not ok". 🔧 Adding U+000D to the shared whitespace set fixes both symptoms from issue #439: ano longer leaks a stray text node, and <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