Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions packages/tailwindcss-language-service/src/util/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function getColorsInString(state: State, str: string): (culori.Color | KeywordCo

function toColor(match: RegExpMatchArray) {
let color = match[1].replace(/var\([^)]+\)/, '1')
return getKeywordColor(color) ?? culori.parse(color)
return getKeywordColor(color) ?? tryParseColor(color)
}

str = replaceCssVarsWithFallbacks(state, str)
Expand Down Expand Up @@ -275,8 +275,8 @@ export function getColorFromValue(value: unknown): culori.Color | KeywordColor |
) {
return null
}
const color = culori.parse(trimmedValue)
return color ?? null

return tryParseColor(trimmedValue)
}

let toRgb = culori.converter('rgb')
Expand All @@ -296,11 +296,21 @@ export function formatColor(color: culori.Color): string {

const COLOR_MIX_REGEX = /color-mix\(in [^,]+,\s*(.*?)\s*(\d+|\.\d+|\d+\.\d+)%,\s*transparent\)/g

function tryParseColor(color: string) {
try {
return culori.parse(color) ?? null
} catch (err) {
console.error('Error parsing color', color)
console.error(err)
return null
}
}

function removeColorMixWherePossible(str: string) {
return str.replace(COLOR_MIX_REGEX, (match, color, percentage) => {
if (color.startsWith('var(')) return match

let parsed = culori.parse(color)
let parsed = tryParseColor(color)
if (!parsed) return match

let alpha = Number(percentage) / 100
Expand Down
1 change: 1 addition & 0 deletions packages/vscode-tailwindcss/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Ignore Python virtual env directories by default ([#1336](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1336))
- Ignore Yarn v2+ metadata & cache directories by default ([#1336](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1336))
- Ignore some build caches by default ([#1336](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1336))
- Gracefully handle color parsing failures ([#1363](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1363))

# 0.14.16

Expand Down