Skip to content

Commit 9468846

Browse files
committed
Declaration fixes for root specifity
1 parent 3e97c9b commit 9468846

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

lib/is-under-scope.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var escapeStringRegexp = require('escape-string-regexp');
22

33
var isPieceAlwaysAncestorSelector = require('./is-piece-always-ancestor-selector');
44
var generateDirectDescendantPiecesFromSelector = require('./generate-direct-descendant-pieces-from-selector');
5+
var isPieceRootSpecificSelector = require('./is-piece-root-specific-selector')
56

67
var RE_AT_RULE_SCOPE_PIECE = (/^@.*/);
78
// This will match pseudo selectors that have a base part
@@ -51,7 +52,12 @@ function getScopeMatchResults(nodeScopeList, scopeNodeScopeList) {
5152
//
5253
// Or the node scope piece could be an always-ancestor selector itself
5354
// And we only want the first occurence so we can keep matching future scope pieces
54-
if(isPieceAlwaysAncestorSelector(scopePiece) || isPieceAlwaysAncestorSelector(nodeScopePiece)) {
55+
if(
56+
!isPieceRootSpecificSelector(scopePiece) && !isPieceRootSpecificSelector(nodeScopePiece) && (
57+
isPieceAlwaysAncestorSelector(scopePiece) ||
58+
isPieceAlwaysAncestorSelector(nodeScopePiece)
59+
)
60+
) {
5561
foundIndex = overallIndex;
5662

5763
break;

lib/resolve-decl.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ function eachMapItemDependencyOfDecl(variablesUsedList, map, decl, cb) {
2828
var innerMostAtRuleSelector = varDeclScopeList[0].slice(-1)[0];
2929
var nodeToSpliceParentOnto = findNodeAncestorWithSelector(innerMostAtRuleSelector, decl.parent);
3030

31+
// attept to add a specific root scope if necessary
32+
if (mapItem.isRootSpecific) {
33+
const varDeclRootRule = mapItem.parent
34+
const nodeToSpliceParentOnto = findNodeAncestorWithSelector(mapItem.parent.selector, decl.parent)
35+
36+
mimicDecl = cloneSpliceParentOntoNodeWhen(decl, varDeclRootRule, (ancestor) => ancestor === nodeToSpliceParentOnto)
37+
}
38+
3139
// Splice on where the selector starts matching the selector inside at-rule
3240
// See: `test/fixtures/cascade-on-nested-rules.css`
3341
var varDeclAtRule = mapItem.parent.parent;
@@ -41,6 +49,12 @@ function eachMapItemDependencyOfDecl(variablesUsedList, map, decl, cb) {
4149
//console.log(generateScopeList(mapItem.parent, true));
4250
//console.log('amd isNodeUnderScope', isNodeUnderScope(mimicDecl.parent, mapItem.parent), mapItem.decl.value);
4351
}
52+
else if (mapItem.isRootSpecific) {
53+
const varDeclRootRule = mapItem.parent
54+
const nodeToSpliceParentOnto = findNodeAncestorWithSelector(mapItem.parent.selector, decl.parent)
55+
56+
mimicDecl = cloneSpliceParentOntoNodeWhen(decl, varDeclRootRule, (ancestor) => ancestor === nodeToSpliceParentOnto)
57+
}
4458
// TODO: use regex from `isUnderScope`
4559
else if(isUnderScope.RE_PSEUDO_SELECTOR.test(mapItem.parent.selector)) {
4660
// Create a detached clone
@@ -115,6 +129,9 @@ function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/preserve
115129
// Add the rule to the atRule
116130
atRuleNode.append(ruleClone);
117131

132+
if (mapItem.isRootSpecific) {
133+
ruleClone.selector = [mapItem.parent.selector, ruleClone.selector].filter(item => Boolean(item)).join(' ');
134+
}
118135

119136
// Since that atRuleNode can be nested in other atRules, we need to make the appropriate structure
120137
var parentAtRuleNode = atRuleNode;
@@ -137,8 +154,13 @@ function resolveDecl(decl, map, /*optional*/shouldPreserve, /*optional*/preserve
137154

138155
// Save referance of previous atRuleStructure
139156
previousAtRuleNode = parentAtRuleNode
140-
}
141-
else {
157+
} else if (mapItem.isRootSpecific) {
158+
ruleClone.selector = [mapItem.parent.selector, mimicDecl.parent.selector].filter(item => Boolean(item)).join(' ');
159+
160+
// Put the first atRuleStructure after the declaration's rule,
161+
// and after that, put them right after the previous one
162+
decl.parent.parent.insertAfter(preserveAtRulesOrder && previousAtRuleNode || decl.parent, ruleClone);
163+
} else {
142164
ruleClone.selector = mimicDecl.parent.selector;
143165

144166
// Put the first atRuleStructure after the declaration's rule,

0 commit comments

Comments
 (0)