From fd1ad969b55c41eb2edb27881ffb28d0019dfc12 Mon Sep 17 00:00:00 2001 From: GoalSmashers Date: Sun, 14 Oct 2012 21:16:12 +0100 Subject: [PATCH] Added stripping special comments (all or all but first one) to CLI and API. * Version 0.7.0. --- History.md | 8 +++++++- bin/cleancss | 8 +++++++- lib/clean.js | 26 ++++++++++++++++++++++++-- package.json | 2 +- test/binary-test.js | 10 ++++++++++ test/unit-test.js | 12 ++++++++++++ 6 files changed, 61 insertions(+), 5 deletions(-) diff --git a/History.md b/History.md index c99bcfde..81f845c0 100644 --- a/History.md +++ b/History.md @@ -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 ================== diff --git a/bin/cleancss b/bin/cleancss index 682f68bd..c04da9b2 100755 --- a/bin/cleancss +++ b/bin/cleancss @@ -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 \n'); + util.puts("usage: cleancss [options] -o \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; } diff --git a/lib/clean.js b/lib/clean.js index 3a3bfaeb..4819c3b5 100644 --- a/lib/clean.js +++ b/lib/clean.js @@ -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 }, diff --git a/package.json b/package.json index 19e507ae..c6f80266 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test/binary-test.js b/test/binary-test.js index 64b0ca06..eb323620 100644 --- a/test/binary-test.js +++ b/test/binary-test.js @@ -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, ""); diff --git a/test/unit-test.js b/test/unit-test.js index 8ef45677..9c8a4672 100644 --- a/test/unit-test.js +++ b/test/unit-test.js @@ -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': [ -- 2.34.1