@@ -122,6 +122,9 @@ final class AsyncImportCache {
122122
123123 /// Canonicalizes [url] according to one of this cache's importers.
124124 ///
125+ /// The [baseUrl] should be the canonical URL of the stylesheet that contains
126+ /// the load, if it exists.
127+ ///
125128 /// Returns the importer that was used to canonicalize [url] , the canonical
126129 /// URL, and the URL that was passed to the importer (which may be resolved
127130 /// relative to [baseUrl] if it's passed).
@@ -139,33 +142,30 @@ final class AsyncImportCache {
139142 if (isBrowser &&
140143 (baseImporter == null || baseImporter is NoOpImporter ) &&
141144 _importers.isEmpty) {
142- throw "Custom importers are required to load stylesheets when compiling in the browser." ;
145+ throw "Custom importers are required to load stylesheets when compiling "
146+ "in the browser." ;
143147 }
144148
145149 if (baseImporter != null && url.scheme == '' ) {
146- var relativeResult = await putIfAbsentAsync (_relativeCanonicalizeCache, (
147- url,
148- forImport: forImport,
149- baseImporter: baseImporter,
150- baseUrl: baseUrl
151- ), () async {
152- var resolvedUrl = baseUrl? .resolveUri (url) ?? url;
153- if (await _canonicalize (baseImporter, resolvedUrl, forImport)
154- case var canonicalUrl? ) {
155- return (baseImporter, canonicalUrl, originalUrl: resolvedUrl);
156- } else {
157- return null ;
158- }
159- });
150+ var relativeResult = await putIfAbsentAsync (
151+ _relativeCanonicalizeCache,
152+ (
153+ url,
154+ forImport: forImport,
155+ baseImporter: baseImporter,
156+ baseUrl: baseUrl
157+ ),
158+ () => _canonicalize (baseImporter, baseUrl? .resolveUri (url) ?? url,
159+ baseUrl, forImport));
160160 if (relativeResult != null ) return relativeResult;
161161 }
162162
163163 return await putIfAbsentAsync (
164164 _canonicalizeCache, (url, forImport: forImport), () async {
165165 for (var importer in _importers) {
166- if (await _canonicalize (importer, url, forImport)
167- case var canonicalUrl ? ) {
168- return (importer, canonicalUrl, originalUrl : url) ;
166+ if (await _canonicalize (importer, url, baseUrl, forImport)
167+ case var result ? ) {
168+ return result ;
169169 }
170170 }
171171
@@ -175,18 +175,36 @@ final class AsyncImportCache {
175175
176176 /// Calls [importer.canonicalize] and prints a deprecation warning if it
177177 /// returns a relative URL.
178- Future <Uri ?> _canonicalize (
179- AsyncImporter importer, Uri url, bool forImport) async {
180- var result = await (forImport
181- ? inImportRule (() => importer.canonicalize (url))
182- : importer.canonicalize (url));
183- if (result? .scheme == '' ) {
184- _logger.warnForDeprecation (Deprecation .relativeCanonical, """
185- Importer $importer canonicalized $url to $result .
186- Relative canonical URLs are deprecated and will eventually be disallowed.
187- """ );
178+ ///
179+ /// If [resolveUrl] is `true` , this resolves [url] relative to [baseUrl]
180+ /// before passing it to [importer] .
181+ Future <AsyncCanonicalizeResult ?> _canonicalize (
182+ AsyncImporter importer, Uri url, Uri ? baseUrl, bool forImport,
183+ {bool resolveUrl = false }) async {
184+ var resolved =
185+ resolveUrl && baseUrl != null ? baseUrl.resolveUri (url) : url;
186+ var canonicalize = forImport
187+ ? () => inImportRule (() => importer.canonicalize (resolved))
188+ : () => importer.canonicalize (resolved);
189+
190+ var passContainingUrl = baseUrl != null &&
191+ (url.scheme == '' || await importer.isNonCanonicalScheme (url.scheme));
192+ var result = await withContainingUrl (
193+ passContainingUrl ? baseUrl : null , canonicalize);
194+ if (result == null ) return null ;
195+
196+ if (result.scheme == '' ) {
197+ _logger.warnForDeprecation (
198+ Deprecation .relativeCanonical,
199+ "Importer $importer canonicalized $resolved to $result .\n "
200+ "Relative canonical URLs are deprecated and will eventually be "
201+ "disallowed." );
202+ } else if (await importer.isNonCanonicalScheme (result.scheme)) {
203+ throw "Importer $importer canonicalized $resolved to $result , which "
204+ "uses a scheme declared as non-canonical." ;
188205 }
189- return result;
206+
207+ return (importer, result, originalUrl: resolved);
190208 }
191209
192210 /// Tries to import [url] using one of this cache's importers.
0 commit comments