tor-browser

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

object-association.js (2700B)


      1 "use strict";
      2 
      3 // This is for testing whether an object (e.g., a global property) is associated with Window, or
      4 // with Document. Recall that Window and Document are 1:1 except when doing a same-origin navigation
      5 // away from the initial about:blank. In that case the Window object gets reused for the new
      6 // Document.
      7 //
      8 // So:
      9 // - If something is per-Window, then it should maintain its identity across an about:blank
     10 //   navigation.
     11 // - If something is per-Document, then it should be recreated across an about:blank navigation.
     12 
     13 window.testIsPerWindow = propertyName => {
     14  runTests(propertyName, assert_equals, "must not");
     15 };
     16 
     17 window.testIsPerDocument = propertyName => {
     18  runTests(propertyName, assert_not_equals, "must");
     19 };
     20 
     21 function runTests(propertyName, equalityOrInequalityAsserter, mustOrMustNotReplace) {
     22  async_test(t => {
     23    const iframe = document.createElement("iframe");
     24    document.body.appendChild(iframe);
     25    const frame = iframe.contentWindow;
     26 
     27    const before = frame[propertyName];
     28    assert_implements(before, `window.${propertyName} must be implemented`);
     29 
     30    iframe.onload = t.step_func_done(() => {
     31      const after = frame[propertyName];
     32      equalityOrInequalityAsserter(after, before);
     33    });
     34 
     35    iframe.src = "/common/blank.html";
     36  }, `Navigating from the initial about:blank ${mustOrMustNotReplace} replace window.${propertyName}`);
     37 
     38  // Per spec, discarding a browsing context should not change any of the global objects.
     39  test(() => {
     40    const iframe = document.createElement("iframe");
     41    document.body.appendChild(iframe);
     42    const frame = iframe.contentWindow;
     43 
     44    const before = frame[propertyName];
     45    assert_implements(before, `window.${propertyName} must be implemented`);
     46 
     47    iframe.remove();
     48 
     49    const after = frame[propertyName];
     50    assert_equals(after, before, `window.${propertyName} should not change after iframe.remove()`);
     51  }, `Discarding the browsing context must not change window.${propertyName}`);
     52 
     53  // Per spec, document.open() should not change any of the global objects. In historical versions
     54  // of the spec, it did, so we test here.
     55  async_test(t => {
     56    const iframe = document.createElement("iframe");
     57 
     58    iframe.onload = t.step_func_done(() => {
     59      const frame = iframe.contentWindow;
     60      const before = frame[propertyName];
     61      assert_implements(before, `window.${propertyName} must be implemented`);
     62 
     63      frame.document.open();
     64 
     65      const after = frame[propertyName];
     66      assert_equals(after, before);
     67 
     68      frame.document.close();
     69    });
     70 
     71    iframe.src = "/common/blank.html";
     72    document.body.appendChild(iframe);
     73  }, `document.open() must not replace window.${propertyName}`);
     74 }