Source code
Revision control
Copy as Markdown
Other Tools
Test Info: Warnings
- This test has a WPT meta file that expects 1 subtest issues.
- This WPT test may be referenced by the following Test IDs:
- /dom/nodes/insertion-removing-steps/Node-appendChild-script-in-script.tentative.html - WPT Dashboard Interop Dashboard
<!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
// 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.
//
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.
//
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>