]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat: resolve simple relative links
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 3 Aug 2020 15:47:29 +0000 (17:47 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 3 Aug 2020 15:47:29 +0000 (17:47 +0200)
Works with links like ../../url but not with ../url/./path

__tests__/location.spec.ts
__tests__/router.spec.ts
src/location.ts

index 3d45a8badb27017e8b8951dc2491e91ee799075d..a770e900db86353ccd22f0f5c9d5b8e7c20dc4dc 100644 (file)
@@ -335,6 +335,12 @@ describe('resolveRelativePath', () => {
     expect(resolveRelativePath('/add', '/users/posva')).toBe('/add')
   })
 
+  it('resolves empty path', () => {
+    expect(resolveRelativePath('', '/users/posva')).toBe('/users/posva')
+    expect(resolveRelativePath('', '/users')).toBe('/users')
+    expect(resolveRelativePath('', '/')).toBe('/')
+  })
+
   it('warns if from path is not absolute', () => {
     resolveRelativePath('path', 'other')
     resolveRelativePath('path', './other')
index 6cb73b0d830b98eb48873b9b867ae976dc9dc47f..98aa0ec94d305560d0704cf5519eb7c86cc1f0ad 100644 (file)
@@ -72,6 +72,7 @@ const routes: RouteRecordRaw[] = [
       },
     ],
   },
+  { path: '/:pathMatch(.*)', component: components.Home, name: 'catch-all' },
 ]
 
 async function newRouter(
@@ -331,6 +332,26 @@ describe('Router', () => {
     })
   })
 
+  it('resolves relative locations', async () => {
+    const { router } = await newRouter()
+    await router.push('/users/posva')
+    await router.push('add')
+    expect(router.currentRoute.value.path).toBe('/users/add')
+    await router.push('/users/posva')
+    await router.push('./add')
+    expect(router.currentRoute.value.path).toBe('/users/add')
+  })
+
+  it('resolves parent relative locations', async () => {
+    const { router } = await newRouter()
+    await router.push('/users/posva')
+    await router.push('../add')
+    expect(router.currentRoute.value.path).toBe('/add')
+    await router.push('/users/posva')
+    await router.push('../../../add')
+    expect(router.currentRoute.value.path).toBe('/add')
+  })
+
   describe('Warnings', () => {
     it.skip('avoid infinite redirection loops', async () => {
       const history = createMemoryHistory()
index 3e67b36702ab853bc90d47031c81b9e57d55b2cb..cd175eebdd66cab804ea2d249d2fb83e1700541f 100644 (file)
@@ -72,16 +72,8 @@ export function parseURL(
   }
 
   // no search and no query
-  path = path != null ? path : location
+  path = resolveRelativePath(path != null ? path : location, currentLocation)
   // empty path means a relative query or hash `?foo=f`, `#thing`
-  if (!path) {
-    path = currentLocation + path
-  } else if (path[0] !== '/') {
-    // relative to current location. Currently we only support simple relative
-    // but no `..`, `.`, or complex like `../.././..`. We will always leave the
-    // leading slash so we can safely append path
-    path = currentLocation.replace(/[^\/]*$/, '') + path
-  }
 
   return {
     fullPath: path + (searchString && '?') + searchString + hash,
@@ -211,6 +203,8 @@ export function resolveRelativePath(to: string, from: string): string {
     return to
   }
 
+  if (!to) return from
+
   const fromSegments = from.split('/')
   const toSegments = to.split('/')