Skip to content

Commit

Permalink
fix: #130 variables in nested comma-separated selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Bondar authored and Sergey Bondar committed Jan 6, 2022
1 parent f791dd2 commit d8cca8f
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 29 deletions.
56 changes: 27 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,40 +234,38 @@ module.exports = (options = {}) => {
});

if (doesRuleUseVariables) {
rulesThatHaveDeclarationsWithVariablesList.push(rule);
if (rule.type === "rule" && rule.selectors.length > 1) {
// Split out the rule into each comma separated selector piece
// We only need to split if it's actually a Rule with multiple selectors (comma separated)
// duplicate rules would be probably merged with cssnano (cannot be sure about nested)
rule.selectors.reverse().forEach(function(selector) {
var ruleClone = rule.cloneAfter();
ruleClone.selector = selector;

return ruleClone;
});

// Rules will be added to list in the next traverse
rule.remove();
} else {
rulesThatHaveDeclarationsWithVariablesList.push(rule);
}
}
});

rulesThatHaveDeclarationsWithVariablesList.forEach(function(rule) {
var rulesToWorkOn = [].concat(rule);
// Split out the rule into each comma separated selector piece
// We only need to split if it's actually a Rule with multiple selectors (comma separated)
if (rule.type === "rule" && rule.selectors.length > 1) {
// Reverse the selectors so that we can cloneAfter in the same comma separated order
rulesToWorkOn = rule.selectors.reverse().map(function(selector) {
var ruleClone = rule.cloneAfter();
ruleClone.selector = selector;

return ruleClone;
});

rule.remove();
}

// Resolve the declarations
rulesToWorkOn.forEach(function(ruleToWorkOn) {
ruleToWorkOn.nodes.slice(0).forEach(function(node) {
if (node.type === "decl") {
var decl = node;
resolveDecl(
decl,
map,
opts.preserve,
opts.preserveAtRulesOrder,
logResolveValueResult
);
}
});
rule.nodes.slice(0).forEach(function(node) {
if (node.type === "decl") {
var decl = node;
resolveDecl(
decl,
map,
opts.preserve,
opts.preserveAtRulesOrder,
logResolveValueResult
);
}
});
});

Expand Down
37 changes: 37 additions & 0 deletions test/fixtures/cascade-and-multiple-on-nested-rules.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
:root {
--some-width: 150px;
}
:root {
--White1: #FFF;
}

.a, .b {
width: var(--some-width);

.simple {
color: var(--White1);
}
}

.a {
width: var(--some-width);

a, label, &:after {
color: var(--White1);
}
}

/* postcss-nested double parent selector case */
.a, .b {
/* here variable */
width: var(--some-width);

/* and here another */
a, label {
background: var(--White1);

ol, ul {
width: var(--some-width);
}
}
}
87 changes: 87 additions & 0 deletions test/fixtures/cascade-and-multiple-on-nested-rules.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
.a {
width: 150px;

.simple {
color: #FFF
}
}

.b {
width: 150px;

.simple {
color: #FFF
}
}

.a {
width: 150px;

a {
color: #FFF
}

label {
color: #FFF
}

&:after {
color: #FFF
}
}

.a {
width: 150px;

a {
background: #FFF;

ol {
width: 150px;
}

ul {
width: 150px;
}
}

label {
background: #FFF;

ol {
width: 150px;
}

ul {
width: 150px;
}
}
}

.b {
width: 150px;

a {
background: #FFF;

ol {
width: 150px;
}

ul {
width: 150px;
}
}

label {
background: #FFF;

ol {
width: 150px;
}

ul {
width: 150px;
}
}
}
2 changes: 2 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ describe("postcss-css-variables", function() {

test("should cascade to nested rules", "cascade-on-nested-rules");

test("should cascade to nested multiple rules", "cascade-and-multiple-on-nested-rules");

test(
"should cascade with calc-expression to nested rules",
"cascade-with-calc-expression-on-nested-rules"
Expand Down

0 comments on commit d8cca8f

Please sign in to comment.