Skip to content

Commit cf26945

Browse files
committed
fix: match class and style directives against attribute selector
1 parent 931f211 commit cf26945

File tree

7 files changed

+55
-6
lines changed

7 files changed

+55
-6
lines changed

.changeset/fair-laws-appear.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: match class and style directives against attribute selector

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -532,12 +532,7 @@ function relative_selector_might_apply_to_node(relative_selector, rule, element,
532532
}
533533

534534
case 'ClassSelector': {
535-
if (
536-
!attribute_matches(element, 'class', name, '~=', false) &&
537-
!element.attributes.some(
538-
(attribute) => attribute.type === 'ClassDirective' && attribute.name === name
539-
)
540-
) {
535+
if (!attribute_matches(element, 'class', name, '~=', false)) {
541536
return false;
542537
}
543538

@@ -633,6 +628,16 @@ function attribute_matches(node, name, expected_value, operator, case_insensitiv
633628
if (attribute.type === 'SpreadAttribute') return true;
634629
if (attribute.type === 'BindDirective' && attribute.name === name) return true;
635630

631+
// 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 == '~=') {
635+
if (attribute.name === expected_value) return true;
636+
} else {
637+
return true;
638+
}
639+
}
640+
636641
if (attribute.type !== 'Attribute') continue;
637642
if (attribute.name.toLowerCase() !== name.toLowerCase()) continue;
638643

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
span[class].svelte-xyz { color: green }
2+
div[style].svelte-xyz { color: green }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<span class:foo={true}></span>
2+
<div style:--foo="bar"></div>
3+
4+
<style>
5+
span[class] { color: green }
6+
div[style] { color: green }
7+
</style>
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+
message: 'Unused CSS selector ".third"\nhttps://svelte.dev/e/css_unused_selector',
8+
start: {
9+
line: 6,
10+
column: 2,
11+
character: 115
12+
},
13+
end: {
14+
line: 6,
15+
column: 8,
16+
character: 121
17+
}
18+
}
19+
]
20+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.first.svelte-xyz { color: green }
2+
.second.svelte-xyz { color: green }
3+
/* (unused) .third { color: red }*/
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div class:first={true} class:second={true}></div>
2+
3+
<style>
4+
.first { color: green }
5+
.second { color: green }
6+
.third { color: red }
7+
</style>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy