Skip to content

Commit 4f8d03f

Browse files
fix(css): keep an :nth-*() argument whose sign stands apart from An as written
CSS Syntax 3 §6 reads a leading sign only against what follows it, so `- n+1` and `+ 5` are no argument an engine reads. The printer took them as `-n+1` and `5`, bringing a rule every browser drops back to life.
1 parent 1c67141 commit 4f8d03f

4 files changed

Lines changed: 41 additions & 1 deletion

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+
Keep an `:nth-*()` argument with a space after its sign as written.

‎lib/css/syntax-printer.js‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2554,7 +2554,7 @@ const _takeCustomSelector = (prelude) => {
25542554
// own: `2n + 1`, `+3` and `2N+1` all say what a shorter spelling does. Its
25552555
// whitespace is CSS's, so U+00A0 keeps an argument no engine reads as written.
25562556
const _NTH_RE =
2557-
/^[ \t\n\f\r]*(?:([+-]?)[ \t\n\f\r]*(\d*)[nN][ \t\n\f\r]*(?:([+-])[ \t\n\f\r]*(\d+))?|([+-]?)[ \t\n\f\r]*(\d+))[ \t\n\f\r]*$/;
2557+
/^[ \t\n\f\r]*(?:([+-]?)(\d*)[nN][ \t\n\f\r]*(?:([+-])[ \t\n\f\r]*(\d+))?|([+-]?)(\d+))[ \t\n\f\r]*$/;
25582558
const _NTH_EDGE_SPACE_RE = /^[ \t\n\f\r]+|[ \t\n\f\r]+$/g;
25592559

25602560
/**
@@ -4906,6 +4906,16 @@ const printer = (path, writer) => {
49064906
_dropImpliedUniversalSelector(parts);
49074907
}
49084908
inner = _join(parts, !minify, minify ? trim : _TRIM_NOTHING);
4909+
// A leading `+` in An+B is a sign, not a combinator: `+ n` is no
4910+
// argument an engine reads, and `+n` would be one.
4911+
if (minify && NTH_PSEUDO_FUNCTIONS.has(lower)) {
4912+
const at = parts.findIndex(
4913+
(part) => part.length !== 0 && part !== _SEP
4914+
);
4915+
if (parts[at] === "+" && parts[at + 1] === _SEP) {
4916+
inner = `+ ${inner.slice(1)}`;
4917+
}
4918+
}
49094919
// Chrome throws out `attr( name unit )` where `attr( name unit)`
49104920
// parses, so the space before the `)` decides whether the declaration
49114921
// runs — `_join` drops a trailing one like any other.

‎test/specCases/syntaxEquivalence.spectest.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,19 @@ const LOWERING_FIXTURES = [
14841484
["#i", "color"]
14851485
]
14861486
},
1487+
{
1488+
name: "an An+B argument whose sign stands apart",
1489+
// Written as they stand: no engine reads either argument, so each rule is
1490+
// dropped, where `-n+1` and `2` would match.
1491+
produces: ["li:nth-child(- n+1){", "li:nth-child(+ 2){"],
1492+
css: "li{color:blue}li:nth-child(- n+1){color:red}li:nth-child(+ 2){color:green}",
1493+
browsers: ["chrome 130"],
1494+
html: "<ul><li id=a>a</li><li id=b>b</li></ul>",
1495+
probes: [
1496+
["#a", "color"],
1497+
["#b", "color"]
1498+
]
1499+
},
14871500
{
14881501
name: "nesting",
14891502
// The rules the lowering writes on their own, which the source states

‎test/unitCases/CssSyntax.unittest.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,6 +2210,18 @@ describe("CssSyntax — minify value-safety edge cases", () => {
22102210
);
22112211
});
22122212

2213+
it.each([
2214+
"a:nth-child(- n+1){b:c}",
2215+
"a:nth-child(+ n){b:c}",
2216+
"a:nth-child(- 2n+1){b:c}",
2217+
"a:nth-child(+ 5){b:c}",
2218+
"a:nth-child(- 5){b:c}",
2219+
"a:nth-child(+ odd){b:c}"
2220+
])("keeps %s, whose sign no engine reads apart from An", (css) => {
2221+
// CSS Syntax 3 §6 reads a leading sign only against what follows it.
2222+
expect(min(css)).toBe(css);
2223+
});
2224+
22132225
it("still normalizes numbers in declaration values", () => {
22142226
expect(min("a{margin:+1.50px}")).toBe("a{margin:1.5px}");
22152227
expect(min("a{margin:0.50px 1.0px 0}")).toBe("a{margin:.5px 1px 0}");

0 commit comments

Comments
 (0)