@@ -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);
0 commit comments