Bug 1249119 - Prevent trying to initialize highlighter on still-loading documents. r=pbro draft
authorAlexandre Poirot <poirot.alex@gmail.com>
Wed, 19 Oct 2016 05:19:48 -0700
changeset 427780 dc1317c5d8474a56209fc75ca6978dbadebd9094
parent 427779 f0f1aaf051d6798e1e73d1feee07ca847333167a
child 427781 b9bcce380d5ba721763c10333b8916a7ba8bf17d
push id33111
push userbmo:poirot.alex@gmail.com
push dateThu, 20 Oct 2016 21:28:50 +0000
reviewerspbro
bugs1249119
milestone52.0a1
Bug 1249119 - Prevent trying to initialize highlighter on still-loading documents. r=pbro MozReview-Commit-ID: 3S0LoB40q2W
devtools/server/actors/highlighters.js
devtools/server/actors/highlighters/utils/markup.js
--- a/devtools/server/actors/highlighters.js
+++ b/devtools/server/actors/highlighters.js
@@ -92,17 +92,22 @@ var HighlighterActor = exports.Highlight
     this._tabActor = this._inspector.tabActor;
     this._highlighterEnv = new HighlighterEnvironment();
     this._highlighterEnv.initFromTabActor(this._tabActor);
 
     this._highlighterReady = this._highlighterReady.bind(this);
     this._highlighterHidden = this._highlighterHidden.bind(this);
     this._onNavigate = this._onNavigate.bind(this);
 
-    this._createHighlighter();
+    let doc = this._tabActor.window.document;
+    // Only try to create the highlighter when the document is loaded,
+    // otherwise, wait for the navigate event to fire.
+    if (doc.documentElement && doc.readyState != "uninitialized") {
+      this._createHighlighter();
+    }
 
     // Listen to navigation events to switch from the BoxModelHighlighter to the
     // SimpleOutlineHighlighter, and back, if the top level window changes.
     events.on(this._tabActor, "navigate", this._onNavigate);
   },
 
   get conn() {
     return this._inspector && this._inspector.conn;
--- a/devtools/server/actors/highlighters/utils/markup.js
+++ b/devtools/server/actors/highlighters/utils/markup.js
@@ -263,23 +263,26 @@ CanvasFrameAnonymousContentHelper.protot
     this.highlighterEnv = this.nodeBuilder = this._content = null;
     this.anonymousContentDocument = null;
     this.anonymousContentGlobal = null;
 
     this._removeAllListeners();
   },
 
   _insert: function () {
-    // Insert the content node only if the page isn't in a XUL window, and if
-    // the document still exists.
-    if (!this.highlighterEnv.document.documentElement ||
+    let doc = this.highlighterEnv.document;
+    // Insert the content node only if the document:
+    // * is loaded (navigate event will fire once it is),
+    // * still exists,
+    // * isn't in XUL.
+    if (doc.readyState == "uninitialized" ||
+        !doc.documentElement ||
         isXUL(this.highlighterEnv.window)) {
       return;
     }
-    let doc = this.highlighterEnv.document;
 
     // For now highlighters.css is injected in content as a ua sheet because
     // <style scoped> doesn't work inside anonymous content (see bug 1086532).
     // If it did, highlighters.css would be injected as an anonymous content
     // node using CanvasFrameAnonymousContentHelper instead.
     installHelperSheet(this.highlighterEnv.window,
       "@import url('" + STYLESHEET_URI + "');");
     let node = this.nodeBuilder();