@@ -71,8 +71,9 @@ function eachMapItemDependencyOfDecl(variablesUsedList, map, decl, cb) {
7171
7272// Resolve the decl with the computed value
7373// Also add in any media queries that change the value as necessary
74- function resolveDecl ( decl , map , /*optional*/ shouldPreserve , /*optional*/ logResolveValueResult ) {
74+ function resolveDecl ( decl , map , /*optional*/ shouldPreserve , /*optional*/ logResolveValueResult , /*optional*/ preserveAtRulesOrder ) {
7575 shouldPreserve = shouldPreserve || false ;
76+ preserveAtRulesOrder = preserveAtRulesOrder !== undefined ? preserveAtRulesOrder : true ;
7677
7778 // Make it chainable
7879 var _logResolveValueResult = function ( valueResults ) {
@@ -93,6 +94,7 @@ function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/logResol
9394 // Resolve the cascade dependencies
9495 // Now find any at-rule declarations that need to be added below each rule
9596 //console.log('resolveDecl 2');
97+ var previousAtRuleNode ;
9698 eachMapItemDependencyOfDecl ( valueResults . variablesUsed , map , decl , function ( mimicDecl , mapItem ) {
9799 var ruleClone = shallowCloneNode ( decl . parent ) ;
98100 var declClone = decl . clone ( ) ;
@@ -112,6 +114,7 @@ function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/logResol
112114
113115 // Add the rule to the atRule
114116 atRuleNode . append ( ruleClone ) ;
117+ //console.log(atRuleNode)
115118
116119
117120 // Since that atRuleNode can be nested in other atRules, we need to make the appropriate structure
@@ -129,14 +132,19 @@ function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/logResol
129132 currentAtRuleNode = currentAtRuleNode . parent ;
130133 }
131134
132- // Put the atRuleStructure after the declaration's rule
133- decl . parent . parent . insertAfter ( decl . parent , parentAtRuleNode ) ;
135+ // Put the first atRuleStructure after the declaration's rule,
136+ // and after that, put them right after the previous one
137+ decl . parent . parent . insertAfter ( preserveAtRulesOrder && previousAtRuleNode || decl . parent , parentAtRuleNode ) ;
138+
139+ // Save referance of previous atRuleStructure
140+ previousAtRuleNode = parentAtRuleNode
134141 }
135142 else {
136143 ruleClone . selector = mimicDecl . parent . selector ;
137144
138- // Put the atRuleStructure after the declaration's rule
139- decl . parent . parent . insertAfter ( decl . parent , ruleClone ) ;
145+ // Put the first atRuleStructure after the declaration's rule,
146+ // and after that, put them right after the previous one
147+ decl . parent . parent . insertAfter ( preserveAtRulesOrder && previousAtRuleNode || decl . parent , ruleClone ) ;
140148 }
141149 } ) ;
142150
0 commit comments