Skip to content

Commit f879f4f

Browse files
Sergey BondarSergey Bondar
Sergey Bondar
authored and
Sergey Bondar
committed
fix: MadLittleMods#130 variables in nested comma-separated selectors
1 parent f791dd2 commit f879f4f

File tree

4 files changed

+153
-29
lines changed

4 files changed

+153
-29
lines changed

index.js

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -234,40 +234,38 @@ module.exports = (options = {}) => {
234234
});
235235

236236
if (doesRuleUseVariables) {
237-
rulesThatHaveDeclarationsWithVariablesList.push(rule);
237+
if (rule.type === "rule" && rule.selectors.length > 1) {
238+
// Split out the rule into each comma separated selector piece
239+
// We only need to split if it's actually a Rule with multiple selectors (comma separated)
240+
// duplicate rules would be probably merged with cssnano (cannot be sure about nested)
241+
rule.selectors.reverse().forEach(function(selector) {
242+
var ruleClone = rule.cloneAfter();
243+
ruleClone.selector = selector;
244+
245+
return ruleClone;
246+
});
247+
248+
// Rules will be added to list in the next traverse
249+
rule.remove();
250+
} else {
251+
rulesThatHaveDeclarationsWithVariablesList.push(rule);
252+
}
238253
}
239254
});
240255

241256
rulesThatHaveDeclarationsWithVariablesList.forEach(function(rule) {
242-
var rulesToWorkOn = [].concat(rule);
243-
// Split out the rule into each comma separated selector piece
244-
// We only need to split if it's actually a Rule with multiple selectors (comma separated)
245-
if (rule.type === "rule" && rule.selectors.length > 1) {
246-
// Reverse the selectors so that we can cloneAfter in the same comma separated order
247-
rulesToWorkOn = rule.selectors.reverse().map(function(selector) {
248-
var ruleClone = rule.cloneAfter();
249-
ruleClone.selector = selector;
250-
251-
return ruleClone;
252-
});
253-
254-
rule.remove();
255-
}
256-
257257
// Resolve the declarations
258-
rulesToWorkOn.forEach(function(ruleToWorkOn) {
259-
ruleToWorkOn.nodes.slice(0).forEach(function(node) {
260-
if (node.type === "decl") {
261-
var decl = node;
262-
resolveDecl(
263-
decl,
264-
map,
265-
opts.preserve,
266-
opts.preserveAtRulesOrder,
267-
logResolveValueResult
268-
);
269-
}
270-
});
258+
rule.nodes.slice(0).forEach(function(node) {
259+
if (node.type === "decl") {
260+
var decl = node;
261+
resolveDecl(
262+
decl,
263+
map,
264+
opts.preserve,
265+
opts.preserveAtRulesOrder,
266+
logResolveValueResult
267+
);
268+
}
271269
});
272270
});
273271

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
:root {
2+
--some-width: 150px;
3+
}
4+
:root {
5+
--White1: #FFF;
6+
}
7+
8+
.a, .b {
9+
width: var(--some-width);
10+
11+
.simple {
12+
color: var(--White1);
13+
}
14+
}
15+
16+
.a {
17+
width: var(--some-width);
18+
19+
a, label, &:after {
20+
color: var(--White1);
21+
}
22+
}
23+
24+
/* postcss-nested double parent selector case */
25+
.a, .b {
26+
/* here variable */
27+
width: var(--some-width);
28+
29+
/* and here another */
30+
a, label {
31+
background: var(--White1);
32+
33+
ol, ul {
34+
width: var(--some-width);
35+
}
36+
}
37+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
.a {
2+
width: 150px;
3+
4+
.simple {
5+
color: #FFF
6+
}
7+
}
8+
9+
.b {
10+
width: 150px;
11+
12+
.simple {
13+
color: #FFF
14+
}
15+
}
16+
17+
.a {
18+
width: 150px;
19+
20+
a {
21+
color: #FFF
22+
}
23+
24+
label {
25+
color: #FFF
26+
}
27+
28+
&:after {
29+
color: #FFF
30+
}
31+
}
32+
33+
.a {
34+
width: 150px;
35+
36+
a {
37+
background: #FFF;
38+
39+
ol {
40+
width: 150px;
41+
}
42+
43+
ul {
44+
width: 150px;
45+
}
46+
}
47+
48+
label {
49+
background: #FFF;
50+
51+
ol {
52+
width: 150px;
53+
}
54+
55+
ul {
56+
width: 150px;
57+
}
58+
}
59+
}
60+
61+
.b {
62+
width: 150px;
63+
64+
a {
65+
background: #FFF;
66+
67+
ol {
68+
width: 150px;
69+
}
70+
71+
ul {
72+
width: 150px;
73+
}
74+
}
75+
76+
label {
77+
background: #FFF;
78+
79+
ol {
80+
width: 150px;
81+
}
82+
83+
ul {
84+
width: 150px;
85+
}
86+
}
87+
}

test/test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ describe("postcss-css-variables", function() {
186186

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

189+
test("should cascade to nested multiple rules", "cascade-and-multiple-on-nested-rules");
190+
189191
test(
190192
"should cascade with calc-expression to nested rules",
191193
"cascade-with-calc-expression-on-nested-rules"

0 commit comments

Comments
 (0)