Skip to content

Commit 529db97

Browse files
authored
fix: handle nested :global(...) selectors (#12365)
fixes #10540
1 parent 6ac1ae8 commit 529db97

File tree

5 files changed

+107
-1
lines changed

5 files changed

+107
-1
lines changed

.changeset/mean-numbers-cry.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 nested `:global(...)` selectors

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const visitors = {
7171
const selectors = truncate(node);
7272
const inner = selectors[selectors.length - 1];
7373

74-
if (node.metadata.rule?.metadata.parent_rule) {
74+
if (node.metadata.rule?.metadata.parent_rule && selectors.length > 0) {
7575
const has_explicit_nesting_selector = selectors.some((selector) =>
7676
selector.selectors.some((s) => s.type === 'NestingSelector')
7777
);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
warnings: [
5+
{
6+
code: 'css_unused_selector',
7+
message: 'Unused CSS selector ".unused :global"',
8+
start: {
9+
line: 25,
10+
column: 2,
11+
character: 229
12+
},
13+
end: {
14+
line: 25,
15+
column: 17,
16+
character: 244
17+
}
18+
},
19+
{
20+
code: 'css_unused_selector',
21+
message: 'Unused CSS selector ".unused :global(.z)"',
22+
start: {
23+
line: 31,
24+
column: 2,
25+
character: 283
26+
},
27+
end: {
28+
line: 31,
29+
column: 21,
30+
character: 302
31+
}
32+
}
33+
]
34+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
div.svelte-xyz {
3+
/* :global {*/
4+
.x {
5+
color: green;
6+
}
7+
/*}*/
8+
9+
.x {
10+
color: green;
11+
}
12+
13+
p:where(.svelte-xyz) {
14+
.y {
15+
color: green;
16+
}
17+
}
18+
19+
p:where(.svelte-xyz) .y {
20+
color: green;
21+
}
22+
23+
/* (unused) .unused :global {
24+
.z {
25+
color: red;
26+
}
27+
}*/
28+
29+
/* (unused) .unused :global(.z) {
30+
color: red;
31+
}*/
32+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<div><p>{@html whatever}</p></div>
2+
3+
<style>
4+
div {
5+
:global {
6+
.x {
7+
color: green;
8+
}
9+
}
10+
11+
:global(.x) {
12+
color: green;
13+
}
14+
15+
p :global {
16+
.y {
17+
color: green;
18+
}
19+
}
20+
21+
p :global(.y) {
22+
color: green;
23+
}
24+
25+
.unused :global {
26+
.z {
27+
color: red;
28+
}
29+
}
30+
31+
.unused :global(.z) {
32+
color: red;
33+
}
34+
}
35+
</style>

0 commit comments

Comments
 (0)