Fixes #287 - adds `--rounding-precision` to control rounding precision.
authorJakub Pawlowicz <jakub@goalsmashers.com>
Thu, 5 Jun 2014 22:34:41 +0000 (23:34 +0100)
committerJakub Pawlowicz <jakub@goalsmashers.com>
Thu, 5 Jun 2014 22:40:56 +0000 (23:40 +0100)
History.md
README.md
bin/cleancss
lib/clean.js
test/binary-test.js
test/unit-test.js

index b33d4c1..9171db5 100644 (file)
@@ -4,6 +4,7 @@
 * Adds a better algorithm for quotation marks' removal.
 * Adds a better non-adjacent optimizer compatible with the upcoming new property optimizer.
 * Adds minifying remote files directly from CLI.
+* Adds `--rounding-precision` to control rounding precision.
 * Moves quotation matching into a `QuoteScanner` class.
 * Fixed issue [#134](https://github.com/GoalSmashers/clean-css/issues/134) - merges properties into shorthand form.
 * Fixed issue [#164](https://github.com/GoalSmashers/clean-css/issues/164) - removes default values if not needed.
index 047b584..79d1979 100644 (file)
--- a/README.md
+++ b/README.md
@@ -71,6 +71,7 @@ cleancss [options] source-file, [source-file, ...]
 --skip-rebase                   Disable URLs rebasing
 --skip-advanced                 Disable advanced optimizations - selector & property merging,
                                 reduction, etc.
+--rounding-precision [value]    Rounding precision, defaults to 2
 -c, --compatibility [ie7|ie8]   Force compatibility mode
 -d, --debug                     Shows debug information (minification time & compression efficiency)
 ```
index 413ef39..8def84d 100755 (executable)
@@ -24,6 +24,7 @@ commands
   .option('-s, --skip-import', 'Disable @import processing')
   .option('--skip-rebase', 'Disable URLs rebasing')
   .option('--skip-advanced', 'Disable advanced optimizations - selector & property merging, reduction, etc.')
+  .option('--rounding-precision [value]', 'Rounding precision, defaults to 2', parseInt)
   .option('-c, --compatibility [ie7|ie8]', 'Force compatibility mode')
   .option('-t, --timeout [seconds]', 'Per connection timeout when fetching remote @imports (defaults to 5 seconds)')
   .option('-d, --debug', 'Shows debug information (minification time & compression efficiency)');
@@ -76,6 +77,8 @@ if (commands.skipAdvanced)
   cleanOptions.noAdvanced = true;
 if (commands.compatibility)
   cleanOptions.compatibility = commands.compatibility;
+if (commands.roundingPrecision !== undefined)
+  cleanOptions.roundingPrecision = commands.roundingPrecision;
 if (commands.debug)
   cleanOptions.debug = true;
 if (commands.timeout)
index 51bf84c..c276076 100644 (file)
@@ -263,8 +263,12 @@ var minify = function(data, callback) {
   replace(/(\s|:|,)0+([1-9])/g, '$1$2');
 
   // round pixels to 2nd decimal place
-  replace(/\.(\d{3,})px/g, function(match, decimalPlaces) {
-    return '.' + Math.round(parseFloat('.' + decimalPlaces) * 100) + 'px';
+  var precision = 'roundingPrecision' in options ? options.roundingPrecision : 2;
+  var decimalMultiplier = Math.pow(10, precision);
+  replace(new RegExp('\\.(\\d{' + (precision + 1) + ',})px', 'g'), function(match, decimalPlaces) {
+    return precision === 0 ?
+      'px' :
+      '.' + Math.round(parseFloat('.' + decimalPlaces) * decimalMultiplier) / decimalMultiplier + 'px';
   });
 
   // .0 to 0
index 68d627d..e9731a3 100644 (file)
@@ -290,5 +290,22 @@ exports.commandsSuite = vows.describe('binary commands').addBatch({
     'should not transform source': function(error, stdout) {
       assert.equal(stdout, readFile('./test/data/unsupported/selectors-ie8.css'));
     }
-  })
+  }),
+  'rounding precision': {
+    defaults: pipedContext('div{width:0.10051px}', '', {
+      'should keep 2 decimal places': function(error, stdout) {
+        assert.equal(stdout, 'div{width:.1px}');
+      }
+    }),
+    custom: pipedContext('div{width:0.00051px}', '--rounding-precision 4', {
+      'should keep 4 decimal places': function(error, stdout) {
+        assert.equal(stdout, 'div{width:.0005px}');
+      }
+    }),
+    zero: pipedContext('div{width:1.5051px}', '--rounding-precision 0', {
+      'should keep 0 decimal places': function(error, stdout) {
+        assert.equal(stdout, 'div{width:1px}');
+      }
+    })
+  }
 });
index 52b89b1..623c853 100644 (file)
@@ -602,6 +602,12 @@ vows.describe('clean-units').addBatch({
     'do not round percentages': 'div{left:20.505%}',
     'do not round ems': 'div{font-size:1.505em}'
   }),
+  'floats custom rounding': cssContext({
+    'rounds to 4 values': [
+      'div{transform:translateY(-418.505123px)}',
+      'div{transform:translateY(-418.5051px)}'
+    ]
+  }, { roundingPrecision: 4 }),
   'colors': cssContext({
     'shorten rgb to standard hexadecimal format': [
       'a{ color:rgb(5, 10, 15) }',