@@ -272,9 +272,10 @@ function resolveCalcs (
272272}
273273
274274function evaluateCalc ( expression : string ) : string | null {
275- if ( expression . includes ( '%' ) ) return null
276- const hasPx = / (?: ^ | [ ^ \w . - ] ) [ + - ] ? (?: \d * \. ) ? \d + p x \b / . test ( expression )
277- const normalized = expression . replace ( / ( [ + - ] ? (?: \d * \. ) ? \d + ) p x \b / g, '$1' )
275+ const unit = getCalcUnit ( expression )
276+ if ( unit === false ) return null
277+
278+ const normalized = expression . replace ( / ( [ + - ] ? (?: \d * \. ) ? \d + ) ( p x \b | % ) / g, ( _match , number : string ) => number )
278279 if ( ! / ^ [ 0 - 9 + \- * / ( ) . \s ] + $ / . test ( normalized ) ) return null
279280
280281 let index = 0
@@ -353,10 +354,25 @@ function evaluateCalc (expression: string): string | null {
353354 skipWhitespace ( )
354355
355356 return result != null && index === normalized . length && Number . isFinite ( result )
356- ? hasPx ? `${ result } px ` : String ( result )
357+ ? unit ? `${ roundCalc ( result ) } ${ unit } ` : String ( roundCalc ( result ) )
357358 : null
358359}
359360
361+ function getCalcUnit ( expression : string ) : 'px' | '%' | '' | false {
362+ const units = new Set < string > ( )
363+ expression . replace ( / (?: ^ | [ ^ \w . - ] ) [ + - ] ? (?: \d * \. ) ? \d + ( p x \b | % ) / g, ( _match , unit : string ) => {
364+ units . add ( unit === '%' ? '%' : 'px' )
365+ return ''
366+ } )
367+
368+ if ( units . size > 1 ) return false
369+ return ( units . values ( ) . next ( ) . value ?? '' ) as 'px' | '%' | ''
370+ }
371+
372+ function roundCalc ( value : number ) : number {
373+ return Math . round ( value * 1000000 ) / 1000000
374+ }
375+
360376function findMatchingParen ( input : string , openIndex : number ) : number {
361377 let depth = 0
362378 for ( let index = openIndex ; index < input . length ; index ++ ) {
0 commit comments