Skip to content

Commit c8da996

Browse files
authored
fix: :global() compound selector validation tweak (#10287)
Allow type selector in `:global()` when it's at a start of a compound selector fixes #10286
1 parent 3409349 commit c8da996

File tree

6 files changed

+43
-3
lines changed

6 files changed

+43
-3
lines changed

.changeset/thirty-pears-hug.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: allow type selector in `:global()` when it's at a start of a compound selector

packages/svelte/src/compiler/phases/2-analyze/css/Selector.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,19 @@ export default class Selector {
181181

182182
for (let i = 0; i < block.selectors.length; i++) {
183183
const selector = block.selectors[i];
184+
184185
if (selector.type === 'PseudoClassSelector' && selector.name === 'global') {
185186
const child = selector.args?.children[0].children[0];
186-
if (child?.type === 'TypeSelector' && !/[.:#]/.test(child.name[0])) {
187+
if (
188+
child?.type === 'TypeSelector' &&
189+
!/[.:#]/.test(child.name[0]) &&
190+
(i !== 0 ||
191+
block.selectors
192+
.slice(1)
193+
.some(
194+
(s) => s.type !== 'PseudoElementSelector' && s.type !== 'PseudoClassSelector'
195+
))
196+
) {
187197
error(selector, 'invalid-css-global-selector-list');
188198
}
189199
}

packages/svelte/tests/validator/samples/css-invalid-global-placement-2/errors.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
"code": "invalid-css-global-placement",
44
"message": ":global(...) can be at the start or end of a selector sequence, but not in the middle",
55
"start": {
6-
"line": 5,
6+
"line": 8,
77
"column": 6
88
},
99
"end": {
10-
"line": 5,
10+
"line": 8,
1111
"column": 19
1212
}
1313
}

packages/svelte/tests/validator/samples/css-invalid-global-placement-2/input.svelte

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
.foo :global(.bar):first-child {
33
color: red;
44
}
5+
.foo :global(p):first-child {
6+
color: red;
7+
}
58
.foo :global(.bar):first-child .baz {
69
color: red;
710
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"code": "invalid-css-global-selector-list",
4+
"message": ":global(...) must not contain type or universal selectors when used in a compound selector",
5+
"start": {
6+
"line": 5,
7+
"column": 5
8+
},
9+
"end": {
10+
"line": 5,
11+
"column": 17
12+
}
13+
}
14+
]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<style>
2+
:global(div):first-child {
3+
color: red;
4+
}
5+
.foo:global(div):first-child {
6+
color: red;
7+
}
8+
</style>

0 commit comments

Comments
 (0)