You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
3.5 KiB

  1. //.CommonJS
  2. var CSSOM = {
  3. CSSStyleDeclaration: require("./CSSStyleDeclaration").CSSStyleDeclaration,
  4. CSSRule: require("./CSSRule").CSSRule
  5. };
  6. ///CommonJS
  7. /**
  8. * @constructor
  9. * @see http://dev.w3.org/csswg/cssom/#cssstylerule
  10. * @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleRule
  11. */
  12. CSSOM.CSSStyleRule = function CSSStyleRule() {
  13. CSSOM.CSSRule.call(this);
  14. this.selectorText = "";
  15. this.style = new CSSOM.CSSStyleDeclaration();
  16. this.style.parentRule = this;
  17. };
  18. CSSOM.CSSStyleRule.prototype = new CSSOM.CSSRule();
  19. CSSOM.CSSStyleRule.prototype.constructor = CSSOM.CSSStyleRule;
  20. CSSOM.CSSStyleRule.prototype.type = 1;
  21. Object.defineProperty(CSSOM.CSSStyleRule.prototype, "cssText", {
  22. get: function() {
  23. var text;
  24. if (this.selectorText) {
  25. text = this.selectorText + " {" + this.style.cssText + "}";
  26. } else {
  27. text = "";
  28. }
  29. return text;
  30. },
  31. set: function(cssText) {
  32. var rule = CSSOM.CSSStyleRule.parse(cssText);
  33. this.style = rule.style;
  34. this.selectorText = rule.selectorText;
  35. }
  36. });
  37. /**
  38. * NON-STANDARD
  39. * lightweight version of parse.js.
  40. * @param {string} ruleText
  41. * @return CSSStyleRule
  42. */
  43. CSSOM.CSSStyleRule.parse = function(ruleText) {
  44. var i = 0;
  45. var state = "selector";
  46. var index;
  47. var j = i;
  48. var buffer = "";
  49. var SIGNIFICANT_WHITESPACE = {
  50. "selector": true,
  51. "value": true
  52. };
  53. var styleRule = new CSSOM.CSSStyleRule();
  54. var name, priority="";
  55. for (var character; (character = ruleText.charAt(i)); i++) {
  56. switch (character) {
  57. case " ":
  58. case "\t":
  59. case "\r":
  60. case "\n":
  61. case "\f":
  62. if (SIGNIFICANT_WHITESPACE[state]) {
  63. // Squash 2 or more white-spaces in the row into 1
  64. switch (ruleText.charAt(i - 1)) {
  65. case " ":
  66. case "\t":
  67. case "\r":
  68. case "\n":
  69. case "\f":
  70. break;
  71. default:
  72. buffer += " ";
  73. break;
  74. }
  75. }
  76. break;
  77. // String
  78. case '"':
  79. j = i + 1;
  80. index = ruleText.indexOf('"', j) + 1;
  81. if (!index) {
  82. throw '" is missing';
  83. }
  84. buffer += ruleText.slice(i, index);
  85. i = index - 1;
  86. break;
  87. case "'":
  88. j = i + 1;
  89. index = ruleText.indexOf("'", j) + 1;
  90. if (!index) {
  91. throw "' is missing";
  92. }
  93. buffer += ruleText.slice(i, index);
  94. i = index - 1;
  95. break;
  96. // Comment
  97. case "/":
  98. if (ruleText.charAt(i + 1) === "*") {
  99. i += 2;
  100. index = ruleText.indexOf("*/", i);
  101. if (index === -1) {
  102. throw new SyntaxError("Missing */");
  103. } else {
  104. i = index + 1;
  105. }
  106. } else {
  107. buffer += character;
  108. }
  109. break;
  110. case "{":
  111. if (state === "selector") {
  112. styleRule.selectorText = buffer.trim();
  113. buffer = "";
  114. state = "name";
  115. }
  116. break;
  117. case ":":
  118. if (state === "name") {
  119. name = buffer.trim();
  120. buffer = "";
  121. state = "value";
  122. } else {
  123. buffer += character;
  124. }
  125. break;
  126. case "!":
  127. if (state === "value" && ruleText.indexOf("!important", i) === i) {
  128. priority = "important";
  129. i += "important".length;
  130. } else {
  131. buffer += character;
  132. }
  133. break;
  134. case ";":
  135. if (state === "value") {
  136. styleRule.style.setProperty(name, buffer.trim(), priority);
  137. priority = "";
  138. buffer = "";
  139. state = "name";
  140. } else {
  141. buffer += character;
  142. }
  143. break;
  144. case "}":
  145. if (state === "value") {
  146. styleRule.style.setProperty(name, buffer.trim(), priority);
  147. priority = "";
  148. buffer = "";
  149. } else if (state === "name") {
  150. break;
  151. } else {
  152. buffer += character;
  153. }
  154. state = "selector";
  155. break;
  156. default:
  157. buffer += character;
  158. break;
  159. }
  160. }
  161. return styleRule;
  162. };
  163. //.CommonJS
  164. exports.CSSStyleRule = CSSOM.CSSStyleRule;
  165. ///CommonJS