Skip to content

Commit 70bb3a9

Browse files
committed
Don’t mangle vars with numbers in them
1 parent c1ed8dd commit 70bb3a9

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

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

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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(var(--headings-h1-size)*100)', 'calc(var(--headings-h1-size) * 100)'],
27+
[
28+
'calc(var(--headings-h1-size)*calc(100%+50%))',
29+
'calc(var(--headings-h1-size) * calc(100% + 50%))',
30+
],
31+
['var(--heading-h1-font-size)', 'var(--heading-h1-font-size)'],
32+
['var(--my-var-with-more-than-3-words)', 'var(--my-var-with-more-than-3-words)'],
33+
['var(--width, calc(100%+1rem))', 'var(--width, calc(100% + 1rem))'],
34+
35+
// Misc
36+
['color(0_0_0/1.0)', 'color(0 0 0 / 1.0)'],
37+
]
38+
39+
it.each(table)('normalize data: %s', (input, output) => {
40+
expect(normalize(input)).toBe(output)
41+
})

0 commit comments

Comments
 (0)