Skip to content

fix: address css class matching regression #16204

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 2 commits into from
Jun 19, 2025
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
5 changes: 5 additions & 0 deletions .changeset/big-carpets-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: address css class matching regression
21 changes: 15 additions & 6 deletions packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js
Original file line number Diff line number Diff line change
Expand Up @@ -628,24 +628,33 @@ 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;
}
}

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);
Expand All @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions packages/svelte/tests/css/samples/class-directive/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -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 }*/
Original file line number Diff line number Diff line change
@@ -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>