Skip to content

Commit 9e3c4d5

Browse files
committed
handle css nth-selector syntax
Fixes #9765
1 parent 2fcd049 commit 9e3c4d5

File tree

5 files changed

+566
-1
lines changed

5 files changed

+566
-1
lines changed

.changeset/sweet-mangos-beg.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: handle css nth-selector syntax

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const REGEX_ATTRIBUTE_FLAGS = /^[a-zA-Z]+/; // only `i` and `s` are valid today,
66
const REGEX_COMBINATOR_WHITESPACE = /^\s*(\+|~|>|\|\|)\s*/;
77
const REGEX_COMBINATOR = /^(\+|~|>|\|\|)/;
88
const REGEX_PERCENTAGE = /^\d+(\.\d+)?%/;
9+
const REGEX_NTH_OF = /^(even|odd|(-?[0-9]?n?(\s*\+\s*[0-9]+)?))(\s+of\s+)?/;
910
const REGEX_WHITESPACE_OR_COLON = /[\s:]/;
1011
const REGEX_BRACE_OR_SEMICOLON = /[{;]/;
1112
const REGEX_LEADING_HYPHEN_OR_DIGIT = /-?\d/;
@@ -293,6 +294,13 @@ function read_selector(parser, inside_pseudo_class = false) {
293294
start,
294295
end: parser.index
295296
});
297+
} else if (parser.match_regex(REGEX_NTH_OF)) {
298+
children.push({
299+
type: 'Nth',
300+
value: /** @type {string} */ (parser.read(REGEX_NTH_OF)),
301+
start,
302+
end: parser.index
303+
});
296304
} else {
297305
let name = read_identifier(parser);
298306
if (parser.match('|')) {

packages/svelte/src/compiler/types/css.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,20 @@ export interface Percentage extends BaseNode {
6767
value: string;
6868
}
6969

70+
export interface Nth extends BaseNode {
71+
type: 'Nth';
72+
value: string;
73+
}
74+
7075
export type SimpleSelector =
7176
| TypeSelector
7277
| IdSelector
7378
| ClassSelector
7479
| AttributeSelector
7580
| PseudoElementSelector
7681
| PseudoClassSelector
77-
| Percentage;
82+
| Percentage
83+
| Nth;
7884

7985
export interface Combinator extends BaseNode {
8086
type: 'Combinator';
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<style>
2+
/* test that all these are parsed correctly */
3+
h1:nth-of-type(2n+1){
4+
background: red;
5+
}
6+
h1:nth-child(-n + 3 of li.important) {
7+
background: red;
8+
}
9+
h1:nth-child(1) {
10+
background: red;
11+
}
12+
h1:nth-child(p) {
13+
background: red;
14+
}
15+
h1:nth-child(n+7) {
16+
background: red;
17+
}
18+
h1:nth-child(even) {
19+
background: red;
20+
}
21+
h1:nth-child(odd) {
22+
background: red;
23+
}
24+
</style>
25+
26+
<h1>Broken</h1>

0 commit comments

Comments
 (0)