Added stripping special comments (all or all but first one) to CLI and API.
authorGoalSmashers <jakub@goalsmashers.com>
Sun, 14 Oct 2012 20:16:12 +0000 (21:16 +0100)
committerGoalSmashers <jakub@goalsmashers.com>
Sun, 14 Oct 2012 20:17:01 +0000 (21:17 +0100)
* Version 0.7.0.

History.md
bin/cleancss
lib/clean.js
package.json
test/binary-test.js
test/unit-test.js

index c99bcfd..81f845c 100644 (file)
@@ -1,7 +1,13 @@
+0.7.0 / 2012-10-14
+==================
+
+ * Added stripping special comments to CLI (--s0 and --s1 options).
+ * Added stripping special comments to programatic interface (keepSpecialComments option).
+
 0.6.0 / 2012-08-05
 ==================
 
- * Full Windows support with tests (./test.bat)
+ * Full Windows support with tests (./test.bat).
 
 0.5.0 / 2012-08-02
 ==================
index 682f68b..c04da9b 100755 (executable)
@@ -16,6 +16,8 @@ var fromStdin = !process.env['__DIRECT__'] && process.stdin.readable;
 if (argv.o) options.target = argv.o;
 if (argv._) options.source = argv._[0];
 if (argv.e) cleanOptions.removeEmpty = true;
+if (argv.s1) cleanOptions.keepSpecialComments = 1;
+if (argv.s0) cleanOptions.keepSpecialComments = 0;
 
 if (argv.v) {
   var packageConfig = fs.readFileSync(path.join(path.dirname(fs.realpathSync(process.argv[1])), '../package.json'));
@@ -24,7 +26,11 @@ if (argv.v) {
 }
 
 if (argv.h || argv.help || (!fromStdin && argv._.length == 0)) {
-  util.print('usage: cleancss [-e] -o <output-file> <input-file>\n');
+  util.puts("usage: cleancss [options] -o <output-file> <input-file>\n");
+  util.puts("options:");
+  util.puts("  -e\tRemove empty declarations (e.g. a{})");
+  util.puts("  --s0\tRemove all special comments (i.e. /*! special comment */)");
+  util.puts("  --s1\tRemove all special comments but the first one");
   return 0;
 }
 
index 3a3bfae..4819c3b 100644 (file)
@@ -22,6 +22,13 @@ var CleanCSS = {
 
     options = options || {};
 
+    // * - leave all important comments
+    // 1 - leave first important comment only
+    // 0 - strip all important comments
+    options.keepSpecialComments = 'keepSpecialComments' in options ?
+      options.keepSpecialComments :
+      '*';
+
     // replace function
     if (options.debug) {
       var originalReplace = replace;
@@ -99,11 +106,26 @@ var CleanCSS = {
     replace(/\} /g, '}') // whitespace after definition
 
     // Get the special comments, content content, and spaces inside calc back
+    var specialCommentsCount = context.specialComments.length;
+
     replace(/calc\([^\}]+\}/g, function(match) {
       return match.replace(/\+/g, ' + ');
     });
-    replace(/__CSSCOMMENT__/g, function() { return context.specialComments.shift(); });
-    replace(/__CSSCONTENT__/g, function() { return context.contentBlocks.shift(); });
+    replace(/__CSSCOMMENT__/g, function(i) {
+      switch (options.keepSpecialComments) {
+        case '*':
+          return context.specialComments.shift();
+        case 1:
+          return context.specialComments.length == specialCommentsCount ?
+            context.specialComments.shift() :
+            '';
+        case 0:
+          return '';
+      }
+    });
+    replace(/__CSSCONTENT__/g, function() {
+      return context.contentBlocks.shift();
+    });
 
     return data.trim() // trim spaces at beginning and end
   },
index 19e507a..c6f8026 100644 (file)
@@ -8,7 +8,7 @@
     "type" : "git",
     "url" : "http://github.com/GoalSmashers/clean-css.git"
   },
-  "version": "0.6.0",
+  "version": "0.7.0",
   "main": "index.js",
   "bin": {
     "cleancss": "./bin/cleancss"
index 64b0ca0..eb32362 100644 (file)
@@ -54,6 +54,16 @@ exports.commandsSuite = vows.describe('binary commands').addBatch({
       assert.equal(stdout, "a{}");
     }
   }),
+  'strip all but first comment': pipedContext('/*!1st*//*! 2nd */a{}', '--s1', {
+    'should keep the 2nd comment': function(error, stdout) {
+      assert.equal(stdout, "/*!1st*/a{}");
+    }
+  }),
+  'strip all comments': pipedContext('/*!1st*//*! 2nd */a{}', '--s0', {
+    'should keep the 2nd comment': function(error, stdout) {
+      assert.equal(stdout, "a{}");
+    }
+  }),
   'empty': pipedContext('a{}', '-e', {
     'should preserve content': function(error, stdout) {
       assert.equal(stdout, "");
index 8ef4567..9c8a467 100644 (file)
@@ -149,6 +149,18 @@ vows.describe('clean-units').addBatch({
       '/*!  \n  a > span { } with some content */'
     ]
   }),
+  'important comments - one': cssContext({
+    'strip all but first': [
+      '/*! important comment */a{color:red}/* some comment *//*! important comment */',
+      '/*! important comment */a{color:red}'
+    ]
+  }, { keepSpecialComments: 1 }),
+  'important comments - none': cssContext({
+    'strip all': [
+      '/*! important comment */a{color:red}/* some comment *//*! important comment */',
+      'a{color:red}'
+    ]
+  }, { keepSpecialComments: 0 }),
   'text content': cssContext({
     'normal #1': 'a{content:"."}',
     'normal #2': [