Preserve whitespace in operations

This commit is contained in:
Luke Page
2013-02-02 15:30:19 +00:00
parent c27922890d
commit 2ffdefa248
6 changed files with 26 additions and 20 deletions

View File

@@ -1378,13 +1378,15 @@ less.Parser = function Parser(env) {
}
},
multiplication: function () {
var m, a, op, operation, expression = [];
var m, a, op, operation, isSpaced, expression = [];
if (m = $(this.operand)) {
isSpaced = isWhitespace(input.charAt(i - 1));
while (!peek(/^\/[*\/]/) && (op = ($('/') || $('*')))) {
if (a = $(this.operand)) {
m.parensInOp = true;
a.parensInOp = true;
operation = new(tree.Operation)(op, [operation || m, a]);
operation = new(tree.Operation)(op, [operation || m, a], isSpaced);
isSpaced = isWhitespace(input.charAt(i - 1));
} else {
break;
}
@@ -1393,13 +1395,15 @@ less.Parser = function Parser(env) {
}
},
addition: function () {
var m, a, op, operation;
var m, a, op, operation, isSpaced;
if (m = $(this.multiplication)) {
while ((op = $(/^[-+]\s+/) || (!isWhitespace(input.charAt(i - 1)) && ($('+') || $('-')))) &&
isSpaced = isWhitespace(input.charAt(i - 1));
while ((op = $(/^[-+]\s+/) || (!isSpaced && ($('+') || $('-')))) &&
(a = $(this.multiplication))) {
m.parensInOp = true;
a.parensInOp = true;
operation = new(tree.Operation)(op, [operation || m, a]);
operation = new(tree.Operation)(op, [operation || m, a], isSpaced);
isSpaced = isWhitespace(input.charAt(i - 1));
}
return operation || m;
}

View File

@@ -1,8 +1,9 @@
(function (tree) {
tree.Operation = function (op, operands) {
tree.Operation = function (op, operands, isSpaced) {
this.op = op.trim();
this.operands = operands;
this.isSpaced = isSpaced;
};
tree.Operation.prototype.eval = function (env) {
var a = this.operands[0].eval(env),
@@ -25,11 +26,12 @@ tree.Operation.prototype.eval = function (env) {
return a.operate(env, this.op, b);
} else {
return new(tree.Operation)(this.op, [a, b]);
return new(tree.Operation)(this.op, [a, b], this.isSpaced);
}
};
tree.Operation.prototype.toCSS = function (env) {
return this.operands[0].toCSS() + " " + this.op + " " + this.operands[1].toCSS();
var separator = this.isSpaced ? " " : "";
return this.operands[0].toCSS() + separator + this.op + separator + this.operands[1].toCSS();
};
tree.operate = function (env, op, a, b) {