Fixes #383 - decimal places and rounding.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Mon, 17 Nov 2014 19:58:06 +0000 (19:58 +0000)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Mon, 17 Nov 2014 20:29:57 +0000 (20:29 +0000)
* Introduced by #380. Should fix it for good.

History.md
lib/selectors/optimizers/simple.js
test/binary-test.js
test/integration-test.js

index ae880d0..0f58f01 100644 (file)
 * Fixed issue [#360](https://github.com/GoalSmashers/clean-css/issues/360) - adds 7 extra CSS colors.
 * Fixed issue [#363](https://github.com/GoalSmashers/clean-css/issues/363) - `rem` units overriding `px`.
 
+[2.2.18 / 2014-11-17](https://github.com/jakubpawlowicz/clean-css/compare/v2.2.17...v2.2.18)
+==================
+
+* Fixed issue [#383](https://github.com/GoalSmashers/clean-css/issues/383) - rounding fractions once again.
+
 [2.2.17 / 2014-11-09](https://github.com/jakubpawlowicz/clean-css/compare/v2.2.16...v2.2.17)
 ==================
 
index 5d1a988..8fd988e 100644 (file)
@@ -22,7 +22,7 @@ function SimpleOptimizer(options) {
     DEFAULT_ROUNDING_PRECISION :
     options.roundingPrecision;
   options.precision.multiplier = Math.pow(10, options.precision.value);
-  options.precision.regexp = new RegExp('\\.(\\d{' + (options.precision.value + 1) + ',})px', 'g');
+  options.precision.regexp = new RegExp('(\\d*\\.\\d{' + (options.precision.value + 1) + ',})px', 'g');
 }
 
 function removeUnsupported(token, compatibility) {
@@ -107,15 +107,8 @@ function precisionMinifier(_, value, precisionOptions) {
     return value;
 
   return value
-    .replace(precisionOptions.regexp, function(match, decimalPlaces) {
-      var newFraction = Math.round(parseFloat('.' + decimalPlaces) * precisionOptions.multiplier) / precisionOptions.multiplier;
-      if (precisionOptions.value === 0 || newFraction === 0) {
-        return 'px';
-      } else {
-        return newFraction < 1 ?
-          '.' + ('' + newFraction).substring('0.'.length) + 'px' :
-          newFraction + 'px';
-      }
+    .replace(precisionOptions.regexp, function(match, number) {
+      return Math.round(parseFloat(number) * precisionOptions.multiplier) / precisionOptions.multiplier + 'px';
     })
     .replace(/(\d)\.($|\D)/g, '$1$2');
 }
@@ -200,8 +193,8 @@ function reduce(body, options) {
     if (valueMinifiers[property])
       value = valueMinifiers[property](value);
 
-    value = zeroMinifier(property, value);
     value = precisionMinifier(property, value, options.precision);
+    value = zeroMinifier(property, value);
     value = unitMinifier(property, value, options.unitsRegexp);
     value = multipleZerosMinifier(property, value);
     value = colorMininifier(property, value, options.compatibility);
index 7e572e8..7a5047a 100644 (file)
@@ -309,7 +309,7 @@ exports.commandsSuite = vows.describe('binary commands').addBatch({
     }),
     zero: pipedContext('div{width:1.5051px}', '--rounding-precision 0', {
       'should keep 0 decimal places': function(error, stdout) {
-        assert.equal(stdout, 'div{width:1px}');
+        assert.equal(stdout, 'div{width:2px}');
       }
     }),
     disabled: pipedContext('div{width:0.12345px}', '--rounding-precision \\\\-1', {
index f3f0270..4f267d8 100644 (file)
@@ -626,6 +626,10 @@ vows.describe('integration tests').addBatch({
     'rounds .9999 correctly': [
       'a{stroke-width:.99999px}',
       'a{stroke-width:1px}'
+    ],
+    'rounds 9.995 correctly': [
+      'a{stroke-width:9.995px}',
+      'a{stroke-width:9.99px}'
     ]
   }),
   'floats custom rounding': cssContext({