Skip to content

Commit 31e6fe1

Browse files
fix(css): read an escaped name before deciding a color is an operand
`\66 rom` names the relative-color keyword, and an escaped `color-mix` or gradient name names those, so the guard compared a spelling the source need not use. The escaped gradient name was already folding before this branch.
1 parent aa51526 commit 31e6fe1

4 files changed

Lines changed: 73 additions & 27 deletions

File tree

‎lib/css/syntax.js‎

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4235,36 +4235,79 @@ const GRADIENT_FUNCTION_RE = /(?:^|-)(?:linear|radial|conic)-gradient$/i;
42354235
*/
42364236
const _gradientName = (i0) => _input.slice(_starts[i0], _aux0[i0]);
42374237

4238+
/**
4239+
* Whether a source range holds an escape, so its unescaped spelling is the one
4240+
* a name has to be read from.
4241+
* @param {number} start range start
4242+
* @param {number} end range end (exclusive)
4243+
* @returns {boolean} true when the range holds a `\`
4244+
*/
4245+
const _rangeHasEscape = (start, end) => {
4246+
for (let i = start; i < end; i++) {
4247+
if (_input.charCodeAt(i) === CC_REVERSE_SOLIDUS) return true;
4248+
}
4249+
return false;
4250+
};
4251+
4252+
/** Longest `from` spelling: each code point as `\` + 6 hex digits + CRLF. */
4253+
const MAX_ESCAPED_FROM_LENGTH = 4 * (1 + 6 + 2);
4254+
4255+
/**
4256+
* Whether an ident names the relative-color keyword — `\66 rom` names it too,
4257+
* and only an escaped ident is longer than the four bytes it spells.
4258+
* @param {number} start ident start offset
4259+
* @param {number} end ident end offset (exclusive)
4260+
* @returns {boolean} true when the ident names `from`
4261+
*/
4262+
const _identNamesFrom = (start, end) => {
4263+
const length = end - start;
4264+
if (length === 4) return rangeEqualsLowerCase(_input, start, end, "from");
4265+
if (length < 4 || length > MAX_ESCAPED_FROM_LENGTH) return false;
4266+
return (
4267+
_rangeHasEscape(start, end) &&
4268+
equalsLowerCase(unescapeIdentifier(_input.slice(start, end)), "from")
4269+
);
4270+
};
4271+
42384272
/**
42394273
* Whether a function reads the colors below it as numbers rather than showing
42404274
* them: a gradient interpolates its stops, `color-mix()` its arguments, and a
42414275
* relative color reads its origin's channels off as the `r` / `l` / `h`
4242-
* keywords. The origin is recognized by the `from` its arguments open with, so
4243-
* a function webpack knows nothing about guards its origin too.
4276+
* keywords. The origin is the `from` its arguments open with, so a function
4277+
* webpack knows nothing about guards its origin too.
42444278
* @param {number} i0 the function's node index
42454279
* @param {number} vs its first child's slot
42464280
* @param {number} ve one past its last child's slot
42474281
* @returns {boolean} true when a color below it is an operand, not a value
42484282
*/
42494283
const _readsColorsAsNumbers = (i0, vs, ve) => {
4250-
// Every gradient name ends in `gradient` and `color-mix` in `mix`, so one
4251-
// code point rejects the rest before either name is sliced.
4252-
const last = _input.charCodeAt(_aux0[i0] - 1) | 0x20;
4253-
if (last === CC_LOWER_T) {
4254-
if (GRADIENT_FUNCTION_RE.test(_gradientName(i0))) return true;
4255-
} else if (
4256-
last === CC_LOWER_X &&
4257-
rangeEqualsLowerCase(_input, _starts[i0], _aux0[i0], "color-mix")
4258-
) {
4259-
return true;
4284+
const start = _starts[i0];
4285+
const end = _aux0[i0];
4286+
if (_rangeHasEscape(start, end)) {
4287+
// An escape spells either name too, so the unescaped spelling decides.
4288+
const name = unescapeIdentifier(_input.slice(start, end));
4289+
if (GRADIENT_FUNCTION_RE.test(name) || equalsLowerCase(name, "color-mix")) {
4290+
return true;
4291+
}
4292+
} else {
4293+
// Every gradient name ends in `gradient` and `color-mix` in `mix`, so one
4294+
// code point rejects the rest before either name is sliced.
4295+
const last = _input.charCodeAt(end - 1) | 0x20;
4296+
if (last === CC_LOWER_T) {
4297+
if (GRADIENT_FUNCTION_RE.test(_gradientName(i0))) return true;
4298+
} else if (
4299+
last === CC_LOWER_X &&
4300+
rangeEqualsLowerCase(_input, start, end, "color-mix")
4301+
) {
4302+
return true;
4303+
}
42604304
}
42614305
for (let i = vs; i < ve; i++) {
42624306
const child = _flat[i];
42634307
const childType = _types[child];
42644308
if (childType === T_WHITESPACE) continue;
42654309
return (
4266-
childType === T_IDENT &&
4267-
rangeEqualsLowerCase(_input, _starts[child], _ends[child], "from")
4310+
childType === T_IDENT && _identNamesFrom(_starts[child], _ends[child])
42684311
);
42694312
}
42704313
return false;
@@ -7107,9 +7150,8 @@ const _minifyColorFunction = (fn, inner, hexAlpha, operand) => {
71077150
} else {
71087151
if (pct) percentChannelCount++;
71097152
const channel = pct ? (v * 255) / 100 : v;
7110-
// The engine keeps the channel as written, so a hex is that color only
7111-
// where it is already a byte — `rgb(50% 50% 50%)` is 127.5, not `gray`.
7112-
// One out of range is not: the engine clamps it to the same byte.
7153+
// A hex is the same color only where the channel is already a byte —
7154+
// `rgb(50% 50% 50%)` is 127.5, not `gray`. One out of range is: it clamps.
71137155
if (!Number.isInteger(channel)) roundedChannel = true;
71147156
channels.push(_clamp255(channel));
71157157
}
@@ -12273,12 +12315,8 @@ const printer = (path, writer) => {
1227312315
// so rewriting it asks a different question (esbuild, clean-css and
1227412316
// lightningcss leave the condition alone too).
1227512317
// A color read as numbers — a gradient stop, a `color-mix()` argument, a
12276-
// relative color's origin — is left as written wherever converting it is
12277-
// not exact: a space other than sRGB is echoed there rather than
12278-
// computed, so a hex moves the ramp, the mix or the channels the
12279-
// keywords read. The legacy sRGB forms all compute to `rgb(…)` whichever
12280-
// way they are spelled, so those still fold where their channels are
12281-
// already bytes.
12318+
// relative color's origin — is left as written unless converting it is
12319+
// exact, which the legacy sRGB forms are where their channels are bytes.
1228212320
const color = _inSupportsPrelude
1228312321
? null
1228412322
: _minifyColorFunction(fn, inner, _hexAlphaAllowed, _inColorOperand) ||
@@ -13039,10 +13077,9 @@ let _inCustomProperty = false;
1303913077
let _rewriteCustomProperties = false;
1304013078
let _inSubstitutedValue = false;
1304113079

13042-
// Whether the color being printed is read as numbers rather than shown: a
13043-
// gradient stop and a `color-mix()` argument are interpolated, and a relative
13044-
// color's origin has its channels read off. Converting one through 8-bit sRGB
13045-
// moves the color the engine computes there, not only how it is spelled.
13080+
// Whether the color being printed is read as numbers rather than shown — a
13081+
// gradient stop, a `color-mix()` argument, a relative color's origin — where an
13082+
// 8-bit hex moves the color the engine computes, not only how it is spelled.
1304613083
let _inColorOperand = false;
1304713084
// How many math functions the walk is inside. Non-zero is where `*` and `/` are
1304813085
// operators that need no whitespace rather than value separators; above one is

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,7 @@ Array [
7171
"/*! ...and \`from\` heading a function's name is not the keyword */.r09{color:from(#976c67)}",
7272
"/*! declined: \`color-mix()\` interpolates its arguments the way a gradient does */.m01{color:color-mix(in oklab,lch(50% 20 30),red)}.m02{color:color-mix(in srgb,hsl(120 50% 50%),red)}",
7373
"/*! the legacy sRGB forms fold there too, where their channels are already bytes */.m03{color:color-mix(in srgb,#010203,red)}",
74+
"/*! declined however the keyword is spelled: \`\\\\66 rom\` names it too, as an
75+
escaped \`color-mix\` or gradient name names those */.r10{color:rgb(from rgb(50% 50% 50%) r g b)}.m04{color:color-\\\\6d ix(in oklab,lch(50% 20 30),red)}.g05{background-image:linear-\\\\67 radient(lch(50 20 30),red)}",
7476
]
7577
`;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,7 @@ Array [
7171
"/*! ...and \`from\` heading a function's name is not the keyword */.r09{color:from(#976c67)}",
7272
"/*! declined: \`color-mix()\` interpolates its arguments the way a gradient does */.m01{color:color-mix(in oklab,lch(50% 20 30),red)}.m02{color:color-mix(in srgb,hsl(120 50% 50%),red)}",
7373
"/*! the legacy sRGB forms fold there too, where their channels are already bytes */.m03{color:color-mix(in srgb,#010203,red)}",
74+
"/*! declined however the keyword is spelled: \`\\\\66 rom\` names it too, as an
75+
escaped \`color-mix\` or gradient name names those */.r10{color:rgb(from rgb(50% 50% 50%) r g b)}.m04{color:color-\\\\6d ix(in oklab,lch(50% 20 30),red)}.g05{background-image:linear-\\\\67 radient(lch(50 20 30),red)}",
7476
]
7577
`;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,8 @@
149149
.m02 { color: color-mix(in srgb, hsl(120 50% 50%), red) }
150150
/*! the legacy sRGB forms fold there too, where their channels are already bytes */
151151
.m03 { color: color-mix(in srgb, rgb(1, 2, 3), red) }
152+
/*! declined however the keyword is spelled: `\66 rom` names it too, as an
153+
escaped `color-mix` or gradient name names those */
154+
.r10 { color: rgb(\66 rom rgb(50% 50% 50%) r g b) }
155+
.m04 { color: color-\6d ix(in oklab, lch(50% 20 30), red) }
156+
.g05 { background-image: linear-\67 radient(lch(50 20 30), red) }

0 commit comments

Comments
 (0)