From 38892f7308825880a64bccc3deba7682046e97f4 Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Wed, 21 Oct 2026 00:25:50 -0500 Subject: [PATCH] Issue #2464 - Part 1: Add support for import maps in ScriptElement/ScriptLoader/ScriptKind. https://bugzilla.mozilla.org/show_bug.cgi?id=1688879 --- dom/html/HTMLScriptElement.cpp | 23 ++++++++++++++++++----- dom/script/ScriptKind.h | 25 +++++++++++++++++++++++++ dom/script/ScriptLoader.cpp | 11 ++++++++--- dom/script/ScriptLoader.h | 10 +--------- dom/script/moz.build | 1 + dom/script/nsIScriptElement.h | 25 ++++++++++++++++++------- 6 files changed, 71 insertions(+), 24 deletions(-) create mode 100644 dom/script/ScriptKind.h diff --git a/dom/html/HTMLScriptElement.cpp b/dom/html/HTMLScriptElement.cpp index 2b7eee510e..ad185ad83a 100644 --- a/dom/html/HTMLScriptElement.cpp +++ b/dom/html/HTMLScriptElement.cpp @@ -294,13 +294,26 @@ HTMLScriptElement::FreezeExecutionAttrs(nsIDocument* aOwnerDoc) return; } - MOZ_ASSERT(!mIsModule && !mAsync && !mDefer && !mExternal); + MOZ_ASSERT((mKind != ScriptKind::Module) && + (mKind != ScriptKind::ImportMap) && + !mAsync && !mDefer && !mExternal); - // Determine whether this is a classic script or a module script. + // Determine whether this is a(n) classic/module/importmap script. nsAutoString type; GetScriptType(type); - mIsModule = aOwnerDoc->ModuleScriptsEnabled() && - !type.IsEmpty() && type.LowerCaseEqualsASCII("module"); + if (!type.IsEmpty()) { + if (aOwnerDoc->ModuleScriptsEnabled() && + type.LowerCaseEqualsASCII("module")) { + mKind = ScriptKind::Module; + } + + // https://wicg.github.io/import-maps/#integration-prepare-a-script + // If the script block’s type string is an ASCII case-insensitive match + // for the string "importmap", the script’s type is "importmap". + if (type.LowerCaseEqualsASCII("importmap")) { + mKind = ScriptKind::ImportMap; + } + } // variation of this code in nsSVGScriptElement - check if changes // need to be transfered when modifying. Note that we don't use GetSrc here @@ -320,7 +333,7 @@ HTMLScriptElement::FreezeExecutionAttrs(nsIDocument* aOwnerDoc) bool defer, async; GetAsync(&async); - mAsync = (mExternal || mIsModule) && async; + mAsync = (mExternal || mKind == ScriptKind::Module) && async; GetDefer(&defer); mDefer = mExternal && !async && defer; diff --git a/dom/script/ScriptKind.h b/dom/script/ScriptKind.h new file mode 100644 index 0000000000..14d5698f83 --- /dev/null +++ b/dom/script/ScriptKind.h @@ -0,0 +1,25 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_ScriptKind_h +#define mozilla_dom_ScriptKind_h + +namespace mozilla { +namespace dom { + +////////////////////////////////////////////////////////////// +// Per-request data structure +////////////////////////////////////////////////////////////// + +enum class ScriptKind { + Classic, + Module, + ImportMap +}; + +} // namespace dom +} // namespace mozilla + +#endif \ No newline at end of file diff --git a/dom/script/ScriptLoader.cpp b/dom/script/ScriptLoader.cpp index 514f558056..fe59fc1b9e 100644 --- a/dom/script/ScriptLoader.cpp +++ b/dom/script/ScriptLoader.cpp @@ -1526,9 +1526,14 @@ ScriptLoader::ProcessScriptElement(nsIScriptElement *aElement) nsAutoString type; bool hasType = aElement->GetScriptType(type); - ScriptKind scriptKind = aElement->GetScriptIsModule() ? - ScriptKind::Module : - ScriptKind::Classic; + ScriptKind scriptKind; + if (aElement->GetScriptIsModule()) { + scriptKind = ScriptKind::Module; + } else if (aElement->GetScriptIsImportMap()) { + scriptKind = ScriptKind::ImportMap; + } else { + scriptKind = ScriptKind::Classic; + } // Step 13. Check that the script is not an eventhandler if (IsScriptEventHandler(scriptKind, scriptContent)) { diff --git a/dom/script/ScriptLoader.h b/dom/script/ScriptLoader.h index 3455621b88..ba0e31b6bf 100644 --- a/dom/script/ScriptLoader.h +++ b/dom/script/ScriptLoader.h @@ -18,6 +18,7 @@ #include "nsIIncrementalStreamLoader.h" #include "nsURIHashKey.h" #include "mozilla/CORSMode.h" +#include "mozilla/dom/ScriptKind.h" #include "mozilla/dom/SRIMetadata.h" #include "mozilla/dom/SRICheck.h" #include "mozilla/MozPromise.h" @@ -39,15 +40,6 @@ class ModuleLoadRequest; class ModuleScript; class ScriptLoadRequestList; -////////////////////////////////////////////////////////////// -// Per-request data structure -////////////////////////////////////////////////////////////// - -enum class ScriptKind { - Classic, - Module -}; - /* * Some options used when fetching script resources. This only loosely * corresponds to HTML's "script fetch options". diff --git a/dom/script/moz.build b/dom/script/moz.build index 125e62545f..0b4d7ca00b 100644 --- a/dom/script/moz.build +++ b/dom/script/moz.build @@ -14,6 +14,7 @@ EXPORTS += ['nsIScriptElement.h'] EXPORTS.mozilla.dom += [ 'ModuleScript.h', 'ScriptElement.h', + 'ScriptKind.h', 'ScriptLoader.h', 'ScriptSettings.h', ] diff --git a/dom/script/nsIScriptElement.h b/dom/script/nsIScriptElement.h index 3149f79cc1..90a4fd3be0 100644 --- a/dom/script/nsIScriptElement.h +++ b/dom/script/nsIScriptElement.h @@ -17,6 +17,7 @@ #include "nsIDOMHTMLScriptElement.h" #include "mozilla/CORSMode.h" #include "mozilla/net/ReferrerPolicy.h" +#include "mozilla/dom/ScriptKind.h" #define NS_ISCRIPTELEMENT_IID \ { 0xe60fca9b, 0x1b96, 0x4e4e, \ @@ -39,10 +40,10 @@ public: mForceAsync(aFromParser == mozilla::dom::NOT_FROM_PARSER || aFromParser == mozilla::dom::FROM_PARSER_FRAGMENT), mFrozen(false), - mIsModule(false), mDefer(false), mAsync(false), mExternal(false), + mKind(mozilla::dom::ScriptKind::Classic), mParserCreated(aFromParser == mozilla::dom::FROM_PARSER_FRAGMENT ? mozilla::dom::NOT_FROM_PARSER : aFromParser), // Fragment parser-created scripts (if executable) @@ -85,6 +86,7 @@ public: * Freezes the return values of the following methods so that subsequent * modifications to the attributes don't change execution behavior: * - GetScriptIsModule() + * - GetScriptIsImportMap() * - GetScriptDeferred() * - GetScriptAsync() * - GetScriptURI() @@ -99,7 +101,16 @@ public: bool GetScriptIsModule() { NS_PRECONDITION(mFrozen, "Execution attributes not yet frozen: Not ready for this call!"); - return mIsModule; + return mKind == mozilla::dom::ScriptKind::Module; + } + + /** + * Is the script an import map. Currently only supported by HTML scripts. + */ + bool GetScriptIsImportMap() + { + NS_PRECONDITION(mFrozen, "Not ready for this call yet!"); + return mKind == mozilla::dom::ScriptKind::ImportMap; } /** @@ -338,11 +349,6 @@ protected: */ bool mFrozen; - /** - * The effective moduleness. - */ - bool mIsModule; - /** * The effective deferredness. */ @@ -359,6 +365,11 @@ protected: */ bool mExternal; + /** + * The effective script kind. + */ + mozilla::dom::ScriptKind mKind; + /** * Whether this element was parser-created. */