From: Eduardo San Martin Morote Date: Fri, 16 Aug 2019 17:53:19 +0000 (+0200) Subject: refactor: refactor decode/encode queries X-Git-Tag: v4.0.0-alpha.0~260 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=90c794f0f1b36bf9d5227d96414437855631d65a;p=thirdparty%2Fvuejs%2Frouter.git refactor: refactor decode/encode queries --- diff --git a/src/history/utils.ts b/src/history/utils.ts index 0e497c8d..f62f2aea 100644 --- a/src/history/utils.ts +++ b/src/history/utils.ts @@ -46,6 +46,32 @@ export function parseURL(location: string): HistoryLocationNormalized { } } +function safeDecodeUriComponent(value: string): string { + try { + value = decodeURIComponent(value) + } catch (err) { + // TODO: handling only URIError? + console.warn( + `[vue-router] error decoding query "${value}". Keeping the original value.` + ) + } + + return value +} + +function safeEncodeUriComponent(value: string): string { + try { + value = encodeURIComponent(value) + } catch (err) { + // TODO: handling only URIError? + console.warn( + `[vue-router] error encoding query "${value}". Keeping the original value.` + ) + } + + return value +} + /** * Transform a queryString into a query object. Accept both, a version with the leading `?` and without * Should work as URLSearchParams @@ -60,22 +86,8 @@ 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('=') - try { - key = decodeURIComponent(key) - } catch (err) { - // TODO: handling only URIError? - console.warn( - `[vue-router] error decoding query key "${value}" while parsing query. Sticking to the original key.` - ) - } - try { - value = decodeURIComponent(value) - } catch (err) { - // TODO: handling only URIError? - console.warn( - `[vue-router] error decoding query value "${value}" while parsing query. Sticking to the original value.` - ) - } + key = safeDecodeUriComponent(key) + value = safeDecodeUriComponent(value) if (key in query) { // an extra variable for ts types let currentValue = query[key] @@ -116,28 +128,10 @@ export function stringifyQuery(query: RawHistoryQuery): string { search += key continue } - let encodedKey: string = key - try { - encodedKey = encodeURIComponent(key) - } catch (err) { - // TODO: handling only URIError? - // TODO: refactor all encoded handling in a custom function - - console.warn( - `[vue-router] invalid query parameter while stringifying query: "${key}": "${value}"` - ) - } - + let encodedKey = safeEncodeUriComponent(key) let values: string[] = Array.isArray(value) ? value : [value] - try { - values = values.map(encodeURIComponent) - } catch (err) { - // TODO: handling only URIError? - - console.warn( - `[vue-router] invalid query parameter while stringifying query: "${key}": "${values}"` - ) - } + values = values.map(safeEncodeUriComponent) + search += `${encodedKey}=${values[0]}` for (let i = 1; i < values.length; i++) { search += `&${encodedKey}=${values[i]}`