Skip to content

Gracefully handle color parsing failures #1363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 13, 2025
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