Skip to content

Commit

Permalink
Fixes clean-css#751 - stringifying CSS variables.
Browse files Browse the repository at this point in the history
CSS variables come in two variants

- defined as a block which can be applied using @apply keyword (used
  in Polymer);
- defined as a simple value or another variable reference.

The second way was broken when source maps were built as stringifying
code was always expecting a block.
  • Loading branch information
jakubpawlowicz authored and silverwind committed Sep 25, 2016
1 parent c84f3ca commit dcb01d0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
5 changes: 5 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

* Requires Node.js 4.0+ to run.

[3.4.14 / 2016-xx-xx](https:/jakubpawlowicz/clean-css/compare/v3.4.13...3.4)
==================

* Fixed issue [#751](https:/jakubpawlowicz/clean-css/issues/751) - stringifying CSS variables.

[3.4.13 / 2016-05-23](https:/jakubpawlowicz/clean-css/compare/v3.4.12...v3.4.13)
==================

Expand Down
3 changes: 2 additions & 1 deletion lib/stringifier/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ function value(tokens, position, isLast, context) {
var store = context.store;
var token = tokens[position];
var isVariableDeclaration = token[0][0].indexOf('--') === 0;
var isBlockVariable = isVariableDeclaration && Array.isArray(token[1][0]);

if (isVariableDeclaration && atRulesOrProperties(token[1])) {
if (isVariableDeclaration && isBlockVariable && atRulesOrProperties(token[1])) {
store('{', context);
body(token[1], context);
store('};', context);
Expand Down
16 changes: 16 additions & 0 deletions test/source-map-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ vows.describe('source-map')
'gets right output': function (minified) {
assert.equal(minified.styles, '@font-face{font-family:si}a{font-family:si!important}');
}
},
'variables': {
'topic': function () {
return new CleanCSS({ sourceMap: true }).minify(':root{--color:red}');
},
'gets right output': function (minified) {
assert.equal(minified.styles, ':root{--color:red}');
}
},
'variables reused': {
'topic': function () {
return new CleanCSS({ sourceMap: true }).minify(':root{--color:var(--otherColor)}');
},
'gets right output': function (minified) {
assert.equal(minified.styles, ':root{--color:var(--otherColor)}');
}
}
})
.addBatch({
Expand Down
10 changes: 10 additions & 0 deletions test/tokenizer/tokenizer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,16 @@ vows.describe(tokenize)
]
]
],
'variable declarations': [
':root{--color:var(--otherColor)}',
[
[
'selector',
[[':root']],
[[['--color'], ['var(--otherColor)']]]
]
]
],
'! important': [
'a{color:red! important}',
[
Expand Down

0 comments on commit dcb01d0

Please sign in to comment.