]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix: allow empty paths children
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 6 May 2019 16:37:55 +0000 (18:37 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 6 May 2019 16:37:55 +0000 (18:37 +0200)
__tests__/matcher.spec.js
src/matcher.ts

index 97f1f111efa5c0c4db61c072efd79d2a209cd998..b0453cb63cd8a4ac5a28f9b4565a8206d809c39d 100644 (file)
@@ -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',
index aa6d1914c2746fa1cf563e66820f83be5a0193ba..f207e23e52f3735df91664f61f7361b114f50150 100644 (file)
@@ -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
-}