Skip to content

Commit dacb618

Browse files
committed
Bug 1916277 - Part 4: Update ImportMap::ResolveModuleSpecifier. r=jonco
This is just a minor update to the latest spec, the logic remains the same. Before: If found a match in scopesImportMatch, return result. After: If found a match in scopesImportMatch, break from the for-loop. if result is not null, return result Differential Revision: https://phabricator.services.mozilla.com/D272778
1 parent d5d5dfa commit dacb618

2 files changed

Lines changed: 60 additions & 67 deletions

File tree

js/loader/ImportMap.cpp

Lines changed: 58 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -725,92 +725,94 @@ ResolveResult ImportMap::ResolveModuleSpecifier(ImportMap* aImportMap,
725725
ScriptLoaderInterface* aLoader,
726726
LoadedScript* aScript,
727727
const nsAString& aSpecifier) {
728-
LOG(("ImportMap::ResolveModuleSpecifier specifier: %s",
729-
NS_ConvertUTF16toUTF8(aSpecifier).get()));
730728
nsCOMPtr<nsIURI> baseURL;
731729
if (aScript && !aScript->IsEventScript()) {
732730
baseURL = aScript->BaseURL();
733731
} else {
734732
baseURL = aLoader->GetBaseURI();
735733
}
736734

737-
// Step 7. Let asURL be the result of resolving a URL-like module specifier
738-
// given specifier and baseURL.
739-
//
740-
// Impl note: Step 6 is done below if aImportMap exists.
735+
// 6. Let serializedBaseURL be baseURL, serialized.
736+
nsCString serializedBaseURL = baseURL->GetSpecOrDefault();
737+
738+
LOG(("ResolveModuleSpecifier baseURL:%s, specifier: %s",
739+
serializedBaseURL.get(), NS_ConvertUTF16toUTF8(aSpecifier).get()));
740+
741+
// 7. Let asURL be the result of resolving a URL-like module specifier
742+
// given specifier and baseURL.
741743
auto parseResult = ResolveURLLikeModuleSpecifier(aSpecifier, baseURL);
742744
nsCOMPtr<nsIURI> asURL;
743745
if (parseResult.isOk()) {
744746
asURL = parseResult.unwrap();
745747
}
746748

747-
if (aImportMap) {
748-
// Step 6. Let baseURLString be baseURL, serialized.
749-
nsCString baseURLString = baseURL->GetSpecOrDefault();
749+
// 8. Let normalizedSpecifier be the serialization of asURL, if asURL
750+
// is non-null; otherwise, specifier.
751+
nsAutoString normalizedSpecifier =
752+
asURL ? NS_ConvertUTF8toUTF16(asURL->GetSpecOrDefault())
753+
: nsAutoString{aSpecifier};
750754

751-
// Step 8. Let normalizedSpecifier be the serialization of asURL, if asURL
752-
// is non-null; otherwise, specifier.
753-
nsAutoString normalizedSpecifier =
754-
asURL ? NS_ConvertUTF8toUTF16(asURL->GetSpecOrDefault())
755-
: nsAutoString{aSpecifier};
755+
// Step 9. Let result be a URL-or-null, initially null.
756+
nsCOMPtr<nsIURI> result;
756757

757-
// Step 9. For each scopePrefix → scopeImports of importMap’s scopes,
758+
if (aImportMap) {
759+
// Step 10. For each scopePrefix → scopeImports of importMap’s scopes,
758760
for (auto&& [scopePrefix, scopeImports] : *aImportMap->mScopes) {
759-
// Step 9.1. If scopePrefix is baseURLString, or if scopePrefix ends with
760-
// U+002F (/) and scopePrefix is a code unit prefix of baseURLString,
761-
// then:
762-
if (scopePrefix.Equals(baseURLString) ||
761+
// 1. If scopePrefix is serializedBaseURL, or if scopePrefix ends with
762+
// U+002F (/) and scopePrefix is a code unit prefix of
763+
// serializedBaseURL, then:
764+
if (scopePrefix.Equals(serializedBaseURL) ||
763765
(StringEndsWith(scopePrefix, "/"_ns) &&
764-
StringBeginsWith(baseURLString, scopePrefix))) {
765-
// Step 9.1.1. Let scopeImportsMatch be the result of resolving an
766-
// imports match given normalizedSpecifier, asURL, and scopeImports.
767-
auto result =
766+
StringBeginsWith(serializedBaseURL, scopePrefix))) {
767+
// 1. Let scopeImportsMatch be the result of resolving an
768+
// imports match given normalizedSpecifier, asURL, and scopeImports.
769+
auto resolveResult =
768770
ResolveImportsMatch(normalizedSpecifier, asURL, scopeImports.get());
769-
if (result.isErr()) {
770-
return result.propagateErr();
771+
if (resolveResult.isErr()) {
772+
return resolveResult.propagateErr();
771773
}
772774

773-
nsCOMPtr<nsIURI> scopeImportsMatch = result.unwrap();
774-
// Step 9.1.2. If scopeImportsMatch is not null, then return
775-
// scopeImportsMatch.
775+
nsCOMPtr<nsIURI> scopeImportsMatch = resolveResult.unwrap();
776+
// 2. If scopeImportsMatch is not null, then set resolveResult to
777+
// scopeImportsMatch, and break.
776778
if (scopeImportsMatch) {
777-
LOG((
778-
"ImportMap::ResolveModuleSpecifier returns scopeImportsMatch: %s",
779-
scopeImportsMatch->GetSpecOrDefault().get()));
780-
return WrapNotNull(scopeImportsMatch);
779+
result = scopeImportsMatch;
780+
break;
781781
}
782782
}
783783
}
784784

785-
// Step 10. Let topLevelImportsMatch be the result of resolving an imports
786-
// match given normalizedSpecifier, asURL, and importMap’s imports.
787-
auto result = ResolveImportsMatch(normalizedSpecifier, asURL,
788-
aImportMap->mImports.get());
789-
if (result.isErr()) {
790-
return result.propagateErr();
791-
}
792-
nsCOMPtr<nsIURI> topLevelImportsMatch = result.unwrap();
793-
794-
// Step 11. If topLevelImportsMatch is not null, then return
795-
// topLevelImportsMatch.
796-
if (topLevelImportsMatch) {
797-
LOG(("ImportMap::ResolveModuleSpecifier returns topLevelImportsMatch: %s",
798-
topLevelImportsMatch->GetSpecOrDefault().get()));
799-
return WrapNotNull(topLevelImportsMatch);
785+
// 11. If result is null, set result to the result of resolving an imports
786+
// match given normalizedSpecifier, asURL, and importMap's imports.
787+
if (!result) {
788+
auto resolveResult = ResolveImportsMatch(normalizedSpecifier, asURL,
789+
aImportMap->mImports.get());
790+
if (resolveResult.isErr()) {
791+
return resolveResult.propagateErr();
792+
}
793+
794+
result = resolveResult.unwrap();
800795
}
801796
}
802797

803-
// Step 12. At this point, the specifier was able to be turned in to a URL,
804-
// but it wasn’t remapped to anything by importMap. If asURL is not null, then
805-
// return asURL.
806-
if (asURL) {
807-
LOG(("ImportMap::ResolveModuleSpecifier returns asURL: %s",
808-
asURL->GetSpecOrDefault().get()));
809-
return WrapNotNull(asURL);
798+
// 12. If result is null, set it to asURL.
799+
if (!result) {
800+
result = asURL;
801+
}
802+
803+
// 13. If result is not null, then:
804+
if (result) {
805+
// 2. Return result.
806+
LOG(("ResolveModuleSpecifier returns result: %s",
807+
result->GetSpecOrDefault().get()));
808+
return WrapNotNull(result);
810809
}
811810

812-
// Step 13. Throw a TypeError indicating that specifier was a bare specifier,
813-
// but was not remapped to anything by importMap.
811+
LOG(("ResolveModuleSpecifier failed to resolve specifier: %s",
812+
NS_ConvertUTF16toUTF8(aSpecifier).get()));
813+
814+
// 14. Throw a TypeError indicating that specifier was a bare specifier,
815+
// but was not remapped to anything by importMap.
814816
if (parseResult.unwrapErr() != ResolveError::FailureMayBeBare) {
815817
// We may have failed to parse a non-bare specifier for another reason.
816818
return Err(ResolveError::Failure);

js/loader/ImportMap.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,6 @@ class ImportMap {
9696
*
9797
* See
9898
* https://html.spec.whatwg.org/multipage/webappapis.html#resolve-a-module-specifier
99-
*
100-
* Impl note: According to the spec, if the specifier cannot be resolved, this
101-
* method will throw a TypeError(Step 13). But the tricky part is when
102-
* creating a module script,
103-
* see
104-
* https://html.spec.whatwg.org/multipage/webappapis.html#validate-requested-module-specifiers
105-
* If the resolving failed, it shall catch the exception and set to the
106-
* script's parse error.
107-
* For implementation we return a ResolveResult here, and the callers will
108-
* need to convert the result to a TypeError if it fails.
10999
*/
110100
static ResolveResult ResolveModuleSpecifier(ImportMap* aImportMap,
111101
ScriptLoaderInterface* aLoader,
@@ -122,9 +112,10 @@ class ImportMap {
122112
/**
123113
* https://html.spec.whatwg.org/multipage/webappapis.html#import-map-processing-model
124114
*
125-
* Formally, an import map is a struct with two items:
115+
* Formally, an import map is a struct with three items:
126116
* 1. imports, a module specifier map, and
127117
* 2. scopes, an ordered map of URLs to module specifier maps.
118+
* 3. integrity, a module integrity map.
128119
*/
129120
mozilla::UniquePtr<SpecifierMap> mImports;
130121
mozilla::UniquePtr<ScopeMap> mScopes;

0 commit comments

Comments
 (0)