Skip to content

Commit

Permalink
fix: correctly prune CSS for elements inside snippets (#14494)
Browse files Browse the repository at this point in the history
fixes #14483
  • Loading branch information
Rich-Harris authored Dec 2, 2024
1 parent 99b4cfb commit 1f25bd4
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/popular-dogs-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: correctly prune CSS for elements inside snippets
15 changes: 9 additions & 6 deletions packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,19 @@ function apply_combinator(relative_selector, parent_selectors, rule, node) {
const parent = path[i];

if (parent.type === 'SnippetBlock') {
if (seen.has(parent)) return true;
seen.add(parent);
if (seen.has(parent)) {
parent_matched = true;
} else {
seen.add(parent);

for (const site of parent.metadata.sites) {
if (apply_combinator(relative_selector, parent_selectors, rule, site)) {
return true;
for (const site of parent.metadata.sites) {
if (apply_combinator(relative_selector, parent_selectors, rule, site)) {
parent_matched = true;
}
}
}

return false;
break;
}

if (parent.type === 'RegularElement' || parent.type === 'SvelteElement') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

div div.svelte-xyz {
div.svelte-xyz div:where(.svelte-xyz) {
color: green;
}
/* (unused) div + div {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

x.svelte-xyz y:where(.svelte-xyz) {
color: green;
}
13 changes: 13 additions & 0 deletions packages/svelte/tests/css/samples/snippets-elements/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{#snippet foo()}
<x>
<y></y>
</x>
{/snippet}

{@render foo()}

<style>
x y {
color: green;
}
</style>

0 comments on commit 1f25bd4

Please sign in to comment.