Bug 1773997 - Part 4: wpt for injecting a <base> element for import-maps. r=jonco,yulia

This test introduces two new functions to test the base url functionality. In
addition, we have a new helper function: testInIframeInjectBase, which injects
a base url before the content with the import map is parsed.

The two tests test the following:
1. Static import coming after the import map and injected base element
2. Dynamic import coming after the import map and injected base element

Differential Revision: https://phabricator.services.mozilla.com/D150719
This commit is contained in:
Yoshi Cheng-Hao Huang
2022-07-26 15:16:12 +00:00
parent 0f0f890e01
commit 9f853f39f4
3 changed files with 68 additions and 0 deletions

View File

@@ -22,6 +22,11 @@ window.addEventListener("load", () => {
testDynamicImport(importMap, baseURL, "bare/bare", "log:bare", "module");
testDynamicImport(importMap, baseURL, "bare/bare", "log:bare",
"text/javascript");
testStaticImportInjectBase(importMap, baseURL, "bare/bare", "log:bare");
testDynamicImportInjectBase(importMap, baseURL, "bare/bare", "log:bare", "module");
testDynamicImportInjectBase(importMap, baseURL, "bare/bare", "log:bare",
"text/javascript");
});
</script>

View File

@@ -0,0 +1,3 @@
const el = document.createElement("base");
el.href = "{{GET[baseurl]}}";
document.currentScript.after(el);

View File

@@ -173,6 +173,66 @@ function testDynamicImport(importMapString, importMapBaseURL, specifier, expecte
`);
}
function testInIframeInjectBase(importMapString, importMapBaseURL, testScript) {
const iframe = document.createElement('iframe');
document.body.appendChild(iframe);
let content = `
<script src="/resources/testharness.js"></script>
<script src="/import-maps/resources/test-helper.js"></script>
<script src="/import-maps/resources/inject-base.js?pipe=sub&baseurl=${importMapBaseURL}"></script>
`;
if (importMapString) {
content += `
<script type="importmap">
${importMapString}
</sc` + `ript>
`;
}
content += `
<body>
<script>
setup({ allow_uncaught_exception: true });
${testScript}
</sc` + `ript>
`;
iframe.contentDocument.write(content);
iframe.contentDocument.close();
fetch_tests_from_window(iframe.contentWindow);
}
function testStaticImportInjectBase(importMapString, importMapBaseURL, specifier, expected) {
testInIframeInjectBase(importMapString, importMapBaseURL, `
const t = async_test("${specifier}: static import with inject <base>");
const handlers = getHandlers(t, "${specifier}", "${expected}");
const script = document.createElement("script");
script.setAttribute("type", "module");
script.setAttribute("src",
"/import-maps/static-import.py?url=" +
encodeURIComponent("${specifier}"));
script.addEventListener("load", handlers[Handler.ScriptLoadEvent]);
script.addEventListener("error", handlers[Handler.ScriptErrorEvent]);
window.addEventListener("error", handlers[Handler.WindowErrorEvent]);
document.body.appendChild(script);
`);
}
function testDynamicImportInjectBase(importMapString, importMapBaseURL, specifier, expected, type) {
testInIframeInjectBase(importMapString, importMapBaseURL, `
const t = async_test("${specifier}: dynamic import (from ${type}) with inject <base>");
const handlers = getHandlers(t, "${specifier}", "${expected}");
const script = document.createElement("script");
script.setAttribute("type", "${type}");
script.innerText =
"import(\\"${specifier}\\")" +
".then(handlers[Handler.DynamicImportResolve], " +
"handlers[Handler.DynamicImportReject]);";
script.addEventListener("error",
t.unreached_func("top-level inline script shouldn't error"));
document.body.appendChild(script);
`);
}
function doTests(importMapString, importMapBaseURL, tests) {
window.addEventListener("load", () => {
for (const specifier in tests) {