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