Skip to content

🐛 fix(minify): keep end tags a stripped comment hides - #2184

Merged
gaborbernat merged 5 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/n56-minify-idempotent
Nov 15, 2026
Merged

gaborbernat merged 5 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/n56-minify-idempotent

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

minify is documented as idempotent, but minifying its own output could change the document. 🔁 Once this merges, a second pass returns minified output unchanged, so pipelines that minify more than once get stable results. Fixes #2182.

The minifier drops comments and some tags that the HTML parser would rebuild anyway. In two places it dropped a comment and then decided as if the comment were still there, so the output lost a tag or a newline the reparse needs. The serializer now skips stripped comments when it makes those decisions.

Behavior changes

  • Output keeps an ancestor's end tag when a stripped comment follows the formatting content that ends it. The second pass used to restore that tag.
  • Output keeps one extra newline at the start of a pre or listing whose first text starts with a newline after a stripped comment, since the parser drops that newline.

Output differs from before only in those two cases.

Minifying the minified output again could change it, so minify was not
a fixed point. Two causes:

- A stripped comment reset the walk's trailing-formatting state, so an
  ancestor end tag was dropped that the reparse needs once the formatting
  element is the last content.
- The body start tag was dropped before base, basefont, bgsound, noframes,
  noscript and title. The parser files those elements under head, which
  changed the tree.

Refs tox-dev#2182
@gaborbernat gaborbernat added the bug Something isn't working label Nov 15, 2026
@codspeed

codspeed Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 582 untouched benchmarks
⏩ 89 skipped benchmarks1


Comparing gaborbernat:sec-fix/n56-minify-idempotent (d06c674) with main (612eda8)

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. ↩

…idempotent

# Conflicts:
#	src/turbohtml/_c/serialize/minify.c
#	tests/serialize/test_minify.py
The parser drops the newline that opens a pre or listing. When a stripped
comment came before the text, the minifier looked at the comment as the
first child, did not write the newline the reparse needs, and re-minifying
changed the output.

The leading-newline check now reads the first content child, skipping
stripped comments. The other serializers keep their first-child check.

Refs tox-dev#2182
The leading-newline check ran for every element before its tag test.
Only pre, textarea and listing need it, so the comment skip now sits
behind that test.

Refs tox-dev#2182
@gaborbernat
gaborbernat merged commit 946e021 into tox-dev:main Nov 15, 2026
30 checks passed
@gaborbernat gaborbernat changed the title 🐛 fix(minify): keep end tags and body start tag 🐛 fix(minify): keep end tags a stripped comment hides Nov 16, 2026
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.

minify output is not a fixed point for some documents

1 participant