From f3c7f70435dbbdb5abfde0ff067eb999125d883c Mon Sep 17 00:00:00 2001 From: Matthew Dean Date: Fri, 9 Feb 2018 23:20:39 -0800 Subject: [PATCH] calc() fix - fixes #974 --- lib/less/contexts.js | 4 ++++ lib/less/parser/parser.js | 8 ++++++-- lib/less/tree/call.js | 11 +++++++++-- lib/less/tree/directive.js | 12 ------------ lib/less/tree/quoted.js | 1 - lib/less/tree/rule.js | 12 ------------ test/css/calc.css | 3 +++ test/css/comments2.css | 2 +- test/css/{ => strict-math}/css.css | 0 test/css/{ => strict-math}/mixins-args.css | 0 test/css/{ => strict-math}/parens.css | 0 test/index.js | 8 ++++++-- test/less/calc.less | 4 ++++ test/less/media.less | 4 ++-- test/less/{ => strict-math}/css.less | 0 test/less/{ => strict-math}/mixins-args.less | 0 test/less/{ => strict-math}/parens.less | 0 17 files changed, 35 insertions(+), 34 deletions(-) delete mode 100644 lib/less/tree/directive.js delete mode 100644 lib/less/tree/rule.js create mode 100644 test/css/calc.css rename test/css/{ => strict-math}/css.css (100%) rename test/css/{ => strict-math}/mixins-args.css (100%) rename test/css/{ => strict-math}/parens.css (100%) create mode 100644 test/less/calc.less rename test/less/{ => strict-math}/css.less (100%) rename test/less/{ => strict-math}/mixins-args.less (100%) rename test/less/{ => strict-math}/parens.less (100%) diff --git a/lib/less/contexts.js b/lib/less/contexts.js index e56f0d3e4..4d21f7662 100644 --- a/lib/less/contexts.js +++ b/lib/less/contexts.js @@ -73,7 +73,11 @@ contexts.Eval.prototype.outOfParenthesis = function () { this.parensStack.pop(); }; +contexts.Eval.prototype.mathOn = true; contexts.Eval.prototype.isMathOn = function () { + if (!this.mathOn) { + return false; + } return this.strictMath ? (this.parensStack && this.parensStack.length) : true; }; diff --git a/lib/less/parser/parser.js b/lib/less/parser/parser.js index 2757d0b18..d5189c126 100644 --- a/lib/less/parser/parser.js +++ b/lib/less/parser/parser.js @@ -421,7 +421,11 @@ var Parser = function Parser(context, imports, fileInfo) { } parserInput.forget(); - return new(tree.Call)(name, args, index, fileInfo); + var call = new(tree.Call)(name, args, index, fileInfo); + if (name === 'calc') { + call.mathOff = true; + } + return call; }, // @@ -596,7 +600,7 @@ var Parser = function Parser(context, imports, fileInfo) { } }, - // A property entity useing the protective {} e.g. @{prop} + // A property entity useing the protective {} e.g. ${prop} propertyCurly: function () { var curly, index = parserInput.i; diff --git a/lib/less/tree/call.js b/lib/less/tree/call.js index 4db9faf9c..539f5bcb2 100644 --- a/lib/less/tree/call.js +++ b/lib/less/tree/call.js @@ -29,8 +29,15 @@ Call.prototype.accept = function (visitor) { // The function should receive the value, not the variable. // Call.prototype.eval = function (context) { - var args = this.args.map(function (a) { return a.eval(context); }), - result, funcCaller = new FunctionCaller(this.name, context, this.getIndex(), this.fileInfo()); + /* Used for calc() */ + if (this.mathOff) { + context.mathOn = false; + } + var args = this.args.map(function (a) { return a.eval(context); }); + if (this.mathOff) { + context.mathOn = true; + } + var result, funcCaller = new FunctionCaller(this.name, context, this.getIndex(), this.fileInfo()); if (funcCaller.isValid()) { try { diff --git a/lib/less/tree/directive.js b/lib/less/tree/directive.js deleted file mode 100644 index 3a9dd835f..000000000 --- a/lib/less/tree/directive.js +++ /dev/null @@ -1,12 +0,0 @@ -// Backwards compatibility shim for Directive (AtRule) -var AtRule = require("./atrule"); - -var Directive = function () { - var args = Array.prototype.slice.call(arguments); - AtRule.apply(this, args); -}; - -Directive.prototype = Object.create(AtRule.prototype); -Directive.prototype.constructor = Directive; - -module.exports = Directive; \ No newline at end of file diff --git a/lib/less/tree/quoted.js b/lib/less/tree/quoted.js index aba6d60c0..dd41f8ce3 100644 --- a/lib/less/tree/quoted.js +++ b/lib/less/tree/quoted.js @@ -1,5 +1,4 @@ var Node = require("./node"), - JsEvalNode = require("./js-eval-node"), Variable = require("./variable"), Property = require("./property"); diff --git a/lib/less/tree/rule.js b/lib/less/tree/rule.js deleted file mode 100644 index a6a4e12a9..000000000 --- a/lib/less/tree/rule.js +++ /dev/null @@ -1,12 +0,0 @@ -// Backwards compatibility shim for Rule (Declaration) -var Declaration = require("./declaration"); - -var Rule = function () { - var args = Array.prototype.slice.call(arguments); - Declaration.apply(this, args); -}; - -Rule.prototype = Object.create(Declaration.prototype); -Rule.prototype.constructor = Rule; - -module.exports = Rule; \ No newline at end of file diff --git a/test/css/calc.css b/test/css/calc.css new file mode 100644 index 000000000..6f5593181 --- /dev/null +++ b/test/css/calc.css @@ -0,0 +1,3 @@ +.no-math { + width: calc(50% + (25vh - 20px)); +} diff --git a/test/css/comments2.css b/test/css/comments2.css index 9fcbd46e8..02cad29d2 100644 --- a/test/css/comments2.css +++ b/test/css/comments2.css @@ -8,7 +8,7 @@ .first, .planning { margin: 10px; - total-width: (1 * 6em * 12) + (2em * 12); + total-width: 96em; } .some-inline-comments { a: yes /* comment */; diff --git a/test/css/css.css b/test/css/strict-math/css.css similarity index 100% rename from test/css/css.css rename to test/css/strict-math/css.css diff --git a/test/css/mixins-args.css b/test/css/strict-math/mixins-args.css similarity index 100% rename from test/css/mixins-args.css rename to test/css/strict-math/mixins-args.css diff --git a/test/css/parens.css b/test/css/strict-math/parens.css similarity index 100% rename from test/css/parens.css rename to test/css/strict-math/parens.css diff --git a/test/index.js b/test/index.js index 648d9c054..0392d3a85 100644 --- a/test/index.js +++ b/test/index.js @@ -8,13 +8,17 @@ console.log("\n" + stylize("Less", 'underline') + "\n"); lessTester.prepBomTest(); var testMap = [ [{ - strictMath: true, + strictMath: false, relativeUrls: true, silent: true, javascriptEnabled: true, // Set explicitly for legacy tests for >3.0 ieCompat: true }], + [{ + strictMath: true, + ieCompat: true + }, "strict-math/"], [{strictMath: true, strictUnits: true, javascriptEnabled: true}, "errors/", lessTester.testErrors, null], [{strictMath: true, strictUnits: true, javascriptEnabled: false}, "no-js-errors/", @@ -56,7 +60,7 @@ testMap.forEach(function(args) { lessTester.runTestSet.apply(lessTester, args) }); lessTester.testSyncronous({syncImport: true}, "import"); -lessTester.testSyncronous({syncImport: true}, "css"); +lessTester.testSyncronous({syncImport: true}, "strict-math/css"); lessTester.testNoOptions(); lessTester.testJSImport(); lessTester.finished(); diff --git a/test/less/calc.less b/test/less/calc.less new file mode 100644 index 000000000..9b0e6cab0 --- /dev/null +++ b/test/less/calc.less @@ -0,0 +1,4 @@ +.no-math { + @var: 50vh/2; + width: calc(50% + (@var - 20px)); +} \ No newline at end of file diff --git a/test/less/media.less b/test/less/media.less index 8bd23bb56..ec90a6618 100644 --- a/test/less/media.less +++ b/test/less/media.less @@ -23,7 +23,7 @@ @ratio_large: 16; @ratio_small: 9; -@media all and (device-aspect-ratio: @ratio_large / @ratio_small) { +@media all and (device-aspect-ratio: ~"@{ratio_large} / @{ratio_small}") { body { max-width: 800px; } } @@ -177,7 +177,7 @@ body { } } -@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) { +@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) { .b { background: red; } diff --git a/test/less/css.less b/test/less/strict-math/css.less similarity index 100% rename from test/less/css.less rename to test/less/strict-math/css.less diff --git a/test/less/mixins-args.less b/test/less/strict-math/mixins-args.less similarity index 100% rename from test/less/mixins-args.less rename to test/less/strict-math/mixins-args.less diff --git a/test/less/parens.less b/test/less/strict-math/parens.less similarity index 100% rename from test/less/parens.less rename to test/less/strict-math/parens.less