Skip to content

Commit

Permalink
Part 4: wpt for injecting a <base> element for import-maps.
Browse files Browse the repository at this point in the history
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

bugzilla-url: https://bugzilla.mozilla.org/show_bug.cgi?id=1773997
gecko-commit: 12620b755d6b0af8909f0249e37af4c5c1893b83
gecko-reviewers: jonco, yulia
  • Loading branch information
allstarschh authored and moz-wptsync-bot committed Jul 26, 2022
1 parent d6a7d48 commit b10070d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
5 changes: 5 additions & 0 deletions import-maps/import-maps-base-url.sub.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
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>
Expand Down
3 changes: 3 additions & 0 deletions import-maps/resources/inject-base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const el = document.createElement("base");
el.href = "{{GET[baseurl]}}";
document.currentScript.after(el);
60 changes: 60 additions & 0 deletions import-maps/resources/test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit b10070d

Please sign in to comment.