Repository navigation
🐛 fix(rewrite): escape < joined by dropped tokens - #2195
Merged
Merged
Conversation
Text the tokenizer reports in pieces, with a dropped token between them (an empty end tag, or a tag or comment a handler removed), was copied unchanged. A `<` left in front of a letter, `/`, `!` or `?` then began markup in the output. Such a `<` is now escaped. A `<` or `</` at the end of a run is still held as the tail that the next write settles. Past the nesting cap the spine does not track an element, so the tokenizer kept reading its raw text as data. The tokenizer now switches to the element's content model there too, and the rewriter records whether it is in raw text, so the escape never touches raw text. Fixes tox-dev#1752
Name the two cases the rewriter does not copy as is: an empty end tag is dropped, and a `<` that a dropped token would join with the text after it is written as `<`. Rewrap the changelog fragment to the docstrfmt width.
Merging this PR will not alter performance
Comparing Footnotes
|
gaborbernat
added a commit
that referenced
this pull request
Nov 15, 2026
# Apply removals past the depth cap `rewrite()` stopped applying its element rules once the open-element stack reached its depth cap. Past the cap a raw-text or RCDATA start tag was not tracked, so the tokenizer stayed in the data state and read the content of that element as markup. Markup that a browser reads as raw text could then hide a tag that a `remove()` rule should have caught. Fixes #1758 ## Causes 1. A raw-text or RCDATA start tag past the cap was not pushed, so its content model never reached the tokenizer. The tree builder switches the content model whatever the depth. 1. `remove`, `replace` and `set_content` on an element past the cap dropped its tags but kept its children, and the strings from `append` and `after` were dropped. 1. An SVG or MathML element past the cap was not tracked, so the spine read what followed it with the wrong namespace. ## Fix - A raw-text or RCDATA element takes the one slot past the cap, so its content is read as text and its edits apply as they do below the cap. No other element can open while that slot is held, because the tokenizer reads no start tags in raw text. - When a th_shadow runs, the content model of such an element comes from the tokenizer, because the tree builder switches it even when it refuses the element at its own cap. - Any other element past the cap is refused, as the tree builder refuses it: its tags stream through and its end tag closes only an element the spine still holds. An edit that needs its content raises `ValueError`. - With a handler registered, an SVG or MathML start tag past the cap raises `ValueError`. Without a handler the output is the input, so nothing changes. - The cap is exposed to the test suite as `turbohtml._html._REWRITE_MAX_DEPTH`. Tests size their inputs from it. ### Behavior changes - On an element nested past the depth limit, `remove()`, `replace()`, `set_content()`, `append()` and `after()` raise `ValueError` instead of applying partly. - A `set_content()` or `append()` on a raw-text element past the limit now behaves as it does below the limit. Text that would end the element raises, as it already did below the limit. - A handler on a page with an SVG or MathML element past the limit raises `ValueError`. The docs (`docs/explanation/streaming.rst` and the `rewrite()` docstring) state the cap as 8,192 and describe these cases. The streaming page said 512, which was already wrong. ## Relation to #2195 #2195 (#1752) also reads raw text past the cap as raw text, and it tracks that state with a flag. It does not suppress the content of a removed raw-text element past the cap, so markup inside such an element is still written to the output. That case is what this PR fixes. The two PRs overlap in the past-cap branch of `rw_handle_element`. Merging this branch with #2195 needs one resolution in `rewrite.c`: keep this branch's cap handling, and drop the flag, because the raw-text slot already makes the spine's top entry raw. `rw_data_context` then tests only the spine entry. The combined tree passes both test files, and the test file merges without conflict. ## Other limits checked The attribute scan limit (16 attributes), the long-value memo (256 code points), the long-name key buffer (256 bytes) and the tokenizer's namespace spine (512 entries) were checked for the same desync. Attribute reads, writes and removals past 16 attributes, long values and names, and foreign content nested past the tokenizer spine all matched the output below the limit. No change was needed there. ## Tests `tests/tokenizer/test_rewrite.py` covers each raw-text element at the cap and nine refused elements before it, every edit kind on those elements compared with the same edit below the cap, removal of a script past the cap, the non-raw edit errors, the SVG/MathML handler error and a raw-text content edit that ends the element. The sizes come from `_REWRITE_MAX_DEPTH` with small offsets. The behavior tests fail on a build of `main` with the same test-visible constant. ## Performance Local callgrind, release LTO builds, `PYTHONHASHSEED=0`, 220 and 20 repetitions, per-iteration Ir. Base `main` at `8c70f88e2`. | Row | main | this PR | delta | | ------------------------------------- | ----------: | ----------: | --------------: | | `rewrite` | 15,089,779 | 15,080,930 | -8,849 (-0.06%) | | `rewrite-attributes-single` | 14,876 | 14,896 | +20 (+0.13%) | | `parse-dense` (control, tree builder) | 356,190,673 | 356,190,080 | -593 (-0.00%) | | main vs main (noise) | 0 | 0 | 0 | The `rewrite-attributes-single` row sits about 20 Ir above main per call. The change sits on the start-tag path in `rw_handle_element`, which this row runs. Two changes did not move it: a cheaper depth test on the hot path, and moving the refusal code into an out-of-line helper. The row is above the noise band, below the 2 % limit, and does not block. The control row was measured on the build before the two follow-up changes, which only touch the rewriter. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
# Conflicts: # src/turbohtml/_c/tokenizer/rewrite.c
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.
rewrite()could emit markup the input never contained. When a token was dropped between two pieces of text, such as an empty end tag or a node a handler removed, a<left in front of a letter,/,!or?started a tag in the output. 🛡️ Once this merges, such a<is written as<, so rewritten pages keep their text and do not gain tags. Fixes #1752.It now checks every
<in a text run gathered from more than one source span, so a<that a dropped token left behind is escaped before it can open markup. A<or</at the end of a run still waits for the next write, which decides whether it is markup.Behavior changes
<followed by a letter,/,!or?, is written with<. A bare<followed by a letter starts a tag in the browser, so the escape keeps the text as text.