Skip to content

Commit 418dc7e

Browse files
committed
fix: attribute selector with spaces being removed
#392
1 parent 62fa6cd commit 418dc7e

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

packages/purgecss/__tests__/attributes.test.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,13 @@ describe("attributes", () => {
6363

6464
it("handles [attribute*=value]", () => {
6565
// keep used css
66-
expect(purgedCSS.includes('a[title~="thin"]')).toBe(true);
66+
expect(purgedCSS.includes('a[title*="thin"]')).toBe(true);
6767
// remove unused css
68-
expect(purgedCSS.includes('a[title~="fat"]')).toBe(false);
68+
expect(purgedCSS.includes('a[title*="fat"]')).toBe(false);
69+
});
70+
71+
it("handles spaces in attribute selector", () => {
72+
expect(purgedCSS.includes('[class*=" class2"]')).toBe(true);
73+
expect(purgedCSS.includes('[class*="class1 class2 "]')).toBe(true);
6974
});
7075
});

packages/purgecss/__tests__/test_examples/attributes/attribute_selector.css

+10-2
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,18 @@ a[href$="http"] {
6666

6767
/* CSS [attribute*="value"] Selector */
6868

69-
a[title~="thin"] {
69+
a[title*="thin"] {
7070
border: 5px solid yellow;
7171
}
7272

73-
a[title~="fat"] {
73+
a[title*="fat"] {
7474
border: 5px solid yellow;
75+
}
76+
77+
/* CSS [attribute*="value"] Selector with spaces */
78+
[class*=" class2"] {
79+
color: green;
80+
}
81+
[class*="class1 class2 "] {
82+
color: blue;
7583
}

packages/purgecss/__tests__/test_examples/attributes/attribute_selector.html

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
<a href="statements.pdf" target="_blank">pdf</a>
66
<a href="https://w3schools.com">go to website</a>
77
<a title="thin-yeah" href="#">hello</a>
8+
<div class="class1 class2 class3">hello</div>
89
</html>

packages/purgecss/src/ExtractorResultSets.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ class ExtractorResultSets {
6565
}
6666

6767
hasAttrSubstr(substr: string): boolean {
68-
return this.someAttrValue((value) => value.includes(substr));
68+
const wordSubstr = substr.trim().split(" ");
69+
return wordSubstr.every((word) =>
70+
this.someAttrValue((value) => value.includes(word))
71+
);
6972
}
7073

7174
hasAttrValue(value: string): boolean {

0 commit comments

Comments
 (0)