@@ -9,9 +9,9 @@ const isSpacing = (node) => node.type === "combinator" && node.value === " ";
9
9
function normalizeNodeArray ( nodes ) {
10
10
const array = [ ] ;
11
11
12
- nodes . forEach ( function ( x ) {
12
+ nodes . forEach ( ( x ) => {
13
13
if ( Array . isArray ( x ) ) {
14
- normalizeNodeArray ( x ) . forEach ( function ( item ) {
14
+ normalizeNodeArray ( x ) . forEach ( ( item ) => {
15
15
array . push ( item ) ;
16
16
} ) ;
17
17
} else if ( x ) {
@@ -43,7 +43,7 @@ function localizeNode(rule, mode, localAliasMap) {
43
43
44
44
context . hasPureGlobals = false ;
45
45
46
- newNodes = node . nodes . map ( function ( n ) {
46
+ newNodes = node . nodes . map ( ( n ) => {
47
47
const nContext = {
48
48
global : context . global ,
49
49
lastWasSpacing : true ,
@@ -301,8 +301,8 @@ function isWordAFunctionArgument(wordNode, functionNode) {
301
301
: false ;
302
302
}
303
303
304
- function localizeDeclValues ( localize , decl , context ) {
305
- const valueNodes = valueParser ( decl . value ) ;
304
+ function localizeDeclarationValues ( localize , declaration , context ) {
305
+ const valueNodes = valueParser ( declaration . value ) ;
306
306
307
307
valueNodes . walk ( ( node , index , nodes ) => {
308
308
const subContext = {
@@ -313,11 +313,12 @@ function localizeDeclValues(localize, decl, context) {
313
313
} ;
314
314
nodes [ index ] = localizeDeclNode ( node , subContext ) ;
315
315
} ) ;
316
- decl . value = valueNodes . toString ( ) ;
316
+
317
+ declaration . value = valueNodes . toString ( ) ;
317
318
}
318
319
319
- function localizeDecl ( decl , context ) {
320
- const isAnimation = / a n i m a t i o n $ / i. test ( decl . prop ) ;
320
+ function localizeDeclaration ( declaration , context ) {
321
+ const isAnimation = / a n i m a t i o n $ / i. test ( declaration . prop ) ;
321
322
322
323
if ( isAnimation ) {
323
324
const validIdent = / ^ - ? [ _ a - z ] [ _ a - z 0 - 9 - ] * $ / i;
@@ -360,7 +361,7 @@ function localizeDecl(decl, context) {
360
361
const didParseAnimationName = false ;
361
362
let parsedAnimationKeywords = { } ;
362
363
let stepsFunctionNode = null ;
363
- const valueNodes = valueParser ( decl . value ) . walk ( ( node ) => {
364
+ const valueNodes = valueParser ( declaration . value ) . walk ( ( node ) => {
364
365
/* If div-token appeared (represents as comma ','), a possibility of an animation-keywords should be reflesh. */
365
366
if ( node . type === "div" ) {
366
367
parsedAnimationKeywords = { } ;
@@ -400,26 +401,24 @@ function localizeDecl(decl, context) {
400
401
return localizeDeclNode ( node , subContext ) ;
401
402
} ) ;
402
403
403
- decl . value = valueNodes . toString ( ) ;
404
+ declaration . value = valueNodes . toString ( ) ;
404
405
405
406
return ;
406
407
}
407
408
408
- const isAnimationName = / a n i m a t i o n ( - n a m e ) ? $ / i. test ( decl . prop ) ;
409
+ const isAnimationName = / a n i m a t i o n ( - n a m e ) ? $ / i. test ( declaration . prop ) ;
409
410
410
411
if ( isAnimationName ) {
411
- return localizeDeclValues ( true , decl , context ) ;
412
+ return localizeDeclarationValues ( true , declaration , context ) ;
412
413
}
413
414
414
- const hasUrl = / u r l \( / i. test ( decl . value ) ;
415
+ const hasUrl = / u r l \( / i. test ( declaration . value ) ;
415
416
416
417
if ( hasUrl ) {
417
- return localizeDeclValues ( false , decl , context ) ;
418
+ return localizeDeclarationValues ( false , declaration , context ) ;
418
419
}
419
420
}
420
421
421
- const isVisited = Symbol ( "isVisited" ) ;
422
-
423
422
module . exports = ( options = { } ) => {
424
423
if ( options && options . mode ) {
425
424
if (
@@ -442,102 +441,96 @@ module.exports = (options = {}) => {
442
441
const localAliasMap = new Map ( ) ;
443
442
444
443
return {
445
- Root ( root ) {
444
+ OnceExit ( root ) {
446
445
const { icssImports } = extractICSS ( root , false ) ;
447
446
448
447
Object . keys ( icssImports ) . forEach ( ( key ) => {
449
448
Object . keys ( icssImports [ key ] ) . forEach ( ( prop ) => {
450
449
localAliasMap . set ( prop , icssImports [ key ] [ prop ] ) ;
451
450
} ) ;
452
451
} ) ;
453
- } ,
454
- AtRule ( atRule ) {
455
- if ( atRule [ isVisited ] ) {
456
- return ;
457
- }
458
452
459
- if ( / k e y f r a m e s $ / i. test ( atRule . name ) ) {
460
- const globalMatch = / ^ \s * : g l o b a l \s * \( ( .+ ) \) \s * $ / . exec (
461
- atRule . params
462
- ) ;
463
- const localMatch = / ^ \s * : l o c a l \s * \( ( .+ ) \) \s * $ / . exec ( atRule . params ) ;
464
-
465
- let globalKeyframes = globalMode ;
453
+ root . walkAtRules ( ( atRule ) => {
454
+ if ( / k e y f r a m e s $ / i. test ( atRule . name ) ) {
455
+ const globalMatch = / ^ \s * : g l o b a l \s * \( ( .+ ) \) \s * $ / . exec (
456
+ atRule . params
457
+ ) ;
458
+ const localMatch = / ^ \s * : l o c a l \s * \( ( .+ ) \) \s * $ / . exec (
459
+ atRule . params
460
+ ) ;
466
461
467
- if ( globalMatch ) {
468
- if ( pureMode ) {
469
- throw atRule . error (
470
- "@keyframes :global(...) is not allowed in pure mode"
471
- ) ;
462
+ let globalKeyframes = globalMode ;
463
+
464
+ if ( globalMatch ) {
465
+ if ( pureMode ) {
466
+ throw atRule . error (
467
+ "@keyframes :global(...) is not allowed in pure mode"
468
+ ) ;
469
+ }
470
+ atRule . params = globalMatch [ 1 ] ;
471
+ globalKeyframes = true ;
472
+ } else if ( localMatch ) {
473
+ atRule . params = localMatch [ 0 ] ;
474
+ globalKeyframes = false ;
475
+ } else if ( ! globalMode ) {
476
+ if ( atRule . params && ! localAliasMap . has ( atRule . params ) ) {
477
+ atRule . params = ":local(" + atRule . params + ")" ;
478
+ }
472
479
}
473
- atRule . params = globalMatch [ 1 ] ;
474
- globalKeyframes = true ;
475
- } else if ( localMatch ) {
476
- atRule . params = localMatch [ 0 ] ;
477
- globalKeyframes = false ;
478
- } else if ( ! globalMode ) {
479
- if ( atRule . params && ! localAliasMap . has ( atRule . params ) ) {
480
- atRule . params = ":local(" + atRule . params + ")" ;
481
- }
482
- }
483
480
484
- atRule . walkDecls ( function ( decl ) {
485
- localizeDecl ( decl , {
486
- localAliasMap,
487
- options : options ,
488
- global : globalKeyframes ,
489
- } ) ;
490
- } ) ;
491
- } else if ( atRule . nodes ) {
492
- atRule . nodes . forEach ( function ( decl ) {
493
- if ( decl . type === "decl" ) {
494
- localizeDecl ( decl , {
481
+ atRule . walkDecls ( ( declaration ) => {
482
+ localizeDeclaration ( declaration , {
495
483
localAliasMap,
496
484
options : options ,
497
- global : globalMode ,
485
+ global : globalKeyframes ,
498
486
} ) ;
499
- }
500
- } ) ;
501
- }
502
-
503
- atRule [ isVisited ] = true ;
504
- } ,
505
- Rule ( rule ) {
506
- if ( rule [ isVisited ] ) {
507
- return ;
508
- }
509
-
510
- if (
511
- rule . parent &&
512
- rule . parent . type === "atrule" &&
513
- / k e y f r a m e s $ / i. test ( rule . parent . name )
514
- ) {
515
- // ignore keyframe rules
516
- return ;
517
- }
487
+ } ) ;
488
+ } else if ( atRule . nodes ) {
489
+ atRule . nodes . forEach ( ( declaration ) => {
490
+ if ( declaration . type === "decl" ) {
491
+ localizeDeclaration ( declaration , {
492
+ localAliasMap,
493
+ options : options ,
494
+ global : globalMode ,
495
+ } ) ;
496
+ }
497
+ } ) ;
498
+ }
499
+ } ) ;
518
500
519
- const context = localizeNode ( rule , options . mode , localAliasMap ) ;
501
+ root . walkRules ( ( rule ) => {
502
+ if (
503
+ rule . parent &&
504
+ rule . parent . type === "atrule" &&
505
+ / k e y f r a m e s $ / i. test ( rule . parent . name )
506
+ ) {
507
+ // ignore keyframe rules
508
+ return ;
509
+ }
520
510
521
- context . options = options ;
522
- context . localAliasMap = localAliasMap ;
511
+ const context = localizeNode ( rule , options . mode , localAliasMap ) ;
523
512
524
- if ( pureMode && context . hasPureGlobals ) {
525
- throw rule . error (
526
- 'Selector "' +
527
- rule . selector +
528
- '" is not pure ' +
529
- "(pure selectors must contain at least one local class or id)"
530
- ) ;
531
- }
513
+ context . options = options ;
514
+ context . localAliasMap = localAliasMap ;
532
515
533
- rule . selector = context . selector ;
516
+ if ( pureMode && context . hasPureGlobals ) {
517
+ throw rule . error (
518
+ 'Selector "' +
519
+ rule . selector +
520
+ '" is not pure ' +
521
+ "(pure selectors must contain at least one local class or id)"
522
+ ) ;
523
+ }
534
524
535
- // Less-syntax mixins parse as rules with no nodes
536
- if ( rule . nodes ) {
537
- rule . nodes . forEach ( ( decl ) => localizeDecl ( decl , context ) ) ;
538
- }
525
+ rule . selector = context . selector ;
539
526
540
- rule [ isVisited ] = true ;
527
+ // Less-syntax mixins parse as rules with no nodes
528
+ if ( rule . nodes ) {
529
+ rule . nodes . forEach ( ( declaration ) =>
530
+ localizeDeclaration ( declaration , context )
531
+ ) ;
532
+ }
533
+ } ) ;
541
534
} ,
542
535
} ;
543
536
} ,
0 commit comments