From f9ab84af10a9079daaeb2e4e3f03eb68fdc451be Mon Sep 17 00:00:00 2001 From: Matthew Dean Date: Sun, 24 Jun 2018 16:24:58 -0700 Subject: [PATCH] WIP - Added strictMath: 'division' option --- lib/less/contexts.js | 10 ++++++++-- lib/less/parser/parser.js | 2 +- lib/less/tree/declaration.js | 7 ++++--- lib/less/tree/operation.js | 8 +++++--- test/css/css-3.css | 8 -------- test/css/no-strict-math/no-sm-operations.css | 3 +++ test/css/strict-math-division/new-division.css | 13 +++++++++++++ test/index.js | 3 +++ test/less/css-3.less | 9 --------- test/less/no-strict-math/no-sm-operations.less | 3 +++ .../less/strict-math-division/new-division.less | 17 +++++++++++++++++ 11 files changed, 57 insertions(+), 26 deletions(-) create mode 100644 test/css/strict-math-division/new-division.css create mode 100644 test/less/strict-math-division/new-division.less diff --git a/lib/less/contexts.js b/lib/less/contexts.js index 4d21f766..e4d5342e 100644 --- a/lib/less/contexts.js +++ b/lib/less/contexts.js @@ -74,11 +74,17 @@ contexts.Eval.prototype.outOfParenthesis = function () { }; contexts.Eval.prototype.mathOn = true; -contexts.Eval.prototype.isMathOn = function () { +contexts.Eval.prototype.isMathOn = function (op) { if (!this.mathOn) { return false; } - return this.strictMath ? (this.parensStack && this.parensStack.length) : true; + if (op === '/' && this.strictMath && (!this.parensStack || !this.parensStack.length)) { + return false; + } + if (this.strictMath === true) { + return this.parensStack && this.parensStack.length; + } + return true; }; contexts.Eval.prototype.isPathRelative = function (path) { diff --git a/lib/less/parser/parser.js b/lib/less/parser/parser.js index 2b31eb7d..811a65b9 100644 --- a/lib/less/parser/parser.js +++ b/lib/less/parser/parser.js @@ -1729,7 +1729,7 @@ var Parser = function Parser(context, imports, fileInfo) { parserInput.save(); - op = parserInput.$char('/') || parserInput.$char('*'); + op = parserInput.$char('/') || parserInput.$char('*') || parserInput.$str('./'); if (!op) { parserInput.forget(); break; } diff --git a/lib/less/tree/declaration.js b/lib/less/tree/declaration.js index 338a6212..9a3d077c 100644 --- a/lib/less/tree/declaration.js +++ b/lib/less/tree/declaration.js @@ -41,7 +41,7 @@ Declaration.prototype.genCSS = function (context, output) { output.add(this.important + ((this.inline || (context.lastRule && context.compress)) ? "" : ";"), this._fileInfo, this._index); }; Declaration.prototype.eval = function (context) { - var strictMathBypass = false, name = this.name, evaldValue, variable = this.variable; + var strictMathBypass = false, prevMath, name = this.name, evaldValue, variable = this.variable; if (typeof name !== "string") { // expand 'primitive' name directly to get // things faster (~10% for benchmark.less): @@ -51,7 +51,8 @@ Declaration.prototype.eval = function (context) { } if (name === "font" && !context.strictMath) { strictMathBypass = true; - context.strictMath = true; + prevMath = context.strictMath; + context.strictMath = 'division'; } try { context.importantScope.push({}); @@ -83,7 +84,7 @@ Declaration.prototype.eval = function (context) { } finally { if (strictMathBypass) { - context.strictMath = false; + context.strictMath = prevMath; } } }; diff --git a/lib/less/tree/operation.js b/lib/less/tree/operation.js index e3c225a9..6974c742 100644 --- a/lib/less/tree/operation.js +++ b/lib/less/tree/operation.js @@ -14,9 +14,11 @@ Operation.prototype.accept = function (visitor) { }; Operation.prototype.eval = function (context) { var a = this.operands[0].eval(context), - b = this.operands[1].eval(context); + b = this.operands[1].eval(context), + op; - if (context.isMathOn()) { + if (context.isMathOn(this.op)) { + op = this.op === './' ? '/' : this.op; if (a instanceof Dimension && b instanceof Color) { a = a.toColor(); } @@ -28,7 +30,7 @@ Operation.prototype.eval = function (context) { message: "Operation on an invalid type" }; } - return a.operate(context, this.op, b); + return a.operate(context, op, b); } else { return new Operation(this.op, [a, b], this.isSpaced); } diff --git a/test/css/css-3.css b/test/css/css-3.css index 5aafe04e..5a14773a 100644 --- a/test/css/css-3.css +++ b/test/css/css-3.css @@ -73,14 +73,6 @@ p::before { font-size: 12px; } } -.units { - font: 1.2rem/2rem; - font: 8vw/9vw; - font: 10vh/12vh; - font: 12vm/15vm; - font: 12vmin/15vmin; - font: 1.2ch/1.5ch; -} @supports ( box-shadow: 2px 2px 2px black ) or ( -moz-box-shadow: 2px 2px 2px black ) { .outline { diff --git a/test/css/no-strict-math/no-sm-operations.css b/test/css/no-strict-math/no-sm-operations.css index 00c55660..f720ffbf 100644 --- a/test/css/no-strict-math/no-sm-operations.css +++ b/test/css/no-strict-math/no-sm-operations.css @@ -13,3 +13,6 @@ .named-colors-in-expressions-barred { a: a; } +.division { + value: 2px; +} diff --git a/test/css/strict-math-division/new-division.css b/test/css/strict-math-division/new-division.css new file mode 100644 index 00000000..cd30653e --- /dev/null +++ b/test/css/strict-math-division/new-division.css @@ -0,0 +1,13 @@ +.units { + font: 1.2rem/2rem; + font: 8vw/9vw; + font: 10vh/12vh; + font: 12vm/15vm; + font: 12vmin/15vmin; + font: 1.2ch/1.5ch; +} +.math { + a: 2; + b: 2px / 2; + c: 1px; +} diff --git a/test/index.js b/test/index.js index 0392d3a8..b2a63b98 100644 --- a/test/index.js +++ b/test/index.js @@ -19,6 +19,9 @@ var testMap = [ strictMath: true, ieCompat: true }, "strict-math/"], + [{ + strictMath: 'division' + }, "strict-math-division/"], [{strictMath: true, strictUnits: true, javascriptEnabled: true}, "errors/", lessTester.testErrors, null], [{strictMath: true, strictUnits: true, javascriptEnabled: false}, "no-js-errors/", diff --git a/test/less/css-3.less b/test/less/css-3.less index 9f206791..999e8019 100644 --- a/test/less/css-3.less +++ b/test/less/css-3.less @@ -79,15 +79,6 @@ p::before { } } -.units { - font: 1.2rem/2rem; - font: 8vw/9vw; - font: 10vh/12vh; - font: 12vm/15vm; - font: 12vmin/15vmin; - font: 1.2ch/1.5ch; -} - @supports ( box-shadow: 2px 2px 2px black ) or ( -moz-box-shadow: 2px 2px 2px black ) { .outline { diff --git a/test/less/no-strict-math/no-sm-operations.less b/test/less/no-strict-math/no-sm-operations.less index e79d0500..d4bfdb97 100644 --- a/test/less/no-strict-math/no-sm-operations.less +++ b/test/less/no-strict-math/no-sm-operations.less @@ -11,3 +11,6 @@ color: green-black; animation: blue-change 5s infinite; } +.division { + value: ((16px ./ 2) / 2) / 2; +} \ No newline at end of file diff --git a/test/less/strict-math-division/new-division.less b/test/less/strict-math-division/new-division.less new file mode 100644 index 00000000..b81c26e2 --- /dev/null +++ b/test/less/strict-math-division/new-division.less @@ -0,0 +1,17 @@ +.units { + font: 1.2rem/2rem; + font: 8vw/9vw; + font: 10vh/12vh; + font: 12vm/15vm; + font: 12vmin/15vmin; + font: 1.2ch/1.5ch; +} + +.math { + a: 1 + 1; + b: 2px / 2; + c: 2px ./ 2; + d: (10px / 10px); + e: ((16px ./ 2) / 2) / 2; + f: ((16px ./ 2) / 2) ./ 2; +} \ No newline at end of file