Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Ensure escaped theme variables are handled correctly
  • Loading branch information
philipp-spiess committed Jan 30, 2025
commit e9c71406365b7736bd70b3f1b8799efb31497fca
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Only generate positive `grid-cols-*` and `grid-rows-*` utilities ([#16020](https://github.com/tailwindlabs/tailwindcss/pull/16020))
- Ensure escaped theme variables are handled correctly ([#16064](https://github.com/tailwindlabs/tailwindcss/pull/16064))

## [4.0.1] - 2025-01-29

Expand Down
3 changes: 1 addition & 2 deletions packages/tailwindcss/src/compat/apply-config-to-theme.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { DesignSystem } from '../design-system'
import { ThemeOptions } from '../theme'
import { escape } from '../utils/escape'
import type { ResolvedConfig } from './config/types'

function resolveThemeValue(value: unknown, subValue: string | null = null): string | null {
Expand Down Expand Up @@ -55,7 +54,7 @@ export function applyConfigToTheme(
if (!name) continue

designSystem.theme.add(
`--${escape(name)}`,
`--${name}`,
'' + value,
ThemeOptions.INLINE | ThemeOptions.REFERENCE | ThemeOptions.DEFAULT,
)
Expand Down
4 changes: 2 additions & 2 deletions packages/tailwindcss/src/compat/plugin-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1425,7 +1425,7 @@ describe('theme', async () => {
:root, :host {
--width-1: 0.25rem;
--width-1\\/2: 60%;
--width-1\\.5: 0.375rem;
--width-1_5: 0.375rem;
--width-2_5: 0.625rem;
}
"
Expand Down Expand Up @@ -1482,7 +1482,7 @@ describe('theme', async () => {
:root, :host {
--width-1: 0.25rem;
--width-1\\/2: 60%;
--width-1\\.5: 0.375rem;
--width-1_5: 0.375rem;
--width-2_5: 0.625rem;
}
"
Expand Down
29 changes: 29 additions & 0 deletions packages/tailwindcss/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,35 @@ describe('compiling CSS', () => {
`)
})

test('unescapes theme variables and handles dots as underscore', async () => {
expect(
await compileCss(
css`
@theme {
--spacing-*: initial;
--spacing-1\.5: 2.5rem;
--spacing-foo\/bar: 3rem;
}
@tailwind utilities;
`,
['m-1.5', 'm-foo/bar'],
),
).toMatchInlineSnapshot(`
":root, :host {
--spacing-1_5: 2.5rem;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be --spacing-1\.5?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adamwathan Ah yeah I think you're right if they're defined with \. they should be CSS variables with \. just the lookup for 1_5 should also work 👍

--spacing-foo\\/bar: 3rem;
}

.m-1\\.5 {
margin: var(--spacing-1_5);
}

.m-foo\\/bar {
margin: var(--spacing-foo\\/bar);
}"
`)
})

test('adds vendor prefixes', async () => {
expect(
await compileCss(
Expand Down
5 changes: 3 additions & 2 deletions packages/tailwindcss/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import * as CSS from './css-parser'
import { buildDesignSystem, type DesignSystem } from './design-system'
import { Theme, ThemeOptions } from './theme'
import { createCssUtility } from './utilities'
import { escape, unescape } from './utils/escape'
import { segment } from './utils/segment'
import { compoundsForSelectors, IS_VALID_VARIANT_NAME } from './variants'
export type Config = UserConfig
Expand Down Expand Up @@ -461,7 +462,7 @@ async function parseCss(

if (child.kind === 'comment') return
if (child.kind === 'declaration' && child.property.startsWith('--')) {
theme.add(child.property, child.value ?? '', themeOptions)
theme.add(unescape(child.property), child.value ?? '', themeOptions)
return
}

Expand Down Expand Up @@ -520,7 +521,7 @@ async function parseCss(

for (let [key, value] of theme.entries()) {
if (value.options & ThemeOptions.REFERENCE) continue
nodes.push(decl(key, value.value))
nodes.push(decl(escape(key), value.value))
}

let keyframesRules = theme.getKeyframes()
Expand Down
8 changes: 3 additions & 5 deletions packages/tailwindcss/src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ export class Theme {
) {}

add(key: string, value: string, options = ThemeOptions.NONE): void {
if (key.endsWith('\\*')) {
key = key.slice(0, -2) + '*'
}
key = key.replaceAll('.', '_')

if (key.endsWith('-*')) {
if (value !== 'initial') {
Expand Down Expand Up @@ -150,7 +148,7 @@ export class Theme {
for (let namespace of themeKeys) {
let themeKey =
candidateValue !== null
? (escape(`${namespace}-${candidateValue.replaceAll('.', '_')}`) as ThemeKey)
? (`${namespace}-${candidateValue.replaceAll('.', '_')}` as ThemeKey)
: namespace

if (!this.values.has(themeKey)) continue
Expand All @@ -167,7 +165,7 @@ export class Theme {
return null
}

return `var(${this.#prefixKey(themeKey)})`
return `var(${escape(this.#prefixKey(themeKey))})`
}

resolve(candidateValue: string | null, themeKeys: ThemeKey[]): string | null {
Expand Down