11var resolveValue = require ( './resolve-value' ) ;
22var generateScopeList = require ( './generate-scope-list' ) ;
33var gatherVariableDependencies = require ( './gather-variable-dependencies' ) ;
4+
5+ var isUnderScope = require ( './is-under-scope' ) ;
46var isNodeUnderScope = require ( './is-node-under-scope' ) ;
57
68var findNodeAncestorWithSelector = require ( './find-node-ancestor-with-selector' ) ;
@@ -24,40 +26,50 @@ function resolveDecl(decl, map, /*optional*/logResolveValueResult) {
2426
2527
2628
27- // Resolve the cascade
29+ // Resolve the cascade dependencies
2830 // Now find any at-rule declarations that need to be added below each rule
29- eachMapItemUnderAtRuleUsedByVariable ( valueResults . variablesUsed , map , decl , function ( mimicDecl , mapItem ) {
30- // Create the clean atRule for which we place the declaration under
31- var atRuleNode = mapItem . parent . parent . clone ( ) . removeAll ( ) ;
32-
31+ eachMapItemDependencyOfDecl ( valueResults . variablesUsed , map , decl , function ( mimicDecl , mapItem ) {
3332 var ruleClone = decl . parent . clone ( ) . removeAll ( ) ;
3433 var declClone = decl . clone ( ) ;
35- declClone . value = _logResolveValueResult ( resolveValue ( mimicDecl , map ) ) . value ;
34+ // No mangle resolve
35+ declClone . value = _logResolveValueResult ( resolveValue ( mimicDecl , map , true ) ) . value ;
3636
3737 // Add the declaration to our new rule
3838 ruleClone . append ( declClone ) ;
39- // Add the rule to the atRule
40- atRuleNode . append ( ruleClone ) ;
4139
4240
43- // Since that atRuleNode can be nested in other atRules, we need to make the appropriate structure
44- var parentAtRuleNode = atRuleNode ;
45- var currentAtRuleNode = mapItem . parent . parent ;
46- while ( currentAtRuleNode . parent . type === 'atrule' ) {
47- // Create a new clean clone of that at rule to nest under
48- var newParentAtRuleNode = currentAtRuleNode . parent . clone ( ) . removeAll ( ) ;
41+ if ( mapItem . isUnderAtRule ) {
42+ // Create the clean atRule for which we place the declaration under
43+ var atRuleNode = mapItem . parent . parent . clone ( ) . removeAll ( ) ;
44+
45+ // Add the rule to the atRule
46+ atRuleNode . append ( ruleClone ) ;
47+
4948
50- // Append the old parent
51- newParentAtRuleNode . append ( parentAtRuleNode ) ;
52- // Then set the new one as the current for next iteration
53- parentAtRuleNode = newParentAtRuleNode ;
49+ // Since that atRuleNode can be nested in other atRules, we need to make the appropriate structure
50+ var parentAtRuleNode = atRuleNode ;
51+ var currentAtRuleNode = mapItem . parent . parent ;
52+ while ( currentAtRuleNode . parent . type === 'atrule' ) {
53+ // Create a new clean clone of that at rule to nest under
54+ var newParentAtRuleNode = currentAtRuleNode . parent . clone ( ) . removeAll ( ) ;
55+
56+ // Append the old parent
57+ newParentAtRuleNode . append ( parentAtRuleNode ) ;
58+ // Then set the new one as the current for next iteration
59+ parentAtRuleNode = newParentAtRuleNode ;
60+
61+ currentAtRuleNode = currentAtRuleNode . parent ;
62+ }
5463
55- currentAtRuleNode = currentAtRuleNode . parent ;
64+ // Put the atRuleStructure after the declaration's rule
65+ decl . parent . parent . insertAfter ( decl . parent , parentAtRuleNode ) ;
5666 }
67+ else {
68+ ruleClone . selector = mimicDecl . parent . selector ;
5769
58- // Put the atRuleStructure after the declaration's rule
59- decl . parent . parent . insertAfter ( decl . parent , parentAtRuleNode ) ;
60-
70+ // Put the atRuleStructure after the declaration's rule
71+ decl . parent . parent . insertAfter ( decl . parent , ruleClone ) ;
72+ }
6173 } ) ;
6274
6375
@@ -72,13 +84,15 @@ function resolveDecl(decl, map, /*optional*/logResolveValueResult) {
7284}
7385
7486
75- function eachMapItemUnderAtRuleUsedByVariable ( variablesUsedList , map , decl , cb ) {
87+ function eachMapItemDependencyOfDecl ( variablesUsedList , map , decl , cb ) {
7688 // Now find any at-rule declarations that pertains to each rule
7789 // Loop through the variables used
7890 variablesUsedList . forEach ( function ( variableUsedName ) {
7991
8092 // Find anything in the map that corresponds to that variable
8193 gatherVariableDependencies ( variablesUsedList , map ) . deps . forEach ( function ( mapItem ) {
94+
95+ var mimicDecl ;
8296 if ( mapItem . isUnderAtRule ) {
8397
8498 // Get the inner-most selector of the at-rule scope variable declaration we are matching
@@ -90,7 +104,7 @@ function eachMapItemUnderAtRuleUsedByVariable(variablesUsedList, map, decl, cb)
90104 // Splice on where the selector starts matching the selector inside at-rule
91105 // See: `test/fixtures/cascade-on-nested-rules.css`
92106 var varDeclAtRule = mapItem . parent . parent ;
93- var mimicDecl = cloneSpliceParentOntoNodeWhen ( decl , varDeclAtRule , function ( ancestor ) {
107+ mimicDecl = cloneSpliceParentOntoNodeWhen ( decl , varDeclAtRule , function ( ancestor ) {
94108 return ancestor === nodeToSpliceParentOnto ;
95109 } ) ;
96110
@@ -99,13 +113,27 @@ function eachMapItemUnderAtRuleUsedByVariable(variablesUsedList, map, decl, cb)
99113 //console.log('amd', generateScopeList(mimicDecl.parent, true));
100114 //console.log(generateScopeList(mapItem.parent, true));
101115 //console.log('amd isNodeUnderScope', isNodeUnderScope(mimicDecl.parent, mapItem.parent), mapItem.decl.value);
116+ }
117+ // TODO: use regex from `isUnderScope`
118+ else if ( isUnderScope . RE_PSEUDO_SELECTOR . test ( mapItem . parent . selector ) ) {
119+ // Create a detached clone
120+ var ruleClone = decl . parent . clone ( ) . removeAll ( ) ;
121+ ruleClone . parent = decl . parent . parent ;
122+
123+ // Add the declaration to it
124+ mimicDecl = decl . clone ( ) ;
125+ ruleClone . append ( mimicDecl ) ;
126+
127+ var lastPseudoSelectorMatches = mapItem . parent . selector . match ( new RegExp ( isUnderScope . RE_PSEUDO_SELECTOR . source + '$' ) ) ;
128+ var lastPseudoSelector = lastPseudoSelectorMatches ? lastPseudoSelectorMatches [ 2 ] : '' ;
129+
130+ ruleClone . selector += lastPseudoSelector ;
131+ }
102132
103- // If it is under the proper scope,
104- // we need to check because we are iterating over all map entries that are `isUnderAtRule`
105- // Then lets create the new rules
106- if ( isNodeUnderScope ( mimicDecl . parent , mapItem . parent ) ) {
107- cb ( mimicDecl , mapItem ) ;
108- }
133+ // If it is under the proper scope,
134+ // we need to check because we are iterating over all map entries
135+ if ( mimicDecl && isNodeUnderScope ( mimicDecl , mapItem . parent , true ) ) {
136+ cb ( mimicDecl , mapItem ) ;
109137 }
110138 } ) ;
111139 } ) ;
0 commit comments