update color parser to avoid false positives (#180)

master
Brad Cornes 2020-11-24 15:50:11 +00:00
parent e6a371921f
commit 689aaa6d3b
1 changed files with 11 additions and 3 deletions

View File

@ -1,7 +1,7 @@
const dlv = require('dlv') const dlv = require('dlv')
import { State } from './state' import { State } from './state'
import removeMeta from './removeMeta' import removeMeta from './removeMeta'
import { TinyColor } from '@ctrl/tinycolor' import { TinyColor, names as colorNames } from '@ctrl/tinycolor'
import { ensureArray, dedupe, flatten } from './array' import { ensureArray, dedupe, flatten } from './array'
const COLOR_PROPS = [ const COLOR_PROPS = [
@ -89,10 +89,18 @@ export function getColor(
export function getColorFromValue(value: unknown): string { export function getColorFromValue(value: unknown): string {
if (typeof value !== 'string') return null if (typeof value !== 'string') return null
if (value === 'transparent') { const trimmedValue = value.trim()
if (trimmedValue === 'transparent') {
return 'rgba(0, 0, 0, 0.01)' return 'rgba(0, 0, 0, 0.01)'
} }
const color = new TinyColor(value) if (
!/^\s*(?:rgba?|hsla?)\s*\([^)]+\)\s*$/.test(trimmedValue) &&
!/^\s*#[0-9a-f]+\s*$/i.test(trimmedValue) &&
!Object.keys(colorNames).includes(trimmedValue)
) {
return null
}
const color = new TinyColor(trimmedValue)
if (color.isValid) { if (color.isValid) {
return color.toRgbString() return color.toRgbString()
} }