Skip to content

Don’t clip slashes inside brackets when using the theme function #8563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/lib/evaluateTailwindFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ let nodeTypePropertyMap = {
export default function ({ tailwindConfig: config }) {
let functions = {
theme: (node, path, ...defaultValue) => {
let matches = path.match(/^([^\/\s]+)(?:\s*\/\s*([^\/\s]+))$/)
let matches = path.match(/^([^\s]+)(?![^\[]*\])(?:\s*\/\s*([^\/\s]+))$/)
let alpha = undefined

if (matches) {
Expand Down
31 changes: 31 additions & 0 deletions tests/evaluateTailwindFunctions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1024,3 +1024,34 @@ test('Theme function can extract alpha values for colors (8)', () => {
expect(result.warnings().length).toBe(0)
})
})

test('Theme functions can reference values with slashes in brackets', () => {
let input = css`
.foo1 {
color: theme(colors[a/b]);
}
.foo2 {
color: theme(colors[a/b]/50%);
}
`

let output = css`
.foo1 {
color: #000000;
}
.foo2 {
color: rgb(0 0 0 / 50%);
}
`

return runFull(input, {
theme: {
colors: {
'a/b': '#000000',
},
},
}).then((result) => {
expect(result.css).toMatchCss(output)
expect(result.warnings().length).toBe(0)
})
})
33 changes: 33 additions & 0 deletions tests/opacity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -728,3 +728,36 @@ it('should be possible to use <alpha-value> inside arbitrary values', () => {
`)
})
})

it('Theme functions can reference values with slashes in brackets', () => {
let config = {
content: [
{
raw: html` <div class="bg-foo1 bg-foo2"></div> `,
},
],
theme: {
colors: {
'a/b': '#000000',
},
extend: {
backgroundColor: ({ theme }) => ({
foo1: theme('colors[a/b]'),
foo2: theme('colors[a/b]/50%'),
}),
},
},
}

return run('@tailwind utilities', config).then((result) => {
expect(result.css).toMatchCss(css`
.bg-foo1 {
--tw-bg-opacity: 1;
background-color: rgb(0 0 0 / var(--tw-bg-opacity));
}
.bg-foo2 {
background-color: rgb(0 0 0 / 50%);
}
`)
})
})