Skip to content

Commit 5af0790

Browse files
pvandeMadLittleMods
authored andcommitted
Allow AtRules containing properties to have their variables expanded
1 parent 47677f2 commit 5af0790

6 files changed

+49
-3
lines changed

index.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,10 @@ module.exports = postcss.plugin('postcss-css-variables', function(options) {
218218

219219
// Collect all the rules that have declarations that use variables
220220
var rulesThatHaveDeclarationsWithVariablesList = [];
221-
css.walkRules(function(rule) {
221+
css.walk(function(rule) {
222+
// We're only interested in Containers with children.
223+
if (rule.nodes === undefined) return;
224+
222225
var doesRuleUseVariables = rule.nodes.some(function(node) {
223226
if(node.type === 'decl') {
224227
var decl = node;
@@ -240,8 +243,8 @@ module.exports = postcss.plugin('postcss-css-variables', function(options) {
240243
rulesThatHaveDeclarationsWithVariablesList.forEach(function(rule) {
241244
var rulesToWorkOn = [].concat(rule);
242245
// Split out the rule into each comma separated selector piece
243-
// We only need to split if is actually comma separted(selectors > 1)
244-
if(rule.selectors.length > 1) {
246+
// We only need to split if it's actually a Rule with multiple selectors
247+
if(rule.type == 'rule' && rule.selectors.length > 1) {
245248
// Reverse the selectors so that we can cloneAfter in the same comma separated order
246249
rulesToWorkOn = rule.selectors.reverse().map(function(selector) {
247250
var ruleClone = rule.cloneAfter();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
:root {
2+
--font-name: 'my-font-family-name';
3+
}
4+
5+
@font-face {
6+
font-family: var(--font-name);
7+
src: url('myfont.woff2') format('woff2');
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@font-face {
2+
font-family: 'my-font-family-name';
3+
src: url('myfont.woff2') format('woff2');
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
:root {
2+
--color: red;
3+
}
4+
5+
/*
6+
Prince XML at-rules.
7+
https://www.princexml.com/doc/11/at-rules/
8+
*/
9+
@page {
10+
@footnote {
11+
background-color: var(--color);
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
Prince XML at-rules.
3+
https://www.princexml.com/doc/11/at-rules/
4+
*/
5+
@page {
6+
@footnote {
7+
background-color: red;
8+
}
9+
}

test/test.js

+9
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ describe("postcss-css-variables", function() {
175175
{ preserveAtRulesOrder: true }
176176
);
177177

178+
test(
179+
"should work with at-rules containing properties",
180+
"at-rules-containing-properties"
181+
);
182+
test(
183+
"should work with nested at-rules containing properties",
184+
"nested-at-rules-containing-properties"
185+
);
186+
178187
test("should cascade to nested rules", "cascade-on-nested-rules");
179188

180189
test(

0 commit comments

Comments
 (0)