From 52ba472bb76089e5b2c1d079a29e70cb99f23b15 Mon Sep 17 00:00:00 2001 From: seven-phases-max Date: Sat, 18 Jan 2014 09:58:14 +0400 Subject: [PATCH] Added rounding of output numbers (hardcoded to max. 8 digits in this commit). --- lib/less/functions.js | 10 ++++++++++ lib/less/parser.js | 3 ++- lib/less/tree/color.js | 9 +++++---- lib/less/tree/dimension.js | 2 +- test/css/css-3.css | 2 +- test/css/functions.css | 16 +++++++++------- test/less/functions.less | 2 ++ 7 files changed, 30 insertions(+), 14 deletions(-) diff --git a/lib/less/functions.js b/lib/less/functions.js index 99ef57e4b..418fe6cc5 100644 --- a/lib/less/functions.js +++ b/lib/less/functions.js @@ -718,6 +718,16 @@ function clamp(val) { return Math.min(1, Math.max(0, val)); } +tree.fround = function(env, value) { + var p; + if (env && (env.numPrecision != null)) { + p = Math.pow(10, env.numPrecision); + return Math.round(value * p) / p; + } else { + return value; + } +}; + tree.functionCall = function(env, currentFileInfo) { this.env = env; this.currentFileInfo = currentFileInfo; diff --git a/lib/less/parser.js b/lib/less/parser.js index 0105f2f33..70d2256ba 100644 --- a/lib/less/parser.js +++ b/lib/less/parser.js @@ -589,7 +589,8 @@ less.Parser = function Parser(env) { css = evaldRoot.toCSS({ compress: Boolean(options.compress), dumpLineNumbers: env.dumpLineNumbers, - strictUnits: Boolean(options.strictUnits)}); + strictUnits: Boolean(options.strictUnits), + numPrecision: 8}); } catch (e) { throw new(LessError)(e, env); } diff --git a/lib/less/tree/color.js b/lib/less/tree/color.js index 1f889a93e..8ea3fe020 100644 --- a/lib/less/tree/color.js +++ b/lib/less/tree/color.js @@ -34,19 +34,20 @@ tree.Color.prototype = { output.add(this.toCSS(env)); }, toCSS: function (env, doNotCompress) { - var compress = env && env.compress && !doNotCompress; + var compress = env && env.compress && !doNotCompress, + alpha = tree.fround(env, this.alpha); // If we have some transparency, the only way to represent it // is via `rgba`. Otherwise, we use the hex representation, // which has better compatibility with older browsers. // Values are capped between `0` and `255`, rounded and zero-padded. - if (this.alpha < 1) { - if (this.alpha === 0 && this.isTransparentKeyword) { + if (alpha < 1) { + if (alpha === 0 && this.isTransparentKeyword) { return transparentKeyword; } return "rgba(" + this.rgb.map(function (c) { return clamp(Math.round(c), 255); - }).concat(clamp(this.alpha, 1)) + }).concat(clamp(alpha, 1)) .join(',' + (compress ? '' : ' ')) + ")"; } else { var color = this.toRGB(); diff --git a/lib/less/tree/dimension.js b/lib/less/tree/dimension.js index c64cd89ac..93bed32f1 100644 --- a/lib/less/tree/dimension.js +++ b/lib/less/tree/dimension.js @@ -25,7 +25,7 @@ tree.Dimension.prototype = { throw new Error("Multiple units in dimension. Correct the units or use the unit function. Bad unit: "+this.unit.toString()); } - var value = this.value, + var value = tree.fround(env, this.value), strValue = String(value); if (value !== 0 && value < 0.000001 && value > -0.000001) { diff --git a/test/css/css-3.css b/test/css/css-3.css index 4e1865db4..61f635c27 100644 --- a/test/css/css-3.css +++ b/test/css/css-3.css @@ -1,7 +1,7 @@ .comma-delimited { text-shadow: -1px -1px 1px #ff0000, 6px 5px 5px #ffff00; -moz-box-shadow: 0pt 0pt 2px rgba(255, 255, 255, 0.4) inset, 0pt 4px 6px rgba(255, 255, 255, 0.4) inset; - -webkit-transform: rotate(-0.0000000001deg); + -webkit-transform: rotate(0deg); } @font-face { font-family: Headline; diff --git a/test/css/functions.css b/test/css/functions.css index 5c899072a..2c7896df1 100644 --- a/test/css/functions.css +++ b/test/css/functions.css @@ -69,15 +69,15 @@ ceil: 11px; floor: 12px; sqrt: 5px; - pi: 3.141592653589793; + pi: 3.14159265; mod: 2m; abs: 4%; - tan: 0.9004040442978399; - sin: 0.17364817766693033; - cos: 0.8438539587324921; + tan: 0.90040404; + sin: 0.17364818; + cos: 0.84385396; atan: 0.1rad; - atan: 34.00000000000001deg; - atan: 45.00000000000001deg; + atan: 34deg; + atan: 45deg; pow: 64px; pow: 64; pow: 27; @@ -91,11 +91,13 @@ tint: #898989; tint-full: #ffffff; tint-percent: #898989; + tint-negative: #656565; shade: #686868; shade-full: #000000; shade-percent: #686868; + shade-negative: #868686; fade-out: rgba(255, 0, 0, 0.95); - fade-in: rgba(255, 0, 0, 0.9500000000000001); + fade-in: rgba(255, 0, 0, 0.95); hsv: #4d2926; hsva: rgba(77, 40, 38, 0.2); mix: #ff3300; diff --git a/test/less/functions.less b/test/less/functions.less index b50a0da58..170a915b4 100644 --- a/test/less/functions.less +++ b/test/less/functions.less @@ -97,9 +97,11 @@ tint: tint(#777777, 13); tint-full: tint(#777777, 100); tint-percent: tint(#777777, 13%); + tint-negative: tint(#777777, -13%); shade: shade(#777777, 13); shade-full: shade(#777777, 100); shade-percent: shade(#777777, 13%); + shade-negative: shade(#777777, -13%); fade-out: fadeOut(red, 5%); // support fadeOut and fadeout fade-in: fadein(fadeout(red, 10%), 5%);