@@ -10,9 +10,6 @@ function isCSSFunction(value) {
10
10
return cssFunctions . some ( ( fn ) => new RegExp ( `^${ fn } \\(.*\\)` ) . test ( value ) )
11
11
}
12
12
13
- const placeholder = '--tw-placeholder'
14
- const placeholderRe = new RegExp ( placeholder , 'g' )
15
-
16
13
// This is not a data type, but rather a function that can normalize the
17
14
// correct values.
18
15
export function normalize ( value , isRoot = true ) {
@@ -63,15 +60,59 @@ export function normalize(value, isRoot = true) {
63
60
*/
64
61
function normalizeMathOperatorSpacing ( value ) {
65
62
return value . replace ( / ( c a l c | m i n | m a x | c l a m p ) \( .+ \) / g, ( match ) => {
66
- let vars = [ ]
63
+ let result = ''
67
64
68
- return match
69
- . replace ( / v a r \( ( - - .+ ?) [ , ) ] / g, ( match , g1 ) => {
70
- vars . push ( g1 )
71
- return match . replace ( g1 , placeholder )
72
- } )
73
- . replace ( / ( - ? \d * \. ? \d (? ! \b - \d .+ [ , ) ] (? ! [ ^ + \- / * ] ) \D ) (?: % | [ a - z ] + ) ? | \) ) ( [ + \- / * ] ) / g, '$1 $2 ' )
74
- . replace ( placeholderRe , ( ) => vars . shift ( ) )
65
+ function lastChar ( ) {
66
+ let char = result . trimEnd ( )
67
+ return char [ char . length - 1 ]
68
+ }
69
+
70
+ for ( let i = 0 ; i < match . length ; i ++ ) {
71
+ function peek ( word ) {
72
+ return word . split ( '' ) . every ( ( char , j ) => match [ i + j ] === char )
73
+ }
74
+
75
+ function consumeUntil ( chars ) {
76
+ let minIndex = Infinity
77
+ for ( let char of chars ) {
78
+ let index = match . indexOf ( char , i )
79
+ if ( index !== - 1 && index < minIndex ) {
80
+ minIndex = index
81
+ }
82
+ }
83
+
84
+ let result = match . slice ( i , minIndex )
85
+ i += result . length - 1
86
+ return result
87
+ }
88
+
89
+ let char = match [ i ]
90
+
91
+ // Handle `var(--variable)`
92
+ if ( peek ( 'var' ) ) {
93
+ // When we consume until `)`, then we are dealing with this scenario:
94
+ // `var(--example)`
95
+ //
96
+ // When we consume until `,`, then we are dealing with this scenario:
97
+ // `var(--example, 1rem)`
98
+ //
99
+ // In this case we do want to "format", the default value as well
100
+ result += consumeUntil ( [ ')' , ',' ] )
101
+ }
102
+
103
+ // Handle operators
104
+ else if (
105
+ [ '+' , '-' , '*' , '/' ] . includes ( char ) &&
106
+ ! [ '(' , '+' , '-' , '*' , '/' ] . includes ( lastChar ( ) )
107
+ ) {
108
+ result += ` ${ char } `
109
+ } else {
110
+ result += char
111
+ }
112
+ }
113
+
114
+ // Simplify multiple spaces
115
+ return result . replace ( / \s + / g, ' ' )
75
116
} )
76
117
}
77
118
0 commit comments