From: Eduardo San Martin Morote Date: Mon, 3 Aug 2020 15:47:29 +0000 (+0200) Subject: feat: resolve simple relative links X-Git-Tag: v4.0.0-beta.5~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=af1deaab5e0fd1597a7cf7ee9a6d01cac507970d;p=thirdparty%2Fvuejs%2Frouter.git feat: resolve simple relative links Works with links like ../../url but not with ../url/./path --- diff --git a/__tests__/location.spec.ts b/__tests__/location.spec.ts index 3d45a8ba..a770e900 100644 --- a/__tests__/location.spec.ts +++ b/__tests__/location.spec.ts @@ -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') diff --git a/__tests__/router.spec.ts b/__tests__/router.spec.ts index 6cb73b0d..98aa0ec9 100644 --- a/__tests__/router.spec.ts +++ b/__tests__/router.spec.ts @@ -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() diff --git a/src/location.ts b/src/location.ts index 3e67b367..cd175eeb 100644 --- a/src/location.ts +++ b/src/location.ts @@ -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('/')