Skip to content

Commit 0e7fc47

Browse files
committed
read selectors before combinators
1 parent 00360b2 commit 0e7fc47

File tree

3 files changed

+29
-42
lines changed

3 files changed

+29
-42
lines changed

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

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -216,30 +216,6 @@ function read_selector(parser, inside_pseudo_class = false) {
216216
while (parser.index < parser.template.length) {
217217
let start = parser.index;
218218

219-
const combinator = read_combinator(parser);
220-
221-
if (combinator) {
222-
if (relative_selector.selectors.length === 0) {
223-
if (!inside_pseudo_class) {
224-
error(start, 'invalid-css-selector');
225-
}
226-
} else {
227-
relative_selector.end = start;
228-
children.push(relative_selector);
229-
}
230-
231-
// ...and start a new one
232-
relative_selector = create_selector(combinator, start);
233-
234-
parser.allow_whitespace();
235-
236-
if (parser.match(',') || (inside_pseudo_class ? parser.match(')') : parser.match('{'))) {
237-
error(parser.index, 'invalid-css-selector');
238-
}
239-
}
240-
241-
start = parser.index;
242-
243219
if (parser.eat('*')) {
244220
let name = '*';
245221

@@ -348,7 +324,7 @@ function read_selector(parser, inside_pseudo_class = false) {
348324
start,
349325
end: parser.index
350326
});
351-
} else {
327+
} else if (!parser.match_regex(REGEX_COMBINATOR)) {
352328
let name = read_identifier(parser);
353329

354330
if (parser.eat('|')) {
@@ -386,6 +362,27 @@ function read_selector(parser, inside_pseudo_class = false) {
386362
}
387363

388364
parser.index = index;
365+
const combinator = read_combinator(parser);
366+
367+
if (combinator) {
368+
if (relative_selector.selectors.length === 0) {
369+
if (!inside_pseudo_class) {
370+
error(start, 'invalid-css-selector');
371+
}
372+
} else {
373+
relative_selector.end = index;
374+
children.push(relative_selector);
375+
}
376+
377+
// ...and start a new one
378+
relative_selector = create_selector(combinator, combinator.start);
379+
380+
parser.allow_whitespace();
381+
382+
if (parser.match(',') || (inside_pseudo_class ? parser.match(')') : parser.match('{'))) {
383+
error(parser.index, 'invalid-css-selector');
384+
}
385+
}
389386
}
390387

391388
error(parser.template.length, 'unexpected-eof');

packages/svelte/tests/parser-modern/samples/css-nth-syntax/output.json

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -945,17 +945,12 @@
945945
"children": [
946946
{
947947
"type": "RelativeSelector",
948-
"combinator": {
949-
"type": "Combinator",
950-
"name": "+",
951-
"start": 707,
952-
"end": 708
953-
},
948+
"combinator": null,
954949
"selectors": [
955950
{
956951
"type": "Nth",
957-
"value": "12",
958-
"start": 708,
952+
"value": "+12",
953+
"start": 707,
959954
"end": 710
960955
}
961956
],
@@ -1031,17 +1026,12 @@
10311026
"children": [
10321027
{
10331028
"type": "RelativeSelector",
1034-
"combinator": {
1035-
"type": "Combinator",
1036-
"name": "+",
1037-
"start": 761,
1038-
"end": 762
1039-
},
1029+
"combinator": null,
10401030
"selectors": [
10411031
{
10421032
"type": "Nth",
1043-
"value": "3n",
1044-
"start": 762,
1033+
"value": "+3n",
1034+
"start": 761,
10451035
"end": 764
10461036
}
10471037
],

packages/svelte/tests/parser-modern/samples/css-pseudo-classes/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@
354354
"end": 355
355355
}
356356
],
357-
"start": 351,
357+
"start": 352,
358358
"end": 355
359359
}
360360
]

0 commit comments

Comments
 (0)