Skip to content

fix: handle leading combinators within :has(...) #13606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,16 @@ function relative_selector_might_apply_to_node(

for (const complex_selector of complex_selectors) {
const selectors = truncate(complex_selector);
const left_most_combinator = selectors[0]?.combinator ?? descendant_combinator;
// In .x:has(> y), we want to search for y, ignoring the left-most combinator
// (else it would try to walk further up and fail because there are no selectors left)
if (selectors.length > 0) {
selectors[0] = {
...selectors[0],
combinator: null
};
}

if (
selectors.length === 0 /* is :global(...) */ ||
apply_selector(selectors, rule, element, stylesheet, check_has)
Expand All @@ -379,7 +389,7 @@ function relative_selector_might_apply_to_node(
// and now looking upwards for the .x part.
if (
apply_combinator(
selectors[0]?.combinator ?? descendant_combinator,
left_most_combinator,
selectors[0] ?? [],
[relative_selector],
rule,
Expand Down
14 changes: 14 additions & 0 deletions packages/svelte/tests/css/samples/has/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ export default test({
column: 17,
line: 81
}
},
{
code: 'css_unused_selector',
message: 'Unused CSS selector "x:has(> z)"',
start: {
line: 88,
column: 1,
character: 968
},
end: {
line: 88,
column: 11,
character: 978
}
}
]
});
7 changes: 7 additions & 0 deletions packages/svelte/tests/css/samples/has/expected.css
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,10 @@
/* (unused) .unused x:has(y) {
color: red;
}*/

x.svelte-xyz:has(> y:where(.svelte-xyz)) {
color: green;
}
/* (unused) x:has(> z) {
color: red;
}*/
7 changes: 7 additions & 0 deletions packages/svelte/tests/css/samples/has/input.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,11 @@
.unused x:has(y) {
color: red;
}

x:has(> y) {
color: green;
}
x:has(> z) {
color: red;
}
</style>