Skip to content

Commit 69cc45e

Browse files
fix(css): read an rgb() separator grammar before folding it (#22251)
* fix(css): read an rgb()'s separator grammar before folding it `_minifyColorFunction` cuts on whitespace, `,` and `/` in one pass without recording which separator stood where, so every spelling collapsed to the same argument list: `rgb(1, 2 3)`, `rgb(1 2, 3)`, `rgb(1 2 3, .5)`, `rgb(1, 2, 3 / .5)`, `rgb(1 2 3 .5)` and `rgb(1 / 2 / 3)` all folded to a color. Each is a declaration an engine drops, so folding it changed what rendered. This is the defect #22242 fixed for `hsl()`, `hwb()` and the Lab family, left out of that change because `rgb()` reads its arguments through a scanner of its own. The same two grammars decide it — the modern `A B C` with an optional `/ D`, and the legacy `A, B, C` with an optional `, D`, both of which `rgb()` has — so the check is the pair of regexps that change already added, applied after the scan rather than before it, where only a plausible color reaches it. The pending changeset for #22242 covers this, so it is widened rather than duplicated. * chore(changeset): keep the color entry within the length limit The union of the two color fixes ran to 148 characters, where `.changeset/**` asks for one imperative sentence of 80 or fewer. Both facts still fit: the unitless channels that now fold, and the separator grammar that decides whether anything folds at all. * test(javascript): assert what load() owes, not what Deno can import `should install onto terser` asserted that under Deno `phases` is empty, because Deno could not import terser's own sources when the test was written. It sometimes can, and then the test fails — it went red once on #22243, a pull request touching only CSS fixtures, and cost a re-run of a 21-minute job to establish it was not that change's doing. Which sources a runtime reaches is the one thing `load` is written to tolerate: it catches any failure and hands back terser's published entry point. So the runtime is the wrong thing to assert. What `load` owes everywhere is that it installed phases this build declares and nothing else, and Node, which always reaches the sources, still owes the whole set — both of which this now states. The fallback path stays covered by behaviour rather than by a hardcoded outcome: every `should mangle exactly as terser does` case compares webpack's `minify` against terser's own, whichever way `load` resolved.
1 parent 0813cba commit 69cc45e

6 files changed

Lines changed: 38 additions & 7 deletions

File tree

‎.changeset/037-css-unitless-hsl-hwb-channels.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": patch
33
---
44

5-
Read a color function's separator grammar before folding it, and fold an `hsl()` or `hwb()` whose modern grammar states its channels as numbers.
5+
Fold unitless `hsl()`/`hwb()` channels, and only where the separators are valid.

‎lib/css/syntax-parser.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7938,6 +7938,14 @@ const _minifyColorFunction = (fn, inner, hexAlpha, operand) => {
79387938
}
79397939
}
79407940
if (args.length !== 3 && args.length !== 4) return null;
7941+
// WHY: the scan above states the runs between the separators without saying
7942+
// which separator stood there, so `rgb(1, 2 3)` and `rgb(1 2 3 .5)` read as
7943+
// three or four arguments like any other spelling. CSS Color 4 gives `rgb()`
7944+
// a modern `A B C` with an optional `/ D` and a legacy `A, B, C` with an
7945+
// optional `, D`; a spelling matching neither is a declaration engines drop.
7946+
if (!_COLOR_LEGACY_RE.test(inner) && !_COLOR_MODERN_RE.test(inner)) {
7947+
return null;
7948+
}
79417949
/** @type {number[]} */
79427950
const channels = [];
79437951
// The channels as written, before the byte: what a mix or a relative

‎test/configCases/css/minimize-colors/__snapshots__/ConfigCacheTest.snap‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ Array [
6666
stands between the channels */.h13{color:hsl(120 50% 50% .5);background:hsl(120 / 50% / 50%)}",
6767
"/*! declined: the Lab family was specified after the legacy form and takes the
6868
modern grammar only, so a comma there is invalid however it is spelled */.h14{color:lch(50,20,30);background:oklch(.5,.1,30)}",
69+
"/*! \`rgb()\` has both grammars, so both spellings fold — and a mixed one matches
70+
neither, whichever separator was the odd one out */.c01{color:#010203;background:#010203}.c02{color:rgb(1,2 3);background:rgb(1 2,3)}",
71+
"/*! ...the same where the alpha is the argument spelled wrong, and where a \`/\`
72+
stands between the channels rather than before the alpha */.c03{color:rgb(1 2 3,.5);background:rgb(1,2,3 / .5)}.c04{color:rgb(1 2 3 .5);background:rgb(1 / 2 / 3)}",
73+
"/*! ...and \`rgba()\` is the same function, so it reads the same grammars */.c05{color:#01020380;background:rgba(255,0 0,.5)}",
6974
"/*! declined: the stops interpolate in the space the gradient names, and a
7075
color converted through sRGB maps back into it as a different point */.g01{background-image:linear-gradient(90deg in lch,red,lch(50 20 30))}.g02{background-image:linear-gradient(in oklch,red,oklch(.7 .1 200))}",
7176
"/*! declined without a space too: a stop outside sRGB is handed back as written

‎test/configCases/css/minimize-colors/__snapshots__/ConfigTest.snap‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ Array [
6666
stands between the channels */.h13{color:hsl(120 50% 50% .5);background:hsl(120 / 50% / 50%)}",
6767
"/*! declined: the Lab family was specified after the legacy form and takes the
6868
modern grammar only, so a comma there is invalid however it is spelled */.h14{color:lch(50,20,30);background:oklch(.5,.1,30)}",
69+
"/*! \`rgb()\` has both grammars, so both spellings fold — and a mixed one matches
70+
neither, whichever separator was the odd one out */.c01{color:#010203;background:#010203}.c02{color:rgb(1,2 3);background:rgb(1 2,3)}",
71+
"/*! ...the same where the alpha is the argument spelled wrong, and where a \`/\`
72+
stands between the channels rather than before the alpha */.c03{color:rgb(1 2 3,.5);background:rgb(1,2,3 / .5)}.c04{color:rgb(1 2 3 .5);background:rgb(1 / 2 / 3)}",
73+
"/*! ...and \`rgba()\` is the same function, so it reads the same grammars */.c05{color:#01020380;background:rgba(255,0 0,.5)}",
6974
"/*! declined: the stops interpolate in the space the gradient names, and a
7075
color converted through sRGB maps back into it as a different point */.g01{background-image:linear-gradient(90deg in lch,red,lch(50 20 30))}.g02{background-image:linear-gradient(in oklch,red,oklch(.7 .1 200))}",
7176
"/*! declined without a space too: a stop outside sRGB is handed back as written

‎test/configCases/css/minimize-colors/style.css‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@
133133
/*! declined: the Lab family was specified after the legacy form and takes the
134134
modern grammar only, so a comma there is invalid however it is spelled */
135135
.h14 { color: lch(50, 20, 30); background: oklch(0.5, 0.1, 30) }
136+
/*! `rgb()` has both grammars, so both spellings fold — and a mixed one matches
137+
neither, whichever separator was the odd one out */
138+
.c01 { color: rgb(1, 2, 3); background: rgb(1 2 3) }
139+
.c02 { color: rgb(1, 2 3); background: rgb(1 2, 3) }
140+
/*! ...the same where the alpha is the argument spelled wrong, and where a `/`
141+
stands between the channels rather than before the alpha */
142+
.c03 { color: rgb(1 2 3, .5); background: rgb(1, 2, 3 / .5) }
143+
.c04 { color: rgb(1 2 3 .5); background: rgb(1 / 2 / 3) }
144+
/*! ...and `rgba()` is the same function, so it reads the same grammars */
145+
.c05 { color: rgba(1, 2, 3, .5); background: rgba(255, 0 0, .5) }
136146
/*! declined: the stops interpolate in the space the gradient names, and a
137147
color converted through sRGB maps back into it as a different point */
138148
.g01 { background-image: linear-gradient(90deg in lch, red, lch(50 20 30)) }

‎test/unitCases/syntax-printer.unittest.js‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// cspell:ignore fnames
44

5-
const { load } = require("../../lib/javascript/syntax").printer;
5+
const { load, PHASES } = require("../../lib/javascript/syntax").printer;
66

77
/**
88
* Sources chosen for the decisions the mangler makes: which scope hands out a
@@ -90,13 +90,16 @@ describe("syntax-printer", () => {
9090
it("should install onto terser", async () => {
9191
const terser = await load();
9292
expect(typeof terser.minify).toBe("function");
93-
// Deno cannot import terser's own sources here, so `load` hands back
94-
// terser's published entry point with no phase of ours installed —
95-
// the fallback every phase is written to allow.
93+
// WHY: whether a runtime reaches terser's own sources is the one thing
94+
// `load` is written to tolerate, and Deno reaches them only sometimes —
95+
// so asserting either outcome there failed at random. What it owes
96+
// everywhere is to install phases this build declares and nothing else;
97+
// Node, which always reaches the sources, owes the whole set.
98+
expect(PHASES.map((phase) => phase.name)).toEqual(
99+
expect.arrayContaining(terser.phases)
100+
);
96101
if (!("Deno" in globalThis)) {
97102
expect(terser.phases).toContain("mangle");
98-
} else {
99-
expect(terser.phases).toEqual([]);
100103
}
101104
});
102105

0 commit comments

Comments
 (0)