Skip to content

Commit 1c24d7a

Browse files
Detect alpha value in CSS theme() function when using quotes (#8625)
* Allow alpha value inside quotes * Optimize regex * Add test * Update changelog
1 parent aad299c commit 1c24d7a

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Fix casing of import of `corePluginList` type definition ([#8587](https://github.com/tailwindlabs/tailwindcss/pull/8587))
1414
- Ignore PostCSS nodes returned by `addVariant` ([#8608](https://github.com/tailwindlabs/tailwindcss/pull/8608))
1515
- Fix missing spaces around arithmetic operators ([#8615](https://github.com/tailwindlabs/tailwindcss/pull/8615))
16+
- Detect alpha value in CSS `theme()` function when using quotes ([#8625](https://github.com/tailwindlabs/tailwindcss/pull/8625))
1617

1718
## [3.1.2] - 2022-06-10
1819

src/lib/evaluateTailwindFunctions.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ function listKeys(obj) {
4040
}
4141

4242
function validatePath(config, path, defaultValue, themeOpts = {}) {
43-
const pathString = Array.isArray(path)
44-
? pathToString(path)
45-
: path.replace(/^['"]+/g, '').replace(/['"]+$/g, '')
43+
const pathString = Array.isArray(path) ? pathToString(path) : path.replace(/^['"]+|['"]+$/g, '')
4644
const pathSegments = Array.isArray(path) ? path : toPath(pathString)
4745
const value = dlv(config.theme, pathSegments, defaultValue)
4846

@@ -162,6 +160,10 @@ let nodeTypePropertyMap = {
162160
export default function ({ tailwindConfig: config }) {
163161
let functions = {
164162
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, '')
166+
165167
let matches = path.match(/^([^\s]+)(?![^\[]*\])(?:\s*\/\s*([^\/\s]+))$/)
166168
let alpha = undefined
167169

tests/evaluateTailwindFunctions.test.js

+50
Original file line numberDiff line numberDiff line change
@@ -1055,3 +1055,53 @@ test('Theme functions can reference values with slashes in brackets', () => {
10551055
expect(result.warnings().length).toBe(0)
10561056
})
10571057
})
1058+
1059+
test('Theme functions with alpha value inside quotes', () => {
1060+
let input = css`
1061+
.foo {
1062+
color: theme('colors.yellow / 50%');
1063+
}
1064+
`
1065+
1066+
let output = css`
1067+
.foo {
1068+
color: rgb(247 204 80 / 50%);
1069+
}
1070+
`
1071+
1072+
return runFull(input, {
1073+
theme: {
1074+
colors: {
1075+
yellow: '#f7cc50',
1076+
},
1077+
},
1078+
}).then((result) => {
1079+
expect(result.css).toMatchCss(output)
1080+
expect(result.warnings().length).toBe(0)
1081+
})
1082+
})
1083+
1084+
test('Theme functions with alpha with quotes value around color only', () => {
1085+
let input = css`
1086+
.foo {
1087+
color: theme('colors.yellow' / 50%);
1088+
}
1089+
`
1090+
1091+
let output = css`
1092+
.foo {
1093+
color: rgb(247 204 80 / 50%);
1094+
}
1095+
`
1096+
1097+
return runFull(input, {
1098+
theme: {
1099+
colors: {
1100+
yellow: '#f7cc50',
1101+
},
1102+
},
1103+
}).then((result) => {
1104+
expect(result.css).toMatchCss(output)
1105+
expect(result.warnings().length).toBe(0)
1106+
})
1107+
})

0 commit comments

Comments
 (0)