Files
sousa-gecko/testing/web-platform/tests/import-maps/dynamic-module-map-key.html
T
Kurt Catti-Schmidt d7286727c1 Bug 1993204 [wpt PR 55291] - [CSS Modules] Adding WPT's for Import Map statefulness, a=testonly
Automatic update from web-platform-tests
[CSS Modules] Adding WPT's for Import Map statefulness

For CSS Modules (https://github.com/whatwg/html/issues/10673), Domenic
suggested offline to follow the static behaviors of Import Maps.

Specifically that they can't be modified into classic scripts once the
Already Started flag is set, that they don't update the underlying
import map when innerText changes, and that import map scripts can be
created dynamically as long as the Already Started flag has not been
set.

There are no WPT's specifically testing these behaviors, so this CL adds
them.

Bug: 449621064

Change-Id: I84ad6a03f4b2f9c5fa80e9d642fb14d7f303d2b9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7004710
Reviewed-by: Shunya Shishido <sisidovski@chromium.org>
Commit-Queue: Kurt Catti-Schmidt <kschmi@microsoft.com>
Cr-Commit-Position: refs/heads/main@{#1526624}

--

wpt-commits: a5677a5cf5423c03a8126747688fe832e5356288
wpt-pr: 55291
2025-10-20 16:37:55 +00:00

87 lines
3.9 KiB
HTML

<!DOCTYPE html>
<html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
const dynamicScript = document.createElement("script");
dynamicScript.type = "importmap";
dynamicScript.innerText = "{ \"imports\": { \"./resources/log.js?pipe=sub&name=A\": \"./resources/log.js?pipe=sub&name=B\" } }";
document.head.appendChild(dynamicScript);
</script>
<script>
let log = [];
promise_test(() => {
return import("./resources/log.js?pipe=sub&name=A")
.then(() => import("./resources/log.js?pipe=sub&name=B"))
.then(() => assert_array_equals(log, ["log:B"]))
},
"Module map's key is the URL after import map resolution");
</script>
<script>
// Ensure that the Already Started flag is passed through to clones,
// ensuring the type cannot be changed after it is set to true.
const dynamicScriptTypeStatic = document.createElement("script");
dynamicScriptTypeStatic.type = "text/javascript";
dynamicScriptTypeStatic.type = "importmap";
dynamicScriptTypeStatic.innerText = "{ \"imports\": { \"./resources/log.js?pipe=sub&name=C\": \"./resources/log.js?pipe=sub&name=D\" } }";
document.head.appendChild(dynamicScriptTypeStatic);
// Because the contents aren't empty, once it's connected as an import
// map, the Already Started flag is set to true and it can't be changed
// into a classic script.
document.head.removeChild(dynamicScriptTypeStatic);
dynamicScriptTypeStatic.type = "text/javascript";
dynamicScriptTypeStatic.innerText = "test(function() { assert_unreached('Script should not be able to execute after Already Started is set.'); }";
document.head.appendChild(dynamicScriptTypeStatic);
// The Already Started flag is copied through to clones, even though
// `type` is set to "text/javascript" when it is connected.
const clonedScriptNode = dynamicScriptTypeStatic.cloneNode(/*deep=*/true);
test(function() {
assert_equals(clonedScriptNode.type, "text/javascript");
assert_equals(clonedScriptNode.innerText, "test(function() { assert_unreached('Script should not be able to execute after Already Started is set.'); }");
}, "Cloned script node copies attribute and text content.");
document.head.appendChild(clonedScriptNode);
// Script tags with empty contents do not set Already Started to true,
// so the type can be changed later and on clones, even if they have
// been connected.
const dynamicScriptEmpty = document.createElement("script");
dynamicScriptEmpty.type = "importmap";
document.head.appendChild(dynamicScriptEmpty);
document.head.removeChild(dynamicScriptEmpty);
// The Already Started flag is copied onto clones.
dynamicScriptEmpty.type = "text/javascript";
const clonedEmptyScript = dynamicScriptEmpty.cloneNode(/*deep=*/true);
test(function() {
assert_equals(clonedEmptyScript.type, "text/javascript");
assert_equals(clonedEmptyScript.innerText, "");
}, "Cloned script node copies attributes and text content.");
// Because Already Started is false, The clone can be set to a different
// type than the original element and both can be inserted as their
// respective types.
clonedEmptyScript.setAttribute("type", "importmap");
clonedEmptyScript.innerText = "{ \"imports\": { \"./resources/log.js?pipe=sub&name=E\": \"./resources/log.js?pipe=sub&name=F\" } }";
document.head.appendChild(clonedEmptyScript);
const t_evaluate = async_test("The Already Started flag is set when a non-empty <script> tag is connected.");
dynamicScriptEmpty.innerText = "t_evaluate.done();";
document.head.appendChild(dynamicScriptEmpty);
</script>
<script>
promise_test(() => {
return import("./resources/log.js?pipe=sub&name=C")
.then(() => import("./resources/log.js?pipe=sub&name=D"))
.then(() => import("./resources/log.js?pipe=sub&name=E"))
.then(() => assert_array_equals(log, ["log:B", "log:D", "log:F"]))
},
"The script tag's Already Started flag is passed to clones.");
</script>
</html>