Skip to content

Commit a48c24c

Browse files
authoredFeb 11, 2018
calc() fix - fixes #974 (partially #1880)
1 parent 367b46a commit a48c24c

21 files changed

+62
-45
lines changed
 

‎dist/less.js

+18-5
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,11 @@ contexts.Eval.prototype.outOfParenthesis = function () {
10031003
this.parensStack.pop();
10041004
};
10051005

1006+
contexts.Eval.prototype.mathOn = true;
10061007
contexts.Eval.prototype.isMathOn = function () {
1008+
if (!this.mathOn) {
1009+
return false;
1010+
}
10071011
return this.strictMath ? (this.parensStack && this.parensStack.length) : true;
10081012
};
10091013

@@ -4019,6 +4023,7 @@ var Parser = function Parser(context, imports, fileInfo) {
40194023
}
40204024

40214025
parserInput.forget();
4026+
40224027
return new(tree.Call)(name, args, index, fileInfo);
40234028
},
40244029

@@ -4194,7 +4199,7 @@ var Parser = function Parser(context, imports, fileInfo) {
41944199
}
41954200
},
41964201

4197-
// A property entity useing the protective {} e.g. @{prop}
4202+
// A property entity useing the protective {} e.g. ${prop}
41984203
propertyCurly: function () {
41994204
var curly, index = parserInput.i;
42004205

@@ -6331,6 +6336,7 @@ var Node = require("./node"),
63316336
var Call = function (name, args, index, currentFileInfo) {
63326337
this.name = name;
63336338
this.args = args;
6339+
this.mathOn = name === 'calc' ? false : true;
63346340
this._index = index;
63356341
this._fileInfo = currentFileInfo;
63366342
};
@@ -6353,8 +6359,16 @@ Call.prototype.accept = function (visitor) {
63536359
// The function should receive the value, not the variable.
63546360
//
63556361
Call.prototype.eval = function (context) {
6356-
var args = this.args.map(function (a) { return a.eval(context); }),
6357-
result, funcCaller = new FunctionCaller(this.name, context, this.getIndex(), this.fileInfo());
6362+
6363+
/**
6364+
* Turn off math for calc(), and switch back on for evaluating nested functions
6365+
*/
6366+
var currentMathContext = context.mathOn;
6367+
context.mathOn = this.mathOn;
6368+
var args = this.args.map(function (a) { return a.eval(context); });
6369+
context.mathOn = currentMathContext;
6370+
6371+
var result, funcCaller = new FunctionCaller(this.name, context, this.getIndex(), this.fileInfo());
63586372

63596373
if (funcCaller.isValid()) {
63606374
try {
@@ -8384,7 +8398,6 @@ module.exports = Property;
83848398

83858399
},{"./declaration":58,"./node":73}],77:[function(require,module,exports){
83868400
var Node = require("./node"),
8387-
JsEvalNode = require("./js-eval-node"),
83888401
Variable = require("./variable"),
83898402
Property = require("./property");
83908403

@@ -8441,7 +8454,7 @@ Quoted.prototype.compare = function (other) {
84418454
};
84428455
module.exports = Quoted;
84438456

8444-
},{"./js-eval-node":67,"./node":73,"./property":76,"./variable":85}],78:[function(require,module,exports){
8457+
},{"./node":73,"./property":76,"./variable":85}],78:[function(require,module,exports){
84458458
var Node = require("./node"),
84468459
Declaration = require("./declaration"),
84478460
Keyword = require("./keyword"),

‎dist/less.min.js

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/less/contexts.js

+4
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ contexts.Eval.prototype.outOfParenthesis = function () {
7373
this.parensStack.pop();
7474
};
7575

76+
contexts.Eval.prototype.mathOn = true;
7677
contexts.Eval.prototype.isMathOn = function () {
78+
if (!this.mathOn) {
79+
return false;
80+
}
7781
return this.strictMath ? (this.parensStack && this.parensStack.length) : true;
7882
};
7983

‎lib/less/parser/parser.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ var Parser = function Parser(context, imports, fileInfo) {
421421
}
422422

423423
parserInput.forget();
424+
424425
return new(tree.Call)(name, args, index, fileInfo);
425426
},
426427

@@ -596,7 +597,7 @@ var Parser = function Parser(context, imports, fileInfo) {
596597
}
597598
},
598599

599-
// A property entity useing the protective {} e.g. @{prop}
600+
// A property entity useing the protective {} e.g. ${prop}
600601
propertyCurly: function () {
601602
var curly, index = parserInput.i;
602603

‎lib/less/tree/call.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var Node = require("./node"),
77
var Call = function (name, args, index, currentFileInfo) {
88
this.name = name;
99
this.args = args;
10+
this.mathOn = name === 'calc' ? false : true;
1011
this._index = index;
1112
this._fileInfo = currentFileInfo;
1213
};
@@ -29,8 +30,16 @@ Call.prototype.accept = function (visitor) {
2930
// The function should receive the value, not the variable.
3031
//
3132
Call.prototype.eval = function (context) {
32-
var args = this.args.map(function (a) { return a.eval(context); }),
33-
result, funcCaller = new FunctionCaller(this.name, context, this.getIndex(), this.fileInfo());
33+
34+
/**
35+
* Turn off math for calc(), and switch back on for evaluating nested functions
36+
*/
37+
var currentMathContext = context.mathOn;
38+
context.mathOn = this.mathOn;
39+
var args = this.args.map(function (a) { return a.eval(context); });
40+
context.mathOn = currentMathContext;
41+
42+
var result, funcCaller = new FunctionCaller(this.name, context, this.getIndex(), this.fileInfo());
3443

3544
if (funcCaller.isValid()) {
3645
try {

‎lib/less/tree/directive.js

-12
This file was deleted.

‎lib/less/tree/quoted.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var Node = require("./node"),
2-
JsEvalNode = require("./js-eval-node"),
32
Variable = require("./variable"),
43
Property = require("./property");
54

‎lib/less/tree/rule.js

-12
This file was deleted.

‎test/browser/runner-browser-options.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var less = {
22
logLevel: 4,
33
errorReporting: "console",
44
javascriptEnabled: true,
5-
strictMath: true
5+
strictMath: false
66
};
77

88
// There originally run inside describe method. However, since they have not

‎test/browser/runner-main-options.js

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ var less = {
22
logLevel: 4,
33
errorReporting: "console"
44
};
5-
less.strictMath = true;
65
less.functions = {
76
add: function(a, b) {
87
return new(less.tree.Dimension)(a.value + b.value);

‎test/css/calc.css

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.no-math {
2+
width: calc(50% + (25vh - 20px));
3+
foo: 3 calc(3 + 4) 11;
4+
bar: calc(1 + 20%);
5+
}

‎test/css/comments2.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
.first,
99
.planning {
1010
margin: 10px;
11-
total-width: (1 * 6em * 12) + (2em * 12);
11+
total-width: 96em;
1212
}
1313
.some-inline-comments {
1414
a: yes /* comment */;
File renamed without changes.
File renamed without changes.
File renamed without changes.

‎test/index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ console.log("\n" + stylize("Less", 'underline') + "\n");
88
lessTester.prepBomTest();
99
var testMap = [
1010
[{
11-
strictMath: true,
11+
strictMath: false,
1212
relativeUrls: true,
1313
silent: true,
1414
javascriptEnabled: true,
1515
// Set explicitly for legacy tests for >3.0
1616
ieCompat: true
1717
}],
18+
[{
19+
strictMath: true,
20+
ieCompat: true
21+
}, "strict-math/"],
1822
[{strictMath: true, strictUnits: true, javascriptEnabled: true}, "errors/",
1923
lessTester.testErrors, null],
2024
[{strictMath: true, strictUnits: true, javascriptEnabled: false}, "no-js-errors/",
@@ -56,7 +60,7 @@ testMap.forEach(function(args) {
5660
lessTester.runTestSet.apply(lessTester, args)
5761
});
5862
lessTester.testSyncronous({syncImport: true}, "import");
59-
lessTester.testSyncronous({syncImport: true}, "css");
63+
lessTester.testSyncronous({syncImport: true}, "strict-math/css");
6064
lessTester.testNoOptions();
6165
lessTester.testJSImport();
6266
lessTester.finished();

‎test/less/calc.less

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.no-math {
2+
@var: 50vh/2;
3+
width: calc(50% + (@var - 20px));
4+
foo: 1 + 2 calc(3 + 4) 5 + 6;
5+
@floor: floor(1 + .1);
6+
bar: calc(@floor + 20%);
7+
}

‎test/less/media.less

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
@ratio_large: 16;
2424
@ratio_small: 9;
2525

26-
@media all and (device-aspect-ratio: @ratio_large / @ratio_small) {
26+
@media all and (device-aspect-ratio: ~"@{ratio_large} / @{ratio_small}") {
2727
body { max-width: 800px; }
2828
}
2929

@@ -177,7 +177,7 @@ body {
177177
}
178178
}
179179

180-
@media (-webkit-min-device-pixel-ratio: 2), (min--moz-device-pixel-ratio: 2), (-o-min-device-pixel-ratio: 2/1), (min-resolution: 2dppx), (min-resolution: 128dpcm) {
180+
@media (-webkit-min-device-pixel-ratio: 2), (min--moz-device-pixel-ratio: 2), (-o-min-device-pixel-ratio: ~"2/1"), (min-resolution: 2dppx), (min-resolution: 128dpcm) {
181181
.b {
182182
background: red;
183183
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)
Please sign in to comment.