From 3c138f897f33357b6deb569098ecabafdef35b05 Mon Sep 17 00:00:00 2001 From: Mate Hegedus Date: Tue, 21 Nov 2017 11:33:51 +0000 Subject: [PATCH] bugfix to basename --- modules/PathUtils.js | 2 +- modules/__tests__/HashHistory-test.js | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/modules/PathUtils.js b/modules/PathUtils.js index c2b9a8a93..2a55e9a80 100644 --- a/modules/PathUtils.js +++ b/modules/PathUtils.js @@ -5,7 +5,7 @@ export const stripLeadingSlash = path => path.charAt(0) === "/" ? path.substr(1) : path export const hasBasename = (path, prefix) => - new RegExp("^" + prefix + "(\\/|\\?|#|$)", "i").test(path) + path.toLowerCase().lastIndexOf(prefix.toLowerCase(), 0) === 0 && "/?#".indexOf(path.charAt(prefix.length)) !== -1 export const stripBasename = (path, prefix) => hasBasename(path, prefix) ? path.substr(prefix.length) : path diff --git a/modules/__tests__/HashHistory-test.js b/modules/__tests__/HashHistory-test.js index 70f30ee01..bfd91dc80 100644 --- a/modules/__tests__/HashHistory-test.js +++ b/modules/__tests__/HashHistory-test.js @@ -268,6 +268,12 @@ describeHistory("a hash history", () => { window.location.hash = "#/prefix#rest" const history = createHistory({ basename: "/prefix" }) expect(history.location.pathname).toEqual("/") + }) + + it('allows URL with regex special characters', () => { + window.location.hash = '/prefix$special/hello' + const history = createHistory({ basename: '/prefix$special'}) + expect(history.location.pathname).toEqual('/hello') }) }) })