Skip to content

Commit 24130c1

Browse files
authored
fix: parse nth-child of syntax without whitespace after of (#18611)
Fixes #18609 A `<an+b> of <selector>` CSS statement does not need whitespace after `of` if it's followed by a CSS-known syntax (like a `.` which marks a class identifier). Adjust the regex accordingly.
1 parent a166761 commit 24130c1

4 files changed

Lines changed: 836 additions & 1 deletion

File tree

‎.changeset/chilled-lions-parse.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: parse `:nth-child(2n of.foo)` where `of` is not followed by whitespace

‎packages/svelte/src/compiler/phases/1-parse/read/style.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ const REGEX_CLOSING_BRACKET = /[\s\]]/;
77
const REGEX_ATTRIBUTE_FLAGS = /[a-zA-Z]+/y; // only `i` and `s` are valid today, but make it future-proof
88
const REGEX_COMBINATOR = /(\+|~|>|\|\|)/y;
99
const REGEX_PERCENTAGE = /\d+(\.\d+)?%/y;
10+
// `of` must be preceded by whitespace, otherwise it would be part of the `<an+b>` token
11+
// (`2nof` is a single dimension token). It does not need to be followed by whitespace,
12+
// because a `.`, `#`, `[`, `*`, `:` or `&` already ends the `of` identifier — minifiers rely on that
1013
const REGEX_NTH_OF =
11-
/(even|odd|\+?(\d+|\d*n(\s*[+-]\s*\d+)?)|-\d*n(\s*\+\s*\d+))((?=\s*[,)])|\s+of\s+)/y;
14+
/(even|odd|\+?(\d+|\d*n(\s*[+-]\s*\d+)?)|-\d*n(\s*\+\s*\d+))((?=\s*[,)])|\s+of(\s+|(?=[.#[*:&])))/y;
1215
const REGEX_WHITESPACE_OR_COLON = /[\s:]/;
1316
const REGEX_LEADING_HYPHEN_OR_DIGIT = /-?\d/y;
1417
const REGEX_VALID_IDENTIFIER_CHAR = /[a-zA-Z0-9_-]/;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<style>
2+
/* minifiers drop the whitespace after `of`, since the following token ends the identifier */
3+
li:nth-child(2n of.important) {
4+
background: red;
5+
}
6+
li:nth-child(even of#first) {
7+
background: red;
8+
}
9+
li:nth-last-child(-n+3 of[data-active]) {
10+
background: red;
11+
}
12+
li:nth-child(2n of*) {
13+
background: red;
14+
}
15+
li:nth-child(2n of:not(.excluded)) {
16+
background: red;
17+
}
18+
li:nth-child(2n of.a, .b) {
19+
background: red;
20+
}
21+
ul {
22+
& li:nth-child(2n of&.important) {
23+
background: red;
24+
}
25+
}
26+
</style>
27+
28+
<li>Foo</li>

0 commit comments

Comments
 (0)