Skip to content

🐛 fix(css): keep at-statements browsers ignore dead in minify_css - #2145

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:sec-fix/n3h9hnaa
Nov 15, 2026
Merged

gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:sec-fix/n3h9hnaa

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

minify_css() could turn an at-statement that browsers ignore into one they honor. 🎨 An @import, @namespace, @charset or @layer statement sits behind rules that make it dead in the source. Minifying removed an empty rule in front of it, and a stray } split a statement that browsers drop whole, so the remainder became a statement of its own. A minified sheet could then load a stylesheet or apply a namespace the original did not. After this change the minified sheet keeps every such statement dead, as the source has it.

A browser honors @import only ahead of every rule other than @charset and @layer statements (CSS Cascade 5), and @namespace only ahead of every rule other than those and @import (CSS Namespaces 3). An empty rule has no effect on matching, but it still counts as a rule. The minifier holds such rules back and writes them in front of the next statement whose order they affect. Top-level <!-- and --> tokens are skipped as in consume a list of rules. Selector minification keeps a separator where removing it would spell a <!-- or --> token.

Behavior changes

  • Top-level <!-- and --> before rules and at-rules are dropped from the output.
  • An empty rule, or an empty @media, @supports or @container, before an @import, @namespace or @charset at the top level is kept. The rule moves to just before that statement, so it can come after an @layer statement that followed it in the source. An empty @media is written as *{}, so such output is a few bytes longer.
  • A top-level at-rule prelude that contains a } is kept through to its ; or block.
  • Selectors keep a space or comment between a -- name and >, and between <! and a -- name. Selector lists with such an arm are not merged.

Fixes #1745.

The CSS minifier could turn an @import, @namespace, @charset or @layer
statement that a browser ignores in the source into a live one. A
top-level at-statement prelude ended at a stray `}`, so the minifier
split a dropped at-rule into a statement of its own. An empty rule or
empty conditional group rule was removed even when it was what kept a
following @import or @namespace out of place. A top-level `<!--` or
`-->` was kept as stray text, and some selector or escape spellings
wrote one the source did not have.

The top-level prelude now reads through `}` to a `;` or a block, as CSS
Syntax 3 parses it. Empty top-level rules are held and written back in
front of the next @import, @namespace or @charset. The top level skips
`<!--` and `-->` like a browser, and the selector and name writers no
longer spell one where the source had none.

Fixes tox-dev#1745.
@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 improve performance by 8.43%

2 warnings

⚠️ 3 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 3 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

See affected benchmarks

⚡ 3 improved benchmarks
✅ 579 untouched benchmarks
⏩ 89 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_mutating_feature[path-xpath-one-cold] 143.2 µs 124.9 µs +14.68%
⚡ test_feature[serialize-inner] 5.8 ms 5.5 ms +5.53%
⚡ test_feature[serialize-inner-indent] 6.2 ms 5.9 ms +5.33%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing gaborbernat:sec-fix/n3h9hnaa (9204b46) with main (398bcb6)

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

@gaborbernat
gaborbernat merged commit 8b034c1 into tox-dev:main Nov 15, 2026
31 checks passed
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_css() writes an @import that the source sheet does not load

1 participant