Skip to content

Commit 8a11e80

Browse files
committed
fix: allow combinator at start of nested CSS selector
Solved by moving the combinator positioning validation into the analysis phase Fixes #13433
1 parent 33ee958 commit 8a11e80

File tree

5 files changed

+57
-5
lines changed

5 files changed

+57
-5
lines changed

packages/svelte/src/compiler/phases/1-parse/read/style.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,7 @@ function read_selector(parser, inside_pseudo_class = false) {
351351
const combinator = read_combinator(parser);
352352

353353
if (combinator) {
354-
if (relative_selector.selectors.length === 0) {
355-
if (!inside_pseudo_class) {
356-
e.css_selector_invalid(start);
357-
}
358-
} else {
354+
if (relative_selector.selectors.length > 0) {
359355
relative_selector.end = index;
360356
children.push(relative_selector);
361357
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,17 @@ const css_visitors = {
116116
);
117117
},
118118
RelativeSelector(node, context) {
119+
const parent = /** @type {Css.ComplexSelector} */ (context.path.at(-1));
120+
121+
if (
122+
node.combinator != null &&
123+
!context.state.rule?.metadata.parent_rule &&
124+
parent.children[0] === node &&
125+
context.path.at(-3)?.type !== 'PseudoClassSelector'
126+
) {
127+
e.css_selector_invalid(node.start); // only highlight the combinator
128+
}
129+
119130
node.metadata.is_global = node.selectors.length >= 1 && is_global(node);
120131

121132
if (node.selectors.length === 1) {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
warnings: [
5+
{
6+
code: 'css_unused_selector',
7+
end: {
8+
character: 109,
9+
column: 11,
10+
line: 11
11+
},
12+
message: 'Unused CSS selector "~ .unused"',
13+
start: {
14+
character: 100,
15+
column: 2,
16+
line: 11
17+
}
18+
}
19+
]
20+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
.foo.svelte-xyz {
3+
> .bar:where(.svelte-xyz) {
4+
color: red;
5+
}
6+
7+
/* (unused) ~ .unused {
8+
color: red;
9+
}*/
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<div class="foo">
2+
<div class="bar"></div>
3+
</div>
4+
5+
<style>
6+
.foo {
7+
> .bar {
8+
color: red;
9+
}
10+
11+
~ .unused {
12+
color: red;
13+
}
14+
}
15+
</style>

0 commit comments

Comments
 (0)