From c597d05d2c260b97a06f924379d076d6fa951dc0 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Wed, 18 Jan 2017 00:17:12 +0100 Subject: [PATCH] Fix active route matching Fix #1091 Fix the doc too --- docs/en/api/router-link.md | 2 +- src/util/route.js | 7 +++++-- test/unit/specs/route.spec.js | 11 +++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/docs/en/api/router-link.md b/docs/en/api/router-link.md index f25227be5..1d92d0e4b 100644 --- a/docs/en/api/router-link.md +++ b/docs/en/api/router-link.md @@ -94,7 +94,7 @@ - default: `false` - The default active class matching behavior is **inclusive match**. For example, `` will get this class applied as long as the current path starts with `/a`. + The default active class matching behavior is **inclusive match**. For example, `` will get this class applied as long as the current path starts with `/a/` or is `/a`. One consequence of this is that `` will be active for every route! To force the link into "exact match mode", use the `exact` prop: diff --git a/src/util/route.js b/src/util/route.js index a60bd08bc..53f46842b 100644 --- a/src/util/route.js +++ b/src/util/route.js @@ -2,6 +2,8 @@ import { stringifyQuery } from './query' +const trailingSlashRE = /\/?$/ + export function createRoute ( record: ?RouteRecord, location: Location, @@ -41,7 +43,6 @@ function getFullPath ({ path, query = {}, hash = '' }) { return (path || '/') + stringifyQuery(query) + hash } -const trailingSlashRE = /\/$/ export function isSameRoute (a: Route, b: ?Route): boolean { if (b === START) { return a === b @@ -76,7 +77,9 @@ function isObjectEqual (a = {}, b = {}): boolean { export function isIncludedRoute (current: Route, target: Route): boolean { return ( - current.path.indexOf(target.path.replace(/\/$/, '')) === 0 && + current.path.replace(trailingSlashRE, '/').indexOf( + target.path.replace(trailingSlashRE, '/') + ) === 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 77a978534..c15d44c33 100644 --- a/test/unit/specs/route.spec.js +++ b/test/unit/specs/route.spec.js @@ -74,5 +74,16 @@ describe('Route utils', () => { expect(isIncludedRoute(a, d)).toBe(false) expect(isIncludedRoute(a, e)).toBe(false) }) + + it('trailing slash', () => { + const a = { path: 'user/1' } + const b = { path: 'user/10' } + const c = { path: 'user/10/' } + const d = { path: 'user/1/' } + expect(isIncludedRoute(a, b)).toBe(false) + expect(isIncludedRoute(a, c)).toBe(false) + expect(isIncludedRoute(b, d)).toBe(false) + expect(isIncludedRoute(c, d)).toBe(false) + }) }) })