Skip to content

Commit 659198a

Browse files
7nik7nik
andauthored
fix: address css class matching regression (#16204)
* fix: address css class matching regression * address feedback --------- Co-authored-by: 7nik <[email protected]>
1 parent d941cf5 commit 659198a

File tree

5 files changed

+34
-16
lines changed

5 files changed

+34
-16
lines changed

.changeset/big-carpets-stare.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: address css class matching regression

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -628,24 +628,33 @@ function attribute_matches(node, name, expected_value, operator, case_insensitiv
628628
if (attribute.type === 'SpreadAttribute') return true;
629629
if (attribute.type === 'BindDirective' && attribute.name === name) return true;
630630

631+
const name_lower = name.toLowerCase();
631632
// match attributes against the corresponding directive but bail out on exact matching
632-
if (attribute.type === 'StyleDirective' && name.toLowerCase() === 'style') return true;
633-
if (attribute.type === 'ClassDirective' && name.toLowerCase() === 'class') {
634-
if (operator == '~=') {
633+
if (attribute.type === 'StyleDirective' && name_lower === 'style') return true;
634+
if (attribute.type === 'ClassDirective' && name_lower === 'class') {
635+
if (operator === '~=') {
635636
if (attribute.name === expected_value) return true;
636637
} else {
637638
return true;
638639
}
639640
}
640641

641642
if (attribute.type !== 'Attribute') continue;
642-
if (attribute.name.toLowerCase() !== name.toLowerCase()) continue;
643+
if (attribute.name.toLowerCase() !== name_lower) continue;
643644

644645
if (attribute.value === true) return operator === null;
645646
if (expected_value === null) return true;
646647

647648
if (is_text_attribute(attribute)) {
648-
return test_attribute(operator, expected_value, case_insensitive, attribute.value[0].data);
649+
const matches = test_attribute(
650+
operator,
651+
expected_value,
652+
case_insensitive,
653+
attribute.value[0].data
654+
);
655+
// continue if we still may match against a class/style directive
656+
if (!matches && (name_lower === 'class' || name_lower === 'style')) continue;
657+
return matches;
649658
}
650659

651660
const chunks = get_attribute_chunks(attribute.value);
@@ -654,7 +663,7 @@ function attribute_matches(node, name, expected_value, operator, case_insensitiv
654663
/** @type {string[]} */
655664
let prev_values = [];
656665
for (const chunk of chunks) {
657-
const current_possible_values = get_possible_values(chunk, name === 'class');
666+
const current_possible_values = get_possible_values(chunk, name_lower === 'class');
658667

659668
// impossible to find out all combinations
660669
if (!current_possible_values) return true;

packages/svelte/tests/css/samples/class-directive/_config.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ export default test({
44
warnings: [
55
{
66
code: 'css_unused_selector',
7-
message: 'Unused CSS selector ".third"\nhttps://svelte.dev/e/css_unused_selector',
7+
message: 'Unused CSS selector ".forth"\nhttps://svelte.dev/e/css_unused_selector',
88
start: {
9-
line: 6,
9+
line: 8,
1010
column: 2,
11-
character: 115
11+
character: 190
1212
},
1313
end: {
14-
line: 6,
14+
line: 8,
1515
column: 8,
16-
character: 121
16+
character: 196
1717
}
1818
}
1919
]
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
.first.svelte-xyz { color: green }
1+
2+
.zero.first.svelte-xyz { color: green }
23
.second.svelte-xyz { color: green }
3-
/* (unused) .third { color: red }*/
4+
.third.svelte-xyz { color: green }
5+
/* (unused) .forth { color: red }*/
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
<div class:first={true} class:second={true}></div>
1+
<div class="zero" class:first={true}></div>
2+
<div class:second={true} class:third={true}></div>
23

34
<style>
4-
.first { color: green }
5+
.zero.first { color: green }
56
.second { color: green }
6-
.third { color: red }
7+
.third { color: green }
8+
.forth { color: red }
79
</style>

0 commit comments

Comments
 (0)