Skip to content

Commit

Permalink
fix: attribute selector with spaces being removed
Browse files Browse the repository at this point in the history
  • Loading branch information
Ffloriel committed Jan 24, 2021
1 parent 62fa6cd commit 418dc7e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
9 changes: 7 additions & 2 deletions packages/purgecss/__tests__/attributes.test.ts
Expand Up @@ -63,8 +63,13 @@ describe("attributes", () => {

it("handles [attribute*=value]", () => {
// keep used css
expect(purgedCSS.includes('a[title~="thin"]')).toBe(true);
expect(purgedCSS.includes('a[title*="thin"]')).toBe(true);
// remove unused css
expect(purgedCSS.includes('a[title~="fat"]')).toBe(false);
expect(purgedCSS.includes('a[title*="fat"]')).toBe(false);
});

it("handles spaces in attribute selector", () => {
expect(purgedCSS.includes('[class*=" class2"]')).toBe(true);
expect(purgedCSS.includes('[class*="class1 class2 "]')).toBe(true);
});
});
Expand Up @@ -66,10 +66,18 @@ a[href$="http"] {

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

a[title~="thin"] {
a[title*="thin"] {
border: 5px solid yellow;
}

a[title~="fat"] {
a[title*="fat"] {
border: 5px solid yellow;
}

/* CSS [attribute*="value"] Selector with spaces */
[class*=" class2"] {
color: green;
}
[class*="class1 class2 "] {
color: blue;
}
Expand Up @@ -5,4 +5,5 @@
<a href="statements.pdf" target="_blank">pdf</a>
<a href="https://w3schools.com">go to website</a>
<a title="thin-yeah" href="#">hello</a>
<div class="class1 class2 class3">hello</div>
</html>
5 changes: 4 additions & 1 deletion packages/purgecss/src/ExtractorResultSets.ts
Expand Up @@ -65,7 +65,10 @@ class ExtractorResultSets {
}

hasAttrSubstr(substr: string): boolean {
return this.someAttrValue((value) => value.includes(substr));
const wordSubstr = substr.trim().split(" ");
return wordSubstr.every((word) =>
this.someAttrValue((value) => value.includes(word))
);
}

hasAttrValue(value: string): boolean {
Expand Down

0 comments on commit 418dc7e

Please sign in to comment.