From: Eduardo San Martin Morote Date: Tue, 12 May 2020 14:00:06 +0000 (+0200) Subject: feat(warn): warn multiple leading slashes X-Git-Tag: v4.0.0-alpha.12~17 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=87c5e53b43c218c83f9db986ac7538d74525ea5b;p=thirdparty%2Fvuejs%2Frouter.git feat(warn): warn multiple leading slashes --- diff --git a/__tests__/warnings.spec.ts b/__tests__/warnings.spec.ts index c321d38f..ec25c745 100644 --- a/__tests__/warnings.spec.ts +++ b/__tests__/warnings.spec.ts @@ -126,4 +126,24 @@ describe('warnings', () => { await expect(router.push('/foo')).resolves.toBe(undefined) expect('with path "/foo" is a function').toHaveBeenWarned() }) + + it('should warn if multiple leading slashes with raw location', async () => { + const router = createRouter({ + history: createMemoryHistory(), + routes: [{ path: '/foo', component }], + }) + + await expect(router.push('//not-valid')).resolves.toBe(undefined) + expect('cannot start with multiple slashes').toHaveBeenWarned() + }) + + it('should warn if multiple leading slashes with object location', async () => { + const router = createRouter({ + history: createMemoryHistory(), + routes: [{ path: '/foo', component }], + }) + + await expect(router.push({ path: '//not-valid' })).resolves.toBe(undefined) + expect('cannot start with multiple slashes').toHaveBeenWarned() + }) }) diff --git a/src/router.ts b/src/router.ts index 69b5b7fe..5edcb187 100644 --- a/src/router.ts +++ b/src/router.ts @@ -238,6 +238,14 @@ export function createRouter(options: RouterOptions): Router { currentLocation ) + if (__DEV__) { + let href = routerHistory.base + locationNormalized.fullPath + if (href.startsWith('//')) + warn( + `Location "${rawLocation}" resolved to "${href}". A resolved location cannot start with multiple slashes.` + ) + } + return { // fullPath: locationNormalized.fullPath, // query: locationNormalized.query, @@ -303,6 +311,14 @@ export function createRouter(options: RouterOptions): Router { path: matchedRoute.path, }) + if (__DEV__) { + let href = routerHistory.base + fullPath + if (href.startsWith('//')) + warn( + `Location "${rawLocation}" resolved to "${href}". A resolved location cannot start with multiple slashes.` + ) + } + return { fullPath, // keep the hash encoded so fullPath is effectively path + encodedQuery +