Skip to content

Commit 8580ff8

Browse files
committed
Merge pull request #1814 from seven-phases-max/numeric-precision
Rounding of output numbers.
2 parents 9a0f813 + 52ba472 commit 8580ff8

File tree

7 files changed

+30
-14
lines changed

7 files changed

+30
-14
lines changed

lib/less/functions.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,16 @@ function clamp(val) {
717717
return Math.min(1, Math.max(0, val));
718718
}
719719

720+
tree.fround = function(env, value) {
721+
var p;
722+
if (env && (env.numPrecision != null)) {
723+
p = Math.pow(10, env.numPrecision);
724+
return Math.round(value * p) / p;
725+
} else {
726+
return value;
727+
}
728+
};
729+
720730
tree.functionCall = function(env, currentFileInfo) {
721731
this.env = env;
722732
this.currentFileInfo = currentFileInfo;

lib/less/parser.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,8 @@ less.Parser = function Parser(env) {
589589
css = evaldRoot.toCSS({
590590
compress: Boolean(options.compress),
591591
dumpLineNumbers: env.dumpLineNumbers,
592-
strictUnits: Boolean(options.strictUnits)});
592+
strictUnits: Boolean(options.strictUnits),
593+
numPrecision: 8});
593594
} catch (e) {
594595
throw new(LessError)(e, env);
595596
}

lib/less/tree/color.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,20 @@ tree.Color.prototype = {
3434
output.add(this.toCSS(env));
3535
},
3636
toCSS: function (env, doNotCompress) {
37-
var compress = env && env.compress && !doNotCompress;
37+
var compress = env && env.compress && !doNotCompress,
38+
alpha = tree.fround(env, this.alpha);
3839

3940
// If we have some transparency, the only way to represent it
4041
// is via `rgba`. Otherwise, we use the hex representation,
4142
// which has better compatibility with older browsers.
4243
// Values are capped between `0` and `255`, rounded and zero-padded.
43-
if (this.alpha < 1) {
44-
if (this.alpha === 0 && this.isTransparentKeyword) {
44+
if (alpha < 1) {
45+
if (alpha === 0 && this.isTransparentKeyword) {
4546
return transparentKeyword;
4647
}
4748
return "rgba(" + this.rgb.map(function (c) {
4849
return clamp(Math.round(c), 255);
49-
}).concat(clamp(this.alpha, 1))
50+
}).concat(clamp(alpha, 1))
5051
.join(',' + (compress ? '' : ' ')) + ")";
5152
} else {
5253
var color = this.toRGB();

lib/less/tree/dimension.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ tree.Dimension.prototype = {
2525
throw new Error("Multiple units in dimension. Correct the units or use the unit function. Bad unit: "+this.unit.toString());
2626
}
2727

28-
var value = this.value,
28+
var value = tree.fround(env, this.value),
2929
strValue = String(value);
3030

3131
if (value !== 0 && value < 0.000001 && value > -0.000001) {

test/css/css-3.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.comma-delimited {
22
text-shadow: -1px -1px 1px #ff0000, 6px 5px 5px #ffff00;
33
-moz-box-shadow: 0pt 0pt 2px rgba(255, 255, 255, 0.4) inset, 0pt 4px 6px rgba(255, 255, 255, 0.4) inset;
4-
-webkit-transform: rotate(-0.0000000001deg);
4+
-webkit-transform: rotate(0deg);
55
}
66
@font-face {
77
font-family: Headline;

test/css/functions.css

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@
6969
ceil: 11px;
7070
floor: 12px;
7171
sqrt: 5px;
72-
pi: 3.141592653589793;
72+
pi: 3.14159265;
7373
mod: 2m;
7474
abs: 4%;
75-
tan: 0.9004040442978399;
76-
sin: 0.17364817766693033;
77-
cos: 0.8438539587324921;
75+
tan: 0.90040404;
76+
sin: 0.17364818;
77+
cos: 0.84385396;
7878
atan: 0.1rad;
79-
atan: 34.00000000000001deg;
80-
atan: 45.00000000000001deg;
79+
atan: 34deg;
80+
atan: 45deg;
8181
pow: 64px;
8282
pow: 64;
8383
pow: 27;
@@ -91,11 +91,13 @@
9191
tint: #898989;
9292
tint-full: #ffffff;
9393
tint-percent: #898989;
94+
tint-negative: #656565;
9495
shade: #686868;
9596
shade-full: #000000;
9697
shade-percent: #686868;
98+
shade-negative: #868686;
9799
fade-out: rgba(255, 0, 0, 0.95);
98-
fade-in: rgba(255, 0, 0, 0.9500000000000001);
100+
fade-in: rgba(255, 0, 0, 0.95);
99101
hsv: #4d2926;
100102
hsva: rgba(77, 40, 38, 0.2);
101103
mix: #ff3300;

test/less/functions.less

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,11 @@
9797
tint: tint(#777777, 13);
9898
tint-full: tint(#777777, 100);
9999
tint-percent: tint(#777777, 13%);
100+
tint-negative: tint(#777777, -13%);
100101
shade: shade(#777777, 13);
101102
shade-full: shade(#777777, 100);
102103
shade-percent: shade(#777777, 13%);
104+
shade-negative: shade(#777777, -13%);
103105

104106
fade-out: fadeOut(red, 5%); // support fadeOut and fadeout
105107
fade-in: fadein(fadeout(red, 10%), 5%);

0 commit comments

Comments
 (0)