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
handle theme(colors.red/var(--opacity))
This means that we have to handle 2 cases:
1. Handle the `/` as a separator
2. Stringify the AST node, instead of using the value, because the
   `var(--opacity)` part is a (nested) AST node.
  • Loading branch information
RobinMalfait committed Oct 14, 2024
commit 594ee5a0ad50b73c757d08ccc54b62a8be53cc45
42 changes: 42 additions & 0 deletions packages/tailwindcss/src/css-functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,27 @@ describe('theme function', () => {
`)
})

test('theme(colors.red.500/75%)', async () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

Same test as below, but without spaces. Already worked, but added for completeness sake.

expect(
await compileCss(css`
@theme {
--color-red-500: #f00;
}
.red {
color: theme(colors.red.500/75%);
}
`),
).toMatchInlineSnapshot(`
":root {
--color-red-500: red;
}

.red {
color: #ff0000bf;
}"
`)
})

test('theme(colors.red.500 / 75%)', async () => {
expect(
await compileCss(css`
Expand Down Expand Up @@ -178,6 +199,27 @@ describe('theme function', () => {
`)
})

test('theme(colors.red.500/var(--opacity))', async () => {
expect(
await compileCss(css`
@theme {
--color-red-500: #f00;
}
.red {
color: theme(colors.red.500/var(--opacity));
}
`),
).toMatchInlineSnapshot(`
":root {
--color-red-500: red;
}

.red {
color: color-mix(in srgb, red calc(var(--opacity) * 100%), transparent);
}"
`)
})

test('theme(spacing.12)', async () => {
expect(
await compileCss(css`
Expand Down
2 changes: 1 addition & 1 deletion packages/tailwindcss/src/css-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function substituteFunctionsInValue(
if (node.nodes[i].value.includes(',')) {
break
}
path += node.nodes[i].value
path += ValueParser.toCss([node.nodes[i]])
skipUntilIndex = i + 1
}

Expand Down
14 changes: 14 additions & 0 deletions packages/tailwindcss/src/value-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ describe('parse', () => {
])
})

it('should parse a function with nested arguments separated by `/`', () => {
expect(parse('theme(colors.red.500/var(--opacity))')).toEqual([
{
kind: 'function',
value: 'theme',
nodes: [
{ kind: 'word', value: 'colors.red.500' },
{ kind: 'separator', value: '/' },
{ kind: 'function', value: 'var', nodes: [{ kind: 'word', value: '--opacity' }] },
],
},
])
})

it('should handle calculations', () => {
expect(parse('calc((1 + 2) * 3)')).toEqual([
{
Expand Down
2 changes: 2 additions & 0 deletions packages/tailwindcss/src/value-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ const SPACE = 0x20
const LESS_THAN = 0x3c
const GREATER_THAN = 0x3e
const EQUALS = 0x3d
const SLASH = 0x2f

export function parse(input: string) {
input = input.replaceAll('\r\n', '\n')
Expand Down Expand Up @@ -143,6 +144,7 @@ export function parse(input: string) {
case COLON:
case COMMA:
case SPACE:
case SLASH:
case LESS_THAN:
case GREATER_THAN:
case EQUALS: {
Expand Down