WIP - Added strictMath: 'division' option

This commit is contained in:
Matthew Dean
2018-06-24 16:24:58 -07:00
parent 3c081ff807
commit f9ab84af10
11 changed files with 57 additions and 26 deletions

View File

@@ -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) {

View File

@@ -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; }

View File

@@ -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;
}
}
};

View File

@@ -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);
}

View File

@@ -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 {

View File

@@ -13,3 +13,6 @@
.named-colors-in-expressions-barred {
a: a;
}
.division {
value: 2px;
}

View File

@@ -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;
}

View File

@@ -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/",

View File

@@ -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 {

View File

@@ -11,3 +11,6 @@
color: green-black;
animation: blue-change 5s infinite;
}
.division {
value: ((16px ./ 2) / 2) / 2;
}

View File

@@ -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;
}