See #850 - allows `on` or `off` as level option values.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Fri, 6 Jan 2017 11:26:18 +0000 (12:26 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Fri, 6 Jan 2017 14:03:27 +0000 (15:03 +0100)
Why:

* In CLI mode it makes more sense to use these instead of `true`
  or `false` as in API (JavaScript) context.

README.md
bin/cleancss
lib/options/optimization-level.js
test/binary-test.js
test/options/optimization-level-test.js

index 8188cf2..8f6cc41 100644 (file)
--- 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?
index 60ee023..35d54ca 100755 (executable)
@@ -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();
 });
index 0cfc5e0..c0ffc1e 100644 (file)
@@ -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;
index 73c6359..2cf8230 100644 (file)
@@ -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}');
         }
index b4aba82..97a6e0b 100644 (file)
@@ -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' });