[Pkg-javascript-commits] [less.js] 282/285: improve legacy units to be more consistent (particularly for multiplicaton). Fixes #2276

Jonas Smedegaard dr at jones.dk
Mon Oct 26 23:24:02 UTC 2015


This is an automated email from the git hooks/post-receive script.

js pushed a commit to annotated tag v2.0.0
in repository less.js.

commit 9017cbe2ad6bcf90609b97121e1bdf54eecb6f1c
Author: Luke Page <luke.a.page at gmail.com>
Date:   Sun Nov 9 14:01:01 2014 +0000

    improve legacy units to be more consistent (particularly for multiplicaton). Fixes #2276
---
 Gruntfile.js                                | 10 +++++++++-
 lib/less/tree/unit.js                       | 30 +++++++++++------------------
 test/browser/runner-strict-units-options.js |  6 ++++++
 test/browser/runner-strict-units-spec.js    |  3 +++
 test/css/strict-units/strict-units.css      |  4 ++++
 test/css/variables.css                      | 12 ++++++++++++
 test/index.js                               |  1 +
 test/less/strict-units/strict-units.less    |  4 ++++
 test/less/variables.less                    | 15 +++++++++++++++
 9 files changed, 65 insertions(+), 20 deletions(-)

diff --git a/Gruntfile.js b/Gruntfile.js
index ccd78c1..45b07eb 100644
--- a/Gruntfile.js
+++ b/Gruntfile.js
@@ -147,6 +147,14 @@ module.exports = function (grunt) {
                     outfile: 'tmp/browser/test-runner-legacy.html'
                 }
             },
+            strictUnits: {
+                src: ['test/less/strict-units/*.less'],
+                options: {
+                    helpers: 'test/browser/runner-strict-units-options.js',
+                    specs: 'test/browser/runner-strict-units-spec.js',
+                    outfile: 'tmp/browser/test-runner-strict-units.html'
+                }
+            },
             errors: {
                 src: ['test/less/errors/*.less', '!test/less/errors/javascript-error.less'],
                 options: {
@@ -234,7 +242,7 @@ module.exports = function (grunt) {
             all: {
                 options: {
                     urls: ["post-processor", "global-vars", "modify-vars", "production", "rootpath-relative",
-                           "rootpath", "relative-urls", "browser", "no-js-errors", "legacy"
+                           "rootpath", "relative-urls", "browser", "no-js-errors", "legacy", "strict-units"
                     ].map(function(testName) {
                         return "http://localhost:8081/tmp/browser/test-runner-" + testName + ".html";
                     }),
diff --git a/lib/less/tree/unit.js b/lib/less/tree/unit.js
index 2a33bc4..0b8441d 100644
--- a/lib/less/tree/unit.js
+++ b/lib/less/tree/unit.js
@@ -4,7 +4,11 @@ var Node = require("./node"),
 var Unit = function (numerator, denominator, backupUnit) {
     this.numerator = numerator ? numerator.slice(0).sort() : [];
     this.denominator = denominator ? denominator.slice(0).sort() : [];
-    this.backupUnit = backupUnit;
+    if (backupUnit) {
+        this.backupUnit = backupUnit;
+    } else if (numerator && numerator.length) {
+        this.backupUnit = numerator[0];
+    }
 };
 
 Unit.prototype = new Node();
@@ -13,13 +17,11 @@ Unit.prototype.clone = function () {
     return new Unit(this.numerator.slice(0), this.denominator.slice(0), this.backupUnit);
 };
 Unit.prototype.genCSS = function (context, output) {
-    if (this.numerator.length >= 1) {
-        output.add(this.numerator[0]);
-    } else
-    if (this.denominator.length >= 1) {
-        output.add(this.denominator[0]);
-    } else
-    if ((!context || !context.strictUnits) && this.backupUnit) {
+    // Dimension checks the unit is singular and throws an error if in strict math mode.
+    var strictUnits = context && context.strictUnits;
+    if (this.numerator.length === 1) {
+        output.add(this.numerator[0]); // the ideal situation
+    } else if (!strictUnits && this.backupUnit) {
         output.add(this.backupUnit);
     }
 };
@@ -79,21 +81,15 @@ Unit.prototype.usedUnits = function() {
     return result;
 };
 Unit.prototype.cancel = function () {
-    var counter = {}, atomicUnit, i, backup;
+    var counter = {}, atomicUnit, i;
 
     for (i = 0; i < this.numerator.length; i++) {
         atomicUnit = this.numerator[i];
-        if (!backup) {
-            backup = atomicUnit;
-        }
         counter[atomicUnit] = (counter[atomicUnit] || 0) + 1;
     }
 
     for (i = 0; i < this.denominator.length; i++) {
         atomicUnit = this.denominator[i];
-        if (!backup) {
-            backup = atomicUnit;
-        }
         counter[atomicUnit] = (counter[atomicUnit] || 0) - 1;
     }
 
@@ -116,10 +112,6 @@ Unit.prototype.cancel = function () {
         }
     }
 
-    if (this.numerator.length === 0 && this.denominator.length === 0 && backup) {
-        this.backupUnit = backup;
-    }
-
     this.numerator.sort();
     this.denominator.sort();
 };
diff --git a/test/browser/runner-strict-units-options.js b/test/browser/runner-strict-units-options.js
new file mode 100644
index 0000000..e292310
--- /dev/null
+++ b/test/browser/runner-strict-units-options.js
@@ -0,0 +1,6 @@
+var less = {
+    logLevel: 4,
+    errorReporting: "console",
+    strictMath: true,
+    strictUnits: true };
+
diff --git a/test/browser/runner-strict-units-spec.js b/test/browser/runner-strict-units-spec.js
new file mode 100644
index 0000000..5ff77c8
--- /dev/null
+++ b/test/browser/runner-strict-units-spec.js
@@ -0,0 +1,3 @@
+describe("less.js strict units tests", function() {
+    testLessEqualsInDocument();
+});
diff --git a/test/css/strict-units/strict-units.css b/test/css/strict-units/strict-units.css
new file mode 100644
index 0000000..f09f008
--- /dev/null
+++ b/test/css/strict-units/strict-units.css
@@ -0,0 +1,4 @@
+.units {
+  cancels-to-nothing: 1;
+  cancels: 6px;
+}
diff --git a/test/css/variables.css b/test/css/variables.css
index f8d8518..faca880 100644
--- a/test/css/variables.css
+++ b/test/css/variables.css
@@ -43,3 +43,15 @@
   mix-units: 2px;
   invalid-units: 1px;
 }
+.units .fallback {
+  div-px-1: 10px;
+  div-px-2: 1px;
+  sub-px-1: 12.6px;
+  sub-cm-1: 9.666625cm;
+  mul-px-1: 19.6px;
+  mul-em-1: 19.6em;
+  mul-em-2: 196em;
+  mul-cm-1: 196cm;
+  add-px-1: 15.4px;
+  add-px-2: 393.35275591px;
+}
diff --git a/test/index.js b/test/index.js
index 600df98..ce602e8 100644
--- a/test/index.js
+++ b/test/index.js
@@ -28,6 +28,7 @@ lessTester.runTestSet({strictMath: true, dumpLineNumbers: 'all'}, "debug/", null
     function(name) { return name + '-all'; });
 lessTester.runTestSet({strictMath: true, relativeUrls: false, rootpath: "folder (1)/"}, "static-urls/");
 lessTester.runTestSet({strictMath: true, compress: true}, "compression/");
+lessTester.runTestSet({strictMath: true, strictUnits: true}, "strict-units/");
 lessTester.runTestSet({}, "legacy/");
 lessTester.runTestSet({strictMath: true, strictUnits: true, sourceMap: true, globalVars: true }, "sourcemaps/",
     lessTester.testSourcemap, null, null,
diff --git a/test/less/strict-units/strict-units.less b/test/less/strict-units/strict-units.less
new file mode 100644
index 0000000..a7d0bb0
--- /dev/null
+++ b/test/less/strict-units/strict-units.less
@@ -0,0 +1,4 @@
+.units {
+  cancels-to-nothing: (1px / 1px);
+  cancels: ((((10px / 5em) / 1px) * 3em) * 1px);
+}
diff --git a/test/less/variables.less b/test/less/variables.less
index 8674a37..96f3555 100644
--- a/test/less/variables.less
+++ b/test/less/variables.less
@@ -80,4 +80,19 @@
   custom-unit-cancelling: (8cats * 9dogs / 4cats);
   mix-units: (1px + 1em);
   invalid-units: (1px * 1px);
+  .fallback {
+    @px: 14px;
+    @em: 1.4em;
+    @cm: 10cm;
+    div-px-1: (@px / @em);
+    div-px-2: ((@px / @em) / @cm);
+    sub-px-1: (@px - @em);
+    sub-cm-1: (@cm - (@px - @em));
+    mul-px-1: (@px * @em);
+    mul-em-1: (@em * @px);
+    mul-em-2: ((@em * @px) * @cm);
+    mul-cm-1: (@cm * (@em * @px));
+    add-px-1: (@px + @em);
+    add-px-2: ((@px + @em) + @cm);
+  }
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/less.js.git



More information about the Pkg-javascript-commits mailing list