From: Eduardo San Martin Morote Date: Fri, 2 Oct 2020 08:48:48 +0000 (+0200) Subject: fix(encoding): decode hash in string location X-Git-Tag: v4.0.0-beta.13~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=11acb3dea072592f00a23b912d39c3fcf72dc6c3;p=thirdparty%2Fvuejs%2Frouter.git fix(encoding): decode hash in string location --- diff --git a/__tests__/urlEncoding.spec.ts b/__tests__/urlEncoding.spec.ts index 699f99d9..49008ac4 100644 --- a/__tests__/urlEncoding.spec.ts +++ b/__tests__/urlEncoding.spec.ts @@ -65,14 +65,16 @@ describe('URL Encoding', () => { it('calls decode with a path', async () => { const router = createRouter() await router.push('/p/foo') - expect(encoding.decode).toHaveBeenCalledTimes(1) + // one extra time for hash + expect(encoding.decode).toHaveBeenCalledTimes(2) expect(encoding.decode).toHaveBeenNthCalledWith(1, 'foo') }) it('calls decode with a path with repeatable params', async () => { const router = createRouter() await router.push('/p/foo/bar') - expect(encoding.decode).toHaveBeenCalledTimes(2) + // one extra time for hash + expect(encoding.decode).toHaveBeenCalledTimes(3) expect(encoding.decode).toHaveBeenNthCalledWith(1, 'foo', 0, ['foo', 'bar']) expect(encoding.decode).toHaveBeenNthCalledWith(2, 'bar', 1, ['foo', 'bar']) }) @@ -102,7 +104,8 @@ describe('URL Encoding', () => { it('calls decode with query', async () => { const router = createRouter() await router.push('/?p=foo') - expect(encoding.decode).toHaveBeenCalledTimes(2) + // one extra time for hash + expect(encoding.decode).toHaveBeenCalledTimes(3) expect(encoding.decode).toHaveBeenNthCalledWith(1, 'p') expect(encoding.decode).toHaveBeenNthCalledWith(2, 'foo') }) @@ -141,4 +144,16 @@ describe('URL Encoding', () => { hash: '#%', }) }) + it('decodes hash', async () => { + // @ts-ignore: override to make the difference + encoding.decode = () => '#d' + // @ts-ignore + encoding.encodeHash = () => '#e' + const router = createRouter() + await router.push('#%20') + expect(router.currentRoute.value).toMatchObject({ + fullPath: '/#%20', + hash: '#d', + }) + }) }) diff --git a/src/router.ts b/src/router.ts index f5e09412..786c4180 100644 --- a/src/router.ts +++ b/src/router.ts @@ -420,6 +420,7 @@ export function createRouter(options: RouterOptions): Router { // locationNormalized is always a new object return assign(locationNormalized, matchedRoute, { params: decodeParams(matchedRoute.params), + hash: decode(locationNormalized.hash), redirectedFrom: undefined, href, })