Skip to content

Commit 3d93086

Browse files
committed
Split box shadows on top-level commas only
1 parent be5d5c9 commit 3d93086

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

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
@@ -249,3 +249,31 @@ it('does not produce duplicate output when seeing variants preceding a wildcard
249249
`)
250250
})
251251
})
252+
253+
it('it can parse box shadows with variables', () => {
254+
let config = {
255+
content: [{ raw: html`<div class="shadow-lg"></div>` }],
256+
theme: {
257+
boxShadow: {
258+
lg: 'var(-a, 0 35px 60px -15px rgba(0, 0, 0)), 0 0 1px rgb(0, 0, 0)',
259+
},
260+
},
261+
corePlugins: { preflight: false },
262+
}
263+
264+
let input = css`
265+
@tailwind utilities;
266+
`
267+
268+
return run(input, config).then((result) => {
269+
expect(result.css).toMatchFormattedCss(css`
270+
.shadow-lg {
271+
--tw-shadow: var(-a, 0 35px 60px -15px rgba(0, 0, 0)), 0 0 1px rgb(0, 0, 0);
272+
--tw-shadow-colored: 0 35px 60px -15px var(--tw-shadow-color),
273+
0 0 1px var(--tw-shadow-color);
274+
box-shadow: var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000),
275+
var(--tw-shadow);
276+
}
277+
`)
278+
})
279+
})

0 commit comments

Comments
 (0)