Repository navigation
🐛 fix(minify): keep noscript trees when scripts run - #2154
Merged
Merged
Conversation
Minifying a script or style inside a <noscript> could write a </noscript end tag into its output. The JavaScript minifier folds string concatenation, and the CSS minifier drops line continuations, so the joined text need not appear in the source. A browser that runs scripts reads the noscript as raw text up to its first end tag, so the rest of the element ran as markup. Emit the content verbatim when the minified text would hold </noscript in any letter case, the same rule that already covers </script and <!--. The stylesheet check runs on the bytes it writes.
Merging this PR will not alter performance
Comparing Footnotes
|
A browser that runs scripts reads <noscript> content as raw text, but minify() serialized the scripting-off tree, so that content could turn into live elements there. The document parse now records each noscript's source span and whether the two readings agree around it. Stable noscripts get their content copied from the source. Where the readings part, minify() copies the source up to the first element both place alike, checks the result against both trees, and falls back to copying to the end, then to the source unchanged. The body start tag is also kept before a leading noscript, title, base, basefont, bgsound or noframes, which the after-head mode would otherwise move into the head.
Parses and serializations without a noscript record now run their own copy of the drain loop and the minify walk, so they pay no per-token or per-node check. The parted path reuses its two located parses for the expected trees.
The minify walk now checks for a source once per node and writes end tags under a single quiet flag, and the parted path computes ancestor chains only for the elements it compares.
Replace the reparse-and-verify regions with a bounded rule. From the first noscript whose content parses differently with scripting on, the rest of the source is copied verbatim when the builder state at its start tag is one the minified markup before it rebuilds: no active formatting element, no open form, only HTML ancestors and no table, and no later html/body attributes. Otherwise the document comes back unchanged. The minifier no longer reparses its output, so the slow path costs no more than a plain minify.
gaborbernat
added a commit
that referenced
this pull request
Nov 15, 2026
`minify` is documented as idempotent, but minifying its own output could change it. Three causes, all in the minifier's serializer walk in `src/turbohtml/_c/serialize/`. - A stripped comment reset the walk's trailing-formatting state. An ancestor's end tag was omitted even though, once the comment is gone, a formatting element is the last content of that ancestor. The reparse then needs the end tag, and the second pass writes it back. The walk now steps over stripped comments when it looks for the next sibling. - The body start tag was omitted before `base`, `basefont`, `bgsound`, `noframes`, `noscript` and `title`. The parser files those elements under `head` when no body tag precedes them, so the tree changed. The keep-list for the body start tag covers them now; `meta`, `link`, `script`, `style` and `template` were already in it. - A `pre` or `listing` whose content starts with a stripped comment and then a newline lost the newline the reparse drops. The leading-newline check now skips stripped comments before it reads the first text. ### Behavior changes - Output keeps an ancestor's end tag when a stripped comment follows the formatting content that ends it. The second pass previously restored the tag. - Output keeps the body start tag before the head elements listed above. - Output keeps one extra newline at the start of a `pre` or `listing` whose first text starts with a newline after a stripped comment. Output differs from before only in those three cases. ### Tests - `test_body_start_kept_before_head_element` covers the head elements and checks that the reparse builds the source tree. - `test_minify_keeps_end_tag_guarded_by_trailing_formatting` lists the comment cases with their expected output, and checks that minifying the output again returns it unchanged. - `test_minify_keeps_leading_newline_after_stripped_comment` covers `pre` and `listing`. - `test_minify_is_idempotent_over_seeded_documents` minifies 2,000 documents from a fixed seed and asserts that each output is a fixed point. Documents whose plain serialization does not round-trip are skipped, because the serializer itself is not a fixed point for them. On a release build of current `upstream/main`, 13 of the new cases fail. The body-start cases already pass there, since the keep-list landed in #2154. On this branch all pass. ### Performance Base: `upstream/main` at `4f112841e`. The minify sources this PR touches are unchanged in later upstream commits, so the table keeps that base. Release LTO builds, run under callgrind with `PYTHONHASHSEED=0` and `setarch -R`. Each row is instructions per call from the 220- and 20-repetition difference. Two builds of `upstream/main` at equal path length give the noise floor, which is exactly 0 on every row. | Row | main, run 1 | main, run 2 | this branch | change | | --------------------------- | ----------: | ----------: | ----------: | ---------------: | | minify: daring fireball | 668,184 | 668,184 | 669,048 | +864 (+0.13%) | | minify: ars technica | 3,129,560 | 3,129,560 | 3,133,244 | +3,684 (+0.12%) | | minify: mozilla blog | 6,703,490 | 6,703,490 | 6,710,350 | +6,860 (+0.10%) | | minify: whatwg spec | 20,177,012 | 20,177,012 | 20,239,859 | +62,847 (+0.31%) | | serialize `Minify()` (tree) | 1,574,017 | 1,574,017 | 1,580,983 | +6,966 (+0.44%) | | control: parse | 4,898,997 | 4,898,997 | 4,898,997 | 0 | One optimization round was used. The first version of the newline check ran for every element before its tag test, which put the rows at +0.4% to +0.8% (whatwg spec +0.78%, serialize tree +1.77%). Moving the comment skip behind the `pre`/`textarea`/`listing` test brought them to the table above. The remaining cost is that per-element tag test in `serialize_minify`; a second round did not seem worth it, and no row is above +2%. Fixes #2182
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.
minifyparsed the document with scripting off and serialized that tree. 🧱 A browser with scripting on reads<noscript>content as raw text, so the minified markup could build a different tree there, with elements the source kept as text. After this changeminifyoutput re-parses to the source's tree under both scripting modes. A generic raw text element with scripting on reads everything up to the first</noscriptas text, and the minifier's whitespace folding, tag omission and comment stripping change that text.Architecture
The parse records each
<noscript>with the source location of its tags and content, and with whether the parser state after it matches the state before it. A noscript closed by the first</noscriptafter its start tag, with that state restored, reads the same under both modes, so the minifier copies it from the source as written.For the first noscript that does not, the minifier minifies the markup before it only when that markup rebuilds the parser state at the noscript, and copies the source from there to the end. Otherwise it returns the source unchanged. Neither path parses the output again.
Behavior changes
minifycopies noscript content from the source, so whitespace, comments and attribute quoting inside it stay as written, except that CR and CRLF become LF.<body>before a first child that isbase,basefont,bgsound,noframes,noscriptortitle, because the "after head" insertion mode would otherwise move that child into<head>.minify_jsorminify_cssset, a<script>or<style>whose minified text would hold</noscriptin any letter case is written verbatim.serialize(Html(layout=Minify()))on a tree has no source to copy, so it still minifies noscript content.Fixes #1776.