From 6ab8350c02d9d49f123797146913b274eea1ff4d Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Fri, 31 Jan 2020 19:46:17 +0100 Subject: [PATCH] wip: refactoring encoding --- src/navigationGuards.ts | 2 +- src/router.ts | 28 +++++----------------------- src/utils/encoding.ts | 11 ++++++++++- 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/src/navigationGuards.ts b/src/navigationGuards.ts index 1846428b..274fa469 100644 --- a/src/navigationGuards.ts +++ b/src/navigationGuards.ts @@ -18,5 +18,5 @@ export function onBeforeRouteLeave(leaveGuard: NavigationGuard) { return } - matched.leaveGuards.push(leaveGuard.bind(instance!.proxy)) + matched.leaveGuards.push(leaveGuard.bind(instance.proxy)) } diff --git a/src/router.ts b/src/router.ts index 865720cb..089d0bc4 100644 --- a/src/router.ts +++ b/src/router.ts @@ -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 { - 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) diff --git a/src/utils/encoding.ts b/src/utils/encoding.ts index 41f69b0b..ba4e10f1 100644 --- a/src/utils/encoding.ts +++ b/src/utils/encoding.ts @@ -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 +} -- 2.47.3