fix(router): disallow root segments with matrix params

This commit is contained in:
vsavkin 2016-07-08 10:56:36 -07:00
parent fa47890032
commit 34b3c534e7
2 changed files with 14 additions and 1 deletions

View File

@ -19,6 +19,8 @@ export function createUrlTree(
} }
const normalizedCommands = normalizeCommands(commands); const normalizedCommands = normalizeCommands(commands);
validateCommands(normalizedCommands);
if (navigateToRoot(normalizedCommands)) { if (navigateToRoot(normalizedCommands)) {
return tree(urlTree.root, new UrlSegment([], {}), urlTree, queryParams, fragment); return tree(urlTree.root, new UrlSegment([], {}), urlTree, queryParams, fragment);
} }
@ -31,6 +33,12 @@ export function createUrlTree(
return tree(startingPosition.segment, segment, urlTree, queryParams, fragment); return tree(startingPosition.segment, segment, urlTree, queryParams, fragment);
} }
function validateCommands(n: NormalizedNavigationCommands): void {
if (n.isAbsolute && n.commands.length > 0 && (typeof n.commands[0] === 'object')) {
throw new Error('Root segment cannot have matrix parameters');
}
}
function tree( function tree(
oldSegment: UrlSegment, newSegment: UrlSegment, urlTree: UrlTree, queryParams: Params, oldSegment: UrlSegment, newSegment: UrlSegment, urlTree: UrlTree, queryParams: Params,
fragment: string): UrlTree { fragment: string): UrlTree {

View File

@ -14,13 +14,18 @@ describe('createUrlTree', () => {
expect(serializer.serialize(t)).toEqual('/'); expect(serializer.serialize(t)).toEqual('/');
}); });
it('should error when navigating to the root segment with params', () => {
const p = serializer.parse('/');
expect(() => createRoot(p, ['/', {p: 11}]))
.toThrowError(/Root segment cannot have matrix parameters/);
});
it('should support nested segments', () => { it('should support nested segments', () => {
const p = serializer.parse('/a/b'); const p = serializer.parse('/a/b');
const t = createRoot(p, ['/one', 11, 'two', 22]); const t = createRoot(p, ['/one', 11, 'two', 22]);
expect(serializer.serialize(t)).toEqual('/one/11/two/22'); expect(serializer.serialize(t)).toEqual('/one/11/two/22');
}); });
it('should stringify positional parameters', () => { it('should stringify positional parameters', () => {
const p = serializer.parse('/a/b'); const p = serializer.parse('/a/b');
const t = createRoot(p, ['/one', 11]); const t = createRoot(p, ['/one', 11]);