Skip to content

Commit

Permalink
Allow both escaped dots and underscores to be registered in theme
Browse files Browse the repository at this point in the history
  • Loading branch information
philipp-spiess committed Jan 30, 2025
1 parent e9c7140 commit 8c193dc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
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
14 changes: 10 additions & 4 deletions packages/tailwindcss/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,21 +158,27 @@ describe('compiling CSS', () => {
css`
@theme {
--spacing-*: initial;
--spacing-1\.5: 2.5rem;
--spacing-1\.5: 1.5px;
--spacing-2_5: 2.5px;
--spacing-foo\/bar: 3rem;
}
@tailwind utilities;
`,
['m-1.5', 'm-foo/bar'],
['m-1.5', 'm-2.5', 'm-2_5', 'm-foo/bar'],
),
).toMatchInlineSnapshot(`
":root, :host {
--spacing-1_5: 2.5rem;
--spacing-1\\.5: 1.5px;
--spacing-2_5: 2.5px;
--spacing-foo\\/bar: 3rem;
}
.m-1\\.5 {
margin: var(--spacing-1_5);
margin: var(--spacing-1\\.5);
}
.m-2\\.5, .m-2_5 {
margin: var(--spacing-2_5);
}
.m-foo\\/bar {
Expand Down
19 changes: 13 additions & 6 deletions packages/tailwindcss/src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ export class Theme {
) {}

add(key: string, value: string, options = ThemeOptions.NONE): void {
key = key.replaceAll('.', '_')

if (key.endsWith('-*')) {
if (value !== 'initial') {
throw new Error(`Invalid theme value \`${value}\` for namespace \`${key}\``)
Expand Down Expand Up @@ -147,11 +145,20 @@ export class Theme {
#resolveKey(candidateValue: string | null, themeKeys: ThemeKey[]): string | null {
for (let namespace of themeKeys) {
let themeKey =
candidateValue !== null
? (`${namespace}-${candidateValue.replaceAll('.', '_')}` as ThemeKey)
: namespace
candidateValue !== null ? (`${namespace}-${candidateValue}` as ThemeKey) : namespace

if (!this.values.has(themeKey)) {
// If the exact theme key is not found, we might be trying to resolve a key containing a dot
// that was registered with an underscore instead:
if (candidateValue !== null && candidateValue.includes('.')) {
themeKey = `${namespace}-${candidateValue.replaceAll('.', '_')}` as ThemeKey

if (!this.values.has(themeKey)) continue
} else {
continue
}
}

if (!this.values.has(themeKey)) continue
if (isIgnoredThemeKey(themeKey, namespace)) continue

return themeKey
Expand Down

0 comments on commit 8c193dc

Please sign in to comment.