Skip to content

Commit 04686b8

Browse files
Split box shadows on top-level commas only (#7479)
* Split box shadows on top-level commas only * Update changelog
1 parent b94d565 commit 04686b8

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- Recursively collapse adjacent rules ([#7565](https://github.com/tailwindlabs/tailwindcss/pull/7565))
1212
- Allow default ring color to be a function ([#7587](https://github.com/tailwindlabs/tailwindcss/pull/7587))
1313
- Preserve source maps for generated CSS ([#7588](https://github.com/tailwindlabs/tailwindcss/pull/7588))
14+
- Split box shadows on top-level commas only ([#7479](https://github.com/tailwindlabs/tailwindcss/pull/7479))
1415

1516
## [3.0.23] - 2022-02-16
1617

src/util/parseBoxShadowValue.js

+50-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,58 @@
11
let KEYWORDS = new Set(['inset', 'inherit', 'initial', 'revert', 'unset'])
2-
let COMMA = /\,(?![^(]*\))/g // Comma separator that is not located between brackets. E.g.: `cubiz-bezier(a, b, c)` these don't count.
32
let SPACE = /\ +(?![^(]*\))/g // Similar to the one above, but with spaces instead.
43
let LENGTH = /^-?(\d+|\.\d+)(.*?)$/g
54

5+
let SPECIALS = /[(),]/g
6+
7+
/**
8+
* This splits a string on top-level commas.
9+
*
10+
* Regex doesn't support recursion (at least not the JS-flavored version).
11+
* So we have to use a tiny state machine to keep track of paren vs comma
12+
* placement. Before we'd only exclude commas from the inner-most nested
13+
* set of parens rather than any commas that were not contained in parens
14+
* at all which is the intended behavior here.
15+
*
16+
* Expected behavior:
17+
* var(--a, 0 0 1px rgb(0, 0, 0)), 0 0 1px rgb(0, 0, 0)
18+
* ─┬─ ┬ ┬ ┬
19+
* x x x ╰──────── Split because top-level
20+
* ╰──────────────┴──┴───────────── Ignored b/c inside >= 1 levels of parens
21+
*
22+
* @param {string} input
23+
*/
24+
function* splitByTopLevelCommas(input) {
25+
SPECIALS.lastIndex = -1
26+
27+
let depth = 0
28+
let lastIndex = 0
29+
let found = false
30+
31+
// Find all parens & commas
32+
// And only split on commas if they're top-level
33+
for (let match of input.matchAll(SPECIALS)) {
34+
if (match[0] === '(') depth++
35+
if (match[0] === ')') depth--
36+
if (match[0] === ',' && depth === 0) {
37+
found = true
38+
39+
yield input.substring(lastIndex, match.index)
40+
lastIndex = match.index + match[0].length
41+
}
42+
}
43+
44+
// Provide the last segment of the string if available
45+
// Otherwise the whole string since no commas were found
46+
// This mirrors the behavior of string.split()
47+
if (found) {
48+
yield input.substring(lastIndex)
49+
} else {
50+
yield input
51+
}
52+
}
53+
654
export function parseBoxShadowValue(input) {
7-
let shadows = input.split(COMMA)
55+
let shadows = Array.from(splitByTopLevelCommas(input))
856
return shadows.map((shadow) => {
957
let value = shadow.trim()
1058
let result = { raw: value }

tests/basic-usage.test.js

+28
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,31 @@ it('does not produce duplicate output when seeing variants preceding a wildcard
346346
`)
347347
})
348348
})
349+
350+
it('it can parse box shadows with variables', () => {
351+
let config = {
352+
content: [{ raw: html`<div class="shadow-lg"></div>` }],
353+
theme: {
354+
boxShadow: {
355+
lg: 'var(-a, 0 35px 60px -15px rgba(0, 0, 0)), 0 0 1px rgb(0, 0, 0)',
356+
},
357+
},
358+
corePlugins: { preflight: false },
359+
}
360+
361+
let input = css`
362+
@tailwind utilities;
363+
`
364+
365+
return run(input, config).then((result) => {
366+
expect(result.css).toMatchFormattedCss(css`
367+
.shadow-lg {
368+
--tw-shadow: var(-a, 0 35px 60px -15px rgba(0, 0, 0)), 0 0 1px rgb(0, 0, 0);
369+
--tw-shadow-colored: 0 35px 60px -15px var(--tw-shadow-color),
370+
0 0 1px var(--tw-shadow-color);
371+
box-shadow: var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000),
372+
var(--tw-shadow);
373+
}
374+
`)
375+
})
376+
})

0 commit comments

Comments
 (0)