-
Notifications
You must be signed in to change notification settings - Fork 711
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(tailwind): extract selector processing from sanitization functions
- Loading branch information
Showing
3 changed files
with
58 additions
and
48 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
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,45 @@ | ||
import selectorParser from "postcss-selector-parser"; | ||
import type { Rule } from "postcss"; | ||
import { sanitizeClassName } from "../compatibility/sanitize-class-name"; | ||
|
||
/** | ||
* Helper function to process selectors and sanitize class names. | ||
* @param rule - The CSS rule to process. | ||
* @param withPseudo - Whether to process selectors with pseudo-classes. | ||
* @param outputClasses - Array to store class names after processing. | ||
* @returns An object containing the processed selector and whether it has a pseudo selector. | ||
*/ | ||
export const processSelector = ( | ||
rule: Rule, | ||
withPseudo: boolean, | ||
outputClasses: string[], | ||
) => { | ||
let hasPseudoSelector = false as boolean; | ||
|
||
const processedSelector = selectorParser((selectorRoot) => { | ||
selectorRoot.walkPseudos(() => { | ||
hasPseudoSelector = true; | ||
}); | ||
|
||
if ( | ||
(withPseudo && hasPseudoSelector) || | ||
(!withPseudo && !hasPseudoSelector) | ||
) { | ||
selectorRoot.walkClasses((singleClass) => { | ||
outputClasses.push(singleClass.value); | ||
|
||
singleClass.replaceWith( | ||
selectorParser.className({ | ||
...singleClass, | ||
value: sanitizeClassName(singleClass.value), | ||
}), | ||
); | ||
}); | ||
} | ||
}).processSync(rule.selector); | ||
|
||
return { | ||
processedSelector, | ||
hasPseudoSelector, | ||
}; | ||
}; |
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