[Pkg-javascript-commits] [node-cssstyle] 28/39: fix rgb/rgba color parsing, added appropriate test cases for it

Wolfgang Borgert debacle at moszumanska.debian.org
Sat Sep 20 20:22:36 UTC 2014


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

debacle pushed a commit to branch master
in repository node-cssstyle.

commit ad4f76d635ef1646d5fb6a703a7815396e51e4ce
Author: Chad Walker <chad at chad-cat-lore-eddie.com>
Date:   Thu Dec 19 15:22:38 2013 -0600

    fix rgb/rgba color parsing, added appropriate test cases for it
    
    (cherry-picked from 0.3.x)
---
 lib/parsers.js | 24 +++++++++++++-----------
 package.json   |  2 +-
 tests/tests.js | 13 +++++++++++++
 3 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/lib/parsers.js b/lib/parsers.js
index 4c60f93..a867b05 100644
--- a/lib/parsers.js
+++ b/lib/parsers.js
@@ -75,7 +75,7 @@ exports.valueType = function valueType(val) {
         if (parts.length !== 3) {
             return undefined;
         }
-        if (parts.every(percentRegEx.test.bind()) || parts.every(integerRegEx.test.bind())) {
+        if (parts.every(percentRegEx.test.bind(percentRegEx)) || parts.every(integerRegEx.test.bind(integerRegEx))) {
             return exports.TYPES.COLOR;
         }
         return undefined;
@@ -86,8 +86,10 @@ exports.valueType = function valueType(val) {
         if (parts.length !== 4) {
             return undefined;
         }
-        if (parts.every(percentRegEx.test.bind()) || parts.every(integerRegEx.test.bind())) {
-            return exports.TYPES.COLOR;
+        if (parts.slice(0, 3).every(percentRegEx.test.bind(percentRegEx)) || parts.every(integerRegEx.test.bind(integerRegEx))) {
+            if (numberRegEx.test(parts[3])) {
+                return exports.TYPES.COLOR;
+            }
         }
         return undefined;
     }
@@ -298,9 +300,9 @@ exports.parseColor = function parseColor(val) {
             return undefined;
         }
         if (parts.every(percentRegEx.test.bind(percentRegEx))) {
-            red = parseFloat(parts[0].substr(0, -1));
-            green = parseFloat(parts[1].substr(0, -1));
-            blue = parseFloat(parts[2].substr(0, -1));
+            red = Math.floor(parseFloat(parts[0].slice(0, -1)) * 255 / 100);
+            green = Math.floor(parseFloat(parts[1].slice(0, -1)) * 255 / 100);
+            blue = Math.floor(parseFloat(parts[2].slice(0, -1)) * 255 / 100);
         } else if (parts.every(integerRegEx.test.bind(integerRegEx))) {
             red = parseInt(parts[0], 10);
             green = parseInt(parts[1], 10);
@@ -320,12 +322,12 @@ exports.parseColor = function parseColor(val) {
         if (parts.length !== 4) {
             return undefined;
         }
-        if (parts.every(percentRegEx.test.bind(percentRegEx))) {
-            red = parseFloat(parts[0].substr(0, -1));
-            green = parseFloat(parts[1].substr(0, -1));
-            blue = parseFloat(parts[2].substr(0, -1));
+        if (parts.slice(0, 3).every(percentRegEx.test.bind(percentRegEx))) {
+            red = Math.floor(parseFloat(parts[0].slice(0, -1)) * 255 / 100);
+            green = Math.floor(parseFloat(parts[1].slice(0, -1)) * 255 / 100);
+            blue = Math.floor(parseFloat(parts[2].slice(0, -1)) * 255 / 100);
             alpha = parseFloat(parts[3]);
-        } else if (parts.every(integerRegEx.test.bind(integerRegEx))) {
+        } else if (parts.slice(0, 3).every(integerRegEx.test.bind(integerRegEx))) {
             red = parseInt(parts[0], 10);
             green = parseInt(parts[1], 10);
             blue = parseInt(parts[2], 10);
diff --git a/package.json b/package.json
index 36f3e13..f20b01b 100644
--- a/package.json
+++ b/package.json
@@ -2,7 +2,7 @@
     "name": "cssstyle",
     "description": "CSSStyleDeclaration Object Model implementation",
     "keywords": ["CSS", "CSSStyleDeclaration", "StyleSheet"],
-    "version": "0.2.7",
+    "version": "0.2.8",
     "homepage": "https://github.com/chad3814/CSSStyleDeclaration",
     "maintainers": [{
         "name": "Chad Walker",
diff --git a/tests/tests.js b/tests/tests.js
index 03b6173..b482176 100644
--- a/tests/tests.js
+++ b/tests/tests.js
@@ -158,5 +158,18 @@ module.exports = {
         test.ok(2 === style.length, 'length is not 2');
         test.ok('clear: both; clip: rect(0px, 3em, 2pt, 50%);' === style.cssText, 'cssText is not "clear: both; clip: rect(0px, 3em, 2pt, 50%);"');
         test.done();
+    },
+    'Test colors': function (test) {
+        var style = new cssstyle.CSSStyleDeclaration();
+        test.expect(4);
+        style.color = 'rgba(0,0,0,0)';
+        test.ok('rgba(0, 0, 0, 0)' === style.color, 'color is not rgba(0, 0, 0, 0)');
+        style.color = 'rgba(5%, 10%, 20%, 0.4)';
+        test.ok('rgba(12, 25, 51, 0.4)' === style.color, 'color is not rgba(12, 25, 51, 0.4)');
+        style.color = 'rgb(33%, 34%, 33%)';
+        test.ok('rgb(84, 86, 84)' === style.color, 'color is not rgb(84, 86, 84)');
+        style.color = 'rgba(300, 200, 100, 1.5)';
+        test.ok('rgb(255, 200, 100)' === style.color, 'color is not rgb(255, 200, 100) ' + style.color);
+        test.done();
     }
 };

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



More information about the Pkg-javascript-commits mailing list