Skip to content

🐛 fix(css): keep token boundaries when unwrapping calc() - #2197

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

gaborbernat merged 5 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/issue-1852

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Folding a calc() could glue its result to a neighbouring token, which changed how the minified stylesheet parses, and a calc() left open at the end of the input folded only on a second pass. 🧮 Once this merges, the minified output keeps token boundaries and is stable on repeat. Fixes #1852.

The minifier folds calc() into a value and writes it in function arguments, filter values and folded colors. The folded text now gets a space wherever it would re-tokenize together with a neighbouring token, using the merge check the minifier already has. The CSS tokenizer decides that a number followed by an identifier is one token, for example. A calc() group that reaches the end of the input now counts as closed, so the first pass folds it.

Behavior changes

  • A calc() group left open at the end of the input folds on the first pass.
  • A folded value that would merge with an adjacent token keeps a separator. Output without such a merge is unchanged.

A calc() that folds to one value was written into its function's
arguments, into a filter value and into a folded color with no
separator. The folded text could then read as one token with the
token before it or after it. The fold now keeps a space wherever
re-tokenizing would merge the two.

A calc() group left open at the end of the input now folds on the
first pass. The renderer already closes such a group, so the old
output folded only on the second pass.

Fixes tox-dev#1852
@gaborbernat gaborbernat added the bug Something isn't working label Nov 15, 2026
pre-commit-ci Bot and others added 3 commits November 15, 2026 16:20
A folded calc() next to a sign, a name or an exponent merged into a
single signed number or dimension on main. Add the reported shapes to
the boundary table, widen the property generator with a name before a
sign and names that read as exponents, and rewrap the changelog
fragment as the formatter requires.
@codspeed

codspeed Bot commented Nov 15, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 11.09%

1 warning

⚠️ 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] 107.1 µs 88.8 µs +20.59%
⚡ test_feature[query-closest] 256.1 µs 237.5 µs +7.84%
⚡ test_feature[select-nth] 462.8 µs 438.9 µs +5.43%

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-1852 (fdc4f68) with main (efd4b6a)

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 11a31c2 into tox-dev:main Nov 15, 2026
30 checks passed
gaborbernat added a commit that referenced this pull request Nov 15, 2026
Fixes #1835

## Summary

Minifying CSS a second time changed the output. The first pass dropped
or merged whitespace and comments that the next pass reads as token
boundaries, or left a construct that the next pass folded or dropped.
This PR keeps the separators the source had where they are needed, folds
an unclosed `calc()` the way its closed form folds, and writes each kept
bang comment once.

## Causes and fixes

- 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 a hex digit follows.
- 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. The
selector helpers that look at the next token skip comments.
- A folded `rgb()` or `hsl()` color followed directly by a name is
separated from it.
- A digit ending an identifier no longer gets a space before a following
`%`.
- 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 ended at a bang comment read its end from the comment's
placeholder, which lies outside the stylesheet. The span now ends at the
last token before it, and a leading bang in a `unicode-range` value is
no longer the span's start.

Folding a `calc()` group that the input leaves open at the end is
handled by #1852 and is not changed here.

## Behavior changes

- Declarations where whitespace or a comment separated a unicode-range
start from the next piece keep that separator, so the output differs
from the previous release for those inputs. Inputs that the source ran
together are unchanged.
- A folded color followed directly by a name or an escape gets a space
before it, as the source would tokenize.
- A universal selector followed by a comment before a class, hash,
attribute or pseudo-class is dropped on the first pass.
- A kept function argument with a bang comment writes it inline only,
not again after the rule.

## Tests

- Behavior tests in `tests/css/minify/test_css.py` pin each shape above
with its expected output and assert that a second pass returns the first
pass's output unchanged. Most of them fail on the base commit.
- Seeded property tests (400 seeds each) generate unicode-range, calc,
comment and bang-comment values, and selectors with comments and an
at-rule prelude, and assert that `minify_css(minify_css(x)) ==
minify_css(x)`.
- Coverage: every added C line and branch is covered by
`tests/css/minify/test_css.py`.

## Performance

Callgrind on release builds, instructions per call from the difference
of 220 and 20 repetitions, `PYTHONHASHSEED=0`.

Base is `upstream/main` at cdd9456, measured on a clean build of that
commit and of this head. Each row is the callgrind difference between
220 and 20 repetitions, in instructions per call.

| Row | main | head | delta |
| -------------------------------- | ----------: | ----------: | -----:
|
| minify-css | 7,707,624.0 | 7,713,952.0 | +0.08% |
| minify-css-nested-functions | 198,622.0 | 198,419.0 | -0.10% |
| minify-css-unicode-ranges-sorted | 439,229.0 | 441,406.0 | +0.50% |
| minify-css-number-arithmetic | 744,524.0 | 743,113.0 | -0.19% |
| minify-js-integers (control) | 4,128,932.0 | 4,128,932.0 | 0.00% |

Noise floor: before the rebase onto #2197, main measured again on a
clean build matched an earlier repeat exactly. The first main run, taken
before that repeat, differed by about 3 instructions per call on two
rows and was not reproduced.

The two rows above the noise floor pay for the range-start check in the
value assembler and the separator check after folded calls. The
unicode-ranges row also pays for the extra range-scan step. Neither is
in the hot loop of ordinary declarations.
@gaborbernat gaborbernat changed the title Keep token boundaries when unwrapping calc() 🐛 fix(css): keep token boundaries when unwrapping calc() 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 calc() unwrapping glues the value to the next token

1 participant