Skip to content

Commit 52d94db

Browse files
fix(css): name a hoisted rule in the dark-scheme copy it writes (#22318)
The copy repeated the rule's prelude as written, so a hoisted rule kept an unresolved `&` in it; it now names the prelude the run settles. A prefix copy the hoist takes out carries the block a top-level copy does. Claude-Session: https://claude.ai/code/session_01M5Jd1hekk3MZ2VHo5jdkio
1 parent 95988b3 commit 52d94db

3 files changed

Lines changed: 78 additions & 8 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Name a hoisted nested rule in the dark-scheme copy it writes.

‎lib/css/syntax-printer.js‎

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,10 @@ const _COLOR_SCHEME_RE = /(?:^|[;{}])color-scheme:([^;{}]*)/i;
12881288
// defers to.
12891289
const _PREFERS_DARK = "@media (prefers-color-scheme:dark)";
12901290

1291+
// Stands for the rule's own prelude in a block the hoist holds, filled in once
1292+
// the run settles it: a NUL is never printed, the tokenizer replacing it.
1293+
const _OWN_PRELUDE = "\0";
1294+
12911295
/**
12921296
* Whether a value names a color function no target reads, wherever it stands.
12931297
* @param {string} value the value, as printed
@@ -2017,10 +2021,19 @@ const _holdsNested = (node) => {
20172021
* @param {string} prelude the rule's printed prelude
20182022
* @param {string | null} rest the rule's own block, or null where it writes none
20192023
* @param {boolean} hoistable false where the rule is several rules with preludes of their own, which stay where they were written
2020-
* @param {readonly string[]=} copies the preludes a prefix copy wrote it as, each carrying this rule's own block
2024+
* @param {readonly string[]=} copies the preludes a prefix copy wrote it as
2025+
* @param {string=} copyRest the block each of those carries, as a prefix copy writes it
20212026
* @returns {string | null} the text to write after the rule, or null when the rule goes on the list itself
20222027
*/
2023-
const _writeNested = (path, node, prelude, rest, hoistable, copies) => {
2028+
const _writeNested = (
2029+
path,
2030+
node,
2031+
prelude,
2032+
rest,
2033+
hoistable,
2034+
copies,
2035+
copyRest
2036+
) => {
20242037
// Only what was written in this rule: the walk is post-order, so an earlier
20252038
// sibling's rules are on the list too, waiting for the parent they share.
20262039
/** @type {{ prelude: string, rest: string, owner: Node | null, apart?: boolean }[]} */
@@ -2040,7 +2053,12 @@ const _writeNested = (path, node, prelude, rest, hoistable, copies) => {
20402053
// written, rather than the whole group staying nested.
20412054
if (rest !== null && copies !== undefined) {
20422055
for (const copy of copies) {
2043-
_pendingNested.push({ prelude: copy, rest, owner, apart: true });
2056+
_pendingNested.push({
2057+
prelude: copy,
2058+
rest: /** @type {string} */ (copyRest),
2059+
owner,
2060+
apart: true
2061+
});
20442062
}
20452063
}
20462064
if (rest !== null) {
@@ -2105,7 +2123,12 @@ const _writeNested = (path, node, prelude, rest, hoistable, copies) => {
21052123
*/
21062124
const _flushRun = (run) => {
21072125
const prelude = _settledPrelude(run);
2108-
const text = prelude + run.rest;
2126+
const at = run.rest.indexOf(_OWN_PRELUDE);
2127+
const text =
2128+
prelude +
2129+
(at === -1
2130+
? run.rest
2131+
: run.rest.slice(0, at) + prelude + run.rest.slice(at + 1));
21092132
_nestedRuns.push({ text, prelude: prelude.length });
21102133
return text;
21112134
};
@@ -4628,11 +4651,17 @@ const printer = (path, writer) => {
46284651
// toggle's last is the one this block now ends on.
46294652
scheme = `${body};${toggle.toggle.slice(0, -1)}`;
46304653
if (toggle.flip) {
4631-
schemeCopy = `${_PREFERS_DARK}${soft}{${prelude}${soft}{${LIGHT_PROPERTY}:;${DARK_PROPERTY}:initial}}`;
4654+
schemeCopy = `${_PREFERS_DARK}${soft}{${_OWN_PRELUDE}${soft}{${LIGHT_PROPERTY}:;${DARK_PROPERTY}:initial}}`;
46324655
}
46334656
}
46344657
}
4635-
const rest = `${soft}{${scheme}${nl}}${schemeCopy}`;
4658+
// The hoist's copy names the prelude as the run settles it, since one
4659+
// taken out is resolved against its parent and may be joined into a list.
4660+
const ownRest = `${soft}{${scheme}${nl}}${schemeCopy}`;
4661+
const rest =
4662+
schemeCopy === ""
4663+
? ownRest
4664+
: ownRest.replace(_OWN_PRELUDE, () => prelude);
46364665
const text = `${prelude}${rest}`;
46374666
// Prefix copies turn one rule into several, so a rewritten rule is not
46384667
// offered for joining: its text no longer describes a single block. Each
@@ -4691,9 +4720,10 @@ const printer = (path, writer) => {
46914720
path,
46924721
/** @type {Node} */ (_S._currentNode),
46934722
prelude,
4694-
rest,
4723+
ownRest,
46954724
out === text || copies !== undefined,
4696-
copies
4725+
copies,
4726+
`${soft}{${body}}`
46974727
);
46984728
if (nested === null) return "";
46994729
if (out !== text) return out + nested;

‎test/unitCases/CssSyntax.unittest.js‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11421,6 +11421,41 @@ describe("CssSyntax minify — nesting the target cannot read", () => {
1142111421
expect(minifyFor(css, HOIST_T)).toBe(expected);
1142211422
});
1142311423

11424+
it("names the hoisted rule in the dark-scheme copy it writes", () => {
11425+
// The copy repeats the rule's prelude, which the hoist resolves against
11426+
// the parent and may join into a list.
11427+
const L = "b{color:light-dark(red,blue)}";
11428+
const tail =
11429+
"b{color:var(--webpack-light,red) var(--webpack-dark,blue)}" +
11430+
":where(:root){--webpack-light:initial;--webpack-dark:}";
11431+
/**
11432+
* @param {string} selector the rule's prelude standing on its own
11433+
* @returns {string} the rule and the dark-scheme copy naming it
11434+
*/
11435+
const scheme = (selector) =>
11436+
`${selector}{color-scheme:light dark;--webpack-light:initial;--webpack-dark:}` +
11437+
`@media (prefers-color-scheme:dark){${selector}{--webpack-light:;--webpack-dark:initial}}`;
11438+
expect(minifyFor(`a{&:hover{color-scheme:light dark}}${L}`, HOIST_T)).toBe(
11439+
scheme("a:hover") + tail
11440+
);
11441+
expect(
11442+
minifyFor(
11443+
`a{&:hover{color-scheme:light dark}& i{color-scheme:light dark}}${L}`,
11444+
HOIST_T
11445+
)
11446+
).toBe(scheme("a i,a:hover") + tail);
11447+
expect(
11448+
minifyFor(`a[x$="$&"]{&:hover{color-scheme:light dark}}${L}`, HOIST_T)
11449+
).toBe(scheme('a[x$="$&"]:hover') + tail);
11450+
});
11451+
11452+
it("writes a hoisted prefix copy the way a top-level one is written", () => {
11453+
const L = "b{color:light-dark(red,blue)}";
11454+
expect(
11455+
minifyFor(`a{&:fullscreen{color-scheme:light dark}}${L}`, HOIST_T)
11456+
).toBe(minifyFor(`a:fullscreen{color-scheme:light dark}${L}`, HOIST_T));
11457+
});
11458+
1142411459
it("leaves a prefixed rule that was never nested alone", () => {
1142511460
expect(minifyFor("a:fullscreen{top:0}", HOIST_T)).toBe(
1142611461
"a:-webkit-full-screen{top:0}a:fullscreen{top:0}"

0 commit comments

Comments
 (0)