Skip to content

Commit

Permalink
fix(module): safelist regex when a : was present before color
Browse files Browse the repository at this point in the history
Also prevents parsing colors already safelisted initially.
  • Loading branch information
benjamincanac committed Jun 21, 2023
1 parent f719111 commit f7e2082
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,18 @@ 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)}`)

for (const match of matches) {
const [, component, color] = match

if (!colors.includes(color)) {
if (!colors.includes(color) || safelistColors.includes(color)) {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]
}
}
Expand Down

0 comments on commit f7e2082

Please sign in to comment.