Skip to content

🐛 fix(css): keep separators minify_css needs - #2198

Merged
gaborbernat merged 3 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/issue-1835
Nov 15, 2026
Merged

gaborbernat merged 3 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/issue-1835

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Minifying a stylesheet a second time could change it, because the first pass dropped or merged whitespace and comments that the next pass reads as token boundaries. Pipelines that minify more than once got different output on each run. 🧩 Once this merges, the minified stylesheet is stable on repeat, and the separators it needs stay in place. Fixes #1835.

The minifier now keeps a separator wherever the next token would otherwise read differently under the CSS tokenizer. That covers unicode-range pieces, a folded color followed by a number, and a universal selector or a class that a comment separates from the rest of its compound. A bang comment inside a kept function argument is written once, where it stands.

Behavior changes

  • A unicode-range declaration whose start is separated from the next piece by whitespace or a comment keeps that separator. Such declarations differ from the previous release. Input that the source ran together is unchanged.
  • The tokenizer now reads a unicode-range as hex digits followed by ? wildcards, with no - range after a wildcard (CSS Syntax 3 §4.3.6).
  • A folded color followed directly by a number gets a space before the number.
  • A universal selector followed by a comment and then a class, hash, attribute or pseudo-class is dropped on the first pass.
  • A kept function argument with a bang comment writes it once, inline.
  • A bang comment at the start or end of a kept raw value no longer crashes the minifier.

Minifying a stylesheet a second time changed its output, because the first
pass removed or merged whitespace and comments that the next pass reads as
token boundaries. The cases fixed here:

- A space between `u+` and a following name, number or `?` that starts with
  a hex digit is kept, so the output does not read as a unicode-range token.
  The range scan also stops at a `?` that is followed by a hex digit.
- A comment or space that separated a range token, a `?` or a `+` from the
  next piece is kept, in declarations and in custom property values.
- A universal selector followed by a comment and then a class, hash,
  attribute or pseudo-class is dropped, as the comment-free form is.
- A folded rgb() or hsl() color followed by a name is separated from it.
- A digit ending an identifier no longer gets a space before a following
  percent sign.
- A bang comment inside a function argument that is kept as written is
  written once, where it stands, instead of again after the rule.
- A raw span that ended at a bang comment read its end from the comment's
  placeholder, which is outside the stylesheet; the span now ends at the
  last token before it.

Fixes tox-dev#1835
@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 19.4%

2 warnings

⚠️ 2 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

⚠️ 2 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

⚡ 2 improved benchmarks
✅ 580 untouched benchmarks
⏩ 89 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_mutating_feature[path-xpath-one-cold] 142.9 µs 107.2 µs +33.35%
⚡ test_feature[text-content] 2.8 ms 2.6 ms +6.91%

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/issue-1835 (5783c93) with main (cdd9456)

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

The comp vector carries a spaced flag that the value assembler reads.
Several shorthand paths declared components without an initializer, so
the flag could be read uninitialized, which -Wmaybe-uninitialized reports
under -Werror.
# Conflicts:
#	src/turbohtml/_c/css/minify/css_calc.h
#	src/turbohtml/_c/css/minify/css_selector.h
@gaborbernat
gaborbernat merged commit 99c346f into tox-dev:main Nov 15, 2026
30 checks passed
gaborbernat added a commit that referenced this pull request Nov 15, 2026
`minify_css` and `minify_css_inline` gave a different result on a second
pass for some input with stray brackets, hex escapes or CDO/CDC tokens.
Four causes, each fixed here:

- A declaration whose name opened a block before its colon was kept on
the first pass and dropped on the second. CSS Syntax 3 §5.4.5 makes a
declaration start with an ident, so such a segment is no declaration,
and the first pass now drops it.
- An ident written apart from a `(` in stray text was joined to it, and
the two formed a function token. The next pass then closed that
function. The minifier now writes a space between them, which keeps the
tokens apart.
- A nested `calc()` or group left open at the end of the input stayed
unfolded on the first pass and folded on the second. The end of the
input closes such a group (CSS Syntax 3 §5.4.7), so the first pass now
folds it. A block left open after a hex escape also gets the closing
space that the second pass writes.
- A raw at-keyword name that ends in a hex escape took no closing space,
so the second pass, reading the escape's space back as part of the name,
wrote one more space. The first pass now writes the closing space a
spelled name already has.
- Stray text that was written after the last delimiter lost the
delimiter owed before an at-statement that followed, when an empty `}`
or `;` came between them. The first pass joined the two, and the second
pass split them again. The delimiter is now kept.

The tests cover a table of stray-bracket, hex-escape and CDO/CDC shapes
for both functions, a seeded property test over 10000 generated inputs
(the generator includes at-keywords, hex escapes, `<!--`, `-->` and
`}`), and a fixed-point check on each expected output.

### Behavior changes

- A declaration whose name holds a block opened before its colon is
dropped, in a stylesheet and in a `style` attribute.
- A stray ident followed by `(` in stylesheet text is separated from it
by a space.
- A nested `calc()` or group left open at the end of the input folds to
its value.
- A block left open at the end of the input after a hex escape closes
after a space.
- A raw at-keyword ending in a hex escape before a prelude gets two
spaces before the prelude, as a spelled name does.
- A stray CDO or CDC followed by an at-statement, with no delimiter
between, gets a `;` before the at-statement.

### Performance

Callgrind on release LTO builds (`-Dbuildtype=release -Db_lto=true`,
`CC=/usr/bin/cc`) of the base and the head, built at the same path
length. Run with `PYTHONHASHSEED=0` under `setarch -R` at 20 and 220
repetitions; the table shows the per-repetition difference in
instructions.

| Benchmark | base (Ir/rep) | head (Ir/rep) | delta |
| ---------------------------- | ------------: | ------------: | -----:
|
| minify-css | 7704434.0 | 7709557.0 | +0.07% |
| minify-css-conflicts | 21345660.0 | 21366630.0 | +0.10% |
| minify-css-merges | 49154.0 | 49264.0 | +0.22% |
| minify-css-number-arithmetic | 742604.0 | 742992.0 | +0.05% |
| minify-css-nested-functions | 196440.0 | 196447.0 | 0.00% |
| minify-js (control, no CSS) | 11692216.0 | 11692216.0 | 0.00% |

The base is `99c346fb1` (upstream #2198), the head `6da3b85ce`, which
merges that base with this branch. Both builds are release LTO, built
fresh at the same path length. A second run of the base gives exactly 0
on `minify-css`, so the noise floor is zero.

Four rows are above the +0.05% noise line, and all are within the 5%
ceiling. The changes in this PR that reach the measured paths are the
at-rule prelude separator, the stray-text delimiter bookkeeping, and the
declaration name check. An earlier round on the merges row removed most
of its cost (gating the new check on the escape flag), but the remainder
is not confirmed to come from the changed code, since no perturbation
build was run. The two optimization rounds on this PR are spent, so the
remaining rows are recorded here rather than optimized further.

Fixes #1853
@gaborbernat gaborbernat changed the title Keep separators minify_css needs 🐛 fix(css): keep separators minify_css needs 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_css is not idempotent

1 participant