summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/semantics/scripting-1/the-script-element/module/dynamic-import/string-compilation-base-url-inline-classic.html
blob: 2fe1f755358e2b1123d99573d792f0cee38571f7 (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<!DOCTYPE html>
<meta charset="utf-8">
<title>import() inside compiled strings uses the script base URL (= document base URL) inside an inline classic script</title>
<link rel="author" title="Domenic Denicola" href="mailto:d@domenic.me">

<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<body>
<div id="dummy"></div>

<base href="scripts/foo/">
<script>
// Tweak the base URL of the document here to distinguish:
// - document URL
// - document base URL = ../
// - This inline script's base URL = ./scripts/foo/
document.querySelector("base").remove();
const base = document.createElement("base");
base.setAttribute("href", "../");
document.body.appendChild(base);

function createTestPromise() {
  return new Promise((resolve, reject) => {
    window.continueTest = resolve;
    window.errorTest = reject;
  });
}

const dummyDiv = document.querySelector("#dummy");

function doTest(label, evaluator, path) {
  promise_test(t => {
    t.add_cleanup(() => {
      dummyDiv.removeAttribute("onclick");
      delete window.evaluated_imports_a;
    });

    const promise = createTestPromise();

    evaluator(`import('${path}/imports-a.js?label=${label}').then(window.continueTest, window.errorTest);`);

    return promise.then(module => {
      assert_true(window.evaluated_imports_a, "The module must have been evaluated");
      assert_equals(module.A.from, "imports-a.js", "The module namespace object must be correct");
    });
  }, label + " should successfully import");
}

// Inline script's base URL should be used.
doTest("setTimeout", setTimeout, "../../..");
doTest("eval", eval, "../../..");
doTest("the Function constructor",
  (x) => {
    Function(x)();
  },
  "../../..");

// Document's base URL should be used, as there are no active scripts.
doTest("reflected inline event handlers",
  (x) => {
    dummyDiv.setAttribute("onclick", x);
    dummyDiv.onclick();
  },
  ".");

doTest("inline event handlers triggered via UA code",
  (x) => {
    dummyDiv.setAttribute("onclick", x);
    dummyDiv.click(); // different from .**on**click()
  },
  ".");
</script>