-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6488984
commit 0906a55
Showing
3 changed files
with
78 additions
and
1 deletion.
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
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) | ||
}) |
26 changes: 26 additions & 0 deletions
26
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,26 @@ | ||
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 | ||
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 | ||
} |