@@ -24,48 +24,26 @@ const Map<String, String> testFontUrls = <String, String>{
2424
2525/// This class downloads assets over the network.
2626///
27- /// Assets are resolved relative to [assetsDir] inside the absolute base
28- /// specified by [assetBase] (optional).
29- ///
30- /// By default, URLs are relative to the `<base>` of the current website.
27+ /// The assets are resolved relative to [assetsDir] inside the directory
28+ /// containing the currently executing JS script.
3129class AssetManager {
32- /// Initializes [AssetManager] with paths.
33- AssetManager ({
34- this .assetsDir = _defaultAssetsDir,
35- String ? assetBase,
36- }) : assert (
37- assetBase == null || assetBase.endsWith ('/' ),
38- '`assetBase` must end with a `/` character.' ,
39- ),
40- _assetBase = assetBase;
30+ /// Initializes [AssetManager] with path to assets relative to baseUrl.
31+ const AssetManager ({this .assetsDir = _defaultAssetsDir});
4132
4233 static const String _defaultAssetsDir = 'assets' ;
4334
4435 /// The directory containing the assets.
4536 final String assetsDir;
4637
47- /// The absolute base URL for assets.
48- String ? _assetBase;
49-
50- // Cache a value for `_assetBase` so we don't hit the DOM multiple times.
51- String get _baseUrl => _assetBase ?? = _deprecatedAssetBase ?? '' ;
52-
53- // Retrieves the `assetBase` value from the DOM.
54- //
55- // This warns the user and points them to the new initializeEngine style.
56- String ? get _deprecatedAssetBase {
57- final DomHTMLMetaElement ? meta = domWindow.document
58- .querySelector ('meta[name=assetBase]' ) as DomHTMLMetaElement ? ;
59-
60- final String ? fallbackBaseUrl = meta? .content;
61-
62- if (fallbackBaseUrl != null ) {
63- // Warn users that they're using a deprecated configuration style...
64- domWindow.console.warn ('The `assetBase` meta tag is now deprecated.\n '
65- 'Use engineInitializer.initializeEngine(config) instead.\n '
66- 'See: https://docs.flutter.dev/development/platform-integration/web/initialization' );
67- }
68- return fallbackBaseUrl;
38+ String ? get _baseUrl {
39+ return domWindow.document
40+ .querySelectorAll ('meta' )
41+ .where ((DomElement domNode) => domInstanceOfString (domNode,
42+ 'HTMLMetaElement' ))
43+ .map ((DomElement domNode) => domNode as DomHTMLMetaElement )
44+ .firstWhereOrNull (
45+ (DomHTMLMetaElement element) => element.name == 'assetBase' )
46+ ? .content;
6947 }
7048
7149 /// Returns the URL to load the asset from, given the asset key.
@@ -89,7 +67,7 @@ class AssetManager {
8967 if (Uri .parse (asset).hasScheme) {
9068 return Uri .encodeFull (asset);
9169 }
92- return Uri .encodeFull ('$_baseUrl $assetsDir /$asset ' );
70+ return Uri .encodeFull ('${ _baseUrl ?? '' } $assetsDir /$asset ' );
9371 }
9472
9573 /// Loads an asset and returns the server response.
@@ -112,7 +90,7 @@ class AssetManager {
11290}
11391
11492/// An asset manager that gives fake empty responses for assets.
115- class WebOnlyMockAssetManager extends AssetManager {
93+ class WebOnlyMockAssetManager implements AssetManager {
11694 /// Mock asset directory relative to base url.
11795 String defaultAssetsDir = '' ;
11896
@@ -135,6 +113,9 @@ class WebOnlyMockAssetManager extends AssetManager {
135113 @override
136114 String get assetsDir => defaultAssetsDir;
137115
116+ @override
117+ String get _baseUrl => '' ;
118+
138119 @override
139120 String getAssetUrl (String asset) => asset;
140121
@@ -146,7 +127,7 @@ class WebOnlyMockAssetManager extends AssetManager {
146127 status: 200 ,
147128 payload: MockHttpFetchPayload (
148129 byteBuffer: _toByteData (utf8.encode (defaultAssetManifest)).buffer,
149- ),
130+ )
150131 );
151132 }
152133 if (asset == getAssetUrl ('FontManifest.json' )) {
@@ -155,7 +136,7 @@ class WebOnlyMockAssetManager extends AssetManager {
155136 status: 200 ,
156137 payload: MockHttpFetchPayload (
157138 byteBuffer: _toByteData (utf8.encode (defaultFontManifest)).buffer,
158- ),
139+ )
159140 );
160141 }
161142
0 commit comments