@@ -49,6 +49,7 @@ function eachMapItemDependencyOfDecl(variablesUsedList, map, decl, cb) {
4949
5050 // Add the declaration to it
5151 mimicDecl = decl . clone ( ) ;
52+ console . log ( mimicDecl )
5253 ruleClone . append ( mimicDecl ) ;
5354
5455 var lastPseudoSelectorMatches = mapItem . parent . selector . match ( new RegExp ( isUnderScope . RE_PSEUDO_SELECTOR . source + '$' ) ) ;
@@ -71,8 +72,9 @@ function eachMapItemDependencyOfDecl(variablesUsedList, map, decl, cb) {
7172
7273// Resolve the decl with the computed value
7374// Also add in any media queries that change the value as necessary
74- function resolveDecl ( decl , map , /*optional*/ shouldPreserve , /*optional*/ logResolveValueResult ) {
75+ function resolveDecl ( decl , map , /*optional*/ shouldPreserve , /*optional*/ logResolveValueResult , /*optional*/ preserveAtRulesOrder ) {
7576 shouldPreserve = shouldPreserve || false ;
77+ preserveAtRulesOrder = preserveAtRulesOrder || false ;
7678
7779 // Make it chainable
7880 var _logResolveValueResult = function ( valueResults ) {
@@ -93,6 +95,7 @@ function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/logResol
9395 // Resolve the cascade dependencies
9496 // Now find any at-rule declarations that need to be added below each rule
9597 //console.log('resolveDecl 2');
98+ var previousAtRuleNode ;
9699 eachMapItemDependencyOfDecl ( valueResults . variablesUsed , map , decl , function ( mimicDecl , mapItem ) {
97100 var ruleClone = shallowCloneNode ( decl . parent ) ;
98101 var declClone = decl . clone ( ) ;
@@ -112,6 +115,7 @@ function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/logResol
112115
113116 // Add the rule to the atRule
114117 atRuleNode . append ( ruleClone ) ;
118+ //console.log(atRuleNode)
115119
116120
117121 // Since that atRuleNode can be nested in other atRules, we need to make the appropriate structure
@@ -129,14 +133,19 @@ function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/logResol
129133 currentAtRuleNode = currentAtRuleNode . parent ;
130134 }
131135
132- // Put the atRuleStructure after the declaration's rule
133- decl . parent . parent . insertAfter ( decl . parent , parentAtRuleNode ) ;
136+ // Put the first atRuleStructure after the declaration's rule,
137+ // and after that, put them right after the previous one
138+ decl . parent . parent . insertAfter ( preserveAtRulesOrder && previousAtRuleNode || decl . parent , parentAtRuleNode ) ;
139+
140+ // Save referance of previous atRuleStructure
141+ previousAtRuleNode = parentAtRuleNode
134142 }
135143 else {
136144 ruleClone . selector = mimicDecl . parent . selector ;
137145
138- // Put the atRuleStructure after the declaration's rule
139- decl . parent . parent . insertAfter ( decl . parent , ruleClone ) ;
146+ // Put the first atRuleStructure after the declaration's rule,
147+ // and after that, put them right after the previous one
148+ decl . parent . parent . insertAfter ( preserveAtRulesOrder && previousAtRuleNode || decl . parent , ruleClone ) ;
140149 }
141150 } ) ;
142151
0 commit comments