Skip to content
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

Handle theme keys with slashes when using theme() in CSS #8831

Merged
merged 3 commits into from Jul 11, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions CHANGELOG.md
Expand Up @@ -7,15 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Nothing yet!
### Fixed

- Fix usage on Node 12.x ([b4e637e](https://github.com/tailwindlabs/tailwindcss/commit/b4e637e2e096a9d6f2210efba9541f6fd4f28e56))
- Handle theme keys with slashes when using `theme()` in CSS ([#8831](https://github.com/tailwindlabs/tailwindcss/pull/8831))

## [3.1.5] - 2022-07-07

### Added

- Support configuring a default `font-weight` for each font size utility ([#8763](https://github.com/tailwindlabs/tailwindcss/pull/8763))
- Add support for alpha values in safe list ([#8774](https://github.com/tailwindlabs/tailwindcss/pull/8774))

### Fixed

- Improve types to support fallback values in the CSS-in-JS syntax used in plugin APIs ([#8762](https://github.com/tailwindlabs/tailwindcss/pull/8762))
Expand Down
56 changes: 41 additions & 15 deletions src/lib/evaluateTailwindFunctions.js
Expand Up @@ -157,26 +157,52 @@ let nodeTypePropertyMap = {
decl: 'value',
}

export default function ({ tailwindConfig: config }) {
let functions = {
theme: (node, path, ...defaultValue) => {
// Strip quotes from beginning and end of string
// This allows the alpha value to be present inside of quotes
path = path.replace(/^['"]+|['"]+$/g, '')
/**
* @param {string} path
* @returns {Iterable<[path: string, alpha: string|undefined]>}
*/
function* toPaths(path) {
// Strip quotes from beginning and end of string
// This allows the alpha value to be present inside of quotes
path = path.replace(/^['"]+|['"]+$/g, '')

let matches = path.match(/^([^\s]+)(?![^\[]*\])(?:\s*\/\s*([^\/\s]+))$/)
let alpha = undefined
let matches = path.match(/^([^\s]+)(?![^\[]*\])(?:\s*\/\s*([^\/\s]+))$/)
let alpha = undefined

if (matches) {
path = matches[1]
alpha = matches[2]
}
yield [path, undefined]

if (matches) {
path = matches[1]
alpha = matches[2]

yield [path, alpha]
}
}

let { isValid, value, error } = validatePath(
/**
*
* @param {any} config
* @param {string} path
* @param {any} defaultValue
*/
function resolvePath(config, path, defaultValue) {
const results = Array.from(toPaths(path)).map(([path, alpha]) => {
return Object.assign(validatePath(config, path, defaultValue, { opacityValue: alpha }), {
resolvedPath: path,
alpha,
})
})

return results.find((result) => result.isValid) ?? results[0]
}

export default function ({ tailwindConfig: config }) {
let functions = {
theme: (node, path, ...defaultValue) => {
let { isValid, value, error, alpha } = resolvePath(
config,
path,
defaultValue.length ? defaultValue : undefined,
{ opacityValue: alpha }
defaultValue.length ? defaultValue : undefined
)

if (!isValid) {
Expand Down
48 changes: 48 additions & 0 deletions tests/evaluateTailwindFunctions.test.js
Expand Up @@ -1135,3 +1135,51 @@ test('Theme functions with alpha with quotes value around color only', () => {
expect(result.warnings().length).toBe(0)
})
})

it('can find values with slashes in the theme key while still allowing for alpha values ', () => {
let input = css`
.foo00 {
color: theme(colors.foo-5);
}
.foo01 {
color: theme(colors.foo-5/10);
}
.foo02 {
color: theme(colors.foo-5/10/25);
}
.foo03 {
color: theme(colors.foo-5 / 10);
}
.foo04 {
color: theme(colors.foo-5/10 / 25);
}
`

return runFull(input, {
theme: {
colors: {
'foo-5': '#050000',
'foo-5/10': '#051000',
'foo-5/10/25': '#051025',
},
},
}).then((result) => {
expect(result.css).toMatchCss(css`
.foo00 {
color: #050000;
}
.foo01 {
color: #051000;
}
.foo02 {
color: #051025;
}
.foo03 {
color: rgb(5 0 0 / 10);
}
.foo04 {
color: rgb(5 16 0 / 25);
}
`)
})
})