summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/browsers/browsing-the-web/navigating-across-documents/multiple-globals/resources/context-helper.js
blob: dda338b4ccb11a7cdfad5d31c929757cd2687865 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Usage: in the top-level Window, include:
//
// <script src="resources/context-helper.js"></script>
// <script>
//   window.scriptToRun = '...';
// </script>
// <iframe id="entry" src="entry/entry.html"></iframe>
//
// Then `scriptToRun` is evaluated, with:
// - The entry Realm is that of entry/entry.html
// - The incumbent Realm is that of incumbent/empty.html
// - The relevant Realm of `relevantWindow`, `relevantWindow.location` etc. is
//   that of relevant/empty.html

window.scriptToRun = '';

const entryUrl = new URL('entry/entry.html', location).href;
const incumbentUrl = new URL('incumbent/empty.html', location).href;
const relevantUrl = new URL('relevant/empty.html', location).href;

function go() {
    const entry = document.querySelector('#entry');
    const incumbent = entry.contentDocument.querySelector('#incumbent');
    const incumbentScript = incumbent.contentDocument.createElement('script');
    incumbentScript.textContent = `
    function go() {
      const relevantWindow =
          parent.document.querySelector('#relevant').contentWindow;
      ${window.scriptToRun}
    }
    `;
    incumbent.contentDocument.head.appendChild(incumbentScript);
    incumbent.contentWindow.go();
}