Skip to content

Commit d1e84fa

Browse files
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.
1 parent e2023b0 commit d1e84fa

5 files changed

Lines changed: 29 additions & 1 deletion

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+
Read every color function's separator grammar before folding it, and fold an `hsl()` or `hwb()` whose modern grammar states its channels as numbers.

‎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)) }

0 commit comments

Comments
 (0)