Skip to content

Commit edd6306

Browse files
authored
fix(css): order the nested list on what is written, not on what was (#22287)
The lowering writes the parent in front of each selector, and the list was canonical as the nested rule spelled it — so `a{>b,+c,d{top:0}}` printed `a+c,a>b,a d` where a second pass writes `a d,a+c,a>b`. Ordered after the substitution, and only where there is a list to order: one selector is already the order it reads in, and pays nothing. Swiper 14 is clean.
1 parent 8d00f74 commit edd6306

7 files changed

Lines changed: 39 additions & 4 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+
Order the list a lowered nested selector lands in on what is written, not on what was.

‎lib/css/syntax-printer.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,7 +1936,11 @@ const _resolveNestedPrelude = (prelude, parent) => {
19361936
const combinator = _COMBINATOR_CODES.has(one.charCodeAt(0));
19371937
written[i] = combinator ? reference + one : `${reference} ${one}`;
19381938
}
1939-
return written.join(",");
1939+
const list = written.join(",");
1940+
// Ordered on what is written, not on what was: the list was canonical as the
1941+
// nested rule spelled it, and writing the parent in front of each decides
1942+
// the order. One selector is already the order it reads in.
1943+
return written.length === 1 ? list : _canonicalSelectorList(list);
19401944
};
19411945

19421946
// Whether the run `_writeNested` built took in the rule the hoist emptied,
@@ -4504,7 +4508,9 @@ const printer = (path, writer) => {
45044508
null,
45054509
true
45064510
) || "";
4507-
if (minify && written.length !== 0) _recordHoistedRule(written);
4511+
if (minify && written.length !== 0) {
4512+
_recordHoistedRule(written);
4513+
}
45084514
return written;
45094515
}
45104516
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

3-
exports[`ConfigCacheTestCases css minify-nested-combinator minify-nested-combinator should compile 1`] = `".a>.child{top:0}.b+.next{top:1px}.c~.later{top:2px}.d>.child{top:3px}.e || .col{top:4px}.f .inside{top:5px}:is(.g,.h)>.child{top:6px}.i>.child{top:7px}"`;
3+
exports[`ConfigCacheTestCases css minify-nested-combinator minify-nested-combinator should compile 1`] = `".a>.child{top:0}.b+.next{top:1px}.c~.later{top:2px}.d>.child{top:3px}.e || .col{top:4px}.f .inside{top:5px}:is(.g,.h)>.child{top:6px}.i>.child{top:7px}.j .z,.j+.y,.j>.x{left:0}"`;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

3-
exports[`ConfigTestCases css minify-nested-combinator minify-nested-combinator should compile 1`] = `".a>.child{top:0}.b+.next{top:1px}.c~.later{top:2px}.d>.child{top:3px}.e || .col{top:4px}.f .inside{top:5px}:is(.g,.h)>.child{top:6px}.i>.child{top:7px}"`;
3+
exports[`ConfigTestCases css minify-nested-combinator minify-nested-combinator should compile 1`] = `".a>.child{top:0}.b+.next{top:1px}.c~.later{top:2px}.d>.child{top:3px}.e || .col{top:4px}.f .inside{top:5px}:is(.g,.h)>.child{top:6px}.i>.child{top:7px}.j .z,.j+.y,.j>.x{left:0}"`;

‎test/configCases/css/minify-nested-combinator/style.css‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@
1717
/* A parent written as a list, and a selector naming it itself. */
1818
.g, .h { > .child { top: 6px } }
1919
.i { &> .child { top: 7px } }
20+
21+
/* The list was canonical as the nested rule spelled it; writing the parent in
22+
front of each selector is what decides the order they read in. */
23+
.j { > .x, + .y, .z { left: 0 } }

‎test/configCases/css/minify-nested-combinator/test.config.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ module.exports = {
2121
expect(css).toContain(":is(.g,.h)>.child{top:6px}");
2222
expect(css).toContain(".i>.child{top:7px}");
2323

24+
// The list reads in the order the written selectors settle on.
25+
expect(css).toContain(".j .z,.j+.y,.j>.x{left:0}");
26+
2427
// What the printer wrote is what a second pass writes.
2528
const again = await cssMinify({ "bundle0.css": css }, undefined, {
2629
environment: { browsers: ["chrome 100"] }

‎test/unitCases/CssSyntax.unittest.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10954,6 +10954,23 @@ describe("CssSyntax minify — a nested selector opening on a combinator", () =>
1095410954
expect(minifyFor("a{ > b{top:0}}", T)).toBe("a>b{top:0}");
1095510955
});
1095610956

10957+
it("orders the list on what is written, not on what was", () => {
10958+
// The list was canonical as the nested rule spelled it; writing the parent
10959+
// in front of each selector is what decides the order they read in.
10960+
expect(minifyFor("a{>b,+c,d{top:0}}", T)).toBe("a d,a+c,a>b{top:0}");
10961+
expect(minifyFor("a,e{>b,+c{top:0}}", T)).toBe(
10962+
":is(a,e)+c,:is(a,e)>b{top:0}"
10963+
);
10964+
});
10965+
10966+
it("reads back out a selector the substitution made a twin", () => {
10967+
expect(minifyFor("a{>b,>b{top:0}}", T)).toBe("a>b{top:0}");
10968+
});
10969+
10970+
it("leaves a list the substitution leaves canonical", () => {
10971+
expect(minifyFor("a{b,c,d{top:0}}", T)).toBe("a b,a c,a d{top:0}");
10972+
});
10973+
1095710974
it("keeps the space in front of a column combinator", () => {
1095810975
// `||` is not one the printer writes tight, so neither is this.
1095910976
expect(minifyFor("a{||b{top:0}}", T)).toBe("a ||b{top:0}");

0 commit comments

Comments
 (0)