Repository navigation
🐛 fix(css): keep token boundaries when unwrapping calc() - #2197
Conversation
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
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.
Merging this PR will improve performance by 11.09%1 warning
Performance Changes
Tip Curious why performance improved? Comment Comparing Footnotes
|
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.
Folding a
calc()could glue its result to a neighbouring token, which changed how the minified stylesheet parses, and acalc()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. Acalc()group that reaches the end of the input now counts as closed, so the first pass folds it.Behavior changes
calc()group left open at the end of the input folds on the first pass.