Ensure classes beginning with numbers are properly purged #185
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Proposed changes
Right now, classes starting with numeric characters are always preserved because of some code that was added to avoid screwing up fractional keyframe values.
This change attempts to solve the keyframe problem in a different way so that classes beginning with numbers can be properly purged.
The current problem comes from this line of code (link):
Specifically the regular expression that tests if the selector value begins with a number. As far as I can tell this was added as sort of a hack to detect that what you've parsed is actually part of a keyframe rule, and not a selector. What happens is a value like
99.9%
gets run through postcss-selector-parser and comes out as a99
tag and a9%
class, which is of course not correct because it's not a selector at all.The regex checks if the value begins with a number as sort of a proxy for figuring out "are we actually doing some pointless parsing on a keyframe rule right now?" which seems reasonable until you remember that a class can actually start with a number too, as long as it's escaped:
Because of this regex, any classes beginning with numbers will always be preserved and never purged.
The change in this PR tries to improve the detection of keyframe rules by adding a guard clause to the top of
evaluateRule
to check "is the parent of this rule a keyframes at-rule?" and if so, the check is completely skipped.This lets us remove the regex check completely, so classes beginning with numbers are properly purged.
This issue first showed up when a Tailwind CSS user was using screen prefixes like
400
which led to a ton of classes in their CSS not being removed:tailwindlabs/tailwindcss#860
I'm not sure exactly how you'd like to organize the tests for this, but for now I've just added a simple additional assertion to an existing test.
I'm also not sure if there are any other consequences to this change that are not covered by the current test suite. As far as I can find, there's no such thing as nested keyframes or anything, so checking if the parent is a keyframes at-rule should be robust enough. I also don't think there's a good reason to evaluate each keyframes child node for any reason because you need to either keep the entire keyframes declaration or throw the whole thing out, not really anything meaningful you can do with the individual children.
Let me know if there's anything I'm not thinking of, thanks!
Types of changes
What types of changes does your code introduce to Purgecss?
Put an
x
in the boxes that applyChecklist
Put an
x
in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.