Bug 1457166: Fire online / offline events at the window. r=bz
authorEmilio Cobos Álvarez <emilio@crisal.io>
Thu, 26 Apr 2018 19:02:37 +0200
changeset 472255 eb451c84fd3cb87276884c5ef478584472dbdafd
parent 472254 ce1d5d55b9a4de3b1bff8cec17b2895a5b83e73a
child 472256 1bc26646a5888024161278a261c90ce2f744baf4
push id1728
push userjlund@mozilla.com
push dateMon, 18 Jun 2018 21:12:27 +0000
treeherdermozilla-release@c296fde26f5f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs1457166
milestone61.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1457166: Fire online / offline events at the window. r=bz MozReview-Commit-ID: 2v5zNwM9qSh
dom/base/nsGlobalWindowInner.cpp
dom/events/test/test_bug336682.js
dom/events/test/test_bug336682_1.html
dom/events/test/test_bug336682_2.xul
testing/web-platform/tests/html/browsers/offline/browser-state/navigator_online_event-manual.https.html
--- a/dom/base/nsGlobalWindowInner.cpp
+++ b/dom/base/nsGlobalWindowInner.cpp
@@ -5199,30 +5199,21 @@ nsGlobalWindowInner::FireOfflineStatusEv
   mWasOffline = !mWasOffline;
 
   nsAutoString name;
   if (mWasOffline) {
     name.AssignLiteral("offline");
   } else {
     name.AssignLiteral("online");
   }
-  // The event is fired at the body element, or if there is no body element,
-  // at the document.
-  nsCOMPtr<EventTarget> eventTarget = mDoc.get();
-  if (mDoc->IsHTMLOrXHTML()) {
-    if (Element* body = mDoc->GetBody()) {
-      eventTarget = body;
-    }
-  } else {
-    Element* documentElement = mDoc->GetDocumentElement();
-    if (documentElement) {
-      eventTarget = documentElement;
-    }
-  }
-  nsContentUtils::DispatchTrustedEvent(mDoc, eventTarget, name, true, false);
+  nsContentUtils::DispatchTrustedEvent(mDoc,
+                                       static_cast<EventTarget*>(this),
+                                       name,
+                                       false,
+                                       false);
 }
 
 class NotifyIdleObserverRunnable : public Runnable
 {
 public:
   NotifyIdleObserverRunnable(nsIIdleObserver* aIdleObserver,
                              uint32_t aTimeInS,
                              bool aCallOnidle,
--- a/dom/events/test/test_bug336682.js
+++ b/dom/events/test/test_bug336682.js
@@ -11,33 +11,16 @@ var gState = 0;
  */
 var MAX_STATE;
 
 function trace(text) {
   var t = text.replace(/&/g, "&" + "amp;").replace(/</g, "&" + "lt;") + "<br>";
   //document.getElementById("display").innerHTML += t;
 }
 
-// window.ononline and window.onclick shouldn't work
-// Right now, <body ononline=...> sets window.ononline (bug 380618)
-// When these start passing, be sure to uncomment the code inside if(0) below.
-todo(typeof window.ononline == "undefined",
-     "window.ononline should be undefined at this point");
-todo(typeof window.onoffline == "undefined",
-     "window.onoffline should be undefined at this point");
-
-if (0) {
-  window.ononline = function() {
-    ok(false, "window.ononline shouldn't be called");
-  }
-  window.onoffline = function() {
-    ok(false, "window.onclick shouldn't be called");
-  }
-}
-
 /**
  * Returns a handler function for an online/offline event. The returned handler
  * ensures the passed event object has expected properties and that the handler
  * is called at the right moment (according to the gState variable).
  * @param nameTemplate The string identifying the hanlder. '%1' in that
  *                     string will be replaced with the event name.
  * @param eventName 'online' or 'offline'
  * @param expectedStates an array listing the possible values of gState at the
@@ -50,21 +33,19 @@ function makeHandler(nameTemplate, event
     var name = nameTemplate.replace(/%1/, eventName);
     ++gState;
     trace(name + ": gState=" + gState);
     ok(expectedStates.includes(gState),
        "handlers called in the right order: " + name + " is called, " + 
        "gState=" + gState + ", expectedStates=" + expectedStates);
     ok(e.constructor == Event, "event should be an Event");
     ok(e.type == eventName, "event type should be " + eventName);
-    ok(e.bubbles, "event should bubble");
+    ok(!e.bubbles, "event should not bubble");
     ok(!e.cancelable, "event should not be cancelable");
-    ok(e.target == (document instanceof HTMLDocument
-                    ? document.body : document.documentElement),
-       "the event target should be the body element");
+    ok(e.target == window, "target should be the window");
   }
 }
 
 function doTest() {
   var iosvc = SpecialPowers.Cc["@mozilla.org/network/io-service;1"]
                            .getService(SpecialPowers.Ci.nsIIOService);
   iosvc.manageOfflineStatus = false;
   iosvc.offline = false;
--- a/dom/events/test/test_bug336682_1.html
+++ b/dom/events/test/test_bug336682_1.html
@@ -23,41 +23,31 @@ http://creativecommons.org/licenses/publ
 <pre id="test">
 </pre>
 <script type="text/javascript" src="test_bug336682.js"></script>
 
 <script class="testbody" type="text/javascript">
 
 function makeBodyHandler(eventName) {
   return function (aThis, aEvent) {
-    var handler = makeHandler("<body on%1='...'>", eventName, [3,4]);
+    var handler = makeHandler("<body on%1='...'>", eventName, [1]);
     handler(aEvent);
   }
 }
 addLoadEvent(function() {
   /** @see test_bug336682.js */
-  MAX_STATE = 4;
+  MAX_STATE = 2;
 
   for (var event of ["online", "offline"]) {
-    document.body.addEventListener(
-      event,
-      makeHandler("document.body.addEventListener('%1', ..., false)",
-                  event, [1]));
-
-    document.addEventListener(
-      event,
-      makeHandler("document.addEventListener('%1', ..., false)",
-                  event, [2]));
-
     window["bodyOn" + event] = makeBodyHandler(event);
 
     window.addEventListener(
       event,
       makeHandler("window.addEventListener('%1', ..., false)",
-                  event, [3,4]));
+                  event, [2]));
   }
 
   doTest();
   SimpleTest.finish();
 });
 
 SimpleTest.waitForExplicitFinish();
 </script>
--- a/dom/events/test/test_bug336682_2.xul
+++ b/dom/events/test/test_bug336682_2.xul
@@ -24,44 +24,32 @@ Mozilla Bug 336682 (online/offline event
 </div>
 </body>
 
 <script type="text/javascript" src="test_bug336682.js"/>
 <script class="testbody" type="text/javascript">
 <![CDATA[
 addLoadEvent(function() {
   /** @see test_bug336682.js */
-  MAX_STATE = 4;
+  MAX_STATE = 2;
 
   function makeWindowHandler(eventName) {
     return function (aThis, aEvent) {
-      var handler = makeHandler("<body on%1='...'>", eventName, [3,4]);
+      var handler = makeHandler("<body on%1='...'>", eventName, [1]);
       handler(aEvent);
     }
   }
 
   for (var event of ["online", "offline"]) {
-    document.documentElement.addEventListener(
-      event,
-      makeHandler("document.body.addEventListener('%1', ..., false)",
-                  event, [1]),
-      false);
-
-    document.addEventListener(
-      event,
-      makeHandler("document.addEventListener('%1', ..., false)",
-                  event, [2]),
-      false);
-
     window["windowOn" + event] = makeWindowHandler(event);
 
     window.addEventListener(
       event,
       makeHandler("window.addEventListener('%1', ..., false)",
-                  event, [3,4]),
+                  event, [2]),
       false);
   }
 
   doTest();
   SimpleTest.finish();
 });
 
 SimpleTest.waitForExplicitFinish();
--- a/testing/web-platform/tests/html/browsers/offline/browser-state/navigator_online_event-manual.https.html
+++ b/testing/web-platform/tests/html/browsers/offline/browser-state/navigator_online_event-manual.https.html
@@ -20,21 +20,27 @@
   </div>
 
   <h2>Expected Result</h2>
   <div id="expectedResult">
     <span id="expectedMsg">apply 'work offline': offline event is raised.<p>release 'work offline': online event is raised.</span>
   </div>
   <script>
 
-  function showOnline() {
-    document.getElementById('actualMsg').innerHTML = 'online event is raised.';
+  function showOnline(e) {
+    let msg = 'online event is raised';
+    if (e.target != window)
+      msg += ' (on the WRONG target)';
+    document.getElementById('actualMsg').innerHTML = msg + '.';
   }
 
-  function showOffline() {
-    document.getElementById('actualMsg').innerHTML = 'offline event is raised.';
+  function showOffline(e) {
+    let msg = 'offline event is raised';
+    if (e.target != window)
+      msg += ' (on the WRONG target)';
+    document.getElementById('actualMsg').innerHTML = msg + '.';
   }
 
   window.addEventListener("online", showOnline, false);
   window.addEventListener("offline", showOffline, false);
   </script>
  </body>
 </html>