@@ -6,6 +6,7 @@ import { CONTROL_NO_NEGATIVE, cssMathFnRE, cssVarFnRE } from '../utils'
66
77const anchoredNumberRE = / ^ - ? [ 0 - 9 . ] + (?: [ a - z ] + | % ) ? $ /
88const numberRE = / - ? [ 0 - 9 . ] + (?: [ a - z ] + | % ) ? /
9+ const spacingMultiplyRE = / v a r \( - - s p a c i n g (?: - [ \w - ] + ) ? \) \s * \* \s * ( - ? [ 0 - 9 . ] + (?: [ a - z ] + | % ) ? ) /
910
1011const ignoreProps = [
1112 / \b ( o p a c i t y | c o l o r | f l e x | b a c k d r o p - f i l t e r | ^ f i l t e r | ^ s c a l e | t r a n s f o r m | m a s k - i m a g e ) \b / ,
@@ -15,8 +16,19 @@ function negateMathFunction(value: string) {
1516 const match = value . match ( cssMathFnRE ) || value . match ( cssVarFnRE )
1617 if ( match ) {
1718 const [ fnBody , rest ] = getStringComponent ( `(${ match [ 2 ] } )${ match [ 3 ] } ` , '(' , ')' , ' ' ) ?? [ ]
18- if ( fnBody )
19+ if ( fnBody ) {
20+ const spacingMultiplyMatch = fnBody . match ( spacingMultiplyRE )
21+ if ( spacingMultiplyMatch ) {
22+ const num = spacingMultiplyMatch [ 1 ]
23+ const nextNum = num . startsWith ( '-' ) ? num . slice ( 1 ) : `-${ num } `
24+ const nextFnBody = fnBody . replace ( spacingMultiplyRE , ( segment ) => {
25+ return segment . replace ( num , nextNum )
26+ } )
27+ return `${ match [ 1 ] } ${ nextFnBody } ${ rest ? ` ${ rest } ` : '' } `
28+ }
29+
1930 return `calc(${ match [ 1 ] } ${ fnBody } * -1)${ rest ? ` ${ rest } ` : '' } `
31+ }
2032 }
2133}
2234
@@ -45,32 +57,35 @@ export const variantNegative: Variant<Theme> = {
4557 body : ( body ) => {
4658 if ( body . find ( v => v [ 0 ] === CONTROL_NO_NEGATIVE ) )
4759 return
60+
4861 let changed = false
49- body . forEach ( ( v ) => {
50- if ( toArray ( v [ 2 ] ) . includes ( CONTROL_NO_NEGATIVE ) )
51- return
52- const value = v [ 1 ] ?. toString ( )
53- if ( ! value || value === '0' )
54- return
55- if ( ignoreProps . some ( i => i . test ( v [ 0 ] ) ) )
56- return
57- const negatedFn = negateMathFunction ( value )
58- if ( negatedFn ) {
59- v [ 1 ] = negatedFn
60- changed = true
61- return
62- }
63- const negatedBody = negateFunctionBody ( value )
64- if ( negatedBody ) {
65- v [ 1 ] = negatedBody
66- changed = true
67- return
68- }
69- if ( anchoredNumberRE . test ( value ) ) {
70- v [ 1 ] = value . replace ( numberRE , i => i . startsWith ( '-' ) ? i . slice ( 1 ) : `-${ i } ` )
71- changed = true
72- }
73- } )
62+ for ( const v of body ) {
63+ const [ prop , rawValue , meta ] = v
64+
65+ // skip `symbols.variants` and other non-string values
66+ if ( typeof rawValue === 'object' )
67+ continue
68+
69+ if ( meta && toArray ( meta ) . includes ( CONTROL_NO_NEGATIVE ) )
70+ continue
71+
72+ const value = rawValue ?. toString ( )
73+ if ( ! value || value === '0' || ignoreProps . some ( i => i . test ( prop ) ) )
74+ continue
75+
76+ const nextValue = negateMathFunction ( value )
77+ ?? negateFunctionBody ( value )
78+ ?? ( anchoredNumberRE . test ( value )
79+ ? value . replace ( numberRE , i => i . startsWith ( '-' ) ? i . slice ( 1 ) : `-${ i } ` )
80+ : undefined )
81+
82+ if ( ! nextValue || nextValue === value )
83+ continue
84+
85+ v [ 1 ] = nextValue
86+ changed = true
87+ }
88+
7489 if ( changed )
7590 return body
7691 return [ ]
0 commit comments