Skip to content

Commit ff406e6

Browse files
committed
feat(UrlResolver): combine a base URL with an URL
1 parent e0cf1c7 commit ff406e6

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {isPresent, isBlank, RegExpWrapper} from 'angular2/src/facade/lang';
2+
import {DOM, Element} from 'angular2/src/facade/dom';
3+
4+
export class UrlResolver {
5+
static a: Element;
6+
7+
constructor() {
8+
if (isBlank(UrlResolver.a)) {
9+
UrlResolver.a = DOM.createElement('a');
10+
}
11+
}
12+
13+
resolve(baseUrl: string, url: string): string {
14+
if (isBlank(baseUrl)) {
15+
UrlResolver.a.href = url;
16+
return UrlResolver.a.href;
17+
}
18+
19+
if (isBlank(url) || url == '') return baseUrl;
20+
21+
if (url[0] == '/') {
22+
throw new BaseException(`Could not resolve the url ${url} from ${baseUrl}`);
23+
}
24+
25+
var m = RegExpWrapper.firstMatch(_schemeRe, url);
26+
27+
if (isPresent(m[1])) {
28+
return url;
29+
}
30+
31+
UrlResolver.a.href = baseUrl + '/../' + url;
32+
return UrlResolver.a.href;
33+
}
34+
}
35+
36+
var _schemeRe = RegExpWrapper.create('^([^:/?#]+:)?');
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {describe, it, expect, beforeEach, ddescribe, iit, xit, el} from 'angular2/test_lib';
2+
import {UrlResolver} from 'angular2/src/core/compiler/url_resolver';
3+
4+
export function main() {
5+
describe('UrlResolver', () => {
6+
var resolver = new UrlResolver();
7+
8+
it('should add a relative path to the base url', () => {
9+
expect(resolver.resolve('http://www.foo.com', 'bar')).toEqual('http://www.foo.com/bar');
10+
expect(resolver.resolve('http://www.foo.com/', 'bar')).toEqual('http://www.foo.com/bar');
11+
expect(resolver.resolve('http://www.foo.com', './bar')).toEqual('http://www.foo.com/bar');
12+
expect(resolver.resolve('http://www.foo.com/', './bar')).toEqual('http://www.foo.com/bar');
13+
});
14+
15+
it('should replace the base path', () => {
16+
expect(resolver.resolve('http://www.foo.com/baz', 'bar')).toEqual('http://www.foo.com/bar');
17+
expect(resolver.resolve('http://www.foo.com/baz', './bar')).toEqual('http://www.foo.com/bar');
18+
});
19+
20+
it('should append to the base path', () => {
21+
expect(resolver.resolve('http://www.foo.com/baz/', 'bar')).toEqual('http://www.foo.com/baz/bar');
22+
expect(resolver.resolve('http://www.foo.com/baz/', './bar')).toEqual('http://www.foo.com/baz/bar');
23+
});
24+
25+
it('should support ".." in the path', () => {
26+
expect(resolver.resolve('http://www.foo.com/baz/', '../bar')).toEqual('http://www.foo.com/bar');
27+
expect(resolver.resolve('http://www.foo.com/1/2/3/', '../../bar')).toEqual('http://www.foo.com/1/bar');
28+
expect(resolver.resolve('http://www.foo.com/1/2/3/', '../biz/bar')).toEqual('http://www.foo.com/1/2/biz/bar');
29+
expect(resolver.resolve('http://www.foo.com/1/2/baz', '../../bar')).toEqual('http://www.foo.com/bar');
30+
});
31+
32+
it('should ignore the base path when the url has a scheme', () => {
33+
expect(resolver.resolve('http://www.foo.com', 'http://www.bar.com')).toEqual('http://www.bar.com');
34+
})
35+
36+
it('should throw when the url start with "/"', () => {
37+
expect(() => {
38+
resolver.resolve('http://www.foo.com/1/2', '/test');
39+
}).toThrowError();
40+
});
41+
});
42+
}

0 commit comments

Comments
 (0)