Bug 1484849 - history.go(0) should reload the current page and not involve the entire session history r=smaug
authorPerry Jiang <perry@mozilla.com>
Thu, 11 Oct 2018 15:43:25 +0000
changeset 496500 858b41d7b9edd2c999ffc616ad76a0e96a9b97ca
parent 496499 4f01697d75235af299f7464d2b6adcbc27eed2ad
child 496501 2099bd3c295d191c41d0015adc986ebe4407faef
push id9984
push userffxbld-merge
push dateMon, 15 Oct 2018 21:07:35 +0000
treeherdermozilla-beta@183d27ea8570 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1484849
milestone64.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 1484849 - history.go(0) should reload the current page and not involve the entire session history r=smaug Differential Revision: https://phabricator.services.mozilla.com/D8322
dom/base/nsHistory.cpp
testing/web-platform/tests/html/browsers/history/the-history-interface/blank-new.html
testing/web-platform/tests/html/browsers/history/the-history-interface/blank-old.html
testing/web-platform/tests/html/browsers/history/the-history-interface/iframe_history_go_0.html
--- a/dom/base/nsHistory.cpp
+++ b/dom/base/nsHistory.cpp
@@ -14,17 +14,19 @@
 #include "nsPresContext.h"
 #include "nsIDocShell.h"
 #include "nsIWebNavigation.h"
 #include "nsIURI.h"
 #include "nsIInterfaceRequestorUtils.h"
 #include "nsReadableUtils.h"
 #include "nsContentUtils.h"
 #include "nsISHistory.h"
+#include "mozilla/dom/Location.h"
 #include "mozilla/Preferences.h"
+#include "mozilla/RefPtr.h"
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
 //
 //  History class implementation
 //
 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(nsHistory)
@@ -181,16 +183,31 @@ nsHistory::Go(int32_t aDelta, ErrorResul
 
       nsPresContext *pcx;
       if (doc && (pcx = doc->GetPresContext())) {
         pcx->RebuildAllStyleData(NS_STYLE_HINT_REFLOW, eRestyle_Subtree);
       }
 
       return;
     }
+
+    // https://html.spec.whatwg.org/multipage/history.html#the-history-interface
+    // "When the go(delta) method is invoked, if delta is zero, the user agent
+    // must act as if the location.reload() method was called instead."
+    RefPtr<Location> location = window ? window->GetLocation() : nullptr;
+
+    if (location) {
+      nsresult rv = location->Reload(false);
+
+      if (NS_FAILED(rv)) {
+        aRv.Throw(NS_ERROR_FAILURE);
+      }
+
+      return;
+    }
   }
 
   RefPtr<ChildSHistory> session_history = GetSessionHistory();
   if (!session_history) {
     aRv.Throw(NS_ERROR_FAILURE);
 
     return;
   }
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/tests/html/browsers/history/the-history-interface/blank-new.html
@@ -0,0 +1,5 @@
+<!DOCTYPE html>
+<head>
+  <title>New page</title>
+</head>
+<body>This is a new page.</body>
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/tests/html/browsers/history/the-history-interface/blank-old.html
@@ -0,0 +1,5 @@
+<!DOCTYPE html>
+<head>
+  <title>Old page</title>
+</head>
+<body>This is an old page.</body>
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/tests/html/browsers/history/the-history-interface/iframe_history_go_0.html
@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<head>
+  <title>iframe_history_go_0</title>
+  <script src="/resources/testharness.js"></script>
+  <script src="/resources/testharnessreport.js"></script>
+</head>
+<body>
+<iframe></iframe>
+<script>
+promise_test(async (t) => {
+  let iframe = null;
+  const OLD_URL = 'blank-old.html';
+  const NEW_URL = 'blank-new.html';
+
+  await new Promise(resolve => {
+    iframe = document.createElement('iframe');
+    iframe.onload = () => resolve();
+    iframe.src = OLD_URL;
+    document.body.appendChild(iframe);
+    t.add_cleanup(() => iframe.remove());
+  });
+
+  assert_equals(iframe.contentDocument.body.textContent, 'This is an old page.\n');
+
+  await new Promise(resolve => {
+    iframe.onload = () => resolve();
+    iframe.src = NEW_URL;
+  });
+
+  assert_equals(iframe.contentDocument.body.textContent, 'This is a new page.\n');
+
+  await new Promise(resolve => {
+    iframe.onload = () => resolve();
+    iframe.contentWindow.history.go(0);
+  });
+
+  assert_equals(iframe.contentDocument.body.textContent, 'This is a new page.\n');
+}, 'iframe\'s history.go(0) performs a location.reload()');
+</script>
+</body>