]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat: escape : and )
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 11 Dec 2019 14:21:49 +0000 (15:21 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 18 Dec 2019 09:26:15 +0000 (10:26 +0100)
__tests__/matcher/path-parser.spec.ts
src/matcher/tokenizer.ts

index db412e3aa697e4eb307f220e5caab2f324610326..a2fb7727958b2f53f7dd0d221962d277fdf4e77c 100644 (file)
@@ -10,6 +10,26 @@ describe('Path parser', () => {
       expect(tokenizePath('/')).toEqual([[]])
     })
 
+    it('escapes :', () => {
+      expect(tokenizePath('/\\:')).toEqual([
+        [{ type: TokenType.Static, value: ':' }],
+      ])
+    })
+
+    it('escapes ( inside custom re', () => {
+      expect(tokenizePath('/:a(\\))')).toEqual([
+        [
+          {
+            type: TokenType.Param,
+            value: 'a',
+            regexp: ')',
+            optional: false,
+            repeatable: false,
+          },
+        ],
+      ])
+    })
+
     it('static single', () => {
       expect(tokenizePath('/home')).toEqual([
         [{ type: TokenType.Static, value: 'home' }],
@@ -493,6 +513,10 @@ describe('Path parser', () => {
       matchStringify('/:a+', { a: ['one', 'two'] }, '/one/two')
     })
 
+    it('repeatable params+ with extra segment', () => {
+      matchStringify('/:a+/other', { a: ['one', 'two'] }, '/one/two/other')
+    })
+
     it('repeatable params*', () => {
       matchStringify('/:a*', { a: ['one', 'two'] }, '/one/two')
     })
index 5c4ff57948dc6e0120ae4c8e299a46cd758e07f2..72f3cdaf9aa601e2f2e133649e487ed68da85181 100644 (file)
@@ -144,7 +144,10 @@ export function tokenizePath(path: string): Array<Token[]> {
 
       case TokenizerState.ParamRegExp:
         if (char === ')') {
-          state = TokenizerState.ParamRegExpEnd
+          // handle the escaped )
+          if (customRe[customRe.length - 1] == '\\')
+            customRe = customRe.slice(0, -1) + char
+          else state = TokenizerState.ParamRegExpEnd
         } else {
           customRe += char
         }
@@ -191,6 +194,10 @@ interface PathParser {
 
 const BASE_PARAM_PATTERN = '[^/]+?'
 
+/**
+ * TODO: add options strict, sensitive, encode, decode
+ */
+
 export function tokensToParser(segments: Array<Token[]>): PathParser {
   let score = 0
   let pattern = '^'