Bug 1675375 Part 7: Update expectations in helper_hittest_clippath.html. r=botond
[gecko.git] / devtools / shared / dom-helpers.js
bloba0b7cf113de4eda7dc01f7111489bc82ab78b32d
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
7 const Services = require("Services");
9 exports.DOMHelpers = {
10   /**
11    * A simple way to be notified (once) when a window becomes
12    * interactive (DOMContentLoaded).
13    *
14    * It is based on the chromeEventHandler. This is useful when
15    * chrome iframes are loaded in content docshells (in Firefox
16    * tabs for example).
17    *
18    * @param nsIDOMWindow win
19    *        The content window, owning the document to traverse.
20    * @param Function callback
21    *        The method to call when the frame is loaded.
22    * @param String targetURL
23    *        (optional) Check that the frame URL corresponds to the provided URL
24    *        before calling the callback.
25    */
26   onceDOMReady: function(win, callback, targetURL) {
27     if (!win) {
28       throw new Error("window can't be null or undefined");
29     }
30     const docShell = win.docShell;
31     const onReady = function(event) {
32       if (event.target == win.document) {
33         docShell.chromeEventHandler.removeEventListener(
34           "DOMContentLoaded",
35           onReady
36         );
37         // If in `callback` the URL of the window is changed and a listener to DOMContentLoaded
38         // is attached, the event we just received will be also be caught by the new listener.
39         // We want to avoid that so we execute the callback in the next queue.
40         Services.tm.dispatchToMainThread(callback);
41       }
42     };
43     if (
44       (win.document.readyState == "complete" ||
45         win.document.readyState == "interactive") &&
46       win.location.href == targetURL
47     ) {
48       Services.tm.dispatchToMainThread(callback);
49     } else {
50       docShell.chromeEventHandler.addEventListener("DOMContentLoaded", onReady);
51     }
52   },