Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#130 nested edge case #131

Merged
merged 1 commit into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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