Skip to content

Commit e2242b9

Browse files
committed
Add support for nested functions in calc()
- Simplify mathOn switch
2 parents f3c7f70 + 367b46a commit e2242b9

File tree

8 files changed

+40
-24
lines changed

8 files changed

+40
-24
lines changed

dist/less.js

Lines changed: 18 additions & 5 deletions
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

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/less/parser/parser.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,8 @@ var Parser = function Parser(context, imports, fileInfo) {
421421
}
422422

423423
parserInput.forget();
424-
var call = new(tree.Call)(name, args, index, fileInfo);
425-
if (name === 'calc') {
426-
call.mathOff = true;
427-
}
428-
return call;
424+
425+
return new(tree.Call)(name, args, index, fileInfo);
429426
},
430427

431428
//

lib/less/tree/call.js

Lines changed: 9 additions & 7 deletions
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,14 +30,15 @@ Call.prototype.accept = function (visitor) {
2930
// The function should receive the value, not the variable.
3031
//
3132
Call.prototype.eval = function (context) {
32-
/* Used for calc() */
33-
if (this.mathOff) {
34-
context.mathOn = false;
35-
}
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;
3639
var args = this.args.map(function (a) { return a.eval(context); });
37-
if (this.mathOff) {
38-
context.mathOn = true;
39-
}
40+
context.mathOn = currentMathContext;
41+
4042
var result, funcCaller = new FunctionCaller(this.name, context, this.getIndex(), this.fileInfo());
4143

4244
if (funcCaller.isValid()) {

test/browser/runner-browser-options.js

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

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

test/less/calc.less

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

0 commit comments

Comments
 (0)