fix(module): safelist regex when a : was present before color

Also prevents parsing colors already safelisted initially.
This commit is contained in:
Benjamin Canac
2023-06-21 17:03:09 +02:00
parent f719111abb
commit f7e2082983
2 changed files with 5 additions and 4 deletions

View File

@@ -139,9 +139,10 @@ export const generateSafelist = (colors: string[]) => {
]
}
export const customSafelistExtractor = (prefix, content: string, colors: string[]) => {
export const customSafelistExtractor = (prefix, content: string, colors: string[], safelistColors: string[]) => {
const classes = []
const regex = /<(\w+)\s+[^>:]*color=["']([^"']+)["'][^>]*>/gs
const regex = /<(\w+)\s+(?![^>]*:color\b)[^>]*\bcolor=["']([^"']+)["'][^>]*>/gs
const matches = content.matchAll(regex)
const components = Object.keys(safelistByComponent).map(component => `${prefix}${component.charAt(0).toUpperCase() + component.slice(1)}`)
@@ -149,7 +150,7 @@ export const customSafelistExtractor = (prefix, content: string, colors: string[
for (const match of matches) {
const [, component, color] = match
if (!colors.includes(color)) {
if (!colors.includes(color) || safelistColors.includes(color)) {
continue
}

View File

@@ -156,7 +156,7 @@ export default defineNuxtModule<ModuleOptions>({
vue: (content) => {
return [
...defaultExtractor(content),
...customSafelistExtractor(options.prefix, content, nuxt.options.appConfig.ui.colors)
...customSafelistExtractor(options.prefix, content, nuxt.options.appConfig.ui.colors, options.safelistColors)
]
}
}