]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix: combining 'end' and 'strict' (#2154)
authorskirtle <65301168+skirtles-code@users.noreply.github.com>
Tue, 29 Oct 2024 16:23:28 +0000 (16:23 +0000)
committerGitHub <noreply@github.com>
Tue, 29 Oct 2024 16:23:28 +0000 (17:23 +0100)
packages/router/__tests__/matcher/pathParser.spec.ts
packages/router/src/matcher/pathParserRanker.ts

index 5ed7ed40667e779aea8c8c1f7040de01aeaa88b4..b7b9969b1b5570bc4a7156d3a147b14074240484 100644 (file)
@@ -619,9 +619,77 @@ describe('Path parser', () => {
       matchParams('/home', '/other/home', {}, { start: false })
     })
 
+    it('defaults to matching the end', () => {
+      // The default should behave like `end: true`
+      const optionSets = [{}, { end: true }]
+
+      for (const options of optionSets) {
+        matchParams('/home', '/home', {}, options)
+        matchParams('/home', '/home/', {}, options)
+        matchParams('/home', '/home/other', null, options)
+        matchParams('/home', '/homepage', null, options)
+
+        matchParams('/home/', '/home', {}, options)
+        matchParams('/home/', '/home/', {}, options)
+        matchParams('/home/', '/home/other', null, options)
+        matchParams('/home/', '/homepage', null, options)
+      }
+    })
+
     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', () => {
index 670013794bbf1b1645a4e7abfac265cb37b7a250..81b0776427444c3e15f265fe29ca630666af746d 100644 (file)
@@ -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')