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
Collect user-defined CSS that can be used in @apply
#13349
Open
RobinMalfait
wants to merge
10
commits into
next
Choose a base branch
from
feat/apply-user-css
base: next
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8ba5c78
implement `@apply <user-defined-css-class>`
RobinMalfait 6488984
improve variable name
RobinMalfait 0906a55
validate the selector
RobinMalfait 3f50c06
add docblock
RobinMalfait dd01105
add more apply tests
RobinMalfait 80a32e9
detect circular `@apply` calls
RobinMalfait c7a1308
adjust comment
RobinMalfait 52f5ef9
add more comments
RobinMalfait a3e3be5
update error message to match v3 error message
RobinMalfait 4ecb61b
run prettier
RobinMalfait File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
packages/tailwindcss/src/utils/is-simple-class-selector.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { expect, it } from 'vitest' | ||
import { isSimpleClassSelector } from './is-simple-class-selector' | ||
|
||
it.each([ | ||
// Simple class selector | ||
['.foo', true], | ||
|
||
// Class selectors with escaped characters | ||
['.w-\\[123px\\]', true], | ||
['.content-\\[\\+\\>\\~\\*\\]', true], | ||
|
||
// ID selector | ||
['#foo', false], | ||
['.foo#foo', false], | ||
|
||
// Element selector | ||
['h1', false], | ||
['h1.foo', false], | ||
|
||
// Attribute selector | ||
['[data-foo]', false], | ||
['.foo[data-foo]', false], | ||
['[data-foo].foo', false], | ||
|
||
// Pseudo-class selector | ||
['.foo:hover', false], | ||
|
||
// Additional class selector | ||
['.foo.bar', false], | ||
|
||
// Combinator | ||
['.foo>.bar', false], | ||
['.foo+.bar', false], | ||
['.foo~.bar', false], | ||
['.foo .bar', false], | ||
|
||
// Selector list | ||
['.foo, .bar', false], | ||
['.foo,.bar', false], | ||
])('should validate %s', (selector, expected) => { | ||
expect(isSimpleClassSelector(selector)).toBe(expected) | ||
}) |
33 changes: 33 additions & 0 deletions
33
packages/tailwindcss/src/utils/is-simple-class-selector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,33 @@ | ||||||
/** | ||||||
* Check if a selector is a simple class selector. | ||||||
* | ||||||
* A simple class selector is a class selector that doesn't contain any other | ||||||
* selector types, such as ID selectors, element selectors, attribute selectors, | ||||||
* pseudo-classes, combinators, or selector lists. | ||||||
*/ | ||||||
export function isSimpleClassSelector(selector: string): boolean { | ||||||
// The selector must start with a dot, otherwise it's not a class selector. | ||||||
if (selector[0] !== '.') return false | ||||||
|
||||||
for (let i = 1; i < selector.length; i++) { | ||||||
switch (selector[i]) { | ||||||
// The character is escaped, skip the next character | ||||||
case '\\': | ||||||
i += 1 | ||||||
continue | ||||||
|
||||||
case ' ': // Descendat combinator | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
case '.': // Class selector | ||||||
case '#': // ID selector | ||||||
case '[': // Attribute selector | ||||||
case ':': // Pseudo-classes and pseudo-elements | ||||||
case '>': // Child combinator | ||||||
case '+': // Next-sibling combinator | ||||||
case '~': // Subsequent-sibling combinator | ||||||
case ',': // Selector list | ||||||
return false | ||||||
} | ||||||
} | ||||||
|
||||||
return true | ||||||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe you don't need
node.selector[0] === '.'
?Because inside
isSimpleClassSelector
, you perform this test with an early return:if (selector[0] !== '.') return false
I would suggest to remove this line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still required, if you comment this out a test should fail. This is basically for the scenario where you have
.foo.bar
which contains 2 classes which is not allowed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch 😉