replacement-enabled.html (2713B)
1 <!DOCTYPE html> 2 <meta charset="utf-8"> 3 <title>Navigating to a fragment should not clear forward history</title> 4 <link rel="help" href="https://html.spec.whatwg.org/multipage/#scroll-to-fragid"> 5 <link rel="help" href="https://github.com/whatwg/html/issues/2796"> 6 <link rel="help" href="https://github.com/whatwg/html/pull/2869"> 7 <link rel="author" href="mailto:d@domenic.me" title="Domenic Denicola"> 8 9 <script src="/resources/testharness.js"></script> 10 <script src="/resources/testharnessreport.js"></script> 11 <script src="navigate-helpers.js"></script> 12 13 <body> 14 15 <script> 16 "use strict"; 17 let resolve, iframe; 18 promise_test(() => { 19 iframe = document.createElement("iframe"); 20 iframe.src = "/common/blank.html"; 21 iframe.addEventListener("load", runTest); 22 document.body.appendChild(iframe); 23 24 return new Promise(r => resolve = r); 25 }); 26 27 function runTest() { 28 iframe.removeEventListener("load", runTest); 29 const frameWindow = iframe.contentWindow; 30 31 resolve((async () => { 32 await navigateAndWaitForChange(frameWindow, w => w.location.href = "/common/blank.html#apple"); 33 await navigateAndWaitForChange(frameWindow, w => w.location.href = "/common/blank.html#banana"); 34 await navigateAndWaitForChange(frameWindow, w => w.location.href = "/common/blank.html#cat"); 35 36 assert_equals(frameWindow.location.hash, "#cat"); 37 38 // Might not be 4 (= 3 for iframe + 1 initial) due to cross-browser differences or if people are 39 // running this test in a window that has previously been places. The important thing for this 40 // test is the delta from this value. 41 const afterThreeNavigations = frameWindow.history.length; 42 43 await navigateAndWaitForChange(frameWindow, w => w.history.back()); 44 45 assert_equals(frameWindow.location.hash, "#banana"); 46 assert_equals(frameWindow.history.length, afterThreeNavigations, 47 "back() must not change the history length"); 48 49 await navigateAndWaitForChange(frameWindow, 50 w => w.location.replace("/common/blank.html#zebra")); 51 52 assert_equals(frameWindow.location.hash, "#zebra"); 53 assert_equals(frameWindow.history.length, afterThreeNavigations, 54 "replace() must not change the history length"); 55 56 // As of the time of this writing (2017-08-14), Firefox is not firing hashchange on forward, so 57 // we automatically assume navigation succeeded after 100 ms. A sibling test will test this 58 // particular Firefox bug. 59 await navigateAndWaitForChange(frameWindow, w => w.history.forward(), 60 { assumeSuccessAfter: 500 }); 61 62 assert_equals(frameWindow.location.hash, "#cat"); 63 assert_equals(frameWindow.history.length, afterThreeNavigations, 64 "forward() must not change the history length"); 65 66 })()); 67 } 68 69 </script>