From f7e2082983c2eb650e95a9040aafde4ce2c88c54 Mon Sep 17 00:00:00 2001 From: Benjamin Canac Date: Wed, 21 Jun 2023 17:03:09 +0200 Subject: [PATCH] fix(module): safelist regex when a `:` was present before color Also prevents parsing colors already safelisted initially. --- src/colors.ts | 7 ++++--- src/module.ts | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/colors.ts b/src/colors.ts index dba167d7..a4c93bcf 100644 --- a/src/colors.ts +++ b/src/colors.ts @@ -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 } diff --git a/src/module.ts b/src/module.ts index 4e9492b7..6a45bd2b 100644 --- a/src/module.ts +++ b/src/module.ts @@ -156,7 +156,7 @@ export default defineNuxtModule({ 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) ] } }