fix: `:global()` compound selector validation tweak (#10287)

Allow type selector in `:global()` when it's at a start of a compound selector
fixes #10286
pull/10291/head
Simon H 11 months ago committed by GitHub
parent 340934917a
commit c8da99646a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: allow type selector in `:global()` when it's at a start of a compound selector

@ -181,9 +181,19 @@ export default class Selector {
for (let i = 0; i < block.selectors.length; i++) { for (let i = 0; i < block.selectors.length; i++) {
const selector = block.selectors[i]; const selector = block.selectors[i];
if (selector.type === 'PseudoClassSelector' && selector.name === 'global') { if (selector.type === 'PseudoClassSelector' && selector.name === 'global') {
const child = selector.args?.children[0].children[0]; const child = selector.args?.children[0].children[0];
if (child?.type === 'TypeSelector' && !/[.:#]/.test(child.name[0])) { if (
child?.type === 'TypeSelector' &&
!/[.:#]/.test(child.name[0]) &&
(i !== 0 ||
block.selectors
.slice(1)
.some(
(s) => s.type !== 'PseudoElementSelector' && s.type !== 'PseudoClassSelector'
))
) {
error(selector, 'invalid-css-global-selector-list'); error(selector, 'invalid-css-global-selector-list');
} }
} }

@ -3,11 +3,11 @@
"code": "invalid-css-global-placement", "code": "invalid-css-global-placement",
"message": ":global(...) can be at the start or end of a selector sequence, but not in the middle", "message": ":global(...) can be at the start or end of a selector sequence, but not in the middle",
"start": { "start": {
"line": 5, "line": 8,
"column": 6 "column": 6
}, },
"end": { "end": {
"line": 5, "line": 8,
"column": 19 "column": 19
} }
} }

@ -2,6 +2,9 @@
.foo :global(.bar):first-child { .foo :global(.bar):first-child {
color: red; color: red;
} }
.foo :global(p):first-child {
color: red;
}
.foo :global(.bar):first-child .baz { .foo :global(.bar):first-child .baz {
color: red; color: red;
} }

@ -0,0 +1,14 @@
[
{
"code": "invalid-css-global-selector-list",
"message": ":global(...) must not contain type or universal selectors when used in a compound selector",
"start": {
"line": 5,
"column": 5
},
"end": {
"line": 5,
"column": 17
}
}
]

@ -0,0 +1,8 @@
<style>
:global(div):first-child {
color: red;
}
.foo:global(div):first-child {
color: red;
}
</style>
Loading…
Cancel
Save