From 4fce5c061404094ac4d24bccf617d9a8659a2451 Mon Sep 17 00:00:00 2001 From: skirtle <65301168+skirtles-code@users.noreply.github.com> Date: Fri, 1 Mar 2024 03:23:29 +0000 Subject: [PATCH] fix: combining 'end' and 'strict' --- .../__tests__/matcher/pathParser.spec.ts | 55 ++++++++++++++++++- .../router/src/matcher/pathParserRanker.ts | 2 +- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/packages/router/__tests__/matcher/pathParser.spec.ts b/packages/router/__tests__/matcher/pathParser.spec.ts index 6cff8d4ec..13c6d0cd5 100644 --- a/packages/router/__tests__/matcher/pathParser.spec.ts +++ b/packages/router/__tests__/matcher/pathParser.spec.ts @@ -619,8 +619,59 @@ describe('Path parser', () => { }) it('can not match the end', () => { - matchParams('/home', '/home/other', null, { end: true }) - matchParams('/home', '/home/other', {}, { end: false }) + const options = { end: false } + + matchParams('/home', '/home', {}, options) + matchParams('/home', '/home/', {}, options) + matchParams('/home', '/home/other', {}, options) + matchParams('/home', '/homepage', {}, options) + + matchParams('/home/:p', '/home', null, options) + matchParams('/home/:p', '/home/', null, options) + matchParams('/home/:p', '/home/a', { p: 'a' }, options) + matchParams('/home/:p', '/home/a/', { p: 'a' }, options) + matchParams('/home/:p', '/home/a/b', { p: 'a' }, options) + matchParams('/home/:p', '/homepage', null, options) + + matchParams('/home/', '/home', {}, options) + matchParams('/home/', '/home/', {}, options) + matchParams('/home/', '/home/other', {}, options) + matchParams('/home/', '/homepage', {}, options) + + matchParams('/home/:p/', '/home', null, options) + matchParams('/home/:p/', '/home/', null, options) + matchParams('/home/:p/', '/home/a', { p: 'a' }, options) + matchParams('/home/:p/', '/home/a/', { p: 'a' }, options) + matchParams('/home/:p/', '/home/a/b', { p: 'a' }, options) + matchParams('/home/:p/', '/homepage', null, options) + }) + + it('can not match the end when strict', () => { + const options = { end: false, strict: true } + + matchParams('/home', '/home', {}, options) + matchParams('/home', '/home/', {}, options) + matchParams('/home', '/home/other', {}, options) + matchParams('/home', '/homepage', null, options) + + matchParams('/home/:p', '/home', null, options) + matchParams('/home/:p', '/home/', null, options) + matchParams('/home/:p', '/home/a', { p: 'a' }, options) + matchParams('/home/:p', '/home/a/', { p: 'a' }, options) + matchParams('/home/:p', '/home/a/b', { p: 'a' }, options) + matchParams('/home/:p', '/homepage', null, options) + + matchParams('/home/', '/home', null, options) + matchParams('/home/', '/home/', {}, options) + matchParams('/home/', '/home/other', {}, options) + matchParams('/home/', '/homepage', null, options) + + matchParams('/home/:p/', '/home', null, options) + matchParams('/home/:p/', '/home/', null, options) + matchParams('/home/:p/', '/home/a', null, options) + matchParams('/home/:p/', '/home/a/', { p: 'a' }, options) + matchParams('/home/:p/', '/home/a/b', { p: 'a' }, options) + matchParams('/home/:p/', '/homepage', null, options) }) it('should not match optional params + static without leading slash', () => { diff --git a/packages/router/src/matcher/pathParserRanker.ts b/packages/router/src/matcher/pathParserRanker.ts index 670013794..81b077642 100644 --- a/packages/router/src/matcher/pathParserRanker.ts +++ b/packages/router/src/matcher/pathParserRanker.ts @@ -217,7 +217,7 @@ export function tokensToParser( if (options.end) pattern += '$' // allow paths like /dynamic to only match dynamic or dynamic/... but not dynamic_something_else - else if (options.strict) pattern += '(?:/|$)' + else if (options.strict && !pattern.endsWith('/')) pattern += '(?:/|$)' const re = new RegExp(pattern, options.sensitive ? '' : 'i')