[Pkg-javascript-commits] [node-cssstyle] 33/39: Fix parsing of shorthand and implicit property parsing #12

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 620239f3b043cb6925101aadc35e1bfd171c5362
Author: Chad Walker <chad at chad-cat-lore-eddie.com>
Date:   Mon Mar 3 12:35:35 2014 -0600

    Fix parsing of shorthand and implicit property parsing #12
    
    thanks @kapouer
---
 lib/parsers.js | 46 ++++++++++++++++++++++++++++++++++++++++++++--
 package.json   |  2 +-
 tests/tests.js |  9 +++++++++
 3 files changed, 54 insertions(+), 3 deletions(-)

diff --git a/lib/parsers.js b/lib/parsers.js
index ca9dc47..69c1a69 100644
--- a/lib/parsers.js
+++ b/lib/parsers.js
@@ -413,6 +413,48 @@ var dashedToCamelCase = function (dashed) {
     return camel;
 };
 
+var is_space = /\s/;
+var opening_deliminators = ['"', '\'', '('];
+var closing_deliminators = ['"', '\'', ')'];
+// this splits on whitespace, but keeps quoted and parened parts together
+var getParts = function (str) {
+    var deliminator_stack = [];
+    var length = str.length;
+    var i;
+    var parts = [];
+    var current_part = '';
+    var opening_index;
+    var closing_index;
+    for (i = 0; i < length; i++) {
+        opening_index = opening_deliminators.indexOf(str[i]);
+        closing_index = closing_deliminators.indexOf(str[i]);
+        if (is_space.test(str[i])) {
+            if (deliminator_stack.length === 0) {
+                parts.push(current_part);
+                current_part = '';
+            } else {
+                current_part += str[i];
+            }
+        } else {
+            if (str[i] === '\\') {
+                i++;
+                current_part += str[i];
+            } else {
+                current_part += str[i];
+                if (closing_index !== -1 && closing_index === deliminator_stack[deliminator_stack.length - 1]) {
+                    deliminator_stack.pop();
+                } else if (opening_index !== -1) {
+                    deliminator_stack.push(opening_index);
+                }
+            }
+        }
+    }
+    if (current_part !== '') {
+        parts.push(current_part);
+    }
+    return parts;
+};
+
 /*
  * this either returns undefined meaning that it isn't valid
  * or returns an object where the keys are dashed short
@@ -436,7 +478,7 @@ exports.shorthandParser = function parse(v, shorthand_for) {
     if (v.toLowerCase() === 'inherit') {
         return {};
     }
-    var parts = v.split(/\s+/);
+    var parts = getParts(v);
     var valid = true;
     var obj = {};
     parts.forEach(function (part) {
@@ -515,7 +557,7 @@ exports.implicitSetter = function (property_before, property_after, isValid, par
         if (v.toLowerCase() === 'inherit') {
             return this.setProperty(property_before + property_after, v);
         }
-        var parts = v.split(/\s+/);
+        var parts = getParts(v);
         if (parts.length < 1 || parts.length > 4) {
             return undefined;
         }
diff --git a/package.json b/package.json
index d6114e2..ac58b89 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.10",
+    "version": "0.2.11",
     "homepage": "https://github.com/chad3814/CSSStyleDeclaration",
     "maintainers": [{
         "name": "Chad Walker",
diff --git a/tests/tests.js b/tests/tests.js
index 4114f09..f2dadf6 100644
--- a/tests/tests.js
+++ b/tests/tests.js
@@ -185,5 +185,14 @@ module.exports = {
         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();
+    },
+    'Test short hand properties with embedded spaces': function (test) {
+        var style = new cssstyle.CSSStyleDeclaration();
+        test.expect(3);
+        style.background = 'rgb(0, 0, 0) url(/something/somewhere.jpg)';
+        test.ok('rgb(0, 0, 0)' === style.backgroundColor, 'backgroundColor is not rgb(0, 0, 0): ' + style.backgroundColor);
+        test.ok('url(/something/somewhere.jpg)' === style.backgroundImage, 'backgroundImage is not url(/something/somewhere.jpg): ' + style.backgroundImage);
+        test.ok('background: rgb(0, 0, 0) url(/something/somewhere.jpg);' === style.cssText, 'cssText is not correct: ' + style.cssText);
+        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