From: Eduardo San Martin Morote Date: Mon, 6 May 2019 16:37:55 +0000 (+0200) Subject: fix: allow empty paths children X-Git-Tag: v4.0.0-alpha.0~383 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a32b5cd1cf41e68150e184f28b36cca48f2382fe;p=thirdparty%2Fvuejs%2Frouter.git fix: allow empty paths children --- diff --git a/__tests__/matcher.spec.js b/__tests__/matcher.spec.js index 97f1f111..b0453cb6 100644 --- a/__tests__/matcher.spec.js +++ b/__tests__/matcher.spec.js @@ -490,6 +490,56 @@ describe('Router Matcher', () => { ) }) + it('resolves children with empty paths', () => { + const Nested = { path: '', name: 'nested', component } + const Foo = { + path: '/foo', + name: 'Foo', + component, + children: [Nested], + } + assertRecordMatch( + Foo, + { path: '/foo' }, + { + name: 'nested', + path: '/foo', + params: {}, + matched: [Foo, { ...Nested, path: `${Foo.path}` }], + } + ) + }) + + it('resolves nested children with empty paths', () => { + const NestedNested = { path: '', name: 'nested', component } + const Nested = { + path: '', + name: 'nested-nested', + component, + children: [NestedNested], + } + const Foo = { + path: '/foo', + name: 'Foo', + component, + children: [Nested], + } + assertRecordMatch( + Foo, + { path: '/foo' }, + { + name: 'nested', + path: '/foo', + params: {}, + matched: [ + Foo, + { ...Nested, path: `${Foo.path}` }, + { ...NestedNested, path: `${Foo.path}` }, + ], + } + ) + }) + it('resolves nested children', () => { const Foo = { path: '/foo', diff --git a/src/matcher.ts b/src/matcher.ts index aa6d1914..f207e23e 100644 --- a/src/matcher.ts +++ b/src/matcher.ts @@ -36,7 +36,11 @@ export class RouterMatcher { if (parent) { // if the child isn't an absolute route if (record.path[0] !== '/') { - recordCopy.path = parent.record.path + '/' + record.path // TODO: check for trailing slash? + let path = parent.record.path + // only add the / delimiter if the child path isn't empty + if (recordCopy.path) path += '/' + path += record.path // TODO: check for trailing slash? + recordCopy.path = path } } @@ -163,7 +167,15 @@ export class RouterMatcher { // that redirects but ended up not redirecting if ('redirect' in matcher.record) throw new InvalidRouteMatch(location) - const matched = extractMatchedRecord(matcher) + const matched: MatcherLocationNormalized['matched'] = [matcher.record] + let parentMatcher: RouteMatcher | void = matcher.parent + while (parentMatcher) { + // reversed order so parents are at the beginning + // TODO: should be doable by typing RouteMatcher in a different way + if ('redirect' in parentMatcher.record) throw new Error('TODO') + matched.unshift(parentMatcher.record) + parentMatcher = parentMatcher.parent + } return { name, @@ -173,28 +185,3 @@ export class RouterMatcher { } } } - -/** - * Generate the array of the matched array. This is an array containing - * all records matching a route, from parent to child. If there are no children - * in the matched record matcher, the array only contains one element - * @param matcher - * @returns an array of MatcherLocationNormalized - */ -function extractMatchedRecord( - matcher: RouteMatcher -): MatcherLocationNormalized['matched'] { - if ('redirect' in matcher.record) throw new Error('TODO') - - const matched: MatcherLocationNormalized['matched'] = [matcher.record] - let parentMatcher: RouteMatcher | void = matcher.parent - while (parentMatcher) { - // reversed order so parents are at the beginning - // TODO: should be doable by typing RouteMatcher in a different way - if ('redirect' in parentMatcher.record) throw new Error('TODO') - matched.unshift(parentMatcher.record) - parentMatcher = parentMatcher.parent - } - - return matched -}