Skip to content
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

bugfix: Classes that contain :is or :where in their names are no longer unintentionally purged #1187

Merged
merged 2 commits into from
Feb 3, 2024
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
8 changes: 8 additions & 0 deletions packages/purgecss/__tests__/pseudo-class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ describe(":where pseudo class", () => {
const resultsPurge = await new PurgeCSS().purge({
content: [`${ROOT_TEST_EXAMPLES}pseudo-class/where.html`],
css: [`${ROOT_TEST_EXAMPLES}pseudo-class/where.css`],
safelist: {
standard: ["[&:where(.a)]:text-black"],
}
Comment on lines +110 to +112
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This was added to the safelist as the default extractor doesn't properly extract the whole class name.

});
purgedCSS = resultsPurge[0].css;
});
Expand All @@ -115,6 +118,7 @@ describe(":where pseudo class", () => {
expect(purgedCSS.includes(".unused")).toBe(false);
expect(purgedCSS.includes(".root :where(.a) .c {")).toBe(true);
expect(purgedCSS.includes(".root:where(.a) .c {")).toBe(true);
expect(purgedCSS.includes(".\\[\\&\\:where\\(\\.a\\)\\]\\:text-black:where(.a) {")).toBe(true);
});
});

Expand All @@ -124,6 +128,9 @@ describe(":is pseudo class", () => {
const resultsPurge = await new PurgeCSS().purge({
content: [`${ROOT_TEST_EXAMPLES}pseudo-class/is.html`],
css: [`${ROOT_TEST_EXAMPLES}pseudo-class/is.css`],
safelist: {
standard: ["[&:is(.a)]:text-black"]
}
});
purgedCSS = resultsPurge[0].css;
});
Expand All @@ -132,5 +139,6 @@ describe(":is pseudo class", () => {
expect(purgedCSS.includes(".unused")).toBe(false);
expect(purgedCSS.includes(".root :is(.a) .c {")).toBe(true);
expect(purgedCSS.includes(".root:is(.a) .c {")).toBe(true);
expect(purgedCSS.includes(".\\[\\&\\:is\\(\\.a\\)\\]\\:text-black:is(.a) {")).toBe(true);
});
});
4 changes: 4 additions & 0 deletions packages/purgecss/__tests__/test_examples/pseudo-class/is.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@
.root :is(.unused) .c,
.root :is(.a, .b) .c :is(.unused, .unused2) {
display: flex;
}

.\[\&\:is\(\.a\)\]\:text-black:is(.a) {
color: black;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@
.root :where(.unused) .c,
.root :where(.a, .b) .c :where(.unused, .unused2) {
display: flex;
}

.\[\&\:where\(\.a\)\]\:text-black:where(.a) {
color: black;
}
11 changes: 9 additions & 2 deletions packages/purgecss/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,16 +553,23 @@ class PurgeCSS {
}
});

// removes selectors containing empty :where and :is
selectorsParsed.walk((selector) => {
if (selector.type !== "selector") {
return;
}

if (
selector.toString() &&
/(:where$)|(:is$)|(:where[^(])|(:is[^(])/.test(selector.toString())
/(:where)|(:is)/.test(selector.toString())
) {
selector.remove();
selector.walk((node) => {
if (node.type !== "pseudo") return;
if (node.value !== ":where" && node.value !== ":is") return;
if (node.nodes.length === 0) {
selector.remove();
}
});
}
});
}).processSync(node.selector);
Expand Down