From: Eduardo San Martin Morote Date: Wed, 15 Jan 2020 16:36:43 +0000 (+0100) Subject: encoding wip X-Git-Tag: v4.0.0-alpha.0~105 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c8acd9d85e66d55ea187a00e8defb0814ddc76e9;p=thirdparty%2Fvuejs%2Frouter.git encoding wip --- diff --git a/src/history/common.ts b/src/history/common.ts index c8a33f93..8149060a 100644 --- a/src/history/common.ts +++ b/src/history/common.ts @@ -1,4 +1,5 @@ import { ListenerRemover } from '../types' +import { encodeQueryProperty, encodeHash, encodePath } from '../utils/encoding' export type HistoryQuery = Record // TODO: is it reall worth allowing null to form queries like ?q&b&c @@ -95,9 +96,6 @@ export interface RouterHistory { // Generic utils -// needed for the global flag -const PERCENT_RE = /%/g - /** * Transforms an URI into a normalized history location * @param location URI to normalize @@ -141,6 +139,9 @@ export function parseURL(location: string): HistoryLocationNormalized { } } +// TODO: the encoding would be handled at a router level instead where encoding functions can be customized +// that way the matcher can encode/decode params properly + function safeDecodeUriComponent(value: string): string { try { value = decodeURIComponent(value) @@ -182,8 +183,6 @@ export function parseQuery(search: string): HistoryQuery { const searchParams = (hasLeadingIM ? search.slice(1) : search).split('&') for (let i = 0; i < searchParams.length; ++i) { let [key, value] = searchParams[i].split('=') - key = safeDecodeUriComponent(key) - value = safeDecodeUriComponent(value) if (key in query) { // an extra variable for ts types let currentValue = query[key] @@ -206,7 +205,9 @@ export function stringifyURL(location: HistoryLocation): string { let url = location.path let query = location.query ? stringifyQuery(location.query) : '' - return url + (query && '?' + query) + (location.hash || '') + return ( + encodePath(url) + (query && '?' + query) + encodeHash(location.hash || '') + ) } /** @@ -215,7 +216,6 @@ export function stringifyURL(location: HistoryLocation): string { */ export function stringifyQuery(query: RawHistoryQuery): string { let search = '' - // TODO: util function? for (const key in query) { if (search.length > 1) search += '&' const value = query[key] @@ -224,13 +224,13 @@ export function stringifyQuery(query: RawHistoryQuery): string { search += key continue } - let encodedKey = safeEncodeUriComponent(key) + const encodedKey = encodeQueryProperty(key) let values: string[] = Array.isArray(value) ? value : [value] - values = values.map(safeEncodeUriComponent) + const encodedValues = values.map(encodeQueryProperty) search += `${encodedKey}=${values[0]}` for (let i = 1; i < values.length; i++) { - search += `&${encodedKey}=${values[i]}` + search += `&${encodedKey}=${encodedValues[i]}` } } @@ -248,16 +248,6 @@ export function normalizeQuery(query: RawHistoryQuery): HistoryQuery { return normalizedQuery } -// FIXME: not used -/** - * Prepare a URI string to be passed to pushState - * @param uri - */ -export function prepareURI(uri: string) { - // encode the % symbol so it also works on IE - return uri.replace(PERCENT_RE, '%25') -} - // use regular decodeURI // Use a renamed export instead of global.decodeURI // to support node and browser at the same time diff --git a/src/router.ts b/src/router.ts index 4dabd2b7..3518dd05 100644 --- a/src/router.ts +++ b/src/router.ts @@ -49,6 +49,7 @@ export interface RouterOptions { history: RouterHistory routes: RouteRecord[] scrollBehavior?: ScrollBehavior + // TODO: allow customizing encoding functions } export interface Router { diff --git a/src/utils/encoding.ts b/src/utils/encoding.ts index 4f18ab73..41f69b0b 100644 --- a/src/utils/encoding.ts +++ b/src/utils/encoding.ts @@ -54,11 +54,14 @@ export function encodeQueryProperty(text: string): string { .replace(ENC_CARET_RE, '^') } -export function encodeParam(text: string): string { +export function encodePath(text: string): string { return commonEncode(text) - .replace(SLASH_RE, '%2F') .replace(HASH_RE, '%23') .replace(IM_RE, '%3F') } +export function encodeParam(text: string): string { + return encodePath(text).replace(SLASH_RE, '%2F') +} + export const decode = decodeURIComponent