Skip to content

Commit 6b1beda

Browse files
fix(css): write an opaque wide-gamut fallback without an alpha, and keep a mix's own hue method
Three findings from the review, two of them colors an engine reads differently: - `_wideGamutColor` was the third caller of `_alphaByteText` and the one still passing it `255`: an alpha above 254.5/255 quantizes to the opaque byte, and the helper answers `.` for it, so `color(display-p3 … / .)` reached a target that reads `color()` but no Lab family. The alpha is quantized before it is written now, and an opaque one is left off. - `_foldColorMix` held the hue interpolation method in module state across the two `_resolveColor` calls, and a nested `color-mix()` folds through the same function — so the inner method decided the outer mix. It is held locally until both colors resolve. - the mark `unusedSymbols` leaves on a rule is now read whichever reason drops an empty rule, rather than after a `||` that a qualified rule short-circuits: an unread mark rides the node back into the pool, which is the hazard `_takeUnusedRule` documents. The two changesets this pull request added are one, as the guide asks. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_018tNx7sU4SbnKFhFeofDVr5
1 parent a32e693 commit 6b1beda

4 files changed

Lines changed: 38 additions & 13 deletions

File tree

‎.changeset/011-css-unused-symbols.md‎

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

5-
Add `optimization.minimize.css.unusedSymbols`, take out `@charset`, squeeze a grid's null cells and name a matrix.
5+
Add `unusedSymbols` and `pseudoClasses`, and evaluate a CSS color function.

‎.changeset/012-css-evaluate-color-functions.md‎

Lines changed: 0 additions & 5 deletions
This file was deleted.

‎lib/css/syntax.js‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4953,11 +4953,13 @@ const _streamClose = (d, rule) => {
49534953
// Not while a `@namespace` after it could still be read: taking this
49544954
// rule out would move one up to where the engine honours it.
49554955
!(d === 0 && _namespacePrologueOpen) &&
4956-
(_types[i0] === T_QUALIFIED_RULE ||
4956+
// The mark is read first so it is cleared whichever reason drops the
4957+
// rule: an unread one would ride the node back into the pool.
4958+
(_takeUnusedRule(rule) ||
4959+
_types[i0] === T_QUALIFIED_RULE ||
49574960
DROPPABLE_WHEN_EMPTY_AT_RULES.has(
49584961
_input.slice(_starts[i0] + 1, _aux0[i0]).toLowerCase()
4959-
) ||
4960-
_takeUnusedRule(rule))
4962+
))
49614963
) {
49624964
writer.dropPending();
49634965
return;
@@ -8395,7 +8397,10 @@ const _wideGamutColor = (raw, alpha) => {
83958397
if (!(component >= -1e-6 && component <= 1 + 1e-6)) return null;
83968398
parts.push(_roundColorComponent(component));
83978399
}
8398-
const opacity = alpha < 1 ? `/${_alphaByteText(_clamp255(alpha * 255))}` : "";
8400+
// Quantized before it is written: an alpha the engine stores as `255` is the
8401+
// opaque color, which carries no alpha at all.
8402+
const byte = _clamp255(alpha * 255);
8403+
const opacity = byte === 255 ? "" : `/${_alphaByteText(byte)}`;
83998404
return `color(display-p3 ${parts.join(" ")}${opacity})`;
84008405
};
84018406

@@ -8736,11 +8741,13 @@ const _foldColorMix = (inner, hexAlpha, forFallback) => {
87368741
if (head.length < 2 || !equalsLowerCase(head[0], "in")) return null;
87378742
const space = COLOR_SPACE_MODEL.get(asciiLowerCaseName(head[1]));
87388743
if (space === undefined) return null;
8739-
// `in <space> <method> hue` — the two words after the space, or nothing.
8740-
_hueMethod = "";
8744+
// `in <space> <method> hue` — the two words after the space, or nothing. Held
8745+
// locally until both colors are resolved: a nested mix folds through here too,
8746+
// and would otherwise leave its own method behind for this one to read.
8747+
let hueMethod = "";
87418748
if (head.length === 4) {
87428749
if (!equalsLowerCase(head[3], "hue")) return null;
8743-
_hueMethod = asciiLowerCaseName(head[2]);
8750+
hueMethod = asciiLowerCaseName(head[2]);
87448751
} else if (head.length !== 2) {
87458752
return null;
87468753
}
@@ -8782,6 +8789,7 @@ const _foldColorMix = (inner, hexAlpha, forFallback) => {
87828789
const components = [colors[0], colors[1]].map((color) =>
87838790
space.to(color.linear)
87848791
);
8792+
_hueMethod = hueMethod;
87858793
/** @type {number[]} */
87868794
const mixed = [];
87878795
for (let at = 0; at < 3; at++) {

‎test/CssSyntax.unittest.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7909,6 +7909,14 @@ describe("CssSyntax minify — the predefined color spaces", () => {
79097909
"a{color:#ff070480;color:color(display-p3 .923508 .207882 .145098/.5);" +
79107910
"color:oklch(.6322 .2577 29.23/.5)}"
79117911
);
7912+
// An alpha the engine stores as `255` is the opaque color, so the rung
7913+
// carries no alpha rather than an empty one.
7914+
expect(
7915+
minifyFor("a{color:oklch(0.6322 0.2577 29.23/.999)}", ["safari 15"])
7916+
).toBe(
7917+
"a{color:#ff0704;color:color(display-p3 .923508 .207882 .145098);" +
7918+
"color:oklch(.6322 .2577 29.23/.999)}"
7919+
);
79127920
// A gradient holding one color the target reads and one it does not takes
79137921
// the rung over the second alone.
79147922
expect(
@@ -8026,6 +8034,20 @@ describe("CssSyntax minify — color-mix()", () => {
80268034
expect(
80278035
minify("a{color:color-mix(in hsl,hsl(350 50% 40%) 30%,hsl(10 50% 40%))}")
80288036
).toBe("a{color:#993a33}");
8037+
});
8038+
8039+
it("reads its own hue method, not one a nested mix left behind", () => {
8040+
// The nested mix resolves through this same fold, so a method held in module
8041+
// state would be the inner one's by the time the outer interpolates.
8042+
const inner = "color-mix(in hsl shorter hue,#804d4d 30%,#4d8080)";
8043+
expect(minify(`a{color:${inner}}`)).toBe("a{color:#4d8052}");
8044+
expect(
8045+
minify(`a{color:color-mix(in hsl longer hue,${inner} 30%,#804d4d)}`)
8046+
).toBe("a{color:#774d80}");
8047+
// Which is the color the outer names once the inner one is written out.
8048+
expect(
8049+
minify("a{color:color-mix(in hsl longer hue,#4d8052 30%,#804d4d)}")
8050+
).toBe("a{color:#774d80}");
80298051
expect(
80308052
minify(
80318053
"a{color:color-mix(in hsl longer hue,hsl(30 50% 40%) 30%,hsl(60 50% 40%))}"

0 commit comments

Comments
 (0)