Skip to content

Commit e4460e3

Browse files
authored
fix '~=' and class selectors with arbitrary whitespace (#4286)
1 parent 2f81365 commit e4460e3

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* Disallow two-way binding to a variable declared by an `{#await}` block ([#4012](https://github.com/sveltejs/svelte/issues/4012))
66
* Allow access to `let:` variables in sibling attributes on slot root ([#4173](https://github.com/sveltejs/svelte/issues/4173))
7+
* Fix `~=` and class selector matching against values separated by any whitespace characters ([#4242](https://github.com/sveltejs/svelte/issues/4242))
78
* Fix code generation for `await`ed expressions that need parentheses ([#4267](https://github.com/sveltejs/svelte/issues/4267))
89
* Add some more known globals ([#4276](https://github.com/sveltejs/svelte/pull/4276))
910
* Correctly apply event modifiers to `<svelte:body>` events ([#4278](https://github.com/sveltejs/svelte/issues/4278))

src/compiler/compile/css/Selector.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ function apply_selector(blocks: Block[], node: Element, stack: Element[], to_enc
171171
if (ancestor_block.global) {
172172
continue;
173173
}
174-
174+
175175
for (const stack_node of stack) {
176176
if (block_might_apply_to_node(ancestor_block, stack_node) !== BlockAppliesToNode.NotPossible) {
177177
to_encapsulate.push({ node: stack_node, block: ancestor_block });
@@ -256,7 +256,7 @@ function test_attribute(operator, expected_value, case_insensitive, value) {
256256
}
257257
switch (operator) {
258258
case '=': return value === expected_value;
259-
case '~=': return ` ${value} `.includes(` ${expected_value} `);
259+
case '~=': return value.split(/\s/).includes(expected_value);
260260
case '|=': return `${value}-`.startsWith(`${expected_value}-`);
261261
case '^=': return value.startsWith(expected_value);
262262
case '$=': return value.endsWith(expected_value);
@@ -295,7 +295,7 @@ function attribute_matches(node: CssNode, name: string, expected_value: string,
295295

296296
// impossible to find out all combinations
297297
if (current_possible_values.has(UNKNOWN)) return true;
298-
298+
299299
if (prev_values.length > 0) {
300300
const start_with_space = [];
301301
const remaining = [];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.foo.svelte-xyz{color:red}[class~="bar"].svelte-xyz{background:blue}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<div class="
2+
foo
3+
bar
4+
"></div>
5+
6+
<style>
7+
.foo {
8+
color: red;
9+
}
10+
[class~="bar"] {
11+
background: blue;
12+
}
13+
</style>

0 commit comments

Comments
 (0)