2015-05-04 11:11:22 -07:00
|
|
|
import {Injectable} from 'angular2/src/di/annotations_impl';
|
2015-02-24 16:05:45 +01:00
|
|
|
import {isPresent, isBlank, RegExpWrapper, BaseException} from 'angular2/src/facade/lang';
|
2015-02-27 14:50:06 -08:00
|
|
|
import {DOM} from 'angular2/src/dom/dom_adapter';
|
2015-02-20 18:00:09 +01:00
|
|
|
|
2015-03-16 14:44:14 -07:00
|
|
|
@Injectable()
|
2015-02-20 18:00:09 +01:00
|
|
|
export class UrlResolver {
|
2015-02-27 14:50:06 -08:00
|
|
|
static a;
|
2015-02-20 18:00:09 +01:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
if (isBlank(UrlResolver.a)) {
|
|
|
|
UrlResolver.a = DOM.createElement('a');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-13 09:28:42 +02:00
|
|
|
/**
|
|
|
|
* Resolves the `url` given the `baseUrl`.
|
|
|
|
*
|
|
|
|
* ## When the `baseUrl` is null
|
|
|
|
*
|
|
|
|
* `url` is resolved in the context of the current document.
|
|
|
|
* If the document location is 'http://www.foo.com/base' and the `url` is 'path/to/here', the resolved url will be
|
|
|
|
* 'http://www.foo.com/base/path/to/here'
|
|
|
|
*
|
|
|
|
* ## When the `baseUrl` is not null
|
|
|
|
*
|
|
|
|
* - when the `url` is null, the `baseUrl` is returned,
|
|
|
|
* - due to a limitation in the process used to resolve urls (a HTMLLinkElement), `url` must not start with a `/`,
|
|
|
|
* - if `url` is relative ('path/to/here', './path/to/here'), the resolved url is a combination of `baseUrl` and `url`,
|
|
|
|
* - if `url` is absolute (it has a scheme: 'http://', 'https://'), the `url` is returned (ignoring the `baseUrl`)
|
|
|
|
*
|
|
|
|
* @param {string} baseUrl
|
|
|
|
* @param {string} url
|
|
|
|
* @returns {string} the resolved URL
|
|
|
|
*/
|
2015-02-20 18:00:09 +01:00
|
|
|
resolve(baseUrl: string, url: string): string {
|
|
|
|
if (isBlank(baseUrl)) {
|
2015-03-09 11:35:46 +01:00
|
|
|
DOM.resolveAndSetHref(UrlResolver.a, url, null);
|
|
|
|
return DOM.getHref(UrlResolver.a);
|
2015-02-20 18:00:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isBlank(url) || url == '') return baseUrl;
|
|
|
|
|
|
|
|
if (url[0] == '/') {
|
2015-05-13 09:28:42 +02:00
|
|
|
// The `HTMLLinkElement` does not allow resolving this case (the `url` would be interpreted as relative):
|
|
|
|
// - `baseUrl` = 'http://www.foo.com/base'
|
|
|
|
// - `url` = '/absolute/path/to/here'
|
|
|
|
// - the result would be 'http://www.foo.com/base/absolute/path/to/here' while 'http://www.foo.com/absolute/path/to/here'
|
|
|
|
// is expected (without the 'base' segment).
|
2015-02-20 18:00:09 +01:00
|
|
|
throw new BaseException(`Could not resolve the url ${url} from ${baseUrl}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
var m = RegExpWrapper.firstMatch(_schemeRe, url);
|
|
|
|
|
|
|
|
if (isPresent(m[1])) {
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2015-03-09 11:35:46 +01:00
|
|
|
DOM.resolveAndSetHref(UrlResolver.a, baseUrl, url);
|
|
|
|
return DOM.getHref(UrlResolver.a);
|
2015-02-20 18:00:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _schemeRe = RegExpWrapper.create('^([^:/?#]+:)?');
|