Repository navigation
🐛 fix(rewrite): keep prepended content on unwrap - #2248
Merged
gaborbernat merged 2 commits intoNov 16, 2026
Merged
Conversation
rewrite() dropped the content a handler added with prepend() when the same handler unwrapped the element with remove_and_keep_content(). The prepended text was written between the start tag and the children. An unwrapped element writes no start tag, so the text was dropped with the other output of the removed tags. Unwrapping now writes the prepended text in that position, and the text of a raw-text element is written escaped, since its tags are gone. remove() and replace() still drop prepended content; a flag records them apart from an unwrap, so the order of the calls does not matter.
Merging this PR will not alter performance
Comparing Footnotes
|
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.
Calling
prepend()on an element and then unwrapping that element withremove_and_keep_content()in the samerewrite()handler dropped the prepended content from the output. Once this merges, the content stays in place, before the element's children, the same way content added withappend()already does. Fixes #2233.Unwrapping skipped the prepended content
The rewriter writes the content from
prepend()right after an element's start tag, where the children begin (old code). An unwrapped element writes no start tag, and the same check that skipped the start tag also skipped that content (removed branch), so the content was lost.remove()andreplace()have to drop it, and they still do.The handle now records whether a handler dropped the whole element, which only
remove()andreplace()do (field,replace(),remove()). The prepended content is written unless that flag is set, so an unwrap keeps it whichever handler call came first.Raw-text elements need escaped text
A browser reads the content of
script,style,xmpand the other raw-text elements without decoding character references, so the rewriter writes that text as is while the tags are present. Without the tags, a browser reads<and&in the same text as markup (data state). The rewriter already kept an escaped copy of appended text for this case.prepend()now keeps one too, and the unwrap path writes it (unwrap path).Behavior changes
prepend()on an element thatremove_and_keep_content()unwraps is now written, in order before the element's children. It used to be dropped.prepend()on a raw-text element such asscriptis written escaped when the element is unwrapped, unlesshtml=Trueis passed, as appended text already was.prepend()orremove_and_keep_content()is called first.prepend()on an element they unwrap.remove(),replace()and the other edits give the same output as before.Performance
Callgrind on release LTO builds (
CC=/usr/bin/cc,-Dbuildtype=release -Db_lto=true) withPYTHONHASHSEED=0, counting extension instructions per repetition as the difference between 220 and 20 repetitions. The base isupstream/mainat 7e6d0f9.upstream/mainrewriterewrite-attributes-singleminify-css(control)The
rewrite-attributes-singledifference is under 5 instructions per call, the noise threshold for rows below 10,000 instructions. The new checks run only for elements that have prepended content or that are removed.Tests
Run with
tests/tokenizer/test_rewrite.py, every changed executable line and branch insrc/turbohtml/_c/tokenizer/rewrite.cis hit under gcc (6 changed executable lines) and under clang 19 in thelocalhost/turbohtml-ossimage, read withllvm-cov gcov(7 changed executable lines). File-level totals from that partial run are not meaningful, so the full-suite gate runs in CI. Thirteen of the new cases, and the updatedtest_rewrite_unwrapped_raw_text_prepend_is_not_checked, fail on a release build ofupstream/main.