]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix(encoding): decode hash in string location
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 2 Oct 2020 08:48:48 +0000 (10:48 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Fri, 2 Oct 2020 08:48:48 +0000 (10:48 +0200)
__tests__/urlEncoding.spec.ts
src/router.ts

index 699f99d9f1867d08c3cbc59d543ac5b057b93dcf..49008ac47eac6d51ac6e86862910dc3d80d7b8ff 100644 (file)
@@ -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',
+    })
+  })
 })
index f5e09412af45bbcf748f8fbea0291c7894781cc0..786c4180a4a0ad1532f8f5c65c397064341c3f1c 100644 (file)
@@ -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,
       })