Skip to content

Commit 1f25bd4

Browse files
authored
fix: correctly prune CSS for elements inside snippets (#14494)
fixes #14483
1 parent 99b4cfb commit 1f25bd4

File tree

5 files changed

+32
-7
lines changed

5 files changed

+32
-7
lines changed

.changeset/popular-dogs-exist.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: correctly prune CSS for elements inside snippets

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,16 +201,19 @@ function apply_combinator(relative_selector, parent_selectors, rule, node) {
201201
const parent = path[i];
202202

203203
if (parent.type === 'SnippetBlock') {
204-
if (seen.has(parent)) return true;
205-
seen.add(parent);
204+
if (seen.has(parent)) {
205+
parent_matched = true;
206+
} else {
207+
seen.add(parent);
206208

207-
for (const site of parent.metadata.sites) {
208-
if (apply_combinator(relative_selector, parent_selectors, rule, site)) {
209-
return true;
209+
for (const site of parent.metadata.sites) {
210+
if (apply_combinator(relative_selector, parent_selectors, rule, site)) {
211+
parent_matched = true;
212+
}
210213
}
211214
}
212215

213-
return false;
216+
break;
214217
}
215218

216219
if (parent.type === 'RegularElement' || parent.type === 'SvelteElement') {

packages/svelte/tests/css/samples/render-tag-loop/expected.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
div div.svelte-xyz {
2+
div.svelte-xyz div:where(.svelte-xyz) {
33
color: green;
44
}
55
/* (unused) div + div {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
x.svelte-xyz y:where(.svelte-xyz) {
3+
color: green;
4+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{#snippet foo()}
2+
<x>
3+
<y></y>
4+
</x>
5+
{/snippet}
6+
7+
{@render foo()}
8+
9+
<style>
10+
x y {
11+
color: green;
12+
}
13+
</style>

0 commit comments

Comments
 (0)