From 74ce6911c94245174dc98d9a3675865c73e2ef76 Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Thu, 17 Oct 2024 10:50:47 -0400 Subject: [PATCH] Only decode URI for matching when necessary --- src/js/Route.js | 4 +++- tests/js/route.test.js | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/js/Route.js b/src/js/Route.js index eb8084c0..d0a9e8de 100644 --- a/src/js/Route.js +++ b/src/js/Route.js @@ -91,7 +91,9 @@ export default class Route { const [location, query] = url.replace(/^\w+:\/\//, '').split('?'); - const matches = new RegExp(`^${pattern}/?$`).exec(decodeURI(location)); + const matches = + new RegExp(`^${pattern}/?$`).exec(location) ?? + new RegExp(`^${pattern}/?$`).exec(decodeURI(location)); if (matches) { for (const k in matches.groups) { diff --git a/tests/js/route.test.js b/tests/js/route.test.js index e530280d..cd2b28fd 100644 --- a/tests/js/route.test.js +++ b/tests/js/route.test.js @@ -1412,6 +1412,13 @@ describe('current()', () => { expect(route().current('statistics')).toBe(true); }); + test('can check the current route with encoded percent sign', () => { + global.window.location.pathname = '/optionalpage/john%25'; + + expect(route().current()).toBe('pages.optional'); + expect(route().current('pages.optional')).toBe(true); + }); + test('can ignore routes that don’t allow GET requests', () => { global.window.location.pathname = '/posts/1';