Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
20dac60
Ignore arbitrary values that start a CSS comment
RobinMalfait Nov 10, 2026
0bc10eb
Ignore arbitrary properties with a `/*` in the property name
RobinMalfait Nov 10, 2026
25ffb66
Ignore arbitrary values with an unescaped newline in a string
RobinMalfait Nov 10, 2026
4e9b018
Ignore arbitrary values with a string that has no closing quote
RobinMalfait Nov 10, 2026
1023b3e
Ignore arbitrary values with a quote in an unquoted `url(…)`
RobinMalfait Nov 10, 2026
6b99599
Keep a quoted string after an `_` in an uppercase `URL(…)` valid
RobinMalfait Nov 10, 2026
c066701
Keep the `_` of a nested `URL(…)` in a lowercase `url(…)`
RobinMalfait Nov 10, 2026
82ef978
Ignore arbitrary values with an escape in a function name
RobinMalfait Nov 10, 2026
80bf6de
Keep a function name that ends in `\_url` valid
RobinMalfait Nov 10, 2026
cac9089
Keep each `_` in a nested `URL(…)` of a function that ends in `\_url`
RobinMalfait Nov 10, 2026
42bb537
Allow a `/*` in an unquoted `url(…)` in a declaration value
RobinMalfait Nov 10, 2026
a01831c
Ignore arbitrary values with a `\/*`
RobinMalfait Nov 10, 2026
f95b4b9
Test random arbitrary values with Lightning CSS
RobinMalfait Nov 10, 2026
d9f7240
Allow a `/*` in a `url(…)` after a type hint
RobinMalfait Nov 10, 2026
49f99ec
Print a space before the arbitrary value of a gradient
RobinMalfait Nov 10, 2026
c39c852
Format the gradient test
RobinMalfait Nov 10, 2026
959209a
Say in the gradient test that a minifier removes the space
RobinMalfait Nov 10, 2026
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
3 changes: 3 additions & 0 deletions .changelog/comments-in-arbitrary-values.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed

- 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
152 changes: 152 additions & 0 deletions packages/tailwindcss/src/candidate.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { transform } from 'lightningcss'
import { describe, expect, it, test } from 'vitest'
import { __unstable__loadDesignSystem } from '.'
import { buildDesignSystem } from './design-system'
Expand Down Expand Up @@ -2160,6 +2161,54 @@ 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',
'[a/*b:red]',
"bg-['a\n/*']",
"[a':'/*']",
"bg-['/*]",
"bg-[url(foo'bar)/*')]",
'[x:url(foo"bar)/*")]',
'bg-[URL(a(b)/*)]',
"bg-[url('a',URL(_'b)/*'))]",
"bg-[u\\72l(foo'bar)/*')]",
"bg-[u\\72_l(foo'bar)/*')]",
// `\\` 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)/*'))]",
'[--x: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)]',
// 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
"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',

// 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', () => [])
Expand All @@ -2172,6 +2221,109 @@ it.each([
expect(run(rawCandidate, { utilities, variants })).toEqual([])
})

it('should not let a random arbitrary value comment out the CSS after it', async () => {
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', () => [])

// `\_` 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)' } },
])

// 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(…)`', () => {
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', () => [])

expect(run(`content-['/*']`, { utilities })).toMatchInlineSnapshot(`
[
{
"important": false,
"kind": "functional",
"modifier": null,
"raw": "content-['/*']",
"root": "content",
"value": {
"dataType": null,
"kind": "arbitrary",
"value": "'/*'",
},
"variants": [],
},
]
`)
})

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)'],
['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', () => [])

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]'],
Expand Down
14 changes: 10 additions & 4 deletions packages/tailwindcss/src/candidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,12 @@ export function* parseCandidate(input: string, designSystem: DesignSystem): Iter
if (idx === -1 || idx === 0 || idx === baseWithoutModifier.length - 1) return

let property = baseWithoutModifier.slice(0, idx)
let value = decodeValidArbitraryValue(baseWithoutModifier.slice(idx + 1))

// 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
Comment thread
RobinMalfait marked this conversation as resolved.
Resolved

let value = decodeValidArbitraryValue(baseWithoutModifier.slice(idx + 1), true)

// Values can't contain `;` or `}` characters at the top-level.
if (value === null) return
Expand Down Expand Up @@ -563,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
Expand Down Expand Up @@ -1298,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
}
20 changes: 20 additions & 0 deletions packages/tailwindcss/src/utilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14354,6 +14354,26 @@ 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 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)]',
'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(
Expand Down
8 changes: 4 additions & 4 deletions packages/tailwindcss/src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2785,15 +2785,15 @@ 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: {
if (negative) return

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}))`),
]
}
}
Expand Down Expand Up @@ -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}))`),
]
}

Expand Down Expand Up @@ -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}))`),
]
}
})
Expand Down
Loading
Loading