Skip to content

Commit 689aaa6

Browse files
committed
update color parser to avoid false positives (tailwindlabs#180)
1 parent e6a3719 commit 689aaa6

File tree

1 file changed

+11
-3
lines changed
  • packages/tailwindcss-language-service/src/util

1 file changed

+11
-3
lines changed

packages/tailwindcss-language-service/src/util/color.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const dlv = require('dlv')
22
import { State } from './state'
33
import removeMeta from './removeMeta'
4-
import { TinyColor } from '@ctrl/tinycolor'
4+
import { TinyColor, names as colorNames } from '@ctrl/tinycolor'
55
import { ensureArray, dedupe, flatten } from './array'
66

77
const COLOR_PROPS = [
@@ -89,10 +89,18 @@ export function getColor(
8989

9090
export function getColorFromValue(value: unknown): string {
9191
if (typeof value !== 'string') return null
92-
if (value === 'transparent') {
92+
const trimmedValue = value.trim()
93+
if (trimmedValue === 'transparent') {
9394
return 'rgba(0, 0, 0, 0.01)'
9495
}
95-
const color = new TinyColor(value)
96+
if (
97+
!/^\s*(?:rgba?|hsla?)\s*\([^)]+\)\s*$/.test(trimmedValue) &&
98+
!/^\s*#[0-9a-f]+\s*$/i.test(trimmedValue) &&
99+
!Object.keys(colorNames).includes(trimmedValue)
100+
) {
101+
return null
102+
}
103+
const color = new TinyColor(trimmedValue)
96104
if (color.isValid) {
97105
return color.toRgbString()
98106
}

0 commit comments

Comments
 (0)