8
8
library angular.core_dom.resource_url_resolver;
9
9
10
10
import 'dart:html' ;
11
- import 'dart:js' as js;
12
11
13
12
import 'package:di/di.dart' ;
14
13
import 'package:di/annotations.dart' ;
@@ -29,12 +28,14 @@ class ResourceUrlResolver {
29
28
static final RegExp quotes = new RegExp ("[\"\' ]" );
30
29
31
30
// Reconstruct the Uri without the http or https restriction due to Uri.base.origin
32
- final _baseUri = _getBaseUri () ;
31
+ final String _baseUri ;
33
32
34
33
final TypeToUriMapper _uriMapper;
35
34
final ResourceResolverConfig _config;
36
35
37
- ResourceUrlResolver (this ._uriMapper, this ._config);
36
+ ResourceUrlResolver (this ._uriMapper, this ._config): _baseUri = _getBaseUri ();
37
+
38
+ ResourceUrlResolver .forTests (this ._uriMapper, this ._config, this ._baseUri);
38
39
39
40
static final NodeTreeSanitizer _nullTreeSanitizer = new _NullTreeSanitizer ();
40
41
static final docForParsing = document.implementation.createHtmlDocument ('' );
@@ -139,34 +140,42 @@ class ResourceUrlResolver {
139
140
/// URIs, while [uri] is assumed to use 'packages/' syntax for
140
141
/// package-relative URIs. Resulting URIs will use 'packages/' to indicate
141
142
/// package-relative URIs.
142
- String combine (Uri baseUri, String uri ) {
143
+ String combine (Uri baseUri, String path ) {
143
144
if (! _config.useRelativeUrls) {
144
- return uri ;
145
+ return path ;
145
146
}
146
147
147
- if (uri == null ) {
148
- uri = baseUri.path;
148
+ Uri resolved;
149
+ if (path == null ) {
150
+ resolved = baseUri;
149
151
} else {
152
+ Uri uri = Uri .parse (path);
150
153
// if it's absolute but not package-relative, then just use that
151
154
// The "packages/" test is just for backward compatibility. It's ok to
152
155
// not resolve them, even through they're relative URLs, because in a Dart
153
156
// application, "packages/" is managed by pub which creates a symlinked
154
157
// hierarchy and they should all resolve to the same file at any level
155
158
// that a "packages/" exists.
156
- if (uri.startsWith ("/" ) || uri.startsWith ('packages/' )) {
157
- return uri;
159
+ if (uri.path.startsWith ('/' ) ||
160
+ uri.path.startsWith ('packages/' ) ||
161
+ uri.path.trim () == '' || // Covers both empty strings and # fragments
162
+ uri.isAbsolute) {
163
+ return _uriToPath (uri);
158
164
}
165
+ // Not an absolute uri. Resolve it to the base.
166
+ resolved = baseUri.resolve (path);
159
167
}
160
- // If it's not absolute, then resolve it first
161
- Uri resolved = baseUri.resolve (uri);
162
-
163
- // If it's package-relative, tack on [packageRoot].
164
- if (resolved.scheme == 'package' ) {
165
- return '${_config .packageRoot }${resolved .path }' ;
166
- } else if (resolved.isAbsolute && resolved.toString ().startsWith (_baseUri)) {
167
- return resolved.path;
168
- } else {
169
- return resolved.toString ();
168
+
169
+ return _uriToPath (resolved);
170
+ }
171
+
172
+ String _uriToPath (Uri uri) {
173
+ if (uri.scheme == 'package' ) {
174
+ return '${_config .packageRoot }${uri .path }' ;
175
+ } else if (uri.isAbsolute && uri.toString ().startsWith (_baseUri)) {
176
+ return uri.path;
177
+ } else {
178
+ return uri.toString ();
170
179
}
171
180
}
172
181
0 commit comments