Skip to content

Commit

Permalink
Merge pull request #1814 from seven-phases-max/numeric-precision
Browse files Browse the repository at this point in the history
Rounding of output numbers.
  • Loading branch information
lukeapage committed Jan 21, 2014
2 parents 9a0f813 + 52ba472 commit 8580ff8
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 14 deletions.
10 changes: 10 additions & 0 deletions lib/less/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,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;
Expand Down
3 changes: 2 additions & 1 deletion lib/less/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
9 changes: 5 additions & 4 deletions lib/less/tree/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion lib/less/tree/dimension.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion test/css/css-3.css
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
16 changes: 9 additions & 7 deletions test/css/functions.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions test/less/functions.less
Original file line number Diff line number Diff line change
Expand Up @@ -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%);
Expand Down

0 comments on commit 8580ff8

Please sign in to comment.