2015-05-18 14:57:20 -04:00
|
|
|
import {Injectable} from 'angular2/di';
|
2015-02-24 10:05:45 -05:00
|
|
|
import {isPresent, isBlank, RegExpWrapper, BaseException} from 'angular2/src/facade/lang';
|
2015-02-27 17:50:06 -05:00
|
|
|
import {DOM} from 'angular2/src/dom/dom_adapter';
|
2015-02-20 12:00:09 -05:00
|
|
|
|
2015-03-16 17:44:14 -04:00
|
|
|
@Injectable()
|
2015-02-20 12:00:09 -05:00
|
|
|
export class UrlResolver {
|
2015-02-27 17:50:06 -05:00
|
|
|
static a;
|
2015-02-20 12:00:09 -05:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
if (isBlank(UrlResolver.a)) {
|
|
|
|
UrlResolver.a = DOM.createElement('a');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-13 03:28:42 -04:00
|
|
|
/**
|
|
|
|
* Resolves the `url` given the `baseUrl`.
|
|
|
|
*
|
|
|
|
* ## When the `baseUrl` is null
|
|
|
|
*
|
|
|
|
* `url` is resolved in the context of the current document.
|
2015-05-18 14:57:20 -04:00
|
|
|
* If the document location is 'http://www.foo.com/base' and the `url` is 'path/to/here', the
|
|
|
|
* resolved url will be
|
2015-05-13 03:28:42 -04:00
|
|
|
* 'http://www.foo.com/base/path/to/here'
|
|
|
|
*
|
|
|
|
* ## When the `baseUrl` is not null
|
|
|
|
*
|
|
|
|
* - when the `url` is null, the `baseUrl` is returned,
|
2015-05-18 14:57:20 -04:00
|
|
|
* - 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`)
|
2015-05-13 03:28:42 -04:00
|
|
|
*
|
|
|
|
* @param {string} baseUrl
|
|
|
|
* @param {string} url
|
|
|
|
* @returns {string} the resolved URL
|
|
|
|
*/
|
2015-02-20 12:00:09 -05:00
|
|
|
resolve(baseUrl: string, url: string): string {
|
|
|
|
if (isBlank(baseUrl)) {
|
2015-03-09 06:35:46 -04:00
|
|
|
DOM.resolveAndSetHref(UrlResolver.a, url, null);
|
|
|
|
return DOM.getHref(UrlResolver.a);
|
2015-02-20 12:00:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isBlank(url) || url == '') return baseUrl;
|
|
|
|
|
|
|
|
if (url[0] == '/') {
|
2015-05-18 14:57:20 -04:00
|
|
|
// The `HTMLLinkElement` does not allow resolving this case (the `url` would be interpreted as
|
|
|
|
// relative):
|
2015-05-13 03:28:42 -04:00
|
|
|
// - `baseUrl` = 'http://www.foo.com/base'
|
|
|
|
// - `url` = '/absolute/path/to/here'
|
2015-05-18 14:57:20 -04:00
|
|
|
// - the result would be 'http://www.foo.com/base/absolute/path/to/here' while
|
|
|
|
// 'http://www.foo.com/absolute/path/to/here'
|
2015-05-13 03:28:42 -04:00
|
|
|
// is expected (without the 'base' segment).
|
2015-02-20 12:00:09 -05: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 06:35:46 -04:00
|
|
|
DOM.resolveAndSetHref(UrlResolver.a, baseUrl, url);
|
|
|
|
return DOM.getHref(UrlResolver.a);
|
2015-02-20 12:00:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _schemeRe = RegExpWrapper.create('^([^:/?#]+:)?');
|