Fixes #163 - adds pixel rounding to 2nd decimal place.
authorGoalSmashers <jakub@goalsmashers.com>
Sat, 16 Nov 2013 12:01:14 +0000 (13:01 +0100)
committerGoalSmashers <jakub@goalsmashers.com>
Sat, 16 Nov 2013 12:01:14 +0000 (13:01 +0100)
History.md
lib/clean.js
test/unit-test.js

index 78a675d..c11f333 100644 (file)
@@ -1,6 +1,7 @@
 [2.1.0 / 2013-xx-xx (UNRELEASED)](https://github.com/GoalSmashers/clean-css/compare/v2.0.0...HEAD)
 ==================
 
+* Fixed issue [#163](https://github.com/GoalSmashers/clean-css/issues/163) - round pixels to 2nd decimal place.
 * Fixed issue [#165](https://github.com/GoalSmashers/clean-css/issues/165) - extra space after trailing parenthesis.
 
 [2.0.1 / 2013-11-14](https://github.com/GoalSmashers/clean-css/compare/v2.0.0...v2.0.1)
index 5238595..262c2b3 100644 (file)
@@ -214,6 +214,11 @@ module.exports = function(options) {
     replace(/(\s|:|,)0(?:px|em|ex|cm|mm|in|pt|pc|%)/g, '$1' + '0');
     replace(/rect\(0(?:px|em|ex|cm|mm|in|pt|pc|%)/g, 'rect(0');
 
+    // round pixels to 2nd decimal place
+    replace(/\.(\d{3,})px/g, function(match, decimalPlaces) {
+      return '.' + Math.round(parseFloat('.' + decimalPlaces) * 100) + 'px';
+    });
+
     // fraction zeros removal
     replace(/\.([1-9]*)0+(\D)/g, function(match, nonZeroPart, suffix) {
       return (nonZeroPart ? '.' : '') + nonZeroPart + suffix;
index d0b95a4..06b2f3a 100644 (file)
@@ -525,7 +525,18 @@ vows.describe('clean-units').addBatch({
     'strip fraction zeros': [
       'div{margin:1.000em 2.00em 3.100em 4.01em}',
       'div{margin:1em 2em 3.1em 4.01em}'
-    ]
+    ],
+    'round pixels up to 2nd decimal place': [
+      'div{transform:translateY(-418.505123px)}',
+      'div{transform:translateY(-418.51px)}'
+    ],
+    'round pixels down to 2nd decimal place': [
+      'div{transform:translateY(0.504123px)}',
+      'div{transform:translateY(0.5px)}'
+    ],
+    'do not round 2nd decimal place pixels': 'div{transform:translateY(20.55px)}',
+    'do not round percentages': 'div{left:20.505%}',
+    'do not round ems': 'div{font-size:1.505em}'
   }),
   'colors': cssContext({
     'shorten rgb to standard hexadecimal format': [