tor-browser

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

external-script-descendent-with-scope.html (2490B)


      1 <!DOCTYPE html>
      2 <html>
      3 <head>
      4  <script src="/resources/testharness.js"></script>
      5  <script src="/resources/testharnessreport.js"></script>
      6 </head>
      7 <body>
      8 <script type="importmap">
      9 {
     10  "imports": {
     11    "a": "../resources/log.sub.js?name=A"
     12  }
     13 }
     14 </script>
     15 <script type="module" src="../resources/importer.sub.js?name=a"></script>
     16 <script>
     17 const variable_defined = (variable) => {
     18  return new Promise((resolve) => {
     19    const interval = setInterval(() => {
     20      if (eval(variable) !== undefined) {
     21        clearInterval(interval);
     22        resolve(variable);
     23      }
     24    }, 10);
     25  });
     26 };
     27 const log = [];
     28 promise_test(async () => {
     29  await variable_defined("log[0]");
     30  // Try to redefine the descendent with an import map
     31  const importMapScript = document.createElement('script');
     32  importMapScript.type = 'importmap';
     33  importMapScript.textContent =
     34 `{
     35  "scopes": {
     36    "/import-maps/resources/": {
     37      "a": "../resources/log.sub.js?name=B"
     38    },
     39    "/resources/": {
     40      "a": "../resources/log.sub.js?name=D",
     41       "../resources/": "/foobar/"
     42    }
     43  },
     44  "imports": {
     45    "a": "../resources/log.sub.js?name=C"
     46  }
     47 }`;
     48  document.head.appendChild(importMapScript);
     49 
     50  const inScopeModule = document.createElement('script');
     51  inScopeModule.src = "../resources/in-scope-test.js";
     52  inScopeModule.type = 'module';
     53  document.head.appendChild(inScopeModule);
     54  await variable_defined("window.inscope_test_result");
     55  assert_true(window.inscope_test_result
     56              .endsWith("/resources/log.sub.js?name=A"), "inscope");
     57 
     58  const outOfScopeModule = document.createElement('script');
     59  outOfScopeModule.src = "/resources/out-of-scope-test.js";
     60  outOfScopeModule.type = 'module';
     61  document.head.appendChild(outOfScopeModule);
     62  await variable_defined("window.outscope_test_result");
     63  assert_true(window.outscope_test_result
     64              .endsWith("/resources/log.sub.js?name=D"), "out of scope 1");
     65  assert_true(window.outscope_test_result2
     66              .endsWith("/resources/log.sub.js?name=E"), "out of scope 2");
     67 
     68  const inlineModule = document.createElement('script');
     69  inlineModule.type = 'module';
     70  inlineModule.innerHTML = `window.inline_test_result = import.meta.resolve("a");`;
     71  document.head.appendChild(inlineModule);
     72  await variable_defined("window.inline_test_result");
     73  assert_true(window.inline_test_result
     74              .endsWith("/resources/log.sub.js?name=A"), "inline");
     75 }, "Testing descendent resolution with scopes");
     76 </script>
     77 </body>
     78 </html>