fix(router): handle url fragments when no url segments present

This commit is contained in:
vsavkin 2016-07-22 16:56:10 -07:00
parent 8d90a5a4cf
commit 43437c175a
3 changed files with 30 additions and 26 deletions

View File

@ -294,7 +294,7 @@ class UrlParser {
this.capture('/'); this.capture('/');
} }
if (this.remaining === '' || this.remaining.startsWith('?')) { if (this.remaining === '' || this.remaining.startsWith('?') || this.remaining.startsWith('#')) {
return new UrlSegment([], {}); return new UrlSegment([], {});
} else { } else {
return new UrlSegment([], this.parseSegmentChildren()); return new UrlSegment([], this.parseSegmentChildren());

View File

@ -1226,35 +1226,34 @@ describe('Integration', () => {
it('should set the class on a parent element when the link is active', it('should set the class on a parent element when the link is active',
fakeAsync(inject( fakeAsync(inject(
[Router, TestComponentBuilder, Location], [Router, TestComponentBuilder, Location],
(router: Router, tcb: TestComponentBuilder, location: Location) => { (router: Router, tcb: TestComponentBuilder, location: Location) => {
const fixture = createRoot(tcb, router, RootCmp); const fixture = createRoot(tcb, router, RootCmp);
router.resetConfig([{ router.resetConfig([{
path: 'team/:id', path: 'team/:id',
component: TeamCmp, component: TeamCmp,
children: [{ children: [{
path: 'link', path: 'link',
component: DummyLinkWithParentCmp, component: DummyLinkWithParentCmp,
children: [ children:
{path: 'simple', component: SimpleCmp}, {path: '', component: BlankCmp} [{path: 'simple', component: SimpleCmp}, {path: '', component: BlankCmp}]
] }]
}] }]);
}]);
router.navigateByUrl('/team/22/link;exact=true'); router.navigateByUrl('/team/22/link;exact=true');
advance(fixture); advance(fixture);
expect(location.path()).toEqual('/team/22/link;exact=true'); expect(location.path()).toEqual('/team/22/link;exact=true');
const native = fixture.debugElement.nativeElement.querySelector('link-parent'); const native = fixture.debugElement.nativeElement.querySelector('link-parent');
expect(native.className).toEqual('active'); expect(native.className).toEqual('active');
router.navigateByUrl('/team/22/link/simple'); router.navigateByUrl('/team/22/link/simple');
advance(fixture); advance(fixture);
expect(location.path()).toEqual('/team/22/link/simple'); expect(location.path()).toEqual('/team/22/link/simple');
expect(native.className).toEqual(''); expect(native.className).toEqual('');
}))); })));
it('should set the class when the link is active', it('should set the class when the link is active',
fakeAsync(inject( fakeAsync(inject(

View File

@ -156,6 +156,11 @@ describe('url serializer', () => {
expect(url.serialize(tree)).toEqual('/one#two'); expect(url.serialize(tree)).toEqual('/one#two');
}); });
it('should parse fragment (root)', () => {
const tree = url.parse('/#one');
expectSegment(tree.root, '');
expect(url.serialize(tree)).toEqual('/#one');
});
it('should parse empty fragment', () => { it('should parse empty fragment', () => {
const tree = url.parse('/one#'); const tree = url.parse('/one#');