feat(UrlResolver): combine a base URL with an URL

This commit is contained in:
Victor Berchet 2015-02-20 18:00:09 +01:00
parent e0cf1c7ab5
commit ff406e65dc
2 changed files with 78 additions and 0 deletions

View File

@ -0,0 +1,36 @@
import {isPresent, isBlank, RegExpWrapper} from 'angular2/src/facade/lang';
import {DOM, Element} from 'angular2/src/facade/dom';
export class UrlResolver {
static a: Element;
constructor() {
if (isBlank(UrlResolver.a)) {
UrlResolver.a = DOM.createElement('a');
}
}
resolve(baseUrl: string, url: string): string {
if (isBlank(baseUrl)) {
UrlResolver.a.href = url;
return UrlResolver.a.href;
}
if (isBlank(url) || url == '') return baseUrl;
if (url[0] == '/') {
throw new BaseException(`Could not resolve the url ${url} from ${baseUrl}`);
}
var m = RegExpWrapper.firstMatch(_schemeRe, url);
if (isPresent(m[1])) {
return url;
}
UrlResolver.a.href = baseUrl + '/../' + url;
return UrlResolver.a.href;
}
}
var _schemeRe = RegExpWrapper.create('^([^:/?#]+:)?');

View File

@ -0,0 +1,42 @@
import {describe, it, expect, beforeEach, ddescribe, iit, xit, el} from 'angular2/test_lib';
import {UrlResolver} from 'angular2/src/core/compiler/url_resolver';
export function main() {
describe('UrlResolver', () => {
var resolver = new UrlResolver();
it('should add a relative path to the base url', () => {
expect(resolver.resolve('http://www.foo.com', 'bar')).toEqual('http://www.foo.com/bar');
expect(resolver.resolve('http://www.foo.com/', 'bar')).toEqual('http://www.foo.com/bar');
expect(resolver.resolve('http://www.foo.com', './bar')).toEqual('http://www.foo.com/bar');
expect(resolver.resolve('http://www.foo.com/', './bar')).toEqual('http://www.foo.com/bar');
});
it('should replace the base path', () => {
expect(resolver.resolve('http://www.foo.com/baz', 'bar')).toEqual('http://www.foo.com/bar');
expect(resolver.resolve('http://www.foo.com/baz', './bar')).toEqual('http://www.foo.com/bar');
});
it('should append to the base path', () => {
expect(resolver.resolve('http://www.foo.com/baz/', 'bar')).toEqual('http://www.foo.com/baz/bar');
expect(resolver.resolve('http://www.foo.com/baz/', './bar')).toEqual('http://www.foo.com/baz/bar');
});
it('should support ".." in the path', () => {
expect(resolver.resolve('http://www.foo.com/baz/', '../bar')).toEqual('http://www.foo.com/bar');
expect(resolver.resolve('http://www.foo.com/1/2/3/', '../../bar')).toEqual('http://www.foo.com/1/bar');
expect(resolver.resolve('http://www.foo.com/1/2/3/', '../biz/bar')).toEqual('http://www.foo.com/1/2/biz/bar');
expect(resolver.resolve('http://www.foo.com/1/2/baz', '../../bar')).toEqual('http://www.foo.com/bar');
});
it('should ignore the base path when the url has a scheme', () => {
expect(resolver.resolve('http://www.foo.com', 'http://www.bar.com')).toEqual('http://www.bar.com');
})
it('should throw when the url start with "/"', () => {
expect(() => {
resolver.resolve('http://www.foo.com/1/2', '/test');
}).toThrowError();
});
});
}