From 2ffdefa2486b4347478dcbdd6e15010acb843897 Mon Sep 17 00:00:00 2001 From: Luke Page Date: Sat, 2 Feb 2013 15:30:19 +0000 Subject: [PATCH] Preserve whitespace in operations --- lib/less/parser.js | 14 +++++++++----- lib/less/tree/operation.js | 8 +++++--- test/css/css-3.css | 12 ++++++------ test/css/css.css | 6 +++--- test/css/mixins-args.css | 4 ++-- test/less/parens.less | 2 +- 6 files changed, 26 insertions(+), 20 deletions(-) diff --git a/lib/less/parser.js b/lib/less/parser.js index f822cdfd..24b9212f 100644 --- a/lib/less/parser.js +++ b/lib/less/parser.js @@ -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; } diff --git a/lib/less/tree/operation.js b/lib/less/tree/operation.js index 23f3180d..fdb4acd9 100644 --- a/lib/less/tree/operation.js +++ b/lib/less/tree/operation.js @@ -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) { diff --git a/test/css/css-3.css b/test/css/css-3.css index 1efaa536..d48dbe5a 100644 --- a/test/css/css-3.css +++ b/test/css/css-3.css @@ -73,12 +73,12 @@ p::before { } } .units { - font: 1.2rem / 2rem; - font: 8vw / 9vw; - font: 10vh / 12vh; - font: 12vm / 15vm; - font: 12vmin / 15vmin; - font: 1.2ch / 1.5ch; + 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 ) { diff --git a/test/css/css.css b/test/css/css.css index 7ce9d111..07055369 100644 --- a/test/css/css.css +++ b/test/css/css.css @@ -50,8 +50,8 @@ p + h1 { } #shorthands { border: 1px solid #000; - font: 12px / 16px Arial; - font: 100% / 16px Arial; + font: 12px/16px Arial; + font: 100%/16px Arial; margin: 1px 0; padding: 0 auto; } @@ -59,7 +59,7 @@ p + h1 { margin: 0; padding: 1px 0 2px 0; font: normal small / 20px 'Trebuchet MS', Verdana, sans-serif; - font: 0 / 0 a; + font: 0/0 a; border-radius: 5px / 10px; background: url("img.jpg") center / 100px; background: #ffffff url(image.png) center / 1px 100px repeat-x scroll content-box padding-box; diff --git a/test/css/mixins-args.css b/test/css/mixins-args.css index 3c63f569..aad45467 100644 --- a/test/css/mixins-args.css +++ b/test/css/mixins-args.css @@ -73,8 +73,8 @@ body { width: "{"; } .slash-vs-math { - border-radius: 2px / 5px; - border-radius: 5px / 10px; + border-radius: 2px/5px; + border-radius: 5px/10px; border-radius: 6px; } .comma-vs-semi-colon { diff --git a/test/less/parens.less b/test/less/parens.less index 11f857ec..43536f48 100644 --- a/test/less/parens.less +++ b/test/less/parens.less @@ -32,7 +32,7 @@ .nested-parens { width: 2 * (4 * (2 + (1 + 6))) - 1; - height: ((2+3)*(2+3) / (9-4)) + 1; + height: ((2 + 3) * (2 + 3) / (9 - 4)) + 1; } .mixed-units {