-
Notifications
You must be signed in to change notification settings - Fork 0
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
c896716
commit 06c6ada
Showing
16 changed files
with
161 additions
and
47 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules/ | ||
dist/ | ||
.eslintcache | ||
local-bench/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -3,3 +3,4 @@ dist/ | |
tests/snapshots/ | ||
bench/source | ||
playground/output.css | ||
local-bench/out.css |
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
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
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
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,65 @@ | ||
export const escapeSelector = (selector: string) => { | ||
const escaped = selector.replaceAll( | ||
// Keep in sync allowed chars below | ||
/[.:/[\]!#='",%&>+~*@()]/g, | ||
(c) => `\\${c}`, | ||
); | ||
return /^\d/.test(escaped) ? `\\${escaped}` : escaped; | ||
}; | ||
|
||
const regularVariant = /[a-z0-9][a-z0-9-]+/; | ||
const dynamicVariant = /[a-z]+-\[[a-z:-]+]/; | ||
// & to position the selector | ||
// []=" for attributes: [&[type="email"]] | ||
// :>+*~.()_ for css selectors: [&:nth-child(3)] [&_p] [&>*] [.sidebar+&] | ||
// @ for media: [@media(min-width:900px)] | ||
const arbitraryVariant = /\[[a-z0-9&[\]=":>+*~.()_@-]+]/; | ||
const variant = new RegExp( | ||
`(?:${regularVariant.source}|${dynamicVariant.source}|${arbitraryVariant.source}):`, | ||
); | ||
const variants = new RegExp(`(?:${variant.source})*`); | ||
|
||
// Opacity/line-height modifiers | ||
const regularModifier = /[a-z0-9]+/; | ||
// .% for opacity | ||
// ()+*/- for calc (line height) | ||
const arbitraryModifier = /\[[a-z0-9.%()+*/-]+]/; | ||
const modifier = new RegExp( | ||
`/(?:${regularModifier.source}|${arbitraryModifier.source})`, | ||
); | ||
|
||
// % linear-background: via-40% | ||
// Requires at least 3 chars, only a constraint for custom utils | ||
const regularUtilities = /[a-z][a-z0-9-]*[a-z0-9%]/; | ||
// # for color | ||
// . for opacity | ||
// _, for linear-background | ||
// ' for content (before/after) | ||
// % for size | ||
// ()+*/- for calc | ||
const arbitraryValueSet = /[a-z0-9#._,'%()+*/-]+/; | ||
const arbitraryValues = new RegExp( | ||
`[a-z][a-z-]*-\\[${arbitraryValueSet.source}]`, | ||
); | ||
|
||
const ruleBasedContent = new RegExp( | ||
`-?(?:${regularUtilities.source}|${arbitraryValues.source})(?:${modifier.source})?`, | ||
); | ||
|
||
const arbitraryProperties = new RegExp( | ||
`\\[[a-z][a-z-]+:${arbitraryValueSet.source}]`, | ||
); | ||
const selectorREWithoutBorders = new RegExp( | ||
`${variants.source}!?(?:${ruleBasedContent.source}|${arbitraryProperties.source})`, | ||
); | ||
|
||
// } for template string: `${base}text-lg` (questionnable) | ||
const leftBorder = /(?<=['"`\s}])/; | ||
// : for object keys | ||
// $ for template string: `text-lg${base}` (questionnable) | ||
const rightBorder = /(?=['"`\s:$])/; | ||
|
||
export const selectorRE = new RegExp( | ||
`${leftBorder.source}${selectorREWithoutBorders.source}${rightBorder.source}`, | ||
"g", | ||
); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import assert from "node:assert"; | ||
import { test } from "node:test"; | ||
import { selectorRE } from "../src/utils/regex.ts"; | ||
|
||
test("regex", () => { | ||
assert.equal( | ||
selectorRE.toString(), | ||
/(?<=['"`\s}])(?:(?:[a-z0-9][a-z0-9-]+|[a-z]+-\[[a-z:-]+]|\[[a-z0-9&[\]=":>+*~.()_@-]+]):)*!?(?:-?(?:[a-z][a-z0-9-]*[a-z0-9%]|[a-z][a-z-]*-\[[a-z0-9#._,'%()+*/-]+])(?:\/(?:[a-z0-9]+|\[[a-z0-9.%()+*/-]+]))?|\[[a-z][a-z-]+:[a-z0-9#._,'%()+*/-]+])(?=['"`\s:$])/g.toString(), | ||
); | ||
}); |
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
Oops, something went wrong.