From: Eduardo San Martin Morote Date: Wed, 5 Feb 2020 11:41:05 +0000 (+0100) Subject: fix: parse empty keys as null X-Git-Tag: v4.0.0-alpha.0~44 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=21b3b95ce87b916523e3b511f94c9962248ba539;p=thirdparty%2Fvuejs%2Frouter.git fix: parse empty keys as null --- diff --git a/__tests__/parseQuery.spec.ts b/__tests__/parseQuery.spec.ts index fd7988ad..bb1863b9 100644 --- a/__tests__/parseQuery.spec.ts +++ b/__tests__/parseQuery.spec.ts @@ -9,6 +9,20 @@ describe('parseQuery', () => { }) }) + it('decodes empty values as null', () => { + expect(parseQuery('e&b&c=a')).toEqual({ + e: null, + b: null, + c: 'a', + }) + }) + + it('decodes empty values as null in arrays', () => { + expect(parseQuery('e&e&e=a')).toEqual({ + e: [null, null, 'a'], + }) + }) + it('decodes array values in query', () => { expect(parseQuery('e=%25&e=%22')).toEqual({ e: ['%', '"'], diff --git a/src/history/common.ts b/src/history/common.ts index 8cc83b43..edade696 100644 --- a/src/history/common.ts +++ b/src/history/common.ts @@ -172,10 +172,13 @@ export function parseQuery(search: string): HistoryQuery { const hasLeadingIM = search[0] === '?' const searchParams = (hasLeadingIM ? search.slice(1) : search).split('&') for (let i = 0; i < searchParams.length; ++i) { - let [key, value] = searchParams[i].split('=') + let [key, rawValue] = searchParams[i].split('=') as [ + string, + string | undefined + ] key = decode(key) // avoid decoding null - value = value && decode(value) + let value = rawValue == null ? null : decode(rawValue) if (key in query) { // an extra variable for ts types let currentValue = query[key]