tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

navigation-api-prevent-default.window.js (1511B)


      1 // META: script=/resources/testdriver.js
      2 // META: script=/resources/testdriver-vendor.js
      3 
      4 // This test shows that preventDefault() on the navigate event can
      5 // prevent a soft navigation, because it ensures that neither the a.href
      6 // (foobar.html in our example) is visited, nor the handler specified in the
      7 // intercept on the navigate event is called.
      8 
      9 const link = document.createElement('a');
     10 link.href = 'foobar.html';
     11 link.textContent = 'Click me!';
     12 document.body.appendChild(link);
     13 
     14 promise_test(async (t) => {
     15  let navigateProcessed = false;
     16 
     17  navigation.addEventListener('navigate', (e) => {
     18    e.intercept({
     19      async handler() {
     20        assert_unreached('preventDefault() should prevent the navigation');
     21      },
     22    });
     23    e.preventDefault();
     24    navigateProcessed = true;
     25  });
     26 
     27  if (test_driver) {
     28    test_driver.click(link);
     29  }
     30 
     31  await t.step_wait(
     32      () => navigateProcessed, '\'navigate\' event not processed');
     33 
     34  const observer = new PerformanceObserver(() => {
     35    assert_unreached('Soft navigation should not be triggered');
     36  });
     37  observer.observe({type: 'soft-navigation', buffered: true});
     38 
     39  await new Promise((resolve) => {
     40    t.step_timeout(resolve, 3000);
     41  }).then(() => {
     42    observer.disconnect();
     43  });
     44  assert_equals(performance.getEntriesByType('soft-navigation').length, 0, 'Soft Navigation not detected');
     45  assert_false(location.href.includes('foobar.html'), 'foobar.html not visited');
     46 }, 'Navigation API: Aborted navigate event is not a soft navigation');