[Pkg-javascript-commits] [less.js] 81/88: allow % to be passed to the unit function. Fixes #1550 and fixes #1571

Jonas Smedegaard dr at jones.dk
Mon Oct 26 23:22:28 UTC 2015


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

js pushed a commit to annotated tag v1.7.0
in repository less.js.

commit beb52739c0aee15e578ee1ad1eb3721e74ee1eda
Author: Luke Page <luke.a.page at gmail.com>
Date:   Sun Feb 23 17:08:59 2014 +0000

    allow % to be passed to the unit function. Fixes #1550 and fixes #1571
---
 lib/less/functions.js                          | 11 ++++++++++-
 lib/less/parser.js                             |  2 +-
 lib/less/tree/keyword.js                       |  1 +
 test/css/functions.css                         |  1 +
 test/less/errors/percentage-missing-space.less |  3 +++
 test/less/errors/percentage-missing-space.txt  |  4 ++++
 test/less/functions.less                       |  1 +
 7 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/lib/less/functions.js b/lib/less/functions.js
index 2c269a9..feee7c4 100644
--- a/lib/less/functions.js
+++ b/lib/less/functions.js
@@ -242,7 +242,16 @@ tree.functions = {
         if(!(val instanceof tree.Dimension)) {
             throw { type: "Argument", message: "the first argument to unit must be a number" + (val instanceof tree.Operation ? ". Have you forgotten parenthesis?" : "") };
         }
-        return new(tree.Dimension)(val.value, unit ? unit.toCSS() : "");
+        if (unit) {
+            if (unit instanceof tree.Keyword) {
+                unit = unit.value;
+            } else {
+                unit = unit.toCSS();
+            }
+        } else {
+            unit = "";
+        }
+        return new(tree.Dimension)(val.value, unit);
     },
     convert: function (val, unit) {
         return val.convertTo(unit.value);
diff --git a/lib/less/parser.js b/lib/less/parser.js
index 032381d..74f2ddd 100644
--- a/lib/less/parser.js
+++ b/lib/less/parser.js
@@ -807,7 +807,7 @@ less.Parser = function Parser(env) {
                 keyword: function () {
                     var k;
 
-                    k = $re(/^[_A-Za-z-][_A-Za-z0-9-]*/);
+                    k = $re(/^%|^[_A-Za-z-][_A-Za-z0-9-]*/);
                     if (k) {
                         var color = tree.Color.fromKeyword(k);
                         if (color) {
diff --git a/lib/less/tree/keyword.js b/lib/less/tree/keyword.js
index 8cfbb09..830594d 100644
--- a/lib/less/tree/keyword.js
+++ b/lib/less/tree/keyword.js
@@ -5,6 +5,7 @@ tree.Keyword.prototype = {
     type: "Keyword",
     eval: function () { return this; },
     genCSS: function (env, output) {
+        if (this.value === '%') { throw { type: "Syntax", message: "Invalid % without number" }; }
         output.add(this.value);
     },
     toCSS: tree.toCSS,
diff --git a/test/css/functions.css b/test/css/functions.css
index 17509c1..b5fe490 100644
--- a/test/css/functions.css
+++ b/test/css/functions.css
@@ -60,6 +60,7 @@
   eformat: rgb(32, 128, 64);
   unitless: 12;
   unit: 14em;
+  unitpercentage: 100%;
   get-unit: px;
   get-unit-empty: ;
   hue: 98;
diff --git a/test/less/errors/percentage-missing-space.less b/test/less/errors/percentage-missing-space.less
new file mode 100644
index 0000000..247f773
--- /dev/null
+++ b/test/less/errors/percentage-missing-space.less
@@ -0,0 +1,3 @@
+.a {
+  error: calc(1 %);
+}
\ No newline at end of file
diff --git a/test/less/errors/percentage-missing-space.txt b/test/less/errors/percentage-missing-space.txt
new file mode 100644
index 0000000..776d8d5
--- /dev/null
+++ b/test/less/errors/percentage-missing-space.txt
@@ -0,0 +1,4 @@
+SyntaxError: Invalid % without number in {path}percentage-missing-space.less on line 2, column 3:
+1 .a {
+2   error: calc(1 %);
+3 }
diff --git a/test/less/functions.less b/test/less/functions.less
index 87686be..4928e85 100644
--- a/test/less/functions.less
+++ b/test/less/functions.less
@@ -65,6 +65,7 @@
   
   unitless: unit(12px);
   unit: unit((13px + 1px), em);
+  unitpercentage: unit(100, %);
   
   get-unit: get-unit(10px);
   get-unit-empty: get-unit(10);

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