Skip to content

Commit 7e2b53c

Browse files
committed
avoid false positives when parsing colors (tailwindlabs#415)
1 parent dce390d commit 7e2b53c

File tree

1 file changed

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

1 file changed

+8
-3
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ function getKeywordColor(value: unknown): KeywordColor | null {
4040

4141
// https://github.com/khalilgharbaoui/coloregex
4242
const colorRegex = new RegExp(
43-
`(#(?:[0-9a-f]{2}){2,4}|(#[0-9a-f]{3})|(rgb|hsl)a?\\((-?[\\d.]+%?[,\\s]+){2,3}\\s*([\\d.]+%?|var\\([^)]+\\))?\\)|transparent|currentColor|${Object.keys(
43+
`(?:^|\\s|,)(#(?:[0-9a-f]{2}){2,4}|(#[0-9a-f]{3})|(rgb|hsl)a?\\((-?[\\d.]+%?[,\\s]+){2,3}\\s*([\\d.]+%?|var\\([^)]+\\))?\\)|transparent|currentColor|${Object.keys(
4444
colorNames
45-
).join('|')})`,
45+
).join('|')})(?:$|\\s|,)`,
4646
'gi'
4747
)
4848

@@ -52,7 +52,12 @@ function getColorsInString(str: string): (TinyColor | KeywordColor)[] {
5252
return (
5353
str
5454
.match(colorRegex)
55-
?.map((color) => color.replace(/var\([^)]+\)/, '1'))
55+
?.map((color) =>
56+
color
57+
.trim()
58+
.replace(/^,|,$/g, '')
59+
.replace(/var\([^)]+\)/, '1')
60+
)
5661
.map((color) => getKeywordColor(color) ?? new TinyColor(color))
5762
.filter((color) => (color instanceof TinyColor ? color.isValid : true)) ?? []
5863
)

0 commit comments

Comments
 (0)