tor-browser

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

about-blank-iframes.html (4149B)


      1 <!doctype html>
      2 
      3 <title>about:blank iframe initiator and prerendered page</title>
      4 <script src="/common/get-host-info.sub.js"></script>
      5 <script src="/resources/testharness.js"></script>
      6 <script src="/resources/testharnessreport.js"></script>
      7 <script src="utils.js"></script>
      8 <script>
      9 // Called by iframe-nav-to-about-blank in case empty.html loads before
     10 // we can add the proper onload handler below.
     11 let iframeLoadedEmptyHtml = false;
     12 function onIframeLoadedEmptyHtml() {
     13  iframeLoadedEmptyHtml = true;
     14 }
     15 </script>
     16 <body>
     17 <iframe id="iframe-no-src"></iframe>
     18 <iframe id="iframe-srcdoc" srcdoc="<!doctype html><p>wow look</p>"></iframe>
     19 <iframe id="iframe-nav-to-about-blank" src="empty.html"
     20        onload="onIframeLoadedEmptyHtml();"></iframe>
     21 <script>
     22 
     23 // When loaded without the "?prerendering" param, this document
     24 // is called the "intiator page". It starts a prerender to the same
     25 // URL, but with the "?prerendering" param.
     26 //
     27 // When loaded with the "?prerendering" param, this document is
     28 // the "prerendered page". It tells the initiator page when it is
     29 // ready to activate, and messages the main test page when it is
     30 // finished.
     31 
     32 // Runs the logic of the prerendered page.
     33 //
     34 // The prerendered page has about:blank/srcdoc iframes and tests their
     35 // document.prerendering state before and after activation.
     36 async function main() {
     37  // The iframe-no-src iframe has no src attribute.
     38  const iframeNoSrc = document.querySelector('#iframe-no-src');
     39 
     40  // The iframe-srcdoc iframe has a srcdoc attribute.
     41  const iframeSrcdoc = document.querySelector('#iframe-srcdoc');
     42 
     43  // The iframe-nav-to-about-blank first navigates to a non-about:blank URL,
     44  // then sets src to about:blank.
     45  const iframeNavToAboutBlank =
     46     document.querySelector('#iframe-nav-to-about-blank');
     47 
     48  await new Promise((resolve, reject) => {
     49    iframeNavToAboutBlank.addEventListener('load', (e) => {
     50      if (iframeNavToAboutBlank.src != 'about:blank')
     51        iframeNavToAboutBlank.src = 'about:blank';
     52      else
     53        resolve();
     54    });
     55 
     56    // In case the original navigation to empty.html already finished before we
     57    // added the event listener above, navigate to about:blank now.
     58    if (iframeLoadedEmptyHtml)
     59      iframeNavToAboutBlank.src = 'about:blank';
     60  });
     61 
     62  // Collect the documents.
     63  let frames = [
     64    {doc: document, name: 'main'},
     65    {doc: iframeNoSrc.contentDocument, name: 'iframeNoSrc'},
     66    {doc: iframeSrcdoc.contentDocument, name: 'iframeSrcdoc'},
     67    {doc: iframeNavToAboutBlank.contentDocument, name: 'iframeNavToAboutBlank'}
     68  ];
     69 
     70  // Test document.prerendering pre-activation for each document.
     71  for (let i = 0; i < frames.length; i++) {
     72    assert_true(frames[i].doc.prerendering,
     73                `document.prerendering pre-activation: ${frames[i].name}`);
     74  }
     75 
     76  // Resolves with [bool, bool, bool, ...] upon activation. Each `bool` is the
     77  // value of document.prerendering in the prerenderingchange event for the
     78  // corresponding document.
     79  let activationPromises = frames.map(x => {
     80    return new Promise((resolve, reject) => {
     81      x.doc.addEventListener('prerenderingchange', (e) => {
     82        resolve(document.prerendering);
     83      });
     84    });
     85  });
     86 
     87  // Ask to activate.
     88  const prerenderChannel = new PrerenderChannel('prerender-channel');
     89  prerenderChannel.postMessage('readyToActivate');
     90 
     91  // Test document.prerendering post-activation for each document.
     92  let activationResults = await Promise.all(activationPromises);
     93  for (let i = 0; i < activationResults.length; i++) {
     94    assert_false(activationResults[i],
     95        `document.prerendering in prerenderingchange for ${frames[i].name}`);
     96  }
     97 }
     98 
     99 // See comment at the top of this file.
    100 const params = new URLSearchParams(location.search);
    101 const isPrerendering = params.has('prerendering');
    102 if (!isPrerendering) {
    103  loadInitiatorPage();
    104 } else {
    105  // For the prerendering page, run main() then message the test page with the
    106  // result.
    107  const testChannel = new PrerenderChannel('test-channel');
    108  main().then(() => {
    109    testChannel.postMessage('PASS');
    110  }).catch((e) => {
    111    testChannel.postMessage('FAIL: ' + e);
    112  });
    113 }
    114 </script>
    115 </body>