CSSStyleSheet.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //.CommonJS
  2. var CSSOM = {
  3. StyleSheet: require("./StyleSheet").StyleSheet,
  4. CSSStyleRule: require("./CSSStyleRule").CSSStyleRule
  5. };
  6. ///CommonJS
  7. /**
  8. * @constructor
  9. * @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleSheet
  10. */
  11. CSSOM.CSSStyleSheet = function CSSStyleSheet() {
  12. this.cssRules = [];
  13. };
  14. CSSOM.CSSStyleSheet.prototype = new CSSOM.StyleSheet;
  15. CSSOM.CSSStyleSheet.prototype.constructor = CSSOM.CSSStyleSheet;
  16. /**
  17. * Used to insert a new rule into the style sheet. The new rule now becomes part of the cascade.
  18. *
  19. * sheet = new Sheet("body {margin: 0}")
  20. * sheet.toString()
  21. * -> "body{margin:0;}"
  22. * sheet.insertRule("img {border: none}", 0)
  23. * -> 0
  24. * sheet.toString()
  25. * -> "img{border:none;}body{margin:0;}"
  26. *
  27. * @param {string} rule
  28. * @param {number} index
  29. * @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleSheet-insertRule
  30. * @return {number} The index within the style sheet's rule collection of the newly inserted rule.
  31. */
  32. CSSOM.CSSStyleSheet.prototype.insertRule = function(rule, index) {
  33. if (index < 0 || index > this.cssRules.length) {
  34. throw new RangeError("INDEX_SIZE_ERR")
  35. }
  36. this.cssRules.splice(index, 0, CSSOM.CSSStyleRule.parse(rule));
  37. return index
  38. };
  39. /**
  40. * Used to delete a rule from the style sheet.
  41. *
  42. * sheet = new Sheet("img{border:none} body{margin:0}")
  43. * sheet.toString()
  44. * -> "img{border:none;}body{margin:0;}"
  45. * sheet.deleteRule(0)
  46. * sheet.toString()
  47. * -> "body{margin:0;}"
  48. *
  49. * @param {number} index
  50. * @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleSheet-deleteRule
  51. * @return {number} The index within the style sheet's rule list of the rule to remove.
  52. */
  53. CSSOM.CSSStyleSheet.prototype.deleteRule = function(index) {
  54. if (index < 0 || index >= this.cssRules.length) {
  55. throw new RangeError("INDEX_SIZE_ERR");
  56. }
  57. this.cssRules.splice(index, 1);
  58. };
  59. /**
  60. * NON-STANDARD
  61. * @return {string} serialize stylesheet
  62. */
  63. CSSOM.CSSStyleSheet.prototype.toString = function() {
  64. var result = "";
  65. var rules = this.cssRules;
  66. for (var i=0; i<rules.length; i++) {
  67. result += rules[i].cssText + "\n";
  68. }
  69. return result;
  70. };
  71. //.CommonJS
  72. exports.CSSStyleSheet = CSSOM.CSSStyleSheet;
  73. ///CommonJS