Skip to content

Commit deefbf5

Browse files
Handle theme keys with slashes when using theme() in CSS (#8831)
* Fix lookup of `theme(…)` paths with slashes in them * Update changelog
1 parent 6f928a8 commit deefbf5

File tree

3 files changed

+90
-15
lines changed

3 files changed

+90
-15
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Fixed
1111

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

1415
## [3.1.5] - 2022-07-07
1516

src/lib/evaluateTailwindFunctions.js

+41-15
Original file line numberDiff line numberDiff line change
@@ -157,26 +157,52 @@ let nodeTypePropertyMap = {
157157
decl: 'value',
158158
}
159159

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

167-
let matches = path.match(/^([^\s]+)(?![^\[]*\])(?:\s*\/\s*([^\/\s]+))$/)
168-
let alpha = undefined
169+
let matches = path.match(/^([^\s]+)(?![^\[]*\])(?:\s*\/\s*([^\/\s]+))$/)
170+
let alpha = undefined
169171

170-
if (matches) {
171-
path = matches[1]
172-
alpha = matches[2]
173-
}
172+
yield [path, undefined]
173+
174+
if (matches) {
175+
path = matches[1]
176+
alpha = matches[2]
177+
178+
yield [path, alpha]
179+
}
180+
}
174181

175-
let { isValid, value, error } = validatePath(
182+
/**
183+
*
184+
* @param {any} config
185+
* @param {string} path
186+
* @param {any} defaultValue
187+
*/
188+
function resolvePath(config, path, defaultValue) {
189+
const results = Array.from(toPaths(path)).map(([path, alpha]) => {
190+
return Object.assign(validatePath(config, path, defaultValue, { opacityValue: alpha }), {
191+
resolvedPath: path,
192+
alpha,
193+
})
194+
})
195+
196+
return results.find((result) => result.isValid) ?? results[0]
197+
}
198+
199+
export default function ({ tailwindConfig: config }) {
200+
let functions = {
201+
theme: (node, path, ...defaultValue) => {
202+
let { isValid, value, error, alpha } = resolvePath(
176203
config,
177204
path,
178-
defaultValue.length ? defaultValue : undefined,
179-
{ opacityValue: alpha }
205+
defaultValue.length ? defaultValue : undefined
180206
)
181207

182208
if (!isValid) {

tests/evaluateTailwindFunctions.test.js

+48
Original file line numberDiff line numberDiff line change
@@ -1135,3 +1135,51 @@ test('Theme functions with alpha with quotes value around color only', () => {
11351135
expect(result.warnings().length).toBe(0)
11361136
})
11371137
})
1138+
1139+
it('can find values with slashes in the theme key while still allowing for alpha values ', () => {
1140+
let input = css`
1141+
.foo00 {
1142+
color: theme(colors.foo-5);
1143+
}
1144+
.foo01 {
1145+
color: theme(colors.foo-5/10);
1146+
}
1147+
.foo02 {
1148+
color: theme(colors.foo-5/10/25);
1149+
}
1150+
.foo03 {
1151+
color: theme(colors.foo-5 / 10);
1152+
}
1153+
.foo04 {
1154+
color: theme(colors.foo-5/10 / 25);
1155+
}
1156+
`
1157+
1158+
return runFull(input, {
1159+
theme: {
1160+
colors: {
1161+
'foo-5': '#050000',
1162+
'foo-5/10': '#051000',
1163+
'foo-5/10/25': '#051025',
1164+
},
1165+
},
1166+
}).then((result) => {
1167+
expect(result.css).toMatchCss(css`
1168+
.foo00 {
1169+
color: #050000;
1170+
}
1171+
.foo01 {
1172+
color: #051000;
1173+
}
1174+
.foo02 {
1175+
color: #051025;
1176+
}
1177+
.foo03 {
1178+
color: rgb(5 0 0 / 10);
1179+
}
1180+
.foo04 {
1181+
color: rgb(5 16 0 / 25);
1182+
}
1183+
`)
1184+
})
1185+
})

0 commit comments

Comments
 (0)