Skip to content

Commit b18bada

Browse files
committed
Fix when no var() with single rule clean up
1 parent 7b7723e commit b18bada

File tree

7 files changed

+32
-16
lines changed

7 files changed

+32
-16
lines changed

CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11

2+
# v0.5.1 - 2015-10-24
3+
4+
- Fix postcss/postcss#611 where we were trying to remove the root node on clean up
5+
- Improved test setup
6+
27
# v0.5.0 - 2015-9-12
38

49
- Upgrade to PostCSS v5. Fix #20
@@ -40,7 +45,7 @@
4045

4146
# v0.3.3 - 2015-5-11
4247

43-
- Add support for last piece of combinator chain in selector resolution matching.
48+
- Add support for last piece of combinator chain in selector resolution matching.
4449
- `.foo + .bar` can match variables declared in `.bar`
4550

4651
# v0.3.1 - 2015-5-5
@@ -65,4 +70,4 @@
6570

6671
# v0.1.0 - 2015-4-29
6772

68-
- First release
73+
- First release

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ CSS variables or CSS Custom Properties limited subset polyfill/shim.
88

99
We strive for the most complete transformation but we/no plugin can achieve true complete parity according to the [specification](http://dev.w3.org/csswg/css-variables/) because of the DOM cascade unknowns.
1010

11-
## Latest Version: v0.5.0
1211
### [Changelog](https://github.com/MadLittleMods/postcss-css-variables/blob/master/CHANGELOG.md)
1312

1413
### Install
@@ -281,7 +280,7 @@ A custom property can be declared multiple times.
281280
### `preserve` (default: `false`)
282281

283282
Allows you to preserve custom properties & var() usage in output.
284-
Allowed values:
283+
Allowed values:
285284
- `false`: Removes `--var` declarations and replaces `var()` with their resolved/computed values.
286285
- `true`: Keeps `var()` declarations in the output and has the computed value as a fallback declaration. Also keeps computed `--var` declarations
287286
- `'computed'`: Keeps computed `--var` declarations in the output. Handy to make them available to your JavaScript.
@@ -292,7 +291,7 @@ Define an object map of variables in JavaScript that will be declared at the `:r
292291

293292
Can be a simple key-value pair or an object with a `value` property and an optional `isImportant` bool property
294293

295-
The object keys are automatically prefixed with `--` (according to CSS custom property syntax) if you do not provide it.
294+
The object keys are automatically prefixed with `--` (according to CSS custom property syntax) if you do not provide it.
296295

297296

298297
```
@@ -337,4 +336,4 @@ Run once:
337336

338337
Run whenever you want to test:
339338

340-
`npm run test`
339+
`npm run test`

index.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,23 @@ function eachCssVariableDeclaration(css, cb) {
3434

3535

3636

37-
function cleanUpNode(currentNodeToRemove) {
38-
// If we removed all of the declarations in the rule(making it empty), then just remove it
39-
var currentNodeToPossiblyCleanUp = currentNodeToRemove;
40-
while(currentNodeToPossiblyCleanUp && currentNodeToPossiblyCleanUp.nodes.length <= 0) {
41-
var nodeToRemove = currentNodeToPossiblyCleanUp;
42-
// Get a reference to it before we remove and lose reference to the child after removing it
43-
currentNodeToPossiblyCleanUp = currentNodeToPossiblyCleanUp.parent;
44-
45-
nodeToRemove.remove();
37+
function cleanUpNode(node) {
38+
// If we removed all of the declarations in the rule(making it empty),
39+
// then just remove it
40+
var nodeToPossiblyCleanUp = node;
41+
while(nodeToPossiblyCleanUp && nodeToPossiblyCleanUp.nodes.length <= 0) {
42+
var nodeToRemove = nodeToPossiblyCleanUp.type !== 'root' ? nodeToPossiblyCleanUp : null;
43+
44+
if(nodeToRemove) {
45+
// Get a reference to it before we remove
46+
// and lose reference to the child after removing it
47+
nodeToPossiblyCleanUp = nodeToRemove.parent;
48+
49+
nodeToRemove.remove();
50+
}
51+
else {
52+
nodeToPossiblyCleanUp = null;
53+
}
4654
}
4755
}
4856

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:root {
2+
--foo-width: 150px;
3+
}

test/fixtures/no-var-func-just-root.expected.css

Whitespace-only changes.

test/fixtures/no-var-func.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55

66
.box {
77
width: 100px;
8-
}
8+
}

test/test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ describe('postcss-css-variables', function() {
6161

6262
// Just make sure it doesn't mangle anything
6363
test('should work when there are no var() functions to consume declarations', 'no-var-func');
64+
test('should work when there are no var() functions(just `:root`) to consume declarations', 'no-var-func-just-root');
6465
test('should work when no variable name passed to `var()`', 'empty-var-func');
6566

6667

0 commit comments

Comments
 (0)