Bug 1815180 : Dump error message when import maps aren't allowed. r=jonco

Differential Revision: https://phabricator.services.mozilla.com/D169185
This commit is contained in:
Yoshi Cheng-Hao Huang 2023-02-08 12:22:04 +00:00
parent d1c53f86f9
commit 0f577f10c3
6 changed files with 91 additions and 0 deletions

View File

@ -23,6 +23,7 @@ prefs =
[test_load_importMap_with_base2.html]
[test_module_script_reject_importMap.html]
[test_parse_importMap_failed.html]
[test_reject_multiple_importMaps.html]
[test_simpleImportMap.html]
[test_sortedImportMap.html]

View File

@ -6,6 +6,21 @@
<body onload='testLoaded()'>
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script>
let gotMsg = false;
let console = Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService);
let listener = {
QueryInterface: ChromeUtils.generateQI(["nsIConsoleListener"]),
observe(msg) {
info("console message:" + msg);
ok(msg.logLevel == Ci.nsIConsoleMessage.warn, "log level should be 'warn'.");
console.unregisterListener(this);
gotMsg = true;
}
};
console.registerListener(listener);
</script>
<!--There is an inline module before the import map tag, so the import map-->
<!--cannot be accepted according to the spec.-->
<!--And because the import map is rejected, so the module specifier-->
@ -35,6 +50,7 @@
import("./module_simpleExport.js").then((ns) => {
ok(ns.x == 42, 'Check simple imported value result: ' + ns.x);
ok(hasError, "onerror of the import map should be called.");
ok(gotMsg, "Should have got the console warning.");
}).catch((e) => {
ok(false, "throws " + e);
}).then(() => {

View File

@ -0,0 +1,64 @@
<!DOCTYPE html>
<head>
<meta charset=utf-8>
<title>Test the 2nd import map should be rejected.</title>
</head>
<body onload='testLoaded()'>
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script>
let gotMsg = false;
let console = Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService);
let listener = {
QueryInterface: ChromeUtils.generateQI(["nsIConsoleListener"]),
observe(msg) {
info("console message:" + msg);
ok(msg.logLevel == Ci.nsIConsoleMessage.warn, "log level should be 'warn'.");
console.unregisterListener(this);
gotMsg = true;
}
};
console.registerListener(listener);
</script>
<script type="importmap" onerror='importMapError1()'>
{
"imports": {
"./module_simpleExport.js": "./scope1/module_simpleExport.js"
}
}
</script>
<!--The 2nd import map should be rejected.-->
<script type="importmap" onerror='importMapError2()'>
{
"imports": {
"./module_simpleExport.js": "./scope1/module_simpleExport.js"
}
}
</script>
<script>
SimpleTest.waitForExplicitFinish();
let hasError = false;
function importMapError1() {
ok(false, "The first import map should be accepted.");
}
function importMapError2() {
hasError = true;
}
function testLoaded() {
import("./module_simpleExport.js").then((ns) => {
ok(ns.x == 84, 'Check simple imported value result: ' + ns.x);
ok(hasError, "onerror of the import map should be called.");
ok(gotMsg, "Should have got the console warning.");
}).catch((e) => {
ok(false, "throws " + e);
}).then(() => {
SimpleTest.finish();
});
}
</script>
</body>

View File

@ -341,6 +341,8 @@ ImportMapResolutionBlockedByBacktrackingPrefix=Resolution of specifier “%S”
ImportMapResolveInvalidBareSpecifierWarnRelative=The specifier “%S” was a bare specifier, but was not remapped to anything. Relative module specifiers must start with “./”, “../” or “/”.
# LOCALIZATION NOTE: Do not translate "<script type='importmap'>", "src".
ImportMapExternalNotSupported=External import maps are not supported: <script type='importmap'> with a src attribute is currently not supported.
ImportMapNotAllowedMultiple=Multiple import maps are not allowed.
ImportMapNotAllowedAfterModuleLoad=Import maps are not allowed after a module load or preload has started.
# LOCALIZATION NOTE: %1$S is the invalid property value and %2$S is the property name.
InvalidKeyframePropertyValue=Keyframe property value “%1$S” is invalid according to the syntax for “%2$S”.
# LOCALIZATION NOTE: Do not translate "ReadableStream".

View File

@ -1160,6 +1160,12 @@ bool ScriptLoader::ProcessInlineScript(nsIScriptElement* aElement,
// to fire an event named error at el, and return.
if (!mModuleLoader->IsImportMapAllowed()) {
NS_WARNING("ScriptLoader: import maps allowed is false.");
const char* msg = mModuleLoader->HasImportMapRegistered()
? "ImportMapNotAllowedMultiple"
: "ImportMapNotAllowedAfterModuleLoad";
nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
"Script Loader"_ns, mDocument,
nsContentUtils::eDOM_PROPERTIES, msg);
NS_DispatchToCurrentThread(
NewRunnableMethod("nsIScriptElement::FireErrorEvent", aElement,
&nsIScriptElement::FireErrorEvent));

View File

@ -288,6 +288,8 @@ class ModuleLoaderBase : public nsISupports {
// https://html.spec.whatwg.org/multipage/webappapis.html#register-an-import-map
void RegisterImportMap(mozilla::UniquePtr<ImportMap> aImportMap);
bool HasImportMapRegistered() const { return bool(mImportMap); }
// Getter for mImportMapsAllowed.
bool IsImportMapAllowed() const { return mImportMapsAllowed; }
// https://html.spec.whatwg.org/multipage/webappapis.html#disallow-further-import-maps