fix: address css class matching regression (#16204)

* fix: address css class matching regression

* address feedback

---------

Co-authored-by: 7nik <kifiranet@gmail.com>
pull/16205/head
7nik 3 months ago committed by GitHub
parent d941cf5d3b
commit 659198a95c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: address css class matching regression

@ -628,10 +628,11 @@ function attribute_matches(node, name, expected_value, operator, case_insensitiv
if (attribute.type === 'SpreadAttribute') return true;
if (attribute.type === 'BindDirective' && attribute.name === name) return true;
const name_lower = name.toLowerCase();
// match attributes against the corresponding directive but bail out on exact matching
if (attribute.type === 'StyleDirective' && name.toLowerCase() === 'style') return true;
if (attribute.type === 'ClassDirective' && name.toLowerCase() === 'class') {
if (operator == '~=') {
if (attribute.type === 'StyleDirective' && name_lower === 'style') return true;
if (attribute.type === 'ClassDirective' && name_lower === 'class') {
if (operator === '~=') {
if (attribute.name === expected_value) return true;
} else {
return true;
@ -639,13 +640,21 @@ function attribute_matches(node, name, expected_value, operator, case_insensitiv
}
if (attribute.type !== 'Attribute') continue;
if (attribute.name.toLowerCase() !== name.toLowerCase()) continue;
if (attribute.name.toLowerCase() !== name_lower) continue;
if (attribute.value === true) return operator === null;
if (expected_value === null) return true;
if (is_text_attribute(attribute)) {
return test_attribute(operator, expected_value, case_insensitive, attribute.value[0].data);
const matches = test_attribute(
operator,
expected_value,
case_insensitive,
attribute.value[0].data
);
// continue if we still may match against a class/style directive
if (!matches && (name_lower === 'class' || name_lower === 'style')) continue;
return matches;
}
const chunks = get_attribute_chunks(attribute.value);
@ -654,7 +663,7 @@ function attribute_matches(node, name, expected_value, operator, case_insensitiv
/** @type {string[]} */
let prev_values = [];
for (const chunk of chunks) {
const current_possible_values = get_possible_values(chunk, name === 'class');
const current_possible_values = get_possible_values(chunk, name_lower === 'class');
// impossible to find out all combinations
if (!current_possible_values) return true;

@ -4,16 +4,16 @@ export default test({
warnings: [
{
code: 'css_unused_selector',
message: 'Unused CSS selector ".third"\nhttps://svelte.dev/e/css_unused_selector',
message: 'Unused CSS selector ".forth"\nhttps://svelte.dev/e/css_unused_selector',
start: {
line: 6,
line: 8,
column: 2,
character: 115
character: 190
},
end: {
line: 6,
line: 8,
column: 8,
character: 121
character: 196
}
}
]

@ -1,3 +1,5 @@
.first.svelte-xyz { color: green }
.zero.first.svelte-xyz { color: green }
.second.svelte-xyz { color: green }
/* (unused) .third { color: red }*/
.third.svelte-xyz { color: green }
/* (unused) .forth { color: red }*/

@ -1,7 +1,9 @@
<div class:first={true} class:second={true}></div>
<div class="zero" class:first={true}></div>
<div class:second={true} class:third={true}></div>
<style>
.first { color: green }
.zero.first { color: green }
.second { color: green }
.third { color: red }
.third { color: green }
.forth { color: red }
</style>
Loading…
Cancel
Save