summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/nodes/insertion-removing-steps/Node-appendChild-script-in-script.tentative.html
blob: 39c439332366816dc8b6b7dce3873648dde26a6c (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
<!doctype html>
<meta charset=utf-8>
<title>Node.appendChild: inserting a script and some code in an empty script</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<body>
<script id="s1"></script>
<script>
const happened = [];
test(() => {
  const s1 = document.getElementById("s1");
  const s2 = document.createElement("script");

  // This script, which is ultimately a *child* of the
  // already-connected-but-empty `s1` script, runs second, after `s1` runs. See
  // the example in
  // http://html.spec.whatwg.org/C/#script-processing-model:children-changed-steps
  // for more information.
  //
  // HISTORICAL CONTEXT: There used to be a condition in the HTML standard that
  // said an "outer" script must be "prepared" when a node gets inserted into
  // the script. BUT it also stipulated that if the insertion consists of any
  // "inner" (nested, essentially) script elements, then this "outer" script
  // must prepare/execute after any of those "inner" newly-inserted scripts
  // themselves get prepared.
  //
  // This changed in https://github.com/whatwg/html/pull/10188.
  s2.textContent = `
    happened.push("s2");

    // This text never executes in the outer script, because by the time this
    // gets appended, the outer script has "already started" [1], so it does not
    // get re-prepared/executed a second time.
    //
    // [1]: https://html.spec.whatwg.org/C#already-started
    s1.appendChild(new Text("happened.push('s1ran');"));

    happened.push("s2ran");
`;

  const df = document.createDocumentFragment();
  df.appendChild(new Text(`happened.push("s1");`));
  df.appendChild(s2);

  assert_array_equals(happened, []);
  s1.appendChild(df);
  assert_array_equals(happened, ["s1", "s2", "s2ran"]);
}, "An outer script whose preparation/execution gets triggered by the " +
   "insertion of a 'nested'/'inner' script, executes *before* the inner " +
   "script executes");
</script>