Skip to content

Commit

Permalink
WIP - Added strictMath: 'division' option
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-dean committed Jun 24, 2018
1 parent 3c081ff commit f9ab84a
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 26 deletions.
10 changes: 8 additions & 2 deletions lib/less/contexts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion lib/less/parser/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
7 changes: 4 additions & 3 deletions lib/less/tree/declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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({});
Expand Down Expand Up @@ -83,7 +84,7 @@ Declaration.prototype.eval = function (context) {
}
finally {
if (strictMathBypass) {
context.strictMath = false;
context.strictMath = prevMath;
}
}
};
Expand Down
8 changes: 5 additions & 3 deletions lib/less/tree/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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);
}
Expand Down
8 changes: 0 additions & 8 deletions test/css/css-3.css
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions test/css/no-strict-math/no-sm-operations.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@
.named-colors-in-expressions-barred {
a: a;
}
.division {
value: 2px;
}
13 changes: 13 additions & 0 deletions test/css/strict-math-division/new-division.css
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 3 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand Down
9 changes: 0 additions & 9 deletions test/less/css-3.less
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions test/less/no-strict-math/no-sm-operations.less
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@
color: green-black;
animation: blue-change 5s infinite;
}
.division {
value: ((16px ./ 2) / 2) / 2;
}
17 changes: 17 additions & 0 deletions test/less/strict-math-division/new-division.less
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit f9ab84a

Please sign in to comment.