From: GoalSmashers Date: Sat, 16 Nov 2013 12:01:14 +0000 (+0100) Subject: Fixes #163 - adds pixel rounding to 2nd decimal place. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=17a2d15e75f3bffcd193f6c7d8cfc3632e95c020;p=clean-css.git Fixes #163 - adds pixel rounding to 2nd decimal place. --- diff --git a/History.md b/History.md index 78a675d5..c11f3331 100644 --- a/History.md +++ b/History.md @@ -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) diff --git a/lib/clean.js b/lib/clean.js index 5238595c..262c2b39 100644 --- a/lib/clean.js +++ b/lib/clean.js @@ -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; diff --git a/test/unit-test.js b/test/unit-test.js index d0b95a4d..06b2f3af 100644 --- a/test/unit-test.js +++ b/test/unit-test.js @@ -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': [