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 e7836e7
Showing
3 changed files
with
74 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
39 changes: 39 additions & 0 deletions
39
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,39 @@ | ||
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], | ||
|
||
// 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) | ||
}) |
25 changes: 25 additions & 0 deletions
25
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,25 @@ | ||
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 '#': // 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 | ||
} |