From 0ed036f8c1872358b5f0a395388aee536e284534 Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Fri, 6 Jan 2017 12:26:18 +0100 Subject: [PATCH] See #850 - allows `on` or `off` as level option values. Why: * In CLI mode it makes more sense to use these instead of `true` or `false` as in API (JavaScript) context. --- README.md | 10 +++++----- bin/cleancss | 10 +++++----- lib/options/optimization-level.js | 11 +++++++++-- test/binary-test.js | 8 ++++---- test/options/optimization-level-test.js | 25 +++++++++++++++++++++++++ 5 files changed, 48 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 8188cf21..8f6cc416 100644 --- a/README.md +++ b/README.md @@ -129,11 +129,11 @@ Level 2 optimizations: ```bash cleancss --O2 one.css -cleancss --O2 mediaMerging:false;restructuring:false;semanticMerging:true;shorthandCompacting:false one.css -# `mediaMerging` controls `@media` merging behavior; defaults to true -# `restructuring` controls content restructuring behavior; defaults to true -# `semanticMerging` controls semantic merging behavior; defaults to false -# `shorthandCompacting` controls shorthand compacting behavior; defaults to true +cleancss --O2 mediaMerging:off;restructuring:off;semanticMerging:on;shorthandCompacting:off one.css +# `mediaMerging` controls `@media` merging behavior; defaults to `on` (alias to `true`) +# `restructuring` controls content restructuring behavior; defaults `on` (alias to `true`) +# `semanticMerging` controls semantic merging behavior; defaults to `off` (alias to `false`) +# `shorthandCompacting` controls shorthand compacting behavior; defaults to `on` (alias to `true`) ``` ### How to use clean-css API? diff --git a/bin/cleancss b/bin/cleancss index 60ee0238..35d54caf 100755 --- a/bin/cleancss +++ b/bin/cleancss @@ -46,11 +46,11 @@ commands.on('--help', function () { console.log(''); console.log(' Level 2 optimizations:'); console.log(' %> cleancss --O2 one.css'); - console.log(' %> cleancss --O2 mediaMerging:false;restructuring:false;semanticMerging:true;shorthandCompacting:false one.css'); - console.log(' %> # `mediaMerging` controls `@media` merging behavior; defaults to true'); - console.log(' %> # `restructuring` controls content restructuring behavior; defaults to true'); - console.log(' %> # `semanticMerging` controls semantic merging behavior; defaults to false'); - console.log(' %> # `shorthandCompacting` controls shorthand compacting behavior; defaults to true'); + console.log(' %> cleancss --O2 mediaMerging:off;restructuring:off;semanticMerging:on;shorthandCompacting:off one.css'); + console.log(' %> # `mediaMerging` controls `@media` merging behavior; defaults to `on` (alias to `true`)'); + console.log(' %> # `restructuring` controls content restructuring behavior; defaults to `on` (alias to `true`)'); + console.log(' %> # `semanticMerging` controls semantic merging behavior; defaults to `off` (alias to `false`)'); + console.log(' %> # `shorthandCompacting` controls shorthand compacting behavior; defaults to `on` (alias to `true`)'); process.exit(); }); diff --git a/lib/options/optimization-level.js b/lib/options/optimization-level.js index 0cfc5e07..c0ffc1e5 100644 --- a/lib/options/optimization-level.js +++ b/lib/options/optimization-level.js @@ -23,6 +23,11 @@ DEFAULTS[OptimizationLevel.Two] = { var ALL_KEYWORD_1 = '*'; var ALL_KEYWORD_2 = 'all'; +var FALSE_KEYWORD_1 = 'false'; +var FALSE_KEYWORD_2 = 'off'; +var TRUE_KEYWORD_1 = 'true'; +var TRUE_KEYWORD_2 = 'on'; + var OPTION_SEPARATOR = ';'; var OPTION_VALUE_SEPARATOR = ':'; @@ -106,9 +111,11 @@ function defaults(level, value) { function normalizeValue(value) { switch (value) { - case 'false': + case FALSE_KEYWORD_1: + case FALSE_KEYWORD_2: return false; - case 'true': + case TRUE_KEYWORD_1: + case TRUE_KEYWORD_2: return true; default: return value; diff --git a/test/binary-test.js b/test/binary-test.js index 73c63594..2cf8230e 100644 --- a/test/binary-test.js +++ b/test/binary-test.js @@ -162,7 +162,7 @@ vows.describe('./bin/cleancss') }) }) .addBatch({ - 'skip restructuring optimizations': pipedContext('div{margin-top:0}.one{margin:0}.two{display:block;margin-top:0}', '--O2 restructuring:false', { + 'skip restructuring optimizations': pipedContext('div{margin-top:0}.one{margin:0}.two{display:block;margin-top:0}', '--O2 restructuring:off', { 'should do basic optimizations only': function (error, stdout) { assert.equal(stdout, 'div{margin-top:0}.one{margin:0}.two{display:block;margin-top:0}'); } @@ -453,7 +453,7 @@ vows.describe('./bin/cleancss') } }) .addBatch({ - '@media merging': pipedContext('@media screen{a{color:red}}@media screen{a{display:block}}', '--O2 mediaMerging:false', { + '@media merging': pipedContext('@media screen{a{color:red}}@media screen{a{display:block}}', '--O2 mediaMerging:off', { 'gets right result': function (error, stdout) { assert.equal(stdout, '@media screen{a{color:red}}@media screen{a{display:block}}'); } @@ -461,7 +461,7 @@ vows.describe('./bin/cleancss') }) .addBatch({ 'shorthand compacting': { - 'of (yet) unmergeable properties': pipedContext('a{background:url(image.png);background-color:red}', '--O2 shorthandCompacting:false', { + 'of (yet) unmergeable properties': pipedContext('a{background:url(image.png);background-color:red}', '--O2 shorthandCompacting:off', { 'gets right result': function (error, stdout) { assert.equal(stdout, 'a{background:url(image.png);background-color:red}'); } @@ -615,7 +615,7 @@ vows.describe('./bin/cleancss') assert.equal(stdout, '.a{margin:0}.b{margin:10px;padding:0}.c{margin:0}'); } }), - 'enabled': pipedContext('.a{margin:0}.b{margin:10px;padding:0}.c{margin:0}', '--O2 semanticMerging:true', { + 'enabled': pipedContext('.a{margin:0}.b{margin:10px;padding:0}.c{margin:0}', '--O2 semanticMerging:on', { 'should output right data': function (error, stdout) { assert.equal(stdout, '.a,.c{margin:0}.b{margin:10px;padding:0}'); } diff --git a/test/options/optimization-level-test.js b/test/options/optimization-level-test.js index b4aba82b..97a6e0b7 100644 --- a/test/options/optimization-level-test.js +++ b/test/options/optimization-level-test.js @@ -213,6 +213,31 @@ vows.describe(optimizationLevelFrom) }); } }, + 'a hash with options as strings with boolean values as on/off': { + 'topic': function () { + return optimizationLevelFrom({ 2: 'mediaMerging:off;semanticMerging:on' }); + }, + 'has all options': function (levelOptions) { + assert.deepEqual(Object.keys(levelOptions), ['0', '1', '2']); + }, + 'has level 0 options': function (levelOptions) { + assert.deepEqual(levelOptions['0'], {}); + }, + 'has level 1 options': function (levelOptions) { + assert.deepEqual(levelOptions['1'], { + roundingPrecision: roundingPrecisionFrom(undefined), + specialComments: 'all' + }); + }, + 'has level 2 options': function (levelOptions) { + assert.deepEqual(levelOptions['2'], { + mediaMerging: false, + restructuring: true, + semanticMerging: true, + shorthandCompacting: true + }); + } + }, 'a hash with options as strings with all keyword': { 'topic': function () { return optimizationLevelFrom({ 2: 'all:false;mediaMerging:true;semanticMerging:true' }); -- 2.34.1