Bug 1457166: Fire online / offline events at the window. r?bz draft
authorEmilio Cobos Álvarez <emilio@crisal.io>
Thu, 26 Apr 2018 19:02:37 +0200
changeset 788596 a31f066c058ac4a26f33dccb22451324dc3cf1fe
parent 788570 422aebe40856002e8e53916433703840dc6fafa9
push id108023
push userbmo:emilio@crisal.io
push dateThu, 26 Apr 2018 17:05:55 +0000
reviewersbz
bugs1457166
milestone61.0a1
Bug 1457166: Fire online / offline events at the window. r?bz MozReview-Commit-ID: 2v5zNwM9qSh
dom/base/nsGlobalWindowInner.cpp
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
@@ -5226,30 +5226,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,
+                                       true,
+                                       false);
 }
 
 class NotifyIdleObserverRunnable : public Runnable
 {
 public:
   NotifyIdleObserverRunnable(nsIIdleObserver* aIdleObserver,
                              uint32_t aTimeInS,
                              bool aCallOnidle,
--- 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>