Bug 1333711 - guard against DOMExceptions when accessing stylesheet cssRules;r=pbro draft
authorJulian Descottes <jdescottes@mozilla.com>
Mon, 13 Feb 2017 16:23:39 +0100
changeset 483009 64e5014d3b163a87be2c78661eeb1156a4d79790
parent 482807 912644a94bcc94ed0e40e76e23f732be1bc99232
child 483478 5d4da3e2009ab869998782697e8c11a000353e15
push id45189
push userjdescottes@mozilla.com
push dateMon, 13 Feb 2017 16:33:24 +0000
reviewerspbro
bugs1333711
milestone54.0a1
Bug 1333711 - guard against DOMExceptions when accessing stylesheet cssRules;r=pbro MozReview-Commit-ID: C2OoN0uZIo
devtools/server/css-logic.js
--- a/devtools/server/css-logic.js
+++ b/devtools/server/css-logic.js
@@ -244,17 +244,17 @@ CssLogic.prototype = {
     }
 
     // Cache the sheet.
     let cssSheet = this.getSheet(domSheet, this._sheetIndex++);
     if (cssSheet._passId != this._passId) {
       cssSheet._passId = this._passId;
 
       // Find import and keyframes rules.
-      for (let aDomRule of domSheet.cssRules) {
+      for (let aDomRule of cssSheet.getCssRules()) {
         if (aDomRule.type == CSSRule.IMPORT_RULE &&
             aDomRule.styleSheet &&
             this.mediaMatches(aDomRule)) {
           this._cacheSheet(aDomRule.styleSheet);
         } else if (aDomRule.type == CSSRule.KEYFRAMES_RULE) {
           this._keyframesRules.push(aDomRule);
         }
       }
@@ -814,19 +814,39 @@ CssSheet.prototype = {
   },
 
   /**
    * Retrieve the number of rules in this stylesheet.
    *
    * @return {number} the number of nsIDOMCSSRule objects in this stylesheet.
    */
   get ruleCount() {
-    return this._ruleCount > -1 ?
-      this._ruleCount :
-      this.domSheet.cssRules.length;
+    try {
+      return this._ruleCount > -1 ?
+        this._ruleCount :
+        this.getCssRules().length;
+    } catch (e) {
+      return 0;
+    }
+  },
+
+  /**
+   * Retrieve the array of css rules for this stylesheet.
+   *
+   * Accessing cssRules on a stylesheet that is not completely loaded can throw a
+   * DOMException (Bug 625013). This wrapper will return an empty array instead.
+   *
+   * @return {Array} array of css rules.
+   **/
+  getCssRules: function () {
+    try {
+      return this.domSheet.cssRules;
+    } catch (e) {
+      return [];
+    }
   },
 
   /**
    * Retrieve a CssRule object for the given CSSStyleRule. The CssRule object is
    * cached, such that subsequent retrievals return the same CssRule object for
    * the same CSSStyleRule object.
    *
    * @param {CSSStyleRule} aDomRule the CSSStyleRule object for which you want a