@@ -24,10 +24,6 @@ export function onCSSFunctionSRgb(node: FunctionNode) {
2424 return ;
2525 }
2626
27- if ( relevantNodes . length > 3 && ( ! nodes . slash || ! nodes . alpha ) ) {
28- return ;
29- }
30-
3127 // rename the Color function to `rgb`
3228 node . value = 'rgb' ;
3329
@@ -191,23 +187,6 @@ function isNumericNode(node: Node): node is WordNode {
191187 return ! ! unitAndValue . number ;
192188}
193189
194- function isNumericNodeNumber ( node ) : node is WordNode {
195- if ( ! node || node . type !== 'word' ) {
196- return false ;
197- }
198-
199- if ( ! canParseAsUnit ( node ) ) {
200- return false ;
201- }
202-
203- const unitAndValue = valueParser . unit ( node . value ) ;
204- if ( ! unitAndValue ) {
205- return false ;
206- }
207-
208- return ! ! unitAndValue . number && unitAndValue . unit === '' ;
209- }
210-
211190function isNumericNodeHueLike ( node : Node ) : node is WordNode {
212191 if ( ! node || node . type !== 'word' ) {
213192 return false ;
@@ -231,23 +210,6 @@ function isNumericNodeHueLike(node: Node): node is WordNode {
231210 ) ;
232211}
233212
234- function isNumericNodePercentage ( node : Node ) : node is WordNode {
235- if ( ! node || node . type !== 'word' ) {
236- return false ;
237- }
238-
239- if ( ! canParseAsUnit ( node ) ) {
240- return false ;
241- }
242-
243- const unitAndValue = valueParser . unit ( node . value ) ;
244- if ( ! unitAndValue ) {
245- return false ;
246- }
247-
248- return unitAndValue . unit === '%' ;
249- }
250-
251213function isNumericNodePercentageOrNumber ( node : Node ) : node is WordNode {
252214 if ( ! node || node . type !== 'word' ) {
253215 return false ;
@@ -289,11 +251,11 @@ type Lch = {
289251}
290252
291253function lchFunctionContents ( nodes ) : Lch | null {
292- if ( ! isNumericNodePercentage ( nodes [ 0 ] ) ) {
254+ if ( ! isNumericNodePercentageOrNumber ( nodes [ 0 ] ) ) {
293255 return null ;
294256 }
295257
296- if ( ! isNumericNodeNumber ( nodes [ 1 ] ) ) {
258+ if ( ! isNumericNodePercentageOrNumber ( nodes [ 1 ] ) ) {
297259 return null ;
298260 }
299261
@@ -323,6 +285,21 @@ function lchFunctionContents(nodes): Lch|null {
323285 out . alpha = nodes [ 4 ] ;
324286 }
325287
288+ if ( nodes . length > 3 && ( ! out . slash || ! out . alpha ) ) {
289+ return null ;
290+ }
291+
292+ // 0% = 0.0, 100% = 100.0
293+ if ( out . l . unit === '%' ) {
294+ out . l . unit = '' ;
295+ }
296+
297+ // 0% = 0, 100% = 150
298+ if ( out . c . unit === '%' ) {
299+ out . c . unit = '' ;
300+ out . c . number = ( ( parseFloat ( out . c . number ) / 100 ) * 150 ) . toFixed ( 10 ) ;
301+ }
302+
326303 return out ;
327304}
328305
@@ -338,15 +315,15 @@ type Lab = {
338315}
339316
340317function labFunctionContents ( nodes ) : Lab | null {
341- if ( ! isNumericNodePercentage ( nodes [ 0 ] ) ) {
318+ if ( ! isNumericNodePercentageOrNumber ( nodes [ 0 ] ) ) {
342319 return null ;
343320 }
344321
345- if ( ! isNumericNodeNumber ( nodes [ 1 ] ) ) {
322+ if ( ! isNumericNodePercentageOrNumber ( nodes [ 1 ] ) ) {
346323 return null ;
347324 }
348325
349- if ( ! isNumericNodeNumber ( nodes [ 2 ] ) ) {
326+ if ( ! isNumericNodePercentageOrNumber ( nodes [ 2 ] ) ) {
350327 return null ;
351328 }
352329
@@ -367,6 +344,27 @@ function labFunctionContents(nodes): Lab|null {
367344 out . alpha = nodes [ 4 ] ;
368345 }
369346
347+ if ( nodes . length > 3 && ( ! out . slash || ! out . alpha ) ) {
348+ return null ;
349+ }
350+
351+ // 0% = 0.0, 100% = 100.0
352+ if ( out . l . unit === '%' ) {
353+ out . l . unit = '' ;
354+ }
355+
356+ // -100% = -125, 100% = 125
357+ if ( out . a . unit === '%' ) {
358+ out . a . unit = '' ;
359+ out . a . number = ( ( parseFloat ( out . a . number ) / 100 ) * 125 ) . toFixed ( 10 ) ;
360+ }
361+
362+ // -100% = -125, 100% = 125
363+ if ( out . b . unit === '%' ) {
364+ out . b . unit = '' ;
365+ out . b . number = ( ( parseFloat ( out . b . number ) / 100 ) * 125 ) . toFixed ( 10 ) ;
366+ }
367+
370368 return out ;
371369}
372370
0 commit comments