From 01a9fff4c1db23735c1f22a82c2f2f1a816197a0 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Wed, 4 Nov 2026 13:11:15 -0500 Subject: [PATCH 1/5] =?UTF-8?q?Skip=20the=20`color-mix(=E2=80=A6)`=20polyf?= =?UTF-8?q?ill=20without=20a=20color=20interpolation=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The color interpolation method of `color-mix(…)` is optional (it defaults to `oklab`). The polyfill assumed that each `color-mix(…)` starts with `in ,`, and took the node after the first `,` as the fallback. For `color-mix(currentcolor 50%, transparent)` that is the second color, so the fallback was `#0000` instead of `currentColor`. Lightning CSS also doesn't compute a `color-mix(…)` without a color interpolation method, so a fallback with inlined theme colors is dropped by browsers without `color-mix(…)`. Thus we skip the polyfill for such a `color-mix(…)`. The check skips leading separators, because the value parser gives `color-mix( in oklab, …)` a leading separator node. The optional chain on the colorspace prevents a crash for e.g. `color-mix(in)`. --- ...x-polyfill-without-interpolation-method.md | 3 + packages/tailwindcss/src/ast.ts | 7 ++ packages/tailwindcss/src/index.test.ts | 71 +++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 .changelog/color-mix-polyfill-without-interpolation-method.md diff --git a/.changelog/color-mix-polyfill-without-interpolation-method.md b/.changelog/color-mix-polyfill-without-interpolation-method.md new file mode 100644 index 000000000000..571a476a5385 --- /dev/null +++ b/.changelog/color-mix-polyfill-without-interpolation-method.md @@ -0,0 +1,3 @@ +### Fixed + +- Don't create a `color-mix(…)` fallback with the wrong color when the `color-mix(…)` has no color interpolation method (e.g. `color-mix(currentcolor 50%, transparent)`) diff --git a/packages/tailwindcss/src/ast.ts b/packages/tailwindcss/src/ast.ts index 3775c1ea044f..8a31c5ecd80b 100644 --- a/packages/tailwindcss/src/ast.ts +++ b/packages/tailwindcss/src/ast.ts @@ -521,6 +521,13 @@ export function optimizeAst( walk(ast, (node) => { if (node.kind !== 'function' || node.value !== 'color-mix') return + // The color interpolation method is optional, e.g.: `color-mix(red 50%, blue)`. In that + // case the node after the first `,` is the second color, not the first one. Lightning CSS + // also doesn't compute a `color-mix(…)` without a color interpolation method, so a + // fallback with inlined colors would be dropped by browsers without `color-mix(…)`. + let firstNode = node.nodes.find((node) => node.kind !== 'separator') + if (firstNode?.kind !== 'word' || firstNode.value.toLowerCase() !== 'in') return + let containsUnresolvableVars = false let containsCurrentcolor = false walk(node.nodes, (node) => { diff --git a/packages/tailwindcss/src/index.test.ts b/packages/tailwindcss/src/index.test.ts index 0a244173b858..25f535410226 100644 --- a/packages/tailwindcss/src/index.test.ts +++ b/packages/tailwindcss/src/index.test.ts @@ -6846,6 +6846,77 @@ describe('`color-mix(…)` polyfill', () => { `) }) + it('uses the first color value as the fallback when the color interpolation method has leading whitespace', async () => { + expect( + await compileCss( + // prettier-ignore + css` + @tailwind utilities; + .foo { + color: color-mix( in oklab, currentcolor 50%, transparent); + } + `, + ), + ).toMatchInlineSnapshot(` + " + .foo { + color: currentColor; + } + + @supports (color: color-mix(in lab, red, red)) { + .foo { + color: color-mix(in oklab, currentcolor 50%, transparent); + } + } + " + `) + }) + + it('does not create a fallback when the `color-mix(…)` function has no color interpolation method', async () => { + expect( + await compileCss(css` + @theme { + --color-red-500: oklch(63.7% 0.237 25.331); + } + @tailwind utilities; + .current { + color: color-mix(currentcolor 50%, transparent); + } + .unknown { + color: color-mix(var(--unknown), red); + } + .theme { + color: color-mix(var(--color-red-500) 50%, transparent); + } + .single { + color: color-mix(var(--color-red-500)); + } + `), + ).toMatchInlineSnapshot(` + " + :root, :host { + --color-red-500: oklch(63.7% .237 25.331); + } + + .current { + color: color-mix(currentcolor 50%, transparent); + } + + .unknown { + color: color-mix(var(--unknown), red); + } + + .theme { + color: color-mix(var(--color-red-500) 50%, transparent); + } + + .single { + color: color-mix(var(--color-red-500)); + } + " + `) + }) + it('uses the first color value as the fallback when the `color-mix(…)` function contains theme variables that resolves to other variables', async () => { expect( await run( From 6cd2b81d851b430cb61397ed8ccdf289690143db Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Wed, 4 Nov 2026 13:23:50 -0500 Subject: [PATCH 2/5] =?UTF-8?q?Ignore=20comments=20before=20the=20color=20?= =?UTF-8?q?interpolation=20method=20of=20`color-mix(=E2=80=A6)`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Custom properties keep their comments, e.g.: --tint: color-mix(/* tint */ in oklab, currentcolor 50%, transparent); The value parser has no comment node, and reads `/*` as the words `/` and `*`. Thus the check for `in` didn't see the color interpolation method, and skipped the polyfill. Before the previous commit, this custom property got the `currentcolor` fallback. Now we drop comments from the arguments before we look for `in`. --- packages/tailwindcss/src/ast.ts | 9 +++++++-- packages/tailwindcss/src/index.test.ts | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/packages/tailwindcss/src/ast.ts b/packages/tailwindcss/src/ast.ts index 8a31c5ecd80b..0b33aef48045 100644 --- a/packages/tailwindcss/src/ast.ts +++ b/packages/tailwindcss/src/ast.ts @@ -525,8 +525,13 @@ export function optimizeAst( // case the node after the first `,` is the second color, not the first one. Lightning CSS // also doesn't compute a `color-mix(…)` without a color interpolation method, so a // fallback with inlined colors would be dropped by browsers without `color-mix(…)`. - let firstNode = node.nodes.find((node) => node.kind !== 'separator') - if (firstNode?.kind !== 'word' || firstNode.value.toLowerCase() !== 'in') return + // + // Custom properties keep their comments, e.g.: `color-mix(/* … */ in oklab, …)`, and the + // value parser doesn't know about comments, so we drop them before we look for `in`. + let args = ValueParser.toCss(node.nodes) + .replace(/\/\*[\s\S]*?\*\//g, ' ') + .trimStart() + if (!/^in\s/i.test(args)) return let containsUnresolvableVars = false let containsCurrentcolor = false diff --git a/packages/tailwindcss/src/index.test.ts b/packages/tailwindcss/src/index.test.ts index 25f535410226..2b4efefc6ad3 100644 --- a/packages/tailwindcss/src/index.test.ts +++ b/packages/tailwindcss/src/index.test.ts @@ -6872,6 +6872,29 @@ describe('`color-mix(…)` polyfill', () => { `) }) + it('uses the first color value as the fallback when a comment comes before the color interpolation method', async () => { + expect( + await compileCss(css` + @tailwind utilities; + .foo { + --tint: color-mix(/* tint */ in oklab, currentcolor 50%, transparent); + } + `), + ).toMatchInlineSnapshot(` + " + .foo { + --tint: currentcolor; + } + + @supports (color: color-mix(in lab, red, red)) { + .foo { + --tint: color-mix(/* tint */ in oklab, currentcolor 50%, transparent); + } + } + " + `) + }) + it('does not create a fallback when the `color-mix(…)` function has no color interpolation method', async () => { expect( await compileCss(css` From a58f849c4ff06616b1d3e4bf5384e835985d1d17 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Wed, 4 Nov 2026 16:06:04 -0500 Subject: [PATCH 3/5] =?UTF-8?q?Update=20the=20`color-mix(=E2=80=A6)`=20tes?= =?UTF-8?q?t=20without=20an=20interpolation=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/tailwindlabs/tailwindcss/pull/20658 landed on `main` after this PR was opened. Its test expected a fallback for `color-mix(var(--color-red-500))`. This PR skips the polyfill for a `color-mix(…)` without a color interpolation method on purpose, so the test now expects no fallback. The test still proves that the build doesn't crash. The `.single` case of our test covered the same input, so it is removed. The comment on the optional chain of the colorspace named the same example, which no longer reaches that code. --- packages/tailwindcss/src/ast.ts | 3 --- packages/tailwindcss/src/index.test.ts | 15 +-------------- 2 files changed, 1 insertion(+), 17 deletions(-) diff --git a/packages/tailwindcss/src/ast.ts b/packages/tailwindcss/src/ast.ts index 0b33aef48045..94a4d5d644b5 100644 --- a/packages/tailwindcss/src/ast.ts +++ b/packages/tailwindcss/src/ast.ts @@ -594,9 +594,6 @@ export function optimizeAst( } else if (requiresPolyfill) { // Change the colorspace to `srgb` since the fallback values should not be represented as // `oklab(…)` functions again as their support in Safari <16 is very limited. - // - // The color interpolation method is optional, so there might not be a - // colorspace, e.g.: `color-mix(var(--color-red-500))`. let colorspace = node.nodes[2] if ( colorspace?.kind === 'word' && diff --git a/packages/tailwindcss/src/index.test.ts b/packages/tailwindcss/src/index.test.ts index 2b4efefc6ad3..fa4505658072 100644 --- a/packages/tailwindcss/src/index.test.ts +++ b/packages/tailwindcss/src/index.test.ts @@ -6768,13 +6768,7 @@ describe('`color-mix(…)` polyfill', () => { } .bg-\\[color-mix\\(var\\(--color-red-500\\)\\)\\] { - background-color: color-mix(oklch(63.7% .237 25.331)); - } - - @supports (color: color-mix(in lab, red, red)) { - .bg-\\[color-mix\\(var\\(--color-red-500\\)\\)\\] { - background-color: color-mix(var(--color-red-500)); - } + background-color: color-mix(var(--color-red-500)); } " `) @@ -6911,9 +6905,6 @@ describe('`color-mix(…)` polyfill', () => { .theme { color: color-mix(var(--color-red-500) 50%, transparent); } - .single { - color: color-mix(var(--color-red-500)); - } `), ).toMatchInlineSnapshot(` " @@ -6932,10 +6923,6 @@ describe('`color-mix(…)` polyfill', () => { .theme { color: color-mix(var(--color-red-500) 50%, transparent); } - - .single { - color: color-mix(var(--color-red-500)); - } " `) }) From 1129b257d894a56e8deea87a735a01e70fe76f8c Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Wed, 4 Nov 2026 16:28:02 -0500 Subject: [PATCH 4/5] =?UTF-8?q?Ignore=20commas=20in=20comments=20before=20?= =?UTF-8?q?the=20color=20interpolation=20method=20of=20`color-mix(?= =?UTF-8?q?=E2=80=A6)`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous commit dropped comments before it looked for `in`, but the fallback search still ran on the nodes with the comment. The value parser reads a `,` in a comment as a separator, so for e.g.: --tint: color-mix(/* tint, blue */ in oklab, currentcolor 50%, transparent); the fallback was `blue` instead of `currentcolor`. Now we parse the arguments again without comments, so that the check for `in` and the fallback search use the same nodes. The `@supports` rule still uses the original declaration, so it keeps the comment. --- packages/tailwindcss/src/ast.ts | 12 ++++++++---- packages/tailwindcss/src/index.test.ts | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/packages/tailwindcss/src/ast.ts b/packages/tailwindcss/src/ast.ts index 94a4d5d644b5..20ed41a04b20 100644 --- a/packages/tailwindcss/src/ast.ts +++ b/packages/tailwindcss/src/ast.ts @@ -527,11 +527,15 @@ export function optimizeAst( // fallback with inlined colors would be dropped by browsers without `color-mix(…)`. // // Custom properties keep their comments, e.g.: `color-mix(/* … */ in oklab, …)`, and the - // value parser doesn't know about comments, so we drop them before we look for `in`. + // value parser doesn't know about comments, so we drop them before we look for `in`. We + // also parse the arguments again without them, otherwise a `,` in a comment would be seen + // as the start of the first color. let args = ValueParser.toCss(node.nodes) - .replace(/\/\*[\s\S]*?\*\//g, ' ') - .trimStart() - if (!/^in\s/i.test(args)) return + if (args.includes('/*')) { + args = args.replace(/\/\*[\s\S]*?\*\//g, ' ') + node.nodes = ValueParser.parse(args) + } + if (!/^in\s/i.test(args.trimStart())) return let containsUnresolvableVars = false let containsCurrentcolor = false diff --git a/packages/tailwindcss/src/index.test.ts b/packages/tailwindcss/src/index.test.ts index fa4505658072..ff4538d69339 100644 --- a/packages/tailwindcss/src/index.test.ts +++ b/packages/tailwindcss/src/index.test.ts @@ -6889,6 +6889,29 @@ describe('`color-mix(…)` polyfill', () => { `) }) + it('uses the first color value as the fallback when a comment with a comma comes before the color interpolation method', async () => { + expect( + await compileCss(css` + @tailwind utilities; + .foo { + --tint: color-mix(/* tint, blue */ in oklab, currentcolor 50%, transparent); + } + `), + ).toMatchInlineSnapshot(` + " + .foo { + --tint: currentcolor; + } + + @supports (color: color-mix(in lab, red, red)) { + .foo { + --tint: color-mix(/* tint, blue */ in oklab, currentcolor 50%, transparent); + } + } + " + `) + }) + it('does not create a fallback when the `color-mix(…)` function has no color interpolation method', async () => { expect( await compileCss(css` From fc211c2253a938aff4fd7ea8def28a42cffed1f3 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Wed, 4 Nov 2026 17:02:30 -0500 Subject: [PATCH 5/5] =?UTF-8?q?Find=20the=20color=20space=20after=20leadin?= =?UTF-8?q?g=20whitespace=20in=20the=20`color-mix(=E2=80=A6)`=20polyfill?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The color space is the node after `in`, not always `node.nodes[2]`. Leading whitespace (or a newline, or a removed comment) is a separator node, so the color space was missed and the fallback kept `oklab` instead of `srgb`. This also changes the snapshot of the multi-line `stacked` test on purpose: its `in lch,` starts after a newline, so the fallback kept `lch`. It now gets the intended `srgb` conversion. --- packages/tailwindcss/src/ast.ts | 4 ++- packages/tailwindcss/src/index.test.ts | 35 +++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/packages/tailwindcss/src/ast.ts b/packages/tailwindcss/src/ast.ts index 20ed41a04b20..8d208b48f348 100644 --- a/packages/tailwindcss/src/ast.ts +++ b/packages/tailwindcss/src/ast.ts @@ -598,7 +598,9 @@ export function optimizeAst( } else if (requiresPolyfill) { // Change the colorspace to `srgb` since the fallback values should not be represented as // `oklab(…)` functions again as their support in Safari <16 is very limited. - let colorspace = node.nodes[2] + // The color space follows `in`, which is not necessarily the first node when there is + // leading whitespace. + let colorspace = node.nodes[node.nodes.findIndex((n) => n.kind !== 'separator') + 2] if ( colorspace?.kind === 'word' && (colorspace.value === 'oklab' || diff --git a/packages/tailwindcss/src/index.test.ts b/packages/tailwindcss/src/index.test.ts index ff4538d69339..7540907aa8f4 100644 --- a/packages/tailwindcss/src/index.test.ts +++ b/packages/tailwindcss/src/index.test.ts @@ -6668,7 +6668,7 @@ describe('`color-mix(…)` polyfill', () => { } .stacked { - color: lch(55.5764% 89.7903 33.1932 / .25098); + color: #fb2c3640; } @supports (color: color-mix(in lab, red, red)) { @@ -6866,6 +6866,39 @@ describe('`color-mix(…)` polyfill', () => { `) }) + it('converts the color space of the fallback when the color interpolation method has leading whitespace', async () => { + expect( + await compileCss( + // prettier-ignore + css` + @theme { + --color-red-500: oklch(0.637 0.237 25.331); + } + @tailwind utilities; + .foo { + color: color-mix( in oklab, var(--color-red-500), transparent); + } + `, + ), + ).toMatchInlineSnapshot(` + " + :root, :host { + --color-red-500: oklch(63.7% .237 25.331); + } + + .foo { + color: #fb2c3680; + } + + @supports (color: color-mix(in lab, red, red)) { + .foo { + color: color-mix(in oklab, var(--color-red-500), transparent); + } + } + " + `) + }) + it('uses the first color value as the fallback when a comment comes before the color interpolation method', async () => { expect( await compileCss(css`