From aa9dbb49dc11ac2c42dbd317fe4590570c7c429c Mon Sep 17 00:00:00 2001 From: goranai Date: Mon, 16 Nov 2026 10:45:58 +0800 Subject: [PATCH] fix(router): keep raw query values that are not valid percent-encoding Fixes #2863 --- src/core/router/util.js | 17 ++++++++++++++++- test/unit/router-util.test.js | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/core/router/util.js b/src/core/router/util.js index 218c14f82..f5e53bd4e 100644 --- a/src/core/router/util.js +++ b/src/core/router/util.js @@ -106,6 +106,21 @@ export function getSidebarNavigationTarget(linkElm) { }; } +/** + * Decode a query value, keeping the raw text when it is not valid + * percent-encoding (e.g. a literal "%") instead of throwing a URIError. + * + * @param {string} str + * @returns {string} + */ +function safeDecode(str) { + try { + return decode(str); + } catch { + return str; + } +} + /** * @param {string} query * @return {Record} @@ -124,7 +139,7 @@ export function parseQuery(query) { query.split('&').forEach(param => { const parts = param.replace(/\+/g, ' ').split('='); - res[parts[0]] = parts[1] && decode(parts[1]); + res[parts[0]] = parts[1] && safeDecode(parts[1]); }); return res; diff --git a/test/unit/router-util.test.js b/test/unit/router-util.test.js index e724f165c..bc26a1b29 100644 --- a/test/unit/router-util.test.js +++ b/test/unit/router-util.test.js @@ -4,11 +4,32 @@ import { getClickedLink, getSidebarNavigationTarget, isCurrentContextNavigation, + parseQuery, } from '../../src/core/router/util.js'; // Suite // ----------------------------------------------------------------------------- describe('router/util', () => { + describe('parseQuery()', () => { + test('does not throw on malformed percent-encoding', () => { + expect(parseQuery('?a=100%&b=%E0%A4%A&c=b%zz')).toEqual({ + a: '100%', + b: '%E0%A4%A', + c: 'b%zz', + }); + }); + + test('still decodes valid percent-encoding', () => { + expect(parseQuery('?a=50%25&b=x%20y&c=m+n&d=&e')).toEqual({ + a: '50%', + b: 'x y', + c: 'm n', + d: '', + e: undefined, + }); + }); + }); + describe('navigation click helpers', () => { test('finds a link from a nested click target', () => { document.body.innerHTML = 'Guide';