Fixes #751 - stringifying CSS variables.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Sun, 29 May 2016 15:09:17 +0000 (17:09 +0200)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Tue, 31 May 2016 08:47:35 +0000 (10:47 +0200)
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.

History.md
lib/stringifier/helpers.js
test/source-map-test.js
test/tokenizer/tokenizer-test.js

index 1e5fc29..0cd9248 100644 (file)
@@ -3,6 +3,11 @@
 
 * Requires Node.js 4.0+ to run.
 
+[3.4.14 / 2016-xx-xx](https://github.com/jakubpawlowicz/clean-css/compare/v3.4.13...3.4)
+==================
+
+* Fixed issue [#751](https://github.com/jakubpawlowicz/clean-css/issues/751) - stringifying CSS variables.
+
 [3.4.13 / 2016-05-23](https://github.com/jakubpawlowicz/clean-css/compare/v3.4.12...v3.4.13)
 ==================
 
index 3f89a95..5e9f117 100644 (file)
@@ -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);
index 1fb5003..b1ba4a5 100644 (file)
@@ -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({
index a43e6b9..735e3eb 100644 (file)
@@ -247,6 +247,16 @@ vows.describe(tokenize)
           ]
         ]
       ],
+      'variable declarations': [
+        ':root{--color:var(--otherColor)}',
+        [
+          [
+            'selector',
+            [[':root']],
+            [[['--color'], ['var(--otherColor)']]]
+          ]
+        ]
+      ],
       '! important': [
         'a{color:red! important}',
         [