]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat(warn): warn multiple leading slashes
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 12 May 2020 14:00:06 +0000 (16:00 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Tue, 12 May 2020 14:40:04 +0000 (16:40 +0200)
__tests__/warnings.spec.ts
src/router.ts

index c321d38f1945b639f890896afe46f512b1d7fe6c..ec25c745718b061a065cb733d5686d5d1a44c44c 100644 (file)
@@ -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()
+  })
 })
index 69b5b7fee2762231f4b03582a1962ed335b05b48..5edcb1879797735052998bbb3c036bd50605bb62 100644 (file)
@@ -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 +