]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
wip: refactoring encoding
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 31 Jan 2020 18:46:17 +0000 (19:46 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 5 Feb 2020 13:06:40 +0000 (14:06 +0100)
src/navigationGuards.ts
src/router.ts
src/utils/encoding.ts

index 1846428b58a2b07be78b949b31c243c590d811ac..274fa469f4a01015d78dd5fc9c2fd2d06429cee8 100644 (file)
@@ -18,5 +18,5 @@ export function onBeforeRouteLeave(leaveGuard: NavigationGuard) {
     return
   }
 
-  matched.leaveGuards.push(leaveGuard.bind(instance!.proxy))
+  matched.leaveGuards.push(leaveGuard.bind(instance.proxy))
 }
index 865720cbfa3f5df17c9a90be9cc2a4da40aa3b9e..089d0bc49c93f0b6e52565f29a54cbbc2e7d6c9f 100644 (file)
@@ -17,7 +17,6 @@ import {
   normalizeLocation,
   stringifyURL,
   normalizeQuery,
-  HistoryLocationNormalized,
 } from './history/common'
 import {
   ScrollToPosition,
@@ -140,6 +139,9 @@ export function createRouter({
     return resolveLocation(
       {
         // TODO: refactor with url utils
+        // TODO: query must be encoded by normalizeLocation
+        // refactor the whole thing so it uses the same overridable functions
+        // sent to history
         query: {},
         hash: '',
         ...to,
@@ -236,36 +238,16 @@ export function createRouter({
   }
 
   async function push(to: RouteLocation): Promise<RouteLocationNormalized> {
-    let url: HistoryLocationNormalized
-    let location: RouteLocationNormalized
-    // TODO: refactor into matchLocation to return location and url
-    if (typeof to === 'string' || ('path' in to && !('name' in to))) {
-      url = normalizeLocation(to)
-      // TODO: should allow a non matching url to allow dynamic routing to work
-      location = resolveLocation(url, currentRoute.value)
-    } else {
-      // named or relative route
-      const query = to.query ? normalizeQuery(to.query) : {}
-      const hash = to.hash || ''
-      // we need to resolve first
-      location = resolveLocation({ ...to, query, hash }, currentRoute.value)
-      // intentionally drop current query and hash
-      url = normalizeLocation({
-        query,
-        hash,
-        ...location,
-      })
-    }
+    const toLocation: RouteLocationNormalized = (pendingLocation = resolve(to))
 
     // TODO: should we throw an error as the navigation was aborted
     // TODO: needs a proper check because order in query could be different
     if (
       currentRoute.value !== START_LOCATION_NORMALIZED &&
-      currentRoute.value.fullPath === url.fullPath
+      currentRoute.value.fullPath === toLocation.fullPath
     )
       return currentRoute.value
 
-    const toLocation: RouteLocationNormalized = (pendingLocation = location)
     // trigger all guards, throw if navigation is rejected
     try {
       await navigate(toLocation, currentRoute.value)
index 41f69b0b7e9cca1b30ad972139b8d1a2fb378c08..ba4e10f104c0b6ea4dcd0f7a6449ab39b0401bbe 100644 (file)
@@ -1,3 +1,5 @@
+import { warn } from 'vue'
+
 /**
  * Encoding Rules
  * ␣ = Space
@@ -64,4 +66,11 @@ export function encodeParam(text: string): string {
   return encodePath(text).replace(SLASH_RE, '%2F')
 }
 
-export const decode = decodeURIComponent
+export function decode(text: string): string {
+  try {
+    return decodeURIComponent(text)
+  } catch (err) {
+    __DEV__ && warn(`Error decoding "${text}". Using original value`)
+  }
+  return text
+}