Permalink
Browse files

Fix Node.js circular decencies problem

  • Loading branch information...
1 parent 48db4b9 commit e2cabcc76e339c06fb879ac45ae6ae1d8d0631e2 @NV committed Oct 26, 2010
Showing with 147 additions and 12 deletions.
  1. +0 −8 README.mdown
  2. +145 −2 lib/CSSStyleRule.js
  3. +2 −2 lib/CSSStyleSheet.js
View
@@ -18,12 +18,4 @@ CSSOM.js is a pure JavaScript parser and a partial implementation of [CSS Object
}
-It doesn't yet work on Node.js because of some sort of circular dependencies problems.
-
- require("./CSSOM").parse("")
- -> TypeError: undefined is not a function
- at CALL_NON_FUNCTION_AS_CONSTRUCTOR (native)
- at Object.parse (/Users/nv/Code/CSSOM/lib/parse.js:29:19)
-
-
## [Tests](http://nv.github.com/CSSOM/test/)
View
@@ -1,7 +1,6 @@
if (typeof require != "undefined") {
var CSSStyleDeclaration = require("./CSSStyleDeclaration").CSSStyleDeclaration;
var CSSRule = require("./CSSRule").CSSRule;
- var parse = require("./parse").parse;
}
/**
@@ -28,12 +27,156 @@ CSSStyleRule.prototype.__defineGetter__("cssText", function() {
});
CSSStyleRule.prototype.__defineSetter__("cssText", function(cssText) {
- var rule = parse(cssText).cssRules[0];
+ var rule = CSSStyleRule.parse(cssText);
this.style = rule.style;
this.selectorText = rule.selectorText;
});
+/**
+ * NON-STANDARD
+ * lightweight version of parse.js.
+ * @param {string} ruleText
+ * @return CSSStyleRule
+ */
+CSSStyleRule.parse = function(ruleText) {
+ var i = 0;
+ var state = "selector";
+ var index;
+ var j = i;
+ var buffer = "";
+
+ var SIGNIFICANT_WHITESPACE = {
+ "selector": true,
+ "value": true
+ };
+
+ var styleRule = new CSSStyleRule;
+ var selector, name, value, priority="";
+
+ for (var character; character = ruleText.charAt(i); i++) {
+
+ switch (character) {
+
+ case " ":
+ case "\t":
+ case "\r":
+ case "\n":
+ case "\f":
+ if (SIGNIFICANT_WHITESPACE[state]) {
+ // Squash 2 or more white-spaces in the row into 1
+ switch (ruleText.charAt(i - 1)) {
+ case " ":
+ case "\t":
+ case "\r":
+ case "\n":
+ case "\f":
+ break;
+ default:
+ buffer += " ";
+ break;
+ }
+ }
+ break;
+
+ // String
+ case '"':
+ j = i + 1;
+ index = ruleText.indexOf('"', j) + 1;
+ if (!index) {
+ throw '" is missing';
+ }
+ buffer += ruleText.slice(i, index);
+ i = index - 1;
+ break;
+
+ case "'":
+ j = i + 1;
+ index = ruleText.indexOf("'", j) + 1;
+ if (!index) {
+ throw "' is missing";
+ }
+ buffer += ruleText.slice(i, index);
+ i = index - 1;
+ break;
+
+ // Comment
+ case "/":
+ if (ruleText.charAt(++i) == "*") {
+ index = ruleText.indexOf("*/", ++i);
+ if (index == -1) {
+ throw SyntaxError("Missing */");
+ } else {
+ i = index + 2;
+ }
+ } else {
+ buffer += character;
+ }
+ break;
+
+ case "{":
+ if (state == "selector") {
+ styleRule.selectorText = buffer.trim();
+ buffer = "";
+ state = "name";
+ }
+ break;
+
+ case ":":
+ if (state == "name") {
+ name = buffer.trim();
+ buffer = "";
+ state = "value";
+ } else {
+ buffer += character;
+ }
+ break;
+
+ case "!":
+ if (state == "value" && ruleText.indexOf("!important", i) === i) {
+ priority = "important";
+ i += "important".length;
+ } else {
+ buffer += character;
+ }
+ break;
+
+ case ";":
+ if (state == "value") {
+ styleRule.style.setProperty(name, buffer.trim(), priority);
+ priority = "";
+ buffer = "";
+ state = "name";
+ } else {
+ buffer += character;
+ }
+ break;
+
+ case "}":
+ if (state == "value") {
+ styleRule.style.setProperty(name, buffer.trim(), priority);
+ priority = "";
+ buffer = "";
+ } else if (state == "name") {
+ break;
+ } else {
+ buffer += character;
+ }
+ state = "selector";
+ break;
+
+ default:
+ buffer += character;
+ break;
+
+ }
+ }
+
+ return styleRule;
+
+};
+
+
if (typeof exports != "undefined") {
exports.CSSStyleRule = CSSStyleRule;
}
@@ -1,6 +1,6 @@
if (typeof require != "undefined") {
var StyleSheet = require("./StyleSheet").StyleSheet;
- var parse = require("./parse").parse;
+ var CSSStyleRule = require("./CSSStyleRule").CSSStyleRule;
}
/**
@@ -35,7 +35,7 @@ CSSStyleSheet.prototype.insertRule = function(rule, index) {
if (index < 0 || index > this.cssRules.length) {
throw new RangeError("INDEX_SIZE_ERR")
}
- this.cssRules.splice(index, 0, parse(rule).cssRules[0]);
+ this.cssRules.splice(index, 0, CSSStyleRule.parse(rule));
return index
};

0 comments on commit e2cabcc

Please sign in to comment.