Skip to content

🐛 fix(rewrite): keep prepended content on unwrap - #2248

Merged
gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:fix/rewrite-prepend-unwrap
Nov 16, 2026
Merged

gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:fix/rewrite-prepend-unwrap

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

Calling prepend() on an element and then unwrapping that element with remove_and_keep_content() in the same rewrite() 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 with append() 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() and replace() have to drop it, and they still do.

The handle now records whether a handler dropped the whole element, which only remove() and replace() 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, xmp and 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

  • Content passed to prepend() on an element that remove_and_keep_content() unwraps is now written, in order before the element's children. It used to be dropped.
  • Text passed to prepend() on a raw-text element such as script is written escaped when the element is unwrapped, unless html=True is passed, as appended text already was.
  • The result no longer depends on whether prepend() or remove_and_keep_content() is called first.
  • Output changes only for handlers that call 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) with PYTHONHASHSEED=0, counting extension instructions per repetition as the difference between 220 and 20 repetitions. The base is upstream/main at 7e6d0f9.

Row upstream/main This PR Change
rewrite 11,439,414 11,436,795 -2,619 (-0.02%)
rewrite-attributes-single 3,922 3,926 +4 (+0.10%)
minify-css (control) 7,695,455 7,695,456 +1 (0.00%)

The rewrite-attributes-single difference 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 in src/turbohtml/_c/tokenizer/rewrite.c is hit under gcc (6 changed executable lines) and under clang 19 in the localhost/turbohtml-oss image, read with llvm-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 updated test_rewrite_unwrapped_raw_text_prepend_is_not_checked, fail on a release build of upstream/main.

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.
@gaborbernat gaborbernat added the bug Something isn't working label Nov 16, 2026
@gaborbernat
gaborbernat merged commit 328ad27 into tox-dev:main Nov 16, 2026
45 checks passed
@codspeed

codspeed Bot commented Nov 16, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 582 untouched benchmarks
⏩ 89 skipped benchmarks1


Comparing gaborbernat:fix/rewrite-prepend-unwrap (82c66b9) with main (7e6d0f9)

Open in CodSpeed

Footnotes

  1. 89 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

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

Development

Successfully merging this pull request may close these issues.

rewrite(): prepend() content is dropped when the handler unwraps the element

1 participant