Skip to content

Commit

Permalink
fix: allow for nesting selector in pseudoclasses (#13209)
Browse files Browse the repository at this point in the history
Fixes #13203

The problem is that we were checking for NestingSelectors only in the selectors of the immediate child of the selectors. Since Nesting could be in selectors like :is or :where or :has that can also be nested i think we need to walk the selectors to find if there's a selector or not.
  • Loading branch information
paoloricciuti authored Sep 12, 2024
1 parent 91299b3 commit e9dc118
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-snails-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: allow for nesting selector in pseudoclasses
20 changes: 17 additions & 3 deletions packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,23 @@ const visitors = {
const inner = selectors[selectors.length - 1];

if (node.metadata.rule?.metadata.parent_rule && selectors.length > 0) {
const has_explicit_nesting_selector = selectors.some((selector) =>
selector.selectors.some((s) => s.type === 'NestingSelector')
);
let has_explicit_nesting_selector = false;

// nesting could be inside pseudo classes like :is, :has or :where
for (let selector of selectors) {
walk(
selector,
{},
{
// @ts-ignore
NestingSelector() {
has_explicit_nesting_selector = true;
}
}
);
// if we found one we can break from the others
if (has_explicit_nesting_selector) break;
}

if (!has_explicit_nesting_selector) {
selectors[0] = {
Expand Down
5 changes: 5 additions & 0 deletions packages/svelte/tests/css/samples/nested-in-pseudo/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from '../../test';

export default test({
warnings: []
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

nav.svelte-xyz{
header:where(.svelte-xyz):has(&){
color: red;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<header class="svelte-xyz"><nav class="svelte-xyz"></nav></header>
11 changes: 11 additions & 0 deletions packages/svelte/tests/css/samples/nested-in-pseudo/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<header>
<nav></nav>
</header>

<style>
nav{
header:has(&){
color: red;
}
}
</style>

0 comments on commit e9dc118

Please sign in to comment.