@@ -49,6 +49,7 @@ function eachMapItemDependencyOfDecl(variablesUsedList, map, decl, cb) {
49
49
50
50
// Add the declaration to it
51
51
mimicDecl = decl . clone ( ) ;
52
+ console . log ( mimicDecl )
52
53
ruleClone . append ( mimicDecl ) ;
53
54
54
55
var lastPseudoSelectorMatches = mapItem . parent . selector . match ( new RegExp ( isUnderScope . RE_PSEUDO_SELECTOR . source + '$' ) ) ;
@@ -71,8 +72,9 @@ function eachMapItemDependencyOfDecl(variablesUsedList, map, decl, cb) {
71
72
72
73
// Resolve the decl with the computed value
73
74
// 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 ) {
75
76
shouldPreserve = shouldPreserve || false ;
77
+ preserveAtRulesOrder = preserveAtRulesOrder || false ;
76
78
77
79
// Make it chainable
78
80
var _logResolveValueResult = function ( valueResults ) {
@@ -93,6 +95,7 @@ function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/logResol
93
95
// Resolve the cascade dependencies
94
96
// Now find any at-rule declarations that need to be added below each rule
95
97
//console.log('resolveDecl 2');
98
+ var previousAtRuleNode ;
96
99
eachMapItemDependencyOfDecl ( valueResults . variablesUsed , map , decl , function ( mimicDecl , mapItem ) {
97
100
var ruleClone = shallowCloneNode ( decl . parent ) ;
98
101
var declClone = decl . clone ( ) ;
@@ -112,6 +115,7 @@ function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/logResol
112
115
113
116
// Add the rule to the atRule
114
117
atRuleNode . append ( ruleClone ) ;
118
+ //console.log(atRuleNode)
115
119
116
120
117
121
// 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
129
133
currentAtRuleNode = currentAtRuleNode . parent ;
130
134
}
131
135
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
134
142
}
135
143
else {
136
144
ruleClone . selector = mimicDecl . parent . selector ;
137
145
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 ) ;
140
149
}
141
150
} ) ;
142
151
0 commit comments