@@ -9,9 +9,9 @@ const isSpacing = (node) => node.type === "combinator" && node.value === " ";
99function normalizeNodeArray ( nodes ) {
1010 const array = [ ] ;
1111
12- nodes . forEach ( function ( x ) {
12+ nodes . forEach ( ( x ) => {
1313 if ( Array . isArray ( x ) ) {
14- normalizeNodeArray ( x ) . forEach ( function ( item ) {
14+ normalizeNodeArray ( x ) . forEach ( ( item ) => {
1515 array . push ( item ) ;
1616 } ) ;
1717 } else if ( x ) {
@@ -43,7 +43,7 @@ function localizeNode(rule, mode, localAliasMap) {
4343
4444 context . hasPureGlobals = false ;
4545
46- newNodes = node . nodes . map ( function ( n ) {
46+ newNodes = node . nodes . map ( ( n ) => {
4747 const nContext = {
4848 global : context . global ,
4949 lastWasSpacing : true ,
@@ -301,8 +301,8 @@ function isWordAFunctionArgument(wordNode, functionNode) {
301301 : false ;
302302}
303303
304- function localizeDeclValues ( localize , decl , context ) {
305- const valueNodes = valueParser ( decl . value ) ;
304+ function localizeDeclarationValues ( localize , declaration , context ) {
305+ const valueNodes = valueParser ( declaration . value ) ;
306306
307307 valueNodes . walk ( ( node , index , nodes ) => {
308308 const subContext = {
@@ -313,11 +313,12 @@ function localizeDeclValues(localize, decl, context) {
313313 } ;
314314 nodes [ index ] = localizeDeclNode ( node , subContext ) ;
315315 } ) ;
316- decl . value = valueNodes . toString ( ) ;
316+
317+ declaration . value = valueNodes . toString ( ) ;
317318}
318319
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 ) ;
321322
322323 if ( isAnimation ) {
323324 const validIdent = / ^ - ? [ _ a - z ] [ _ a - z 0 - 9 - ] * $ / i;
@@ -360,7 +361,7 @@ function localizeDecl(decl, context) {
360361 const didParseAnimationName = false ;
361362 let parsedAnimationKeywords = { } ;
362363 let stepsFunctionNode = null ;
363- const valueNodes = valueParser ( decl . value ) . walk ( ( node ) => {
364+ const valueNodes = valueParser ( declaration . value ) . walk ( ( node ) => {
364365 /* If div-token appeared (represents as comma ','), a possibility of an animation-keywords should be reflesh. */
365366 if ( node . type === "div" ) {
366367 parsedAnimationKeywords = { } ;
@@ -400,26 +401,24 @@ function localizeDecl(decl, context) {
400401 return localizeDeclNode ( node , subContext ) ;
401402 } ) ;
402403
403- decl . value = valueNodes . toString ( ) ;
404+ declaration . value = valueNodes . toString ( ) ;
404405
405406 return ;
406407 }
407408
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 ) ;
409410
410411 if ( isAnimationName ) {
411- return localizeDeclValues ( true , decl , context ) ;
412+ return localizeDeclarationValues ( true , declaration , context ) ;
412413 }
413414
414- const hasUrl = / u r l \( / i. test ( decl . value ) ;
415+ const hasUrl = / u r l \( / i. test ( declaration . value ) ;
415416
416417 if ( hasUrl ) {
417- return localizeDeclValues ( false , decl , context ) ;
418+ return localizeDeclarationValues ( false , declaration , context ) ;
418419 }
419420}
420421
421- const isVisited = Symbol ( "isVisited" ) ;
422-
423422module . exports = ( options = { } ) => {
424423 if ( options && options . mode ) {
425424 if (
@@ -442,102 +441,96 @@ module.exports = (options = {}) => {
442441 const localAliasMap = new Map ( ) ;
443442
444443 return {
445- Root ( root ) {
444+ OnceExit ( root ) {
446445 const { icssImports } = extractICSS ( root , false ) ;
447446
448447 Object . keys ( icssImports ) . forEach ( ( key ) => {
449448 Object . keys ( icssImports [ key ] ) . forEach ( ( prop ) => {
450449 localAliasMap . set ( prop , icssImports [ key ] [ prop ] ) ;
451450 } ) ;
452451 } ) ;
453- } ,
454- AtRule ( atRule ) {
455- if ( atRule [ isVisited ] ) {
456- return ;
457- }
458452
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+ ) ;
466461
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+ }
472479 }
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- }
483480
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 , {
495483 localAliasMap,
496484 options : options ,
497- global : globalMode ,
485+ global : globalKeyframes ,
498486 } ) ;
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+ } ) ;
518500
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+ }
520510
521- context . options = options ;
522- context . localAliasMap = localAliasMap ;
511+ const context = localizeNode ( rule , options . mode , localAliasMap ) ;
523512
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 ;
532515
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+ }
534524
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 ;
539526
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+ } ) ;
541534 } ,
542535 } ;
543536 } ,
0 commit comments