From 20dac60de308f96b3b6c7c186641f9e26af8ae86 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Tue, 10 Nov 2026 13:58:54 +0100 Subject: [PATCH 01/17] Ignore arbitrary values that start a CSS comment A `/*` outside of a string in an arbitrary value, property, modifier or variant (e.g. `bg-[red/*]`) ends up in the generated CSS as the start of a comment. When it is not closed, it comments out all the CSS that comes after it, so one stray candidate in a scanned file removes other utilities from the output. `isValidArbitrary` now rejects each `/*` outside of a string. A complete comment is valid CSS, but no candidate needs one. A `/*` in a string, like `content-['/*']`, stays valid. --- .changelog/comments-in-arbitrary-values.md | 3 ++ packages/tailwindcss/src/candidate.test.ts | 31 +++++++++++++++++++ .../src/utils/is-valid-arbitrary.ts | 11 ++++++- 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 .changelog/comments-in-arbitrary-values.md diff --git a/.changelog/comments-in-arbitrary-values.md b/.changelog/comments-in-arbitrary-values.md new file mode 100644 index 000000000000..15317324ccc3 --- /dev/null +++ b/.changelog/comments-in-arbitrary-values.md @@ -0,0 +1,3 @@ +### Fixed + +- Ignore arbitrary values, properties, modifiers and variants with a `/*` outside of a string (e.g. `bg-[red/*]`) instead of commenting out the CSS that comes after them diff --git a/packages/tailwindcss/src/candidate.test.ts b/packages/tailwindcss/src/candidate.test.ts index 8c2b5de26b7b..b7b15a19f750 100644 --- a/packages/tailwindcss/src/candidate.test.ts +++ b/packages/tailwindcss/src/candidate.test.ts @@ -2160,6 +2160,14 @@ it.each([ 'data-foo=bar:flex', 'bg-foo=bar', 'bg-red-500/foo=bar', + + // Arbitrary values, properties, modifiers and variants that start a comment + 'bg-[red/*]', + 'bg-[red/*comment*/]', + '[color:red/*]', + 'bg-red-500/[50%/*]', + '[&/*]:flex', + 'data-[a/*]:flex', ])('should not parse invalid values: %s', (rawCandidate) => { let utilities = new Utilities() utilities.static('flex', () => []) @@ -2172,6 +2180,29 @@ it.each([ expect(run(rawCandidate, { utilities, variants })).toEqual([]) }) +it('should parse a `/*` inside of a string in an arbitrary value', () => { + let utilities = new Utilities() + utilities.functional('content', () => []) + + expect(run(`content-['/*']`, { utilities })).toMatchInlineSnapshot(` + [ + { + "important": false, + "kind": "functional", + "modifier": null, + "raw": "content-['/*']", + "root": "content", + "value": { + "dataType": null, + "kind": "arbitrary", + "value": "'/*'", + }, + "variants": [], + }, + ] + `) +}) + const candidates = [ // Arbitrary candidates ['[color:red]', '[color:red]'], diff --git a/packages/tailwindcss/src/utils/is-valid-arbitrary.ts b/packages/tailwindcss/src/utils/is-valid-arbitrary.ts index 6dc885e00e61..a750195b2040 100644 --- a/packages/tailwindcss/src/utils/is-valid-arbitrary.ts +++ b/packages/tailwindcss/src/utils/is-valid-arbitrary.ts @@ -8,6 +8,8 @@ const CLOSE_BRACKET = 0x5d const DOUBLE_QUOTE = 0x22 const SINGLE_QUOTE = 0x27 const SEMICOLON = 0x3b +const SLASH = 0x2f +const ASTERISK = 0x2a // This is a shared buffer that is used to keep track of the current nesting level // of parens, brackets, and braces. It is used to determine if a character is at @@ -19,7 +21,7 @@ const closingBracketStack = new Uint8Array(256) * Determine if a given string might be a valid arbitrary value. * * Unbalanced parens, brackets, and braces are not allowed. Additionally, a - * top-level `;` is not allowed. + * top-level `;` is not allowed, and neither is `/*` outside of a string. * * This function is very similar to `segment` but `segment` cannot be used * because we'd need to split on a bracket stack character. @@ -89,6 +91,13 @@ export function isValidArbitrary(input: string) { case SEMICOLON: if (stackPos === 0) return false break + // A `/*` starts a CSS comment. An unterminated one would comment out the + // CSS that comes after it. We reject every `/*`, not only unterminated + // ones, because no candidate needs a comment. Use a string if you need + // a literal `/*`, e.g. `content-['/*']` or `bg-[url('/*.png')]`. + case SLASH: + if (input.charCodeAt(idx + 1) === ASTERISK) return false + break } } From 0bc10eb611a471b9569f5450b91dbc351d24c0df Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Tue, 10 Nov 2026 15:13:05 +0100 Subject: [PATCH 02/17] Ignore arbitrary properties with a `/*` in the property name The previous commit checked the value of an arbitrary property, but not its name. `[a/*b:red]` still emitted `a/*b: red;`, and the comment removed the CSS after it. The name now goes through `isValidArbitrary` too. The scanner only extracts names with `a-z`, `-` or a `--custom` property, but other paths can reach the parser: the browser build, `@source inline()` and direct calls to `build()`. Reported by CodeRabbit on https://github.com/tailwindlabs/tailwindcss/pull/20690. --- packages/tailwindcss/src/candidate.test.ts | 1 + packages/tailwindcss/src/candidate.ts | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/packages/tailwindcss/src/candidate.test.ts b/packages/tailwindcss/src/candidate.test.ts index b7b15a19f750..9b9d433eddb0 100644 --- a/packages/tailwindcss/src/candidate.test.ts +++ b/packages/tailwindcss/src/candidate.test.ts @@ -2168,6 +2168,7 @@ it.each([ 'bg-red-500/[50%/*]', '[&/*]:flex', 'data-[a/*]:flex', + '[a/*b:red]', ])('should not parse invalid values: %s', (rawCandidate) => { let utilities = new Utilities() utilities.static('flex', () => []) diff --git a/packages/tailwindcss/src/candidate.ts b/packages/tailwindcss/src/candidate.ts index 1a3327464e86..9b883cd71945 100644 --- a/packages/tailwindcss/src/candidate.ts +++ b/packages/tailwindcss/src/candidate.ts @@ -435,6 +435,11 @@ export function* parseCandidate(input: string, designSystem: DesignSystem): Iter if (idx === -1 || idx === 0 || idx === baseWithoutModifier.length - 1) return let property = baseWithoutModifier.slice(0, idx) + + // The property ends up in the CSS as-is, so it has the same restrictions + // as the value. E.g.: `[a/*b:red]` would start a comment. + if (!isValidArbitrary(property)) return + let value = decodeValidArbitraryValue(baseWithoutModifier.slice(idx + 1)) // Values can't contain `;` or `}` characters at the top-level. From 25ffb66301d93f92e47f7768cac2dd4b7d22bcb9 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Tue, 10 Nov 2026 15:13:06 +0100 Subject: [PATCH 03/17] Ignore arbitrary values with an unescaped newline in a string CSS ends a string at an unescaped newline. `isValidArbitrary` treated the rest of the input as part of the string, so `content-['a\n/*']` passed the check, and the `/*` started a real comment in the output. A newline, carriage return or form feed in a string now makes the value invalid. The scanner never extracts a candidate with whitespace, so only direct calls to `build()` can reach this. Reported by CodeRabbit on https://github.com/tailwindlabs/tailwindcss/pull/20690. --- packages/tailwindcss/src/candidate.test.ts | 1 + packages/tailwindcss/src/utils/is-valid-arbitrary.ts | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/packages/tailwindcss/src/candidate.test.ts b/packages/tailwindcss/src/candidate.test.ts index 9b9d433eddb0..86d3fd20a444 100644 --- a/packages/tailwindcss/src/candidate.test.ts +++ b/packages/tailwindcss/src/candidate.test.ts @@ -2169,6 +2169,7 @@ it.each([ '[&/*]:flex', 'data-[a/*]:flex', '[a/*b:red]', + "bg-['a\n/*']", ])('should not parse invalid values: %s', (rawCandidate) => { let utilities = new Utilities() utilities.static('flex', () => []) diff --git a/packages/tailwindcss/src/utils/is-valid-arbitrary.ts b/packages/tailwindcss/src/utils/is-valid-arbitrary.ts index a750195b2040..546db53f9370 100644 --- a/packages/tailwindcss/src/utils/is-valid-arbitrary.ts +++ b/packages/tailwindcss/src/utils/is-valid-arbitrary.ts @@ -10,6 +10,9 @@ const SINGLE_QUOTE = 0x27 const SEMICOLON = 0x3b const SLASH = 0x2f const ASTERISK = 0x2a +const LINE_FEED = 0x0a +const CARRIAGE_RETURN = 0x0d +const FORM_FEED = 0x0c // This is a shared buffer that is used to keep track of the current nesting level // of parens, brackets, and braces. It is used to determine if a character is at @@ -57,6 +60,12 @@ export function isValidArbitrary(input: string) { if (nextChar === char) { break } + + // An unescaped newline ends a string in CSS, so the rest of the + // input is not inside of a string anymore. E.g.: `content-['a\n/*']` + if (nextChar === LINE_FEED || nextChar === CARRIAGE_RETURN || nextChar === FORM_FEED) { + return false + } } break case OPEN_PAREN: From 4e9b01899967685f6d977fedd666b36ca2bcd9d7 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Tue, 10 Nov 2026 15:24:25 +0100 Subject: [PATCH 04/17] Ignore arbitrary values with a string that has no closing quote `isValidArbitrary` accepted a string without a closing quote. In the generated CSS, that quote can pair up with a quote that comes after it. `[a':'/*']` has the property `a'` and the value `'/*'`, and becomes `a': '/*';`. The `/*` is then outside of a string, and starts a comment. A string without a closing quote now makes the input invalid. This applies to property names and values. Reported by CodeRabbit on https://github.com/tailwindlabs/tailwindcss/pull/20690. --- packages/tailwindcss/src/candidate.test.ts | 2 ++ packages/tailwindcss/src/utils/is-valid-arbitrary.ts | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/packages/tailwindcss/src/candidate.test.ts b/packages/tailwindcss/src/candidate.test.ts index 86d3fd20a444..b8d7cb8c562b 100644 --- a/packages/tailwindcss/src/candidate.test.ts +++ b/packages/tailwindcss/src/candidate.test.ts @@ -2170,6 +2170,8 @@ it.each([ 'data-[a/*]:flex', '[a/*b:red]', "bg-['a\n/*']", + "[a':'/*']", + "bg-['/*]", ])('should not parse invalid values: %s', (rawCandidate) => { let utilities = new Utilities() utilities.static('flex', () => []) diff --git a/packages/tailwindcss/src/utils/is-valid-arbitrary.ts b/packages/tailwindcss/src/utils/is-valid-arbitrary.ts index 546db53f9370..bc86523d7922 100644 --- a/packages/tailwindcss/src/utils/is-valid-arbitrary.ts +++ b/packages/tailwindcss/src/utils/is-valid-arbitrary.ts @@ -67,6 +67,11 @@ export function isValidArbitrary(input: string) { return false } } + + // A string without a closing quote is invalid. Otherwise, a quote can + // pair up with a quote in the CSS around it. E.g.: `[a':'/*']` becomes + // `a': '/*';`, and the `/*` is not inside of a string anymore. + if (idx >= len) return false break case OPEN_PAREN: closingBracketStack[stackPos] = CLOSE_PAREN From 1023b3ea1fcf9f0bf4ff83b1c9a99556938f2331 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Tue, 10 Nov 2026 15:26:07 +0100 Subject: [PATCH 05/17] =?UTF-8?q?Ignore=20arbitrary=20values=20with=20a=20?= =?UTF-8?q?quote=20in=20an=20unquoted=20`url(=E2=80=A6)`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CSS reads an unquoted `url(…)` as a single token that ends at the first `)`. A quote in it does not start a string, it makes a bad URL. `isValidArbitrary` treated the quote as the start of a string, so `bg-[url(foo'bar)/*')]` passed the check. In the generated CSS, the `/*` after the first `)` started a comment. In an unquoted `url(…)`, a quote, a `(` or a `/*` now makes the value invalid, and so does a missing `)`. A quoted `url('…')` stays a normal function with a string in it, so `bg-[url('/*.png')]` stays valid. Reported by CodeRabbit on https://github.com/tailwindlabs/tailwindcss/pull/20690. --- packages/tailwindcss/src/candidate.test.ts | 3 + .../src/utils/is-valid-arbitrary.ts | 92 +++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/packages/tailwindcss/src/candidate.test.ts b/packages/tailwindcss/src/candidate.test.ts index b8d7cb8c562b..9a91e8107808 100644 --- a/packages/tailwindcss/src/candidate.test.ts +++ b/packages/tailwindcss/src/candidate.test.ts @@ -2172,6 +2172,9 @@ it.each([ "bg-['a\n/*']", "[a':'/*']", "bg-['/*]", + "bg-[url(foo'bar)/*')]", + '[x:url(foo"bar)/*")]', + "bg-[URL(a(b)/*)]", ])('should not parse invalid values: %s', (rawCandidate) => { let utilities = new Utilities() utilities.static('flex', () => []) diff --git a/packages/tailwindcss/src/utils/is-valid-arbitrary.ts b/packages/tailwindcss/src/utils/is-valid-arbitrary.ts index bc86523d7922..8695af774233 100644 --- a/packages/tailwindcss/src/utils/is-valid-arbitrary.ts +++ b/packages/tailwindcss/src/utils/is-valid-arbitrary.ts @@ -13,6 +13,15 @@ const ASTERISK = 0x2a const LINE_FEED = 0x0a const CARRIAGE_RETURN = 0x0d const FORM_FEED = 0x0c +const LOWER_A = 0x61 +const LOWER_Z = 0x7a +const UPPER_A = 0x41 +const UPPER_Z = 0x5a +const ZERO = 0x30 +const NINE = 0x39 +const DASH = 0x2d +const SPACE = 0x20 +const TAB = 0x09 // This is a shared buffer that is used to keep track of the current nesting level // of parens, brackets, and braces. It is used to determine if a character is at @@ -74,6 +83,37 @@ export function isValidArbitrary(input: string) { if (idx >= len) return false break case OPEN_PAREN: + // CSS reads an unquoted `url(…)` as a single token that ends at the + // first `)`. A quote in it does not start a string, so we reject it, + // and a `(` too. E.g.: `url(foo'bar)/*')` would start a comment after + // the first `)`. We also reject a `/*`, like everywhere else. + if (isUnquotedUrl(input, idx)) { + while (++idx < len) { + let nextChar = input.charCodeAt(idx) + + // The next character is escaped, so we skip it. + if (nextChar === BACKSLASH) { + idx += 1 + continue + } + + if (nextChar === CLOSE_PAREN) break + + if ( + nextChar === SINGLE_QUOTE || + nextChar === DOUBLE_QUOTE || + nextChar === OPEN_PAREN || + (nextChar === SLASH && input.charCodeAt(idx + 1) === ASTERISK) + ) { + return false + } + } + + // The `url(…)` has no closing `)`. + if (idx >= len) return false + break + } + closingBracketStack[stackPos] = CLOSE_PAREN stackPos++ break @@ -117,3 +157,55 @@ export function isValidArbitrary(input: string) { return stackPos === 0 } + +/** + * Determine if the `(` at `idx` starts an unquoted `url(…)`, like + * `url(foo.png)`, but not `url('foo.png')` or `my-url(foo.png)`. + */ +function isUnquotedUrl(input: string, idx: number) { + if (idx < 3) return false + + // Function names are case-insensitive, so `URL(…)` is a `url(…)` too. + if ( + (input.charCodeAt(idx - 3) | 0x20) !== 0x75 /* u */ || + (input.charCodeAt(idx - 2) | 0x20) !== 0x72 /* r */ || + (input.charCodeAt(idx - 1) | 0x20) !== 0x6c /* l */ + ) { + return false + } + + // The `url` must be the whole function name. An `_` becomes a space later, + // so `no-repeat_url(…)` has a `url(…)` too. + if (idx > 3) { + let char = input.charCodeAt(idx - 4) + if ( + (char >= LOWER_A && char <= LOWER_Z) || + (char >= UPPER_A && char <= UPPER_Z) || + (char >= ZERO && char <= NINE) || + char === DASH || + char === BACKSLASH || + char >= 0x80 + ) { + return false + } + } + + // CSS skips the whitespace after the `(`, then a quote makes it a normal + // function with a string in it. + let next = idx + 1 + while (next < input.length) { + let char = input.charCodeAt(next) + if ( + char !== SPACE && + char !== TAB && + char !== LINE_FEED && + char !== CARRIAGE_RETURN && + char !== FORM_FEED + ) + break + next++ + } + + let char = input.charCodeAt(next) + return char !== SINGLE_QUOTE && char !== DOUBLE_QUOTE +} From 6b99599f6346cf3efaee07a7a8afa414d8842555 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Tue, 10 Nov 2026 15:34:30 +0100 Subject: [PATCH 06/17] =?UTF-8?q?Keep=20a=20quoted=20string=20after=20an?= =?UTF-8?q?=20`=5F`=20in=20an=20uppercase=20`URL(=E2=80=A6)`=20valid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `decodeArbitraryValue` keeps the `_` in a lowercase `url(…)` only. In `URL(…)`, the `_` becomes a space. So `bg-[URL(_'image.png')]` becomes the valid `URL( 'image.png')`, but the previous commit read it as an unquoted URL with a quote in it, and rejected it. `isUnquotedUrl` now skips an `_` like whitespace when the function name is not a lowercase `url`, the same as the decoder. Reported by Greptile on https://github.com/tailwindlabs/tailwindcss/pull/20690. --- packages/tailwindcss/src/candidate.test.ts | 12 +++++++++++- .../src/utils/is-valid-arbitrary.ts | 18 ++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/packages/tailwindcss/src/candidate.test.ts b/packages/tailwindcss/src/candidate.test.ts index 9a91e8107808..736ea1faf572 100644 --- a/packages/tailwindcss/src/candidate.test.ts +++ b/packages/tailwindcss/src/candidate.test.ts @@ -2174,7 +2174,7 @@ it.each([ "bg-['/*]", "bg-[url(foo'bar)/*')]", '[x:url(foo"bar)/*")]', - "bg-[URL(a(b)/*)]", + 'bg-[URL(a(b)/*)]', ])('should not parse invalid values: %s', (rawCandidate) => { let utilities = new Utilities() utilities.static('flex', () => []) @@ -2187,6 +2187,16 @@ it.each([ expect(run(rawCandidate, { utilities, variants })).toEqual([]) }) +it('should parse a quoted string after an `_` in an uppercase `URL(…)`', () => { + let utilities = new Utilities() + utilities.functional('bg', () => []) + + // The `_` becomes a space, so this is a quoted `URL( 'image.png')`. + expect(run(`bg-[URL(_'image.png')]`, { utilities })).toMatchObject([ + { value: { kind: 'arbitrary', value: "URL( 'image.png')" } }, + ]) +}) + it('should parse a `/*` inside of a string in an arbitrary value', () => { let utilities = new Utilities() utilities.functional('content', () => []) diff --git a/packages/tailwindcss/src/utils/is-valid-arbitrary.ts b/packages/tailwindcss/src/utils/is-valid-arbitrary.ts index 8695af774233..f0994e079bcd 100644 --- a/packages/tailwindcss/src/utils/is-valid-arbitrary.ts +++ b/packages/tailwindcss/src/utils/is-valid-arbitrary.ts @@ -22,6 +22,7 @@ const NINE = 0x39 const DASH = 0x2d const SPACE = 0x20 const TAB = 0x09 +const UNDERSCORE = 0x5f // This is a shared buffer that is used to keep track of the current nesting level // of parens, brackets, and braces. It is used to determine if a character is at @@ -165,15 +166,19 @@ export function isValidArbitrary(input: string) { function isUnquotedUrl(input: string, idx: number) { if (idx < 3) return false + let u = input.charCodeAt(idx - 3) + let r = input.charCodeAt(idx - 2) + let l = input.charCodeAt(idx - 1) + // Function names are case-insensitive, so `URL(…)` is a `url(…)` too. - if ( - (input.charCodeAt(idx - 3) | 0x20) !== 0x75 /* u */ || - (input.charCodeAt(idx - 2) | 0x20) !== 0x72 /* r */ || - (input.charCodeAt(idx - 1) | 0x20) !== 0x6c /* l */ - ) { + if ((u | 0x20) !== 0x75 /* u */ || (r | 0x20) !== 0x72 /* r */ || (l | 0x20) !== 0x6c /* l */) { return false } + // `decodeArbitraryValue` keeps the `_` in a lowercase `url(…)` only. In + // other cases, like `URL(_'a.png')`, the `_` becomes a space. + let underscoreIsSpace = u !== 0x75 || r !== 0x72 || l !== 0x6c + // The `url` must be the whole function name. An `_` becomes a space later, // so `no-repeat_url(…)` has a `url(…)` too. if (idx > 3) { @@ -200,7 +205,8 @@ function isUnquotedUrl(input: string, idx: number) { char !== TAB && char !== LINE_FEED && char !== CARRIAGE_RETURN && - char !== FORM_FEED + char !== FORM_FEED && + !(char === UNDERSCORE && underscoreIsSpace) ) break next++ From c0667012b7b37dcd050aededb8f135aa7f475432 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Tue, 10 Nov 2026 15:51:48 +0100 Subject: [PATCH 07/17] =?UTF-8?q?Keep=20the=20`=5F`=20of=20a=20nested=20`U?= =?UTF-8?q?RL(=E2=80=A6)`=20in=20a=20lowercase=20`url(=E2=80=A6)`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `decodeArbitraryValue` does not decode the children of a lowercase `url(…)` or `*_url(…)`, so an `_` in a nested `URL(…)` stays an `_`. The previous commit treated that `_` as a space. So `bg-[url('a',URL(_'b)/*'))]` passed as a quoted `URL( 'b)/*')`, but the output kept `URL(_'b)`. CSS reads that as a bad URL that ends at the first `)`, and the `/*` after it started a comment. `isValidArbitrary` now tracks when it is inside a lowercase `url(…)`, and keeps the `_` there, the same as the decoder. --- packages/tailwindcss/src/candidate.test.ts | 1 + .../src/utils/is-valid-arbitrary.ts | 55 +++++++++++++------ 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/packages/tailwindcss/src/candidate.test.ts b/packages/tailwindcss/src/candidate.test.ts index 736ea1faf572..1faa73003a7a 100644 --- a/packages/tailwindcss/src/candidate.test.ts +++ b/packages/tailwindcss/src/candidate.test.ts @@ -2175,6 +2175,7 @@ it.each([ "bg-[url(foo'bar)/*')]", '[x:url(foo"bar)/*")]', 'bg-[URL(a(b)/*)]', + "bg-[url('a',URL(_'b)/*'))]", ])('should not parse invalid values: %s', (rawCandidate) => { let utilities = new Utilities() utilities.static('flex', () => []) diff --git a/packages/tailwindcss/src/utils/is-valid-arbitrary.ts b/packages/tailwindcss/src/utils/is-valid-arbitrary.ts index f0994e079bcd..026209415ad9 100644 --- a/packages/tailwindcss/src/utils/is-valid-arbitrary.ts +++ b/packages/tailwindcss/src/utils/is-valid-arbitrary.ts @@ -45,6 +45,10 @@ export function isValidArbitrary(input: string) { let stackPos = 0 let len = input.length + // The stack position of a lowercase `url(…)` that we are inside of, or -1. + // `decodeArbitraryValue` keeps each `_` in it, also in nested functions. + let keepUnderscoresUntil = -1 + for (let idx = 0; idx < len; idx++) { let char = input.charCodeAt(idx) @@ -83,12 +87,19 @@ export function isValidArbitrary(input: string) { // `a': '/*';`, and the `/*` is not inside of a string anymore. if (idx >= len) return false break - case OPEN_PAREN: + case OPEN_PAREN: { + let url = urlFunctionAt(input, idx) + // CSS reads an unquoted `url(…)` as a single token that ends at the // first `)`. A quote in it does not start a string, so we reject it, // and a `(` too. E.g.: `url(foo'bar)/*')` would start a comment after // the first `)`. We also reject a `/*`, like everywhere else. - if (isUnquotedUrl(input, idx)) { + // + // An `_` becomes a space, except in a lowercase `url(…)`. So + // `URL(_'a.png')` is a quoted `URL( 'a.png')`, but `url(_'a.png')` and + // `url('a',URL(_'b'))` keep the `_`. + let underscoreIsSpace = url === OTHER_URL && keepUnderscoresUntil === -1 + if (url !== NO_URL && !startsWithQuote(input, idx, underscoreIsSpace)) { while (++idx < len) { let nextChar = input.charCodeAt(idx) @@ -115,9 +126,14 @@ export function isValidArbitrary(input: string) { break } + if (url === LOWERCASE_URL && keepUnderscoresUntil === -1) { + keepUnderscoresUntil = stackPos + } + closingBracketStack[stackPos] = CLOSE_PAREN stackPos++ break + } case OPEN_BRACKET: closingBracketStack[stackPos] = CLOSE_BRACKET stackPos++ @@ -138,6 +154,7 @@ export function isValidArbitrary(input: string) { // the buffer can be dirty for the next use and still be correct since // reading/writing always starts at position `0`. stackPos-- + if (stackPos === keepUnderscoresUntil) keepUnderscoresUntil = -1 } else if (char !== CLOSE_CURLY) { return false } @@ -159,26 +176,26 @@ export function isValidArbitrary(input: string) { return stackPos === 0 } +const NO_URL = 0 +const LOWERCASE_URL = 1 +const OTHER_URL = 2 + /** - * Determine if the `(` at `idx` starts an unquoted `url(…)`, like - * `url(foo.png)`, but not `url('foo.png')` or `my-url(foo.png)`. + * Determine if the `(` at `idx` starts a `url(…)`, like `url(…)` or + * `no-repeat_url(…)`, but not `my-url(…)`. Function names are case-insensitive, + * so `URL(…)` is a `url(…)` too. */ -function isUnquotedUrl(input: string, idx: number) { - if (idx < 3) return false +function urlFunctionAt(input: string, idx: number) { + if (idx < 3) return NO_URL let u = input.charCodeAt(idx - 3) let r = input.charCodeAt(idx - 2) let l = input.charCodeAt(idx - 1) - // Function names are case-insensitive, so `URL(…)` is a `url(…)` too. if ((u | 0x20) !== 0x75 /* u */ || (r | 0x20) !== 0x72 /* r */ || (l | 0x20) !== 0x6c /* l */) { - return false + return NO_URL } - // `decodeArbitraryValue` keeps the `_` in a lowercase `url(…)` only. In - // other cases, like `URL(_'a.png')`, the `_` becomes a space. - let underscoreIsSpace = u !== 0x75 || r !== 0x72 || l !== 0x6c - // The `url` must be the whole function name. An `_` becomes a space later, // so `no-repeat_url(…)` has a `url(…)` too. if (idx > 3) { @@ -191,12 +208,18 @@ function isUnquotedUrl(input: string, idx: number) { char === BACKSLASH || char >= 0x80 ) { - return false + return NO_URL } } - // CSS skips the whitespace after the `(`, then a quote makes it a normal - // function with a string in it. + return u === 0x75 && r === 0x72 && l === 0x6c ? LOWERCASE_URL : OTHER_URL +} + +/** + * Determine if the argument of the function at `idx` starts with a quote. + * CSS skips the whitespace after the `(` first. + */ +function startsWithQuote(input: string, idx: number, underscoreIsSpace: boolean) { let next = idx + 1 while (next < input.length) { let char = input.charCodeAt(next) @@ -213,5 +236,5 @@ function isUnquotedUrl(input: string, idx: number) { } let char = input.charCodeAt(next) - return char !== SINGLE_QUOTE && char !== DOUBLE_QUOTE + return char === SINGLE_QUOTE || char === DOUBLE_QUOTE } From 82ef9785cabe99cd58734d3c8bd5219548cf71c9 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Tue, 10 Nov 2026 18:28:11 +0100 Subject: [PATCH 08/17] Ignore arbitrary values with an escape in a function name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CSS decodes escapes in a function name, so `u\72l(…)` is a `url(…)`. The `url(…)` check compares the name as written, so `bg-[u\72l(foo'bar)/*')]` passed as a normal function with a string in it. CSS reads it as an unquoted URL that ends at the first `)`, and the `/*` after it started a comment. `isValidArbitrary` now rejects a `(` after a word with a CSS escape in it. An escaped name like `u\72l('a.png')` is valid CSS, but no candidate needs one. - A hex escape takes up to 6 hex digits and one whitespace after them, and an `_` becomes that whitespace. So `u\72_l(…)` is a `url(…)` too. - `\_` is a literal `_` in a candidate, not a CSS escape, so `w-[--my\_fn(1px)]` stays valid. - An unescaped `_` ends the word, because it becomes a space, so `bg-[\#fff_url(a.png)]` stays valid. Reported by CodeRabbit on https://github.com/tailwindlabs/tailwindcss/pull/20690. --- packages/tailwindcss/src/candidate.test.ts | 12 ++++ .../src/utils/is-valid-arbitrary.ts | 71 ++++++++++++++++--- 2 files changed, 72 insertions(+), 11 deletions(-) diff --git a/packages/tailwindcss/src/candidate.test.ts b/packages/tailwindcss/src/candidate.test.ts index 1faa73003a7a..9fd2259270bb 100644 --- a/packages/tailwindcss/src/candidate.test.ts +++ b/packages/tailwindcss/src/candidate.test.ts @@ -2176,6 +2176,8 @@ it.each([ '[x:url(foo"bar)/*")]', 'bg-[URL(a(b)/*)]', "bg-[url('a',URL(_'b)/*'))]", + "bg-[u\\72l(foo'bar)/*')]", + "bg-[u\\72_l(foo'bar)/*')]", ])('should not parse invalid values: %s', (rawCandidate) => { let utilities = new Utilities() utilities.static('flex', () => []) @@ -2188,6 +2190,16 @@ it.each([ expect(run(rawCandidate, { utilities, variants })).toEqual([]) }) +it('should parse a function name with an escaped `_`', () => { + let utilities = new Utilities() + utilities.functional('w', () => []) + + // `\_` is a literal `_`, so this calls the `--my_fn` function. + expect(run('w-[--my\\_fn(1px)]', { utilities })).toMatchObject([ + { value: { kind: 'arbitrary', value: '--my_fn(1px)' } }, + ]) +}) + it('should parse a quoted string after an `_` in an uppercase `URL(…)`', () => { let utilities = new Utilities() utilities.functional('bg', () => []) diff --git a/packages/tailwindcss/src/utils/is-valid-arbitrary.ts b/packages/tailwindcss/src/utils/is-valid-arbitrary.ts index 026209415ad9..5391ec652769 100644 --- a/packages/tailwindcss/src/utils/is-valid-arbitrary.ts +++ b/packages/tailwindcss/src/utils/is-valid-arbitrary.ts @@ -49,14 +49,46 @@ export function isValidArbitrary(input: string) { // `decodeArbitraryValue` keeps each `_` in it, also in nested functions. let keepUnderscoresUntil = -1 + // If the current word has an escape in it. CSS decodes escapes in function + // names, so `u\72l(…)` is a `url(…)`. No candidate needs that, so we reject + // a function name with an escape in it. + let escapeInWord = false + for (let idx = 0; idx < len; idx++) { let char = input.charCodeAt(idx) switch (char) { - case BACKSLASH: + case BACKSLASH: { + let next = input.charCodeAt(idx + 1) + + // `\_` is a literal `_` in a candidate, not a CSS escape. E.g.: + // `w-[--my\_fn(1px)]` calls the `--my_fn` function. + if (next !== UNDERSCORE) escapeInWord = true + // The next character is escaped, so we skip it. idx += 1 + + // A hex escape has up to 6 hex digits, and one whitespace after them + // is part of it. An `_` becomes that whitespace later. E.g.: `u\72_l` + // is `url`. + if (isHexDigit(next)) { + let end = idx + 6 + while (idx + 1 < end && isHexDigit(input.charCodeAt(idx + 1))) idx++ + + let after = input.charCodeAt(idx + 1) + if ( + after === UNDERSCORE || + after === SPACE || + after === TAB || + after === LINE_FEED || + after === CARRIAGE_RETURN || + after === FORM_FEED + ) { + idx++ + } + } break + } // Strings should be handled as-is until the end of the string. No need to // worry about balancing parens, brackets, or curlies inside a string. case SINGLE_QUOTE: @@ -88,6 +120,8 @@ export function isValidArbitrary(input: string) { if (idx >= len) return false break case OPEN_PAREN: { + if (escapeInWord) return false + let url = urlFunctionAt(input, idx) // CSS reads an unquoted `url(…)` as a single token that ends at the @@ -171,11 +205,35 @@ export function isValidArbitrary(input: string) { if (input.charCodeAt(idx + 1) === ASTERISK) return false break } + + // The word ends at a character that can't be part of a name. An `_` + // becomes a space later, so it ends the word too. + if (escapeInWord && char !== BACKSLASH && !isNameChar(char)) { + escapeInWord = false + } } return stackPos === 0 } +function isHexDigit(char: number) { + return ( + (char >= ZERO && char <= NINE) || + (char >= LOWER_A && char <= 0x66) /* f */ || + (char >= UPPER_A && char <= 0x46) /* F */ + ) +} + +function isNameChar(char: number) { + return ( + (char >= LOWER_A && char <= LOWER_Z) || + (char >= UPPER_A && char <= UPPER_Z) || + (char >= ZERO && char <= NINE) || + char === DASH || + char >= 0x80 + ) +} + const NO_URL = 0 const LOWERCASE_URL = 1 const OTHER_URL = 2 @@ -200,16 +258,7 @@ function urlFunctionAt(input: string, idx: number) { // so `no-repeat_url(…)` has a `url(…)` too. if (idx > 3) { let char = input.charCodeAt(idx - 4) - if ( - (char >= LOWER_A && char <= LOWER_Z) || - (char >= UPPER_A && char <= UPPER_Z) || - (char >= ZERO && char <= NINE) || - char === DASH || - char === BACKSLASH || - char >= 0x80 - ) { - return NO_URL - } + if (isNameChar(char) || char === BACKSLASH) return NO_URL } return u === 0x75 && r === 0x72 && l === 0x6c ? LOWERCASE_URL : OTHER_URL From 80bf6deccf4cb912592f715e2b5907c51ea4c1de Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Tue, 10 Nov 2026 22:39:18 +0100 Subject: [PATCH 09/17] Keep a function name that ends in `\_url` valid MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `\_` is a literal `_` in a candidate, so `--my\_url(var(--size))` calls the `--my_url` function, not `url(…)`. The check for an unquoted `url(…)` took the `_` for a space, and rejected the `(` of `var(…)`. This utility worked before. An `_` after an odd number of backslashes is escaped now, so it is part of the function name. `a\\_url(…)` still has a real `url(…)`, because `\\` is an escaped `\`. --- packages/tailwindcss/src/candidate.test.ts | 7 +++++++ .../tailwindcss/src/utils/is-valid-arbitrary.ts | 14 +++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/tailwindcss/src/candidate.test.ts b/packages/tailwindcss/src/candidate.test.ts index 9fd2259270bb..4e347ecaa573 100644 --- a/packages/tailwindcss/src/candidate.test.ts +++ b/packages/tailwindcss/src/candidate.test.ts @@ -2178,6 +2178,8 @@ it.each([ "bg-[url('a',URL(_'b)/*'))]", "bg-[u\\72l(foo'bar)/*')]", "bg-[u\\72_l(foo'bar)/*')]", + // `\\` is an escaped `\`, so the `_` becomes a space before the `url(…)`. + "bg-[a\\\\_url(foo'bar)/*')]", ])('should not parse invalid values: %s', (rawCandidate) => { let utilities = new Utilities() utilities.static('flex', () => []) @@ -2198,6 +2200,11 @@ it('should parse a function name with an escaped `_`', () => { expect(run('w-[--my\\_fn(1px)]', { utilities })).toMatchObject([ { value: { kind: 'arbitrary', value: '--my_fn(1px)' } }, ]) + + // The function is `--my_url`, not a `url(…)`, so its argument can have a `(`. + expect(run('w-[--my\\_url(var(--size))]', { utilities })).toMatchObject([ + { value: { kind: 'arbitrary', value: '--my_url(var(--size))' } }, + ]) }) it('should parse a quoted string after an `_` in an uppercase `URL(…)`', () => { diff --git a/packages/tailwindcss/src/utils/is-valid-arbitrary.ts b/packages/tailwindcss/src/utils/is-valid-arbitrary.ts index 5391ec652769..80463fd3fd5e 100644 --- a/packages/tailwindcss/src/utils/is-valid-arbitrary.ts +++ b/packages/tailwindcss/src/utils/is-valid-arbitrary.ts @@ -255,15 +255,27 @@ function urlFunctionAt(input: string, idx: number) { } // The `url` must be the whole function name. An `_` becomes a space later, - // so `no-repeat_url(…)` has a `url(…)` too. + // so `no-repeat_url(…)` has a `url(…)` too. But `\_` stays an `_`, so + // `--my\_url(…)` calls the `--my_url` function. if (idx > 3) { let char = input.charCodeAt(idx - 4) if (isNameChar(char) || char === BACKSLASH) return NO_URL + if (char === UNDERSCORE && isEscaped(input, idx - 4)) return NO_URL } return u === 0x75 && r === 0x72 && l === 0x6c ? LOWERCASE_URL : OTHER_URL } +/** + * Determine if the character at `idx` is escaped. It is, if an odd number of + * backslashes comes before it. E.g.: `\_` is, but `\\_` is not. + */ +function isEscaped(input: string, idx: number) { + let count = 0 + while (idx - count - 1 >= 0 && input.charCodeAt(idx - count - 1) === BACKSLASH) count++ + return count % 2 === 1 +} + /** * Determine if the argument of the function at `idx` starts with a quote. * CSS skips the whitespace after the `(` first. From cac9089baaa7d9903284a65d337827f67132b268 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Tue, 10 Nov 2026 22:50:36 +0100 Subject: [PATCH 10/17] =?UTF-8?q?Keep=20each=20`=5F`=20in=20a=20nested=20`?= =?UTF-8?q?URL(=E2=80=A6)`=20of=20a=20function=20that=20ends=20in=20`\=5Fu?= =?UTF-8?q?rl`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `decodeArbitraryValue` keeps each `_` in a function with a name that ends in `_url`, also in `--my\_url(…)`. Since the previous commit, that function is not a `url(…)` for the check anymore, so the check also stopped to track that it keeps each `_`. Then `--my\_url(URL(_'a)/*'))` was valid: the check read `URL( 'a)/*')` as a quoted string, but CSS gets `URL(_'a)/*')`, an unquoted `url(…)` that ends at the first `)`, and the `/*` starts a comment. The check now has two parts. `isUrlFunctionAt` tells if CSS reads the function as a `url(…)`. `keepsUnderscoresAt` tells if `decodeArbitraryValue` keeps each `_` in it. --- packages/tailwindcss/src/candidate.test.ts | 3 + .../src/utils/is-valid-arbitrary.ts | 60 ++++++++++++------- 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/packages/tailwindcss/src/candidate.test.ts b/packages/tailwindcss/src/candidate.test.ts index 4e347ecaa573..14f973530a92 100644 --- a/packages/tailwindcss/src/candidate.test.ts +++ b/packages/tailwindcss/src/candidate.test.ts @@ -2180,6 +2180,9 @@ it.each([ "bg-[u\\72_l(foo'bar)/*')]", // `\\` is an escaped `\`, so the `_` becomes a space before the `url(…)`. "bg-[a\\\\_url(foo'bar)/*')]", + // `--my\_url(…)` is not a `url(…)`, but it keeps the `_`, so the inner + // `URL(…)` has no quoted string. + "[x:--my\\_url(URL(_'a)/*'))]", ])('should not parse invalid values: %s', (rawCandidate) => { let utilities = new Utilities() utilities.static('flex', () => []) diff --git a/packages/tailwindcss/src/utils/is-valid-arbitrary.ts b/packages/tailwindcss/src/utils/is-valid-arbitrary.ts index 80463fd3fd5e..83663756b341 100644 --- a/packages/tailwindcss/src/utils/is-valid-arbitrary.ts +++ b/packages/tailwindcss/src/utils/is-valid-arbitrary.ts @@ -45,8 +45,8 @@ export function isValidArbitrary(input: string) { let stackPos = 0 let len = input.length - // The stack position of a lowercase `url(…)` that we are inside of, or -1. - // `decodeArbitraryValue` keeps each `_` in it, also in nested functions. + // The stack position of a function that `decodeArbitraryValue` keeps each + // `_` in, also in nested functions, or -1. E.g.: a lowercase `url(…)`. let keepUnderscoresUntil = -1 // If the current word has an escape in it. CSS decodes escapes in function @@ -122,18 +122,19 @@ export function isValidArbitrary(input: string) { case OPEN_PAREN: { if (escapeInWord) return false - let url = urlFunctionAt(input, idx) + let keepsUnderscores = keepsUnderscoresAt(input, idx) // CSS reads an unquoted `url(…)` as a single token that ends at the // first `)`. A quote in it does not start a string, so we reject it, // and a `(` too. E.g.: `url(foo'bar)/*')` would start a comment after // the first `)`. We also reject a `/*`, like everywhere else. // - // An `_` becomes a space, except in a lowercase `url(…)`. So - // `URL(_'a.png')` is a quoted `URL( 'a.png')`, but `url(_'a.png')` and - // `url('a',URL(_'b'))` keep the `_`. - let underscoreIsSpace = url === OTHER_URL && keepUnderscoresUntil === -1 - if (url !== NO_URL && !startsWithQuote(input, idx, underscoreIsSpace)) { + // An `_` becomes a space, except in a lowercase `url(…)` or a function + // name that ends in `_url`. So `URL(_'a.png')` is a quoted + // `URL( 'a.png')`, but `url(_'a.png')`, `url('a',URL(_'b'))` and + // `--my\_url(URL(_'b'))` keep the `_`. + let underscoreIsSpace = !keepsUnderscores && keepUnderscoresUntil === -1 + if (isUrlFunctionAt(input, idx) && !startsWithQuote(input, idx, underscoreIsSpace)) { while (++idx < len) { let nextChar = input.charCodeAt(idx) @@ -160,7 +161,7 @@ export function isValidArbitrary(input: string) { break } - if (url === LOWERCASE_URL && keepUnderscoresUntil === -1) { + if (keepsUnderscores && keepUnderscoresUntil === -1) { keepUnderscoresUntil = stackPos } @@ -234,24 +235,20 @@ function isNameChar(char: number) { ) } -const NO_URL = 0 -const LOWERCASE_URL = 1 -const OTHER_URL = 2 - /** - * Determine if the `(` at `idx` starts a `url(…)`, like `url(…)` or - * `no-repeat_url(…)`, but not `my-url(…)`. Function names are case-insensitive, - * so `URL(…)` is a `url(…)` too. + * Determine if CSS reads the `(` at `idx` as the start of a `url(…)`, like in + * `url(…)` or `no-repeat_url(…)`, but not in `my-url(…)`. Function names are + * case-insensitive, so `URL(…)` is a `url(…)` too. */ -function urlFunctionAt(input: string, idx: number) { - if (idx < 3) return NO_URL +function isUrlFunctionAt(input: string, idx: number) { + if (idx < 3) return false let u = input.charCodeAt(idx - 3) let r = input.charCodeAt(idx - 2) let l = input.charCodeAt(idx - 1) if ((u | 0x20) !== 0x75 /* u */ || (r | 0x20) !== 0x72 /* r */ || (l | 0x20) !== 0x6c /* l */) { - return NO_URL + return false } // The `url` must be the whole function name. An `_` becomes a space later, @@ -259,11 +256,30 @@ function urlFunctionAt(input: string, idx: number) { // `--my\_url(…)` calls the `--my_url` function. if (idx > 3) { let char = input.charCodeAt(idx - 4) - if (isNameChar(char) || char === BACKSLASH) return NO_URL - if (char === UNDERSCORE && isEscaped(input, idx - 4)) return NO_URL + if (isNameChar(char) || char === BACKSLASH) return false + if (char === UNDERSCORE && isEscaped(input, idx - 4)) return false + } + + return true +} + +/** + * Determine if `decodeArbitraryValue` keeps each `_` in the function at `idx`. + * It does for a function name that is `url` or that ends in `_url`, also in + * `\_url`. So `--my\_url(…)` keeps them, but it is not a `url(…)` in CSS. + */ +function keepsUnderscoresAt(input: string, idx: number) { + if (idx < 3) return false + + if ( + input.charCodeAt(idx - 3) !== 0x75 /* u */ || + input.charCodeAt(idx - 2) !== 0x72 /* r */ || + input.charCodeAt(idx - 1) !== 0x6c /* l */ + ) { + return false } - return u === 0x75 && r === 0x72 && l === 0x6c ? LOWERCASE_URL : OTHER_URL + return idx === 3 || !isNameChar(input.charCodeAt(idx - 4)) } /** From 42bb537dd61fdebf2b90c5ad27dd58c452c19bc2 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Tue, 10 Nov 2026 23:12:04 +0100 Subject: [PATCH 11/17] =?UTF-8?q?Allow=20a=20`/*`=20in=20an=20unquoted=20`?= =?UTF-8?q?url(=E2=80=A6)`=20in=20a=20declaration=20value?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A `/*` in an unquoted `url(…)` is not a comment in CSS, so `bg-[url(/a/*.png)]` is a valid class. The check rejected it. PostCSS reads fewer forms as a `url(…)` than CSS does, and Vite and webpack run PostCSS on the generated CSS. In the other forms, the `/*` starts a comment for PostCSS. So we only allow the forms that both read as a `url(…)`: a lowercase `url` at the start of the value, after whitespace (also an `_`) or after a `(`, without whitespace after the `(`. E.g.: `url(/a/*.png)`, `url(a.png),_url(/b/*.png)` and `image-set(url(/a/*.png)_1x)`, but not `URL(/a/*.png)` or `url(a.png),url(/b/*.png)`. The start of the input only starts a token in a declaration value. A `data-[…]` value becomes `[data-…]`, so `data-url(` is a normal function there. So only the value of a utility and of an arbitrary property can have a `/*` in a `url(…)`. CSS reads `#url(` and `@url(` as a hash and an at-keyword, followed by a normal `(`. So they are not a `url(…)` anymore for the check. --- .changelog/comments-in-arbitrary-values.md | 2 +- packages/tailwindcss/src/candidate.test.ts | 37 +++++++++- packages/tailwindcss/src/candidate.ts | 9 +-- .../src/utils/is-valid-arbitrary.ts | 72 ++++++++++++++++--- 4 files changed, 103 insertions(+), 17 deletions(-) diff --git a/.changelog/comments-in-arbitrary-values.md b/.changelog/comments-in-arbitrary-values.md index 15317324ccc3..dd2c0c39d5c2 100644 --- a/.changelog/comments-in-arbitrary-values.md +++ b/.changelog/comments-in-arbitrary-values.md @@ -1,3 +1,3 @@ ### Fixed -- Ignore arbitrary values, properties, modifiers and variants with a `/*` outside of a string (e.g. `bg-[red/*]`) instead of commenting out the CSS that comes after them +- Ignore arbitrary values, properties, modifiers and variants with a `/*` outside of a string or an unquoted `url(…)` (e.g. `bg-[red/*]`) instead of commenting out the CSS that comes after them diff --git a/packages/tailwindcss/src/candidate.test.ts b/packages/tailwindcss/src/candidate.test.ts index 14f973530a92..b46c672666bc 100644 --- a/packages/tailwindcss/src/candidate.test.ts +++ b/packages/tailwindcss/src/candidate.test.ts @@ -2178,11 +2178,28 @@ it.each([ "bg-[url('a',URL(_'b)/*'))]", "bg-[u\\72l(foo'bar)/*')]", "bg-[u\\72_l(foo'bar)/*')]", - // `\\` is an escaped `\`, so the `_` becomes a space before the `url(…)`. + // `\\` is an escaped `\`, so the `_` before the `url(…)` is not escaped. "bg-[a\\\\_url(foo'bar)/*')]", // `--my\_url(…)` is not a `url(…)`, but it keeps the `_`, so the inner // `URL(…)` has no quoted string. "[x:--my\\_url(URL(_'a)/*'))]", + + // A `/*` inside of an unquoted `url(…)` that PostCSS does not read as a + // `url(…)`, or that CSS does not read as a `url(…)` at all + 'bg-[URL(/a/*.png)]', + 'bg-[Url(/a/*.png)]', + 'bg-[url(a.png),url(/b/*.png)]', + 'bg-[#url(/a/*.png)]', + 'bg-[@url(/a/*.png)]', + 'bg-[1url(/a/*.png)]', + // `decodeArbitraryValue` turns this into `a\_url(…)` + 'bg-[a\\\\_url(/a/*.png)]', + // The outer `url(…)` keeps the `_`, so this calls the `b_url` function + "bg-[url('a',b_url(/c/*.png))]", + // Only a value of a declaration can have one, e.g. not `[data-url(…)]` + 'data-[url(/a/*.png)]:flex', + 'bg-red-500/[url(/a/*.png)]', + '[&_url(/a/*.png)]:flex', ])('should not parse invalid values: %s', (rawCandidate) => { let utilities = new Utilities() utilities.static('flex', () => []) @@ -2243,6 +2260,24 @@ it('should parse a `/*` inside of a string in an arbitrary value', () => { `) }) +it.each([ + ['bg-[url(/a/*.png)]', 'url(/a/*.png)'], + ['bg-[url(a.png),_url(/b/*.png)]', 'url(a.png), url(/b/*.png)'], + ['bg-[image-set(url(/a/*.png)_1x)]', 'image-set(url(/a/*.png) 1x)'], + ['bg-[no-repeat_url(/a/*.png)]', 'no-repeat url(/a/*.png)'], +])('should parse a `/*` inside of an unquoted `url(…)`: %s', (rawCandidate, value) => { + let utilities = new Utilities() + utilities.functional('bg', () => []) + + expect(run(rawCandidate, { utilities })).toMatchObject([{ value: { kind: 'arbitrary', value } }]) +}) + +it('should parse a `/*` inside of an unquoted `url(…)` in an arbitrary property', () => { + expect(run('[background-image:url(/a/*.png)]')).toMatchObject([ + { kind: 'arbitrary', property: 'background-image', value: 'url(/a/*.png)' }, + ]) +}) + const candidates = [ // Arbitrary candidates ['[color:red]', '[color:red]'], diff --git a/packages/tailwindcss/src/candidate.ts b/packages/tailwindcss/src/candidate.ts index 9b883cd71945..6e32f0dc1cab 100644 --- a/packages/tailwindcss/src/candidate.ts +++ b/packages/tailwindcss/src/candidate.ts @@ -440,7 +440,7 @@ export function* parseCandidate(input: string, designSystem: DesignSystem): Iter // as the value. E.g.: `[a/*b:red]` would start a comment. if (!isValidArbitrary(property)) return - let value = decodeValidArbitraryValue(baseWithoutModifier.slice(idx + 1)) + let value = decodeValidArbitraryValue(baseWithoutModifier.slice(idx + 1), true) // Values can't contain `;` or `}` characters at the top-level. if (value === null) return @@ -568,7 +568,8 @@ export function* parseCandidate(input: string, designSystem: DesignSystem): Iter // Arbitrary values must end with a `]`. if (value[value.length - 1] !== ']') return - let arbitraryValue = decodeValidArbitraryValue(value.slice(startArbitraryIdx + 1, -1)) + // The value becomes the value of a declaration, e.g. `bg-[url(/a/*.png)]`. + let arbitraryValue = decodeValidArbitraryValue(value.slice(startArbitraryIdx + 1, -1), true) // Values can't contain `;` or `}` characters at the top-level. if (arbitraryValue === null) continue @@ -1303,6 +1304,6 @@ function escapeUnderscore(value: string): string { // validates the raw value first and only then decodes it // the order matters: decoding breaks unbalanced parens (`calc(1px` becomes // `calc()1px`) so validating the decoded value would hide them -function decodeValidArbitraryValue(raw: string): string | null { - return isValidArbitrary(raw) ? decodeArbitraryValue(raw) : null +function decodeValidArbitraryValue(raw: string, isDeclarationValue = false): string | null { + return isValidArbitrary(raw, isDeclarationValue) ? decodeArbitraryValue(raw) : null } diff --git a/packages/tailwindcss/src/utils/is-valid-arbitrary.ts b/packages/tailwindcss/src/utils/is-valid-arbitrary.ts index 83663756b341..64409be659d0 100644 --- a/packages/tailwindcss/src/utils/is-valid-arbitrary.ts +++ b/packages/tailwindcss/src/utils/is-valid-arbitrary.ts @@ -23,6 +23,8 @@ const DASH = 0x2d const SPACE = 0x20 const TAB = 0x09 const UNDERSCORE = 0x5f +const HASH = 0x23 +const AT_SIGN = 0x40 // This is a shared buffer that is used to keep track of the current nesting level // of parens, brackets, and braces. It is used to determine if a character is at @@ -36,10 +38,14 @@ const closingBracketStack = new Uint8Array(256) * Unbalanced parens, brackets, and braces are not allowed. Additionally, a * top-level `;` is not allowed, and neither is `/*` outside of a string. * + * Set `isDeclarationValue` when the input becomes the value of a declaration, + * like `bg-[…]` or `[color:…]`. Then a `/*` is allowed in an unquoted `url(…)` + * too, e.g. `bg-[url(/a/*.png)]`. + * * This function is very similar to `segment` but `segment` cannot be used * because we'd need to split on a bracket stack character. */ -export function isValidArbitrary(input: string) { +export function isValidArbitrary(input: string, isDeclarationValue = false) { // SAFETY: We can use an index into a shared buffer because this function is // synchronous, non-recursive, and runs in a single-threaded environment. let stackPos = 0 @@ -127,7 +133,7 @@ export function isValidArbitrary(input: string) { // CSS reads an unquoted `url(…)` as a single token that ends at the // first `)`. A quote in it does not start a string, so we reject it, // and a `(` too. E.g.: `url(foo'bar)/*')` would start a comment after - // the first `)`. We also reject a `/*`, like everywhere else. + // the first `)`. // // An `_` becomes a space, except in a lowercase `url(…)` or a function // name that ends in `_url`. So `URL(_'a.png')` is a quoted @@ -135,6 +141,12 @@ export function isValidArbitrary(input: string) { // `--my\_url(URL(_'b'))` keep the `_`. let underscoreIsSpace = !keepsUnderscores && keepUnderscoresUntil === -1 if (isUrlFunctionAt(input, idx) && !startsWithQuote(input, idx, underscoreIsSpace)) { + // A `/*` in the `url(…)` is not a comment in CSS. But PostCSS reads + // fewer forms as a `url(…)`, and a `/*` in the other forms starts a + // comment there. E.g.: `URL(/a/*.png)` or `a,url(/a/*.png)`. + let allowComment = + isDeclarationValue && isPostCssUrlAt(input, idx, keepUnderscoresUntil === -1) + while (++idx < len) { let nextChar = input.charCodeAt(idx) @@ -146,12 +158,11 @@ export function isValidArbitrary(input: string) { if (nextChar === CLOSE_PAREN) break - if ( - nextChar === SINGLE_QUOTE || - nextChar === DOUBLE_QUOTE || - nextChar === OPEN_PAREN || - (nextChar === SLASH && input.charCodeAt(idx + 1) === ASTERISK) - ) { + if (nextChar === SINGLE_QUOTE || nextChar === DOUBLE_QUOTE || nextChar === OPEN_PAREN) { + return false + } + + if (nextChar === SLASH && input.charCodeAt(idx + 1) === ASTERISK && !allowComment) { return false } } @@ -201,7 +212,8 @@ export function isValidArbitrary(input: string) { // A `/*` starts a CSS comment. An unterminated one would comment out the // CSS that comes after it. We reject every `/*`, not only unterminated // ones, because no candidate needs a comment. Use a string if you need - // a literal `/*`, e.g. `content-['/*']` or `bg-[url('/*.png')]`. + // a literal `/*`, e.g. `content-['/*']` or `bg-[url('/*.png')]`. An + // unquoted `url(…)` can have one too, e.g. `bg-[url(/*.png)]` (see above). case SLASH: if (input.charCodeAt(idx + 1) === ASTERISK) return false break @@ -253,16 +265,54 @@ function isUrlFunctionAt(input: string, idx: number) { // The `url` must be the whole function name. An `_` becomes a space later, // so `no-repeat_url(…)` has a `url(…)` too. But `\_` stays an `_`, so - // `--my\_url(…)` calls the `--my_url` function. + // `--my\_url(…)` calls the `--my_url` function. CSS reads `#url` and `@url` + // as a hash and an at-keyword, so a normal `(` comes after them. if (idx > 3) { let char = input.charCodeAt(idx - 4) - if (isNameChar(char) || char === BACKSLASH) return false + if (isNameChar(char) || char === BACKSLASH || char === HASH || char === AT_SIGN) return false if (char === UNDERSCORE && isEscaped(input, idx - 4)) return false } return true } +/** + * Determine if PostCSS reads the `(` at `idx` as the start of a `url(…)` too. + * It does for a lowercase `url` at the start of the value, after whitespace or + * after a `(`, if no whitespace comes after the `(`. E.g.: `url(…)`, + * `no-repeat_url(…)` and `image-set(url(…))`, but not `URL(…)` or `a,url(…)`. + */ +function isPostCssUrlAt(input: string, idx: number, underscoreIsSpace: boolean) { + if ( + input.charCodeAt(idx - 3) !== 0x75 /* u */ || + input.charCodeAt(idx - 2) !== 0x72 /* r */ || + input.charCodeAt(idx - 1) !== 0x6c /* l */ + ) { + return false + } + + if (isWhitespace(input.charCodeAt(idx + 1))) return false + + if (idx === 3) return true + + let char = input.charCodeAt(idx - 4) + if (char === OPEN_PAREN || isWhitespace(char)) return true + + // `decodeArbitraryValue` turns `\\_` into `\_`, not into `\ `. So we only + // count an `_` without a `\` in front of it. + return char === UNDERSCORE && underscoreIsSpace && input.charCodeAt(idx - 5) !== BACKSLASH +} + +function isWhitespace(char: number) { + return ( + char === SPACE || + char === TAB || + char === LINE_FEED || + char === CARRIAGE_RETURN || + char === FORM_FEED + ) +} + /** * Determine if `decodeArbitraryValue` keeps each `_` in the function at `idx`. * It does for a function name that is `url` or that ends in `_url`, also in From a01831cc71a0b03f48e909b811c7827e567536c9 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Tue, 10 Nov 2026 23:12:12 +0100 Subject: [PATCH 12/17] Ignore arbitrary values with a `\/*` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CSS reads `\/` as an escaped `/`, but PostCSS does not. It reads `\/*` as a `\` and the start of a comment. Tailwind CSS prints the `\/*` as written, so PostCSS throws "Unclosed comment" for `bg-[\/*]`, and for `bg-[URL(a\/*b)]` too, because PostCSS does not read an uppercase `URL(…)` as a `url(…)`. A `url(…)` that PostCSS reads as a `url(…)` can still have one, because PostCSS reads it up to the first `)` there. --- packages/tailwindcss/src/candidate.test.ts | 4 ++++ .../tailwindcss/src/utils/is-valid-arbitrary.ts | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/tailwindcss/src/candidate.test.ts b/packages/tailwindcss/src/candidate.test.ts index b46c672666bc..1b7bd2ead0d2 100644 --- a/packages/tailwindcss/src/candidate.test.ts +++ b/packages/tailwindcss/src/candidate.test.ts @@ -2200,6 +2200,10 @@ it.each([ 'data-[url(/a/*.png)]:flex', 'bg-red-500/[url(/a/*.png)]', '[&_url(/a/*.png)]:flex', + + // PostCSS reads `\/*` as a `\` and the start of a comment + 'bg-[\\/*]', + 'bg-[URL(a\\/*b)]', ])('should not parse invalid values: %s', (rawCandidate) => { let utilities = new Utilities() utilities.static('flex', () => []) diff --git a/packages/tailwindcss/src/utils/is-valid-arbitrary.ts b/packages/tailwindcss/src/utils/is-valid-arbitrary.ts index 64409be659d0..ff3a5ca2fd15 100644 --- a/packages/tailwindcss/src/utils/is-valid-arbitrary.ts +++ b/packages/tailwindcss/src/utils/is-valid-arbitrary.ts @@ -67,6 +67,10 @@ export function isValidArbitrary(input: string, isDeclarationValue = false) { case BACKSLASH: { let next = input.charCodeAt(idx + 1) + // CSS reads `\/` as an escaped `/`, but PostCSS does not. So `\/*` + // starts a comment there. + if (next === SLASH && input.charCodeAt(idx + 2) === ASTERISK) return false + // `\_` is a literal `_` in a candidate, not a CSS escape. E.g.: // `w-[--my\_fn(1px)]` calls the `--my_fn` function. if (next !== UNDERSCORE) escapeInWord = true @@ -150,8 +154,17 @@ export function isValidArbitrary(input: string, isDeclarationValue = false) { while (++idx < len) { let nextChar = input.charCodeAt(idx) - // The next character is escaped, so we skip it. + // The next character is escaped, so we skip it. But PostCSS reads + // `\/*` as a comment, if it does not read this as a `url(…)`. if (nextChar === BACKSLASH) { + if ( + !allowComment && + input.charCodeAt(idx + 1) === SLASH && + input.charCodeAt(idx + 2) === ASTERISK + ) { + return false + } + idx += 1 continue } From f95b4b92a402923d45bbde664ba473b77e2b56a0 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Tue, 10 Nov 2026 23:12:19 +0100 Subject: [PATCH 13/17] Test random arbitrary values with Lightning CSS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Each fix in the check for comments found one more edge case. So we now also test 5000 random values, made from the parts that matter (`url(`, quotes, `/*`, `\`, `_`, …). For each value that a utility or an arbitrary property accepts, Lightning CSS must keep the rule after it. The random numbers have a fixed seed, so a failure can be reproduced. The test fails on `main`, e.g. with `[--x:,/*]`. Also add `[--x:a() { + let designSystem = await __unstable__loadDesignSystem('@tailwind utilities', { base: __dirname }) + + // A seeded random number generator (mulberry32), so a failure can be reproduced. + let seed = 20690 + function random() { + seed = (seed + 0x6d2b79f5) | 0 + let t = Math.imul(seed ^ (seed >>> 15), 1 | seed) + t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t + return ((t ^ (t >>> 14)) >>> 0) / 4294967296 + } + + let parts = ['url(', 'URL(', 'a(', '(', ')', "'", '"', '/*', '*/', '/', '*', '_', '\\'] + parts.push('\\_', '\\72', '#', '@', ',', ':', 'a', '1', '-', ' ') + + for (let i = 0; i < 5000; i++) { + let value = '' + let count = 1 + Math.floor(random() * 10) + for (let j = 0; j < count; j++) value += parts[Math.floor(random() * parts.length)] + + for (let candidate of [`[--x:${value}]`, `bg-[${value}]`]) { + let [css] = designSystem.candidatesToCss([candidate]) + if (!css) continue + + let { code } = transform({ + filename: 'input.css', + code: Buffer.from(`${css}.b{color:red}`), + minify: true, + errorRecovery: true, + }) + + expect(code.toString(), candidate).toContain('.b{color:red}') + } + } +}) + it('should parse a function name with an escaped `_`', () => { let utilities = new Utilities() utilities.functional('w', () => []) From d9f7240bf48bfb792314838c95eb3e023a3eca2d Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Tue, 10 Nov 2026 23:17:48 +0100 Subject: [PATCH 14/17] =?UTF-8?q?Allow=20a=20`/*`=20in=20a=20`url(?= =?UTF-8?q?=E2=80=A6)`=20after=20a=20type=20hint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A type hint like `image:` is not in the CSS, so `bg-[image:url(/a/*.png)]` gives `background-image: url(/a/*.png)`. PostCSS ends the word before the `url` at the `:`, so it reads the `url(…)` as a url too. An at-word is the exception: PostCSS reads `@image:url` as one at-word, so it reads the `/*` as a comment. These values stay invalid. --- packages/tailwindcss/src/candidate.test.ts | 4 ++ .../src/utils/is-valid-arbitrary.ts | 43 +++++++++++++++++-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/packages/tailwindcss/src/candidate.test.ts b/packages/tailwindcss/src/candidate.test.ts index d05c80363aa9..929bceb9789c 100644 --- a/packages/tailwindcss/src/candidate.test.ts +++ b/packages/tailwindcss/src/candidate.test.ts @@ -2194,6 +2194,9 @@ it.each([ 'bg-[#url(/a/*.png)]', 'bg-[@url(/a/*.png)]', 'bg-[1url(/a/*.png)]', + // PostCSS reads `@image:url` as one at-word + 'bg-[@image:url(/a/*.png)]', + 'bg-[a@b:url(/a/*.png)]', // `decodeArbitraryValue` turns this into `a\_url(…)` 'bg-[a\\\\_url(/a/*.png)]', // The outer `url(…)` keeps the `_`, so this calls the `b_url` function @@ -2307,6 +2310,7 @@ it.each([ ['bg-[url(a.png),_url(/b/*.png)]', 'url(a.png), url(/b/*.png)'], ['bg-[image-set(url(/a/*.png)_1x)]', 'image-set(url(/a/*.png) 1x)'], ['bg-[no-repeat_url(/a/*.png)]', 'no-repeat url(/a/*.png)'], + ['bg-[image:url(/a/*.png)]', 'url(/a/*.png)'], ])('should parse a `/*` inside of an unquoted `url(…)`: %s', (rawCandidate, value) => { let utilities = new Utilities() utilities.functional('bg', () => []) diff --git a/packages/tailwindcss/src/utils/is-valid-arbitrary.ts b/packages/tailwindcss/src/utils/is-valid-arbitrary.ts index ff3a5ca2fd15..a47caabdc0f6 100644 --- a/packages/tailwindcss/src/utils/is-valid-arbitrary.ts +++ b/packages/tailwindcss/src/utils/is-valid-arbitrary.ts @@ -25,6 +25,7 @@ const TAB = 0x09 const UNDERSCORE = 0x5f const HASH = 0x23 const AT_SIGN = 0x40 +const COLON = 0x3a // This is a shared buffer that is used to keep track of the current nesting level // of parens, brackets, and braces. It is used to determine if a character is at @@ -291,9 +292,10 @@ function isUrlFunctionAt(input: string, idx: number) { /** * Determine if PostCSS reads the `(` at `idx` as the start of a `url(…)` too. - * It does for a lowercase `url` at the start of the value, after whitespace or - * after a `(`, if no whitespace comes after the `(`. E.g.: `url(…)`, - * `no-repeat_url(…)` and `image-set(url(…))`, but not `URL(…)` or `a,url(…)`. + * It does for a lowercase `url` at the start of the value, after whitespace, + * after a `(` or after a `:`, if no whitespace comes after the `(`. E.g.: + * `url(…)`, `no-repeat_url(…)`, `image-set(url(…))` and `image:url(…)`, but + * not `URL(…)` or `a,url(…)`. */ function isPostCssUrlAt(input: string, idx: number, underscoreIsSpace: boolean) { if ( @@ -311,11 +313,46 @@ function isPostCssUrlAt(input: string, idx: number, underscoreIsSpace: boolean) let char = input.charCodeAt(idx - 4) if (char === OPEN_PAREN || isWhitespace(char)) return true + // A type hint like `image:` is not in the CSS. Else CSS reads the `:` as a + // colon, and PostCSS ends the word before the `url` at it too. But not an + // at-word, so in `@a:url(…)` the `url` is part of the `@a:url` at-word. + if (char === COLON) return !isInAtWord(input, idx - 4) + // `decodeArbitraryValue` turns `\\_` into `\_`, not into `\ `. So we only // count an `_` without a `\` in front of it. return char === UNDERSCORE && underscoreIsSpace && input.charCodeAt(idx - 5) !== BACKSLASH } +/** + * Determine if PostCSS reads the character at `idx` as part of an at-word. An + * at-word starts at an `@`, and ends at whitespace or one of `"#'()/;[\]{}`. + */ +function isInAtWord(input: string, idx: number) { + for (let i = idx - 1; i >= 0; i--) { + let char = input.charCodeAt(i) + if (char === AT_SIGN) return true + if ( + isWhitespace(char) || + char === DOUBLE_QUOTE || + char === HASH || + char === SINGLE_QUOTE || + char === OPEN_PAREN || + char === CLOSE_PAREN || + char === SLASH || + char === SEMICOLON || + char === OPEN_BRACKET || + char === BACKSLASH || + char === CLOSE_BRACKET || + char === OPEN_CURLY || + char === CLOSE_CURLY + ) { + return false + } + } + + return false +} + function isWhitespace(char: number) { return ( char === SPACE || From 49f99ec128596744e0d383b5efabe0295cfec5d5 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Tue, 10 Nov 2026 23:21:37 +0100 Subject: [PATCH 15/17] Print a space before the arbitrary value of a gradient MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `bg-linear-[url(/a/*.png)]` printed `var(--tw-gradient-stops,url(/a/*.png))`. PostCSS reads `,url` as one word, so it does not read the `(…)` as a url, and it reads the `/*` as the start of a comment. An audit of all functional utilities with a `/*` in a `url(…)` found only these four templates. The snapshots do not change, because the test helper formats the output. --- packages/tailwindcss/src/utilities.test.ts | 15 +++++++++++++++ packages/tailwindcss/src/utilities.ts | 8 ++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/tailwindcss/src/utilities.test.ts b/packages/tailwindcss/src/utilities.test.ts index 99426a68fe7b..1b267e31ad40 100644 --- a/packages/tailwindcss/src/utilities.test.ts +++ b/packages/tailwindcss/src/utilities.test.ts @@ -14354,6 +14354,21 @@ test('bg', async () => { `) }) +test('bg gradients print a space before an arbitrary value', async () => { + let { build } = await compile(css` + @tailwind utilities; + `) + + // PostCSS reads `,url` as one word, so it reads the `/*` in + // `,url(/a/*.png)` as the start of a comment. The output is not optimized + // here, because the optimizer adds the space. + let output = build(['bg-linear-[url(/a/*.png)]', 'bg-conic-[url(/a/*.png)]', 'bg-radial-[url(/a/*.png)]']) + + expect(output).toContain('linear-gradient(var(--tw-gradient-stops, url(/a/*.png)))') + expect(output).toContain('conic-gradient(var(--tw-gradient-stops, url(/a/*.png)))') + expect(output).toContain('radial-gradient(var(--tw-gradient-stops, url(/a/*.png)))') +}) + test('bg-position', async () => { expect( await run( diff --git a/packages/tailwindcss/src/utilities.ts b/packages/tailwindcss/src/utilities.ts index 4255d4a2a35e..0eb5151663b1 100644 --- a/packages/tailwindcss/src/utilities.ts +++ b/packages/tailwindcss/src/utilities.ts @@ -2785,7 +2785,7 @@ export function createUtilities(theme: Theme) { return [ decl('--tw-gradient-position', value), - decl('background-image', `linear-gradient(var(--tw-gradient-stops,${value}))`), + decl('background-image', `linear-gradient(var(--tw-gradient-stops, ${value}))`), ] } default: { @@ -2793,7 +2793,7 @@ export function createUtilities(theme: Theme) { return [ decl('--tw-gradient-position', value), - decl('background-image', `linear-gradient(var(--tw-gradient-stops,${value}))`), + decl('background-image', `linear-gradient(var(--tw-gradient-stops, ${value}))`), ] } } @@ -2843,7 +2843,7 @@ export function createUtilities(theme: Theme) { let value = candidate.value.value return [ decl('--tw-gradient-position', value), - decl('background-image', `conic-gradient(var(--tw-gradient-stops,${value}))`), + decl('background-image', `conic-gradient(var(--tw-gradient-stops, ${value}))`), ] } @@ -2898,7 +2898,7 @@ export function createUtilities(theme: Theme) { let value = candidate.value.value return [ decl('--tw-gradient-position', value), - decl('background-image', `radial-gradient(var(--tw-gradient-stops,${value}))`), + decl('background-image', `radial-gradient(var(--tw-gradient-stops, ${value}))`), ] } }) From c39c852af0782317a1012782c53c4a018044d3ef Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Tue, 10 Nov 2026 23:22:37 +0100 Subject: [PATCH 16/17] Format the gradient test --- packages/tailwindcss/src/utilities.test.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/tailwindcss/src/utilities.test.ts b/packages/tailwindcss/src/utilities.test.ts index 1b267e31ad40..4457fcae65a0 100644 --- a/packages/tailwindcss/src/utilities.test.ts +++ b/packages/tailwindcss/src/utilities.test.ts @@ -14362,7 +14362,11 @@ test('bg gradients print a space before an arbitrary value', async () => { // PostCSS reads `,url` as one word, so it reads the `/*` in // `,url(/a/*.png)` as the start of a comment. The output is not optimized // here, because the optimizer adds the space. - let output = build(['bg-linear-[url(/a/*.png)]', 'bg-conic-[url(/a/*.png)]', 'bg-radial-[url(/a/*.png)]']) + let output = build([ + 'bg-linear-[url(/a/*.png)]', + 'bg-conic-[url(/a/*.png)]', + 'bg-radial-[url(/a/*.png)]', + ]) expect(output).toContain('linear-gradient(var(--tw-gradient-stops, url(/a/*.png)))') expect(output).toContain('conic-gradient(var(--tw-gradient-stops, url(/a/*.png)))') From 959209ae235465fafbd863f16a8e8adcd4715f36 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Tue, 10 Nov 2026 23:47:25 +0100 Subject: [PATCH 17/17] Say in the gradient test that a minifier removes the space --- packages/tailwindcss/src/utilities.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/tailwindcss/src/utilities.test.ts b/packages/tailwindcss/src/utilities.test.ts index 4457fcae65a0..bac4efa2e80c 100644 --- a/packages/tailwindcss/src/utilities.test.ts +++ b/packages/tailwindcss/src/utilities.test.ts @@ -14361,7 +14361,8 @@ test('bg gradients print a space before an arbitrary value', async () => { // PostCSS reads `,url` as one word, so it reads the `/*` in // `,url(/a/*.png)` as the start of a comment. The output is not optimized - // here, because the optimizer adds the space. + // here, because the formatter adds the space. A minifier removes it again, + // see https://github.com/postcss/postcss/issues/2194. let output = build([ 'bg-linear-[url(/a/*.png)]', 'bg-conic-[url(/a/*.png)]',