From: Eduardo San Martin Morote Date: Wed, 11 Dec 2019 14:21:49 +0000 (+0100) Subject: feat: escape : and ) X-Git-Tag: v4.0.0-alpha.0~153 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8dc52b9644327616a9f199caf92ab24dde0cb8fe;p=thirdparty%2Fvuejs%2Frouter.git feat: escape : and ) --- diff --git a/__tests__/matcher/path-parser.spec.ts b/__tests__/matcher/path-parser.spec.ts index db412e3a..a2fb7727 100644 --- a/__tests__/matcher/path-parser.spec.ts +++ b/__tests__/matcher/path-parser.spec.ts @@ -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') }) diff --git a/src/matcher/tokenizer.ts b/src/matcher/tokenizer.ts index 5c4ff579..72f3cdaf 100644 --- a/src/matcher/tokenizer.ts +++ b/src/matcher/tokenizer.ts @@ -144,7 +144,10 @@ export function tokenizePath(path: string): Array { 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): PathParser { let score = 0 let pattern = '^'