@@ -3,7 +3,68 @@ import { StringValueComponent } from '../types/syntax'
33import functions from '../config/functions'
44import { BASE_UNIT_REGEX } from '../common'
55
6- export default function coreCalc ( this : SyntaxRule , value : string , bypassVariableNames : string [ ] ) {
6+ interface CoreMathData {
7+ name ?: string
8+ wrapArguments ?: boolean
9+ }
10+
11+ function splitTopLevelArguments ( value : string ) {
12+ const parts : string [ ] = [ ]
13+ let depth = 0
14+ let start = 0
15+ for ( let i = 0 ; i < value . length ; i ++ ) {
16+ const char = value [ i ]
17+ if ( char === '(' ) {
18+ depth ++
19+ } else if ( char === ')' ) {
20+ depth --
21+ } else if ( char === ',' && depth === 0 ) {
22+ parts . push ( value . slice ( start , i ) , char )
23+ start = i + 1
24+ }
25+ }
26+ parts . push ( value . slice ( start ) )
27+ return parts
28+ }
29+
30+ function hasTopLevelOperator ( value : string ) {
31+ let depth = 0
32+ for ( let i = 0 ; i < value . length ; i ++ ) {
33+ const char = value [ i ]
34+ if ( char === '(' ) {
35+ depth ++
36+ } else if ( char === ')' ) {
37+ depth --
38+ } else if ( depth === 0 && ( char === '+' || char === '-' || char === '*' || char === '/' ) ) {
39+ let previousIndex = i - 1
40+ while ( value [ previousIndex ] === ' ' ) {
41+ previousIndex --
42+ }
43+ const previousChar = value [ previousIndex ]
44+ if ( ( char === '+' || char === '-' ) && ( ! previousChar || previousChar === '(' || previousChar === ',' || previousChar === ' ' ) ) {
45+ continue
46+ }
47+ return true
48+ }
49+ }
50+ return false
51+ }
52+
53+ function wrapCalcArguments ( value : string ) {
54+ return splitTopLevelArguments ( value ) . map ( ( part ) => {
55+ if ( part === ',' ) return part
56+ const trimmed = part . trim ( )
57+ if ( ! trimmed || / ^ (?: c a l c | c l a m p | m i n | m a x | v a r ) \( / . test ( trimmed ) || ! hasTopLevelOperator ( trimmed ) ) {
58+ return part
59+ }
60+ const leadingWhitespace = part . match ( / ^ \s * / ) ! [ 0 ]
61+ const trailingWhitespace = part . match ( / \s * $ / ) ! [ 0 ]
62+ return `${ leadingWhitespace } calc(${ trimmed } )${ trailingWhitespace } `
63+ } ) . join ( '' )
64+ }
65+
66+ export default function coreMath ( this : SyntaxRule , value : string , bypassVariableNames : string [ ] , data ?: CoreMathData ) {
67+ const functionName = data ?. name ?? 'calc'
768 const valueComponents : SyntaxRule [ 'valueComponents' ] = [ ]
869 let i = 0
970
@@ -201,5 +262,10 @@ export default function coreCalc(this: SyntaxRule, value: string, bypassVariable
201262 handleUnitChecking ( )
202263 }
203264 anaylzeDeeply ( valueComponents , false , false , false , false )
204- return 'calc(' + this . resolveValue ( valueComponents , this . definition . unit , bypassVariableNames , true ) + ')'
205- }
265+
266+ let resolvedValue = this . resolveValue ( valueComponents , this . definition . unit , bypassVariableNames , true )
267+ if ( data ?. wrapArguments ) {
268+ resolvedValue = wrapCalcArguments ( resolvedValue )
269+ }
270+ return functionName + '(' + resolvedValue + ')'
271+ }
0 commit comments