Skip to content

Commit 34c1e36

Browse files
Don’t split vars with numbers in them inside arbitrary values (#8091)
* Don’t mangle vars with numbers in them * Update changelog
1 parent 1d4c5c7 commit 34c1e36

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
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
- Types: allow for arbitrary theme values (for 3rd party plugins) ([#7926](https://github.com/tailwindlabs/tailwindcss/pull/7926))
13+
- Don’t split vars with numbers in them inside arbitrary values ([#8091](https://github.com/tailwindlabs/tailwindcss/pull/8091))
1314

1415
### Added
1516

src/util/dataTypes.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,16 @@ export function normalize(value, isRoot = true) {
4242

4343
// Add spaces around operators inside calc() that do not follow an operator
4444
// or '('.
45-
return value.replace(
46-
/(-?\d*\.?\d(?!\b-.+[,)](?![^+\-/*])\D)(?:%|[a-z]+)?|\))([+\-/*])/g,
47-
'$1 $2 '
48-
)
45+
value = value.replace(/calc\(.+\)/g, (match) => {
46+
return match.replace(
47+
/(-?\d*\.?\d(?!\b-.+[,)](?![^+\-/*])\D)(?:%|[a-z]+)?|\))([+\-/*])/g,
48+
'$1 $2 '
49+
)
50+
})
51+
52+
// Add spaces around some operators not inside calc() that do not follow an operator
53+
// or '('.
54+
return value.replace(/(-?\d*\.?\d(?!\b-.+[,)](?![^+\-/*])\D)(?:%|[a-z]+)?|\))([\/])/g, '$1 $2 ')
4955
}
5056

5157
export function url(value) {

tests/normalize-data-types.test.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { normalize } from '../src/util/dataTypes'
2+
3+
let table = [
4+
['foo', 'foo'],
5+
['foo-bar', 'foo-bar'],
6+
['16/9', '16 / 9'],
7+
8+
// '_'s are converted to spaces except when escaped
9+
['foo_bar', 'foo bar'],
10+
['foo__bar', 'foo bar'],
11+
['foo\\_bar', 'foo_bar'],
12+
13+
// Urls are preserved as-is
14+
[
15+
'url("https://example.com/abc+def/some-path/2022-01-01-abc/some_underscoered_path")',
16+
'url("https://example.com/abc+def/some-path/2022-01-01-abc/some_underscoered_path")',
17+
],
18+
19+
// var(…) is preserved as is
20+
['var(--foo)', 'var(--foo)'],
21+
['var(--headings-h1-size)', 'var(--headings-h1-size)'],
22+
23+
// calc(…) get's spaces around operators
24+
['calc(1+2)', 'calc(1 + 2)'],
25+
['calc(100%+1rem)', 'calc(100% + 1rem)'],
26+
['calc(1+calc(100%-20px))', 'calc(1 + calc(100% - 20px))'],
27+
['calc(var(--headings-h1-size)*100)', 'calc(var(--headings-h1-size) * 100)'],
28+
[
29+
'calc(var(--headings-h1-size)*calc(100%+50%))',
30+
'calc(var(--headings-h1-size) * calc(100% + 50%))',
31+
],
32+
['var(--heading-h1-font-size)', 'var(--heading-h1-font-size)'],
33+
['var(--my-var-with-more-than-3-words)', 'var(--my-var-with-more-than-3-words)'],
34+
['var(--width, calc(100%+1rem))', 'var(--width, calc(100% + 1rem))'],
35+
36+
// Misc
37+
['color(0_0_0/1.0)', 'color(0 0 0 / 1.0)'],
38+
]
39+
40+
it.each(table)('normalize data: %s', (input, output) => {
41+
expect(normalize(input)).toBe(output)
42+
})

0 commit comments

Comments
 (0)