From bceff5647e1a56d555611bbe0e2a2ed703be2096 Mon Sep 17 00:00:00 2001 From: kyle Date: Thu, 28 Oct 2021 20:44:47 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20treat=20isActive=20to=20c?= =?UTF-8?q?ase-insensitive?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ✅ Closes: #3656 --- src/components/link.js | 3 ++- src/util/route.js | 14 +++++++++----- test/unit/specs/route.spec.js | 8 ++++++++ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/components/link.js b/src/components/link.js index 9c2a25784..3eaa5a33e 100644 --- a/src/components/link.js +++ b/src/components/link.js @@ -72,10 +72,11 @@ export default { ? createRoute(null, normalizeLocation(route.redirectedFrom), null, router) : route + const ignoreCase = current.matched[0].regex.ignoreCase classes[exactActiveClass] = isSameRoute(current, compareTarget, this.exactPath) classes[activeClass] = this.exact || this.exactPath ? classes[exactActiveClass] - : isIncludedRoute(current, compareTarget) + : isIncludedRoute(current, compareTarget, ignoreCase) const ariaCurrentValue = classes[exactActiveClass] ? this.ariaCurrentValue : null diff --git a/src/util/route.js b/src/util/route.js index 9d8e31cd1..730b49db3 100644 --- a/src/util/route.js +++ b/src/util/route.js @@ -116,11 +116,15 @@ function isObjectEqual (a = {}, b = {}): boolean { }) } -export function isIncludedRoute (current: Route, target: Route): boolean { - return ( - current.path.replace(trailingSlashRE, '/').indexOf( - target.path.replace(trailingSlashRE, '/') - ) === 0 && +export function isIncludedRoute (current: Route, target: Route, ignoreCase: boolean = false): boolean { + let currentPath = current.path.replace(trailingSlashRE, '/') + let targetPath = target.path.replace(trailingSlashRE, '/') + if (ignoreCase) { + currentPath = currentPath.toLowerCase() + targetPath = targetPath.toLowerCase() + } + const index = currentPath.indexOf(targetPath) + return (index === 0 && (!target.hash || current.hash === target.hash) && queryIncludes(current.query, target.query) ) diff --git a/test/unit/specs/route.spec.js b/test/unit/specs/route.spec.js index c9483ab7d..6b78e4106 100644 --- a/test/unit/specs/route.spec.js +++ b/test/unit/specs/route.spec.js @@ -149,5 +149,13 @@ describe('Route utils', () => { expect(isIncludedRoute(d, e)).toBe(true) expect(isIncludedRoute(d, f)).toBe(false) }) + + it('ignore case', () => { + const a = { path: '/about' } + const b = { path: '/ABOUT' } + expect(isIncludedRoute(a, b)).toBe(false) + expect(isIncludedRoute(a, b, false)).toBe(false) + expect(isIncludedRoute(a, b, true)).toBe(true) + }) }) })