Skip to content

Commit 33e25d6

Browse files
committed
Split box shadows on top-level commas only
1 parent db475be commit 33e25d6

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
@@ -188,3 +188,31 @@ it('can scan extremely long classes without crashing', () => {
188188
expect(result.css).toMatchFormattedCss(css``)
189189
})
190190
})
191+
192+
it('it can parse box shadows with variables', () => {
193+
let config = {
194+
content: [{ raw: html`<div class="shadow-lg"></div>` }],
195+
theme: {
196+
boxShadow: {
197+
lg: 'var(-a, 0 35px 60px -15px rgba(0, 0, 0)), 0 0 1px rgb(0, 0, 0)',
198+
},
199+
},
200+
corePlugins: { preflight: false },
201+
}
202+
203+
let input = css`
204+
@tailwind utilities;
205+
`
206+
207+
return run(input, config).then((result) => {
208+
expect(result.css).toMatchFormattedCss(css`
209+
.shadow-lg {
210+
--tw-shadow: var(-a, 0 35px 60px -15px rgba(0, 0, 0)), 0 0 1px rgb(0, 0, 0);
211+
--tw-shadow-colored: 0 35px 60px -15px var(--tw-shadow-color),
212+
0 0 1px var(--tw-shadow-color);
213+
box-shadow: var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000),
214+
var(--tw-shadow);
215+
}
216+
`)
217+
})
218+
})

0 commit comments

Comments
 (0)