Skip to content

Commit f733496

Browse files
fix(css): read a color function's separator grammar before folding it
`_COLOR_ARGUMENT_RE` states the runs between the separators without saying which separator stood there, so every spelling collapsed to the same argument list and a mixed one folded: `hsl(120, 50% 50%)` became `#40bf40`, and `lch(50, 20, 30)` and `oklch(0.5, 0.1, 30)` folded although the Lab family was specified after the legacy form and has no comma grammar at all. Each is a declaration an engine drops, so folding it changed what rendered. The arguments are now checked against the two grammars CSS Color 4 states — `A B C` with an optional `/ D`, and the legacy `A, B, C` with an optional `, D`, which `hsl()` alone among these functions has. That subsumes the percentage guard this branch added, which asked `inner.includes(",")`. `rgb()` reads its arguments through a scanner of its own in `_minifyColorFunction` and has the same defect; it is left for its own change rather than widened into this one.
1 parent 1c4e4a1 commit f733496

5 files changed

Lines changed: 43 additions & 11 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-
Fold an `hsl()` or `hwb()` whose modern grammar states its channels as numbers.
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.

‎lib/css/syntax-parser.js‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9133,6 +9133,11 @@ const _LAB_SCALES = new Map([
91339133
// One argument of a color function: a run between the whitespace, `,` and `/`
91349134
// its grammar separates on.
91359135
const _COLOR_ARGUMENT_RE = /[^\s,/]+/g;
9136+
// The two separator grammars a color function states: the modern `A B C` with
9137+
// an optional `/ D`, and the legacy `A, B, C` with an optional `, D`.
9138+
const _COLOR_MODERN_RE =
9139+
/^\s*[^\s,/]+(?:\s+[^\s,/]+){2}(?:\s*\/\s*[^\s,/]+)?\s*$/;
9140+
const _COLOR_LEGACY_RE = /^\s*[^\s,/]+(?:\s*,\s*[^\s,/]+){2,3}\s*$/;
91369141

91379142
/**
91389143
* Minify `hsl()` / `hwb()` / `lab()` / `lch()` / `oklab()` / `oklch()` to the
@@ -9194,6 +9199,13 @@ const _minifyPolarColorFunction = (
91949199
// reach the same few strings.
91959200
const args = inner.match(_COLOR_ARGUMENT_RE);
91969201
if (args === null || (args.length !== 3 && args.length !== 4)) return null;
9202+
// WHY: the read above states the runs between the separators without saying
9203+
// which separator stood there, and CSS Color 4 gives `hsl()` alone a legacy
9204+
// comma grammar — `hwb()` and the Lab family were specified later and take
9205+
// the modern form only. A mixed spelling such as `hsl(120, 50% 50%)` matches
9206+
// neither, so folding it would hand a color to a declaration engines drop.
9207+
const legacy = _COLOR_LEGACY_RE.test(inner);
9208+
if (legacy ? !isHsl : !_COLOR_MODERN_RE.test(inner)) return null;
91979209
let alpha = 1;
91989210
if (args.length === 4) {
91999211
const parsed = _COLOR_NUMBER_RE.exec(args[3]);
@@ -9214,10 +9226,8 @@ const _minifyPolarColorFunction = (
92149226
// WHY: CSS Color 4 admits a bare `<number>` for these two channels in the
92159227
// modern space-separated grammar alone — the legacy comma form takes
92169228
// percentages only. Folding `hsl(120, 50, 50)` would hand invalid CSS a
9217-
// color, so the separator decides whether a missing `%` is readable.
9218-
if (inner.includes(",") && (first[2] !== "%" || second[2] !== "%")) {
9219-
return null;
9220-
}
9229+
// color, so the grammar decides whether a missing `%` is readable.
9230+
if (legacy && (first[2] !== "%" || second[2] !== "%")) return null;
92219231
const x = Number(first[1]) / 100;
92229232
const y = Number(second[1]) / 100;
92239233
if (x < 0 || x > 1 || y < 0 || y > 1) return null;

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,14 @@ Array [
5858
"/*! declined: hwb() — esbuild says #00c3ff, lightningcss says #00c4ff */.h08{color:hwb(194 0% 0%)}",
5959
"/*! the modern grammar takes a bare number for the two channels after the hue,
6060
so these name the colors their percentage spellings do */.h09{color:#40bf40;background:#933}",
61-
"/*! declined: the legacy comma form takes percentages alone, so a bare number
62-
there is invalid CSS rather than a color to fold */.h10{color:hsl(120,50,50);background:hwb(0,20,40)}",
61+
"/*! declined: \`hsl()\`'s legacy comma form takes percentages alone, and \`hwb()\`
62+
has no comma form at all — both spellings are invalid CSS, not a color */.h10{color:hsl(120,50,50);background:hwb(0,20,40)}",
63+
"/*! declined: a spelling mixing the two grammars matches neither, so the
64+
channels are read as written rather than folded */.h11{color:hsl(120,50% 50%);background:hsl(120 50%,50%)}.h12{color:hsl(120,50%,50% / .5);background:hsl(120 50% 50%,.5)}",
65+
"/*! ...including one with no separator before the alpha, and one where a \`/\`
66+
stands between the channels */.h13{color:hsl(120 50% 50% .5);background:hsl(120 / 50% / 50%)}",
67+
"/*! declined: the Lab family was specified after the legacy form and takes the
68+
modern grammar only, so a comma there is invalid however it is spelled */.h14{color:lch(50,20,30);background:oklch(.5,.1,30)}",
6369
"/*! declined: the stops interpolate in the space the gradient names, and a
6470
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))}",
6571
"/*! declined without a space too: a stop outside sRGB is handed back as written

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,14 @@ Array [
5858
"/*! declined: hwb() — esbuild says #00c3ff, lightningcss says #00c4ff */.h08{color:hwb(194 0% 0%)}",
5959
"/*! the modern grammar takes a bare number for the two channels after the hue,
6060
so these name the colors their percentage spellings do */.h09{color:#40bf40;background:#933}",
61-
"/*! declined: the legacy comma form takes percentages alone, so a bare number
62-
there is invalid CSS rather than a color to fold */.h10{color:hsl(120,50,50);background:hwb(0,20,40)}",
61+
"/*! declined: \`hsl()\`'s legacy comma form takes percentages alone, and \`hwb()\`
62+
has no comma form at all — both spellings are invalid CSS, not a color */.h10{color:hsl(120,50,50);background:hwb(0,20,40)}",
63+
"/*! declined: a spelling mixing the two grammars matches neither, so the
64+
channels are read as written rather than folded */.h11{color:hsl(120,50% 50%);background:hsl(120 50%,50%)}.h12{color:hsl(120,50%,50% / .5);background:hsl(120 50% 50%,.5)}",
65+
"/*! ...including one with no separator before the alpha, and one where a \`/\`
66+
stands between the channels */.h13{color:hsl(120 50% 50% .5);background:hsl(120 / 50% / 50%)}",
67+
"/*! declined: the Lab family was specified after the legacy form and takes the
68+
modern grammar only, so a comma there is invalid however it is spelled */.h14{color:lch(50,20,30);background:oklch(.5,.1,30)}",
6369
"/*! declined: the stops interpolate in the space the gradient names, and a
6470
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))}",
6571
"/*! declined without a space too: a stop outside sRGB is handed back as written

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,19 @@
120120
/*! the modern grammar takes a bare number for the two channels after the hue,
121121
so these name the colors their percentage spellings do */
122122
.h09 { color: hsl(120 50 50); background: hwb(0 20 40) }
123-
/*! declined: the legacy comma form takes percentages alone, so a bare number
124-
there is invalid CSS rather than a color to fold */
123+
/*! declined: `hsl()`'s legacy comma form takes percentages alone, and `hwb()`
124+
has no comma form at all — both spellings are invalid CSS, not a color */
125125
.h10 { color: hsl(120, 50, 50); background: hwb(0, 20, 40) }
126+
/*! declined: a spelling mixing the two grammars matches neither, so the
127+
channels are read as written rather than folded */
128+
.h11 { color: hsl(120, 50% 50%); background: hsl(120 50%, 50%) }
129+
.h12 { color: hsl(120, 50%, 50% / .5); background: hsl(120 50% 50%, .5) }
130+
/*! ...including one with no separator before the alpha, and one where a `/`
131+
stands between the channels */
132+
.h13 { color: hsl(120 50% 50% .5); background: hsl(120 / 50% / 50%) }
133+
/*! declined: the Lab family was specified after the legacy form and takes the
134+
modern grammar only, so a comma there is invalid however it is spelled */
135+
.h14 { color: lch(50, 20, 30); background: oklch(0.5, 0.1, 30) }
126136
/*! declined: the stops interpolate in the space the gradient names, and a
127137
color converted through sRGB maps back into it as a different point */
128138
.g01 { background-image: linear-gradient(90deg in lch, red, lch(50 20 30)) }

0 commit comments

Comments
 (0)