From 4c8b5a1418d73ed90fbf2ae3a6ef7c7d05abf6cc Mon Sep 17 00:00:00 2001 From: Riki Fridrich Date: Thu, 28 Jul 2016 20:47:49 +0200 Subject: [PATCH] fix: match patterns containing special characters in fragment refs InlineManual/player#702 --- src/fragment.js | 4 +++- src/pattern.js | 10 +++++----- test/fragment.spec.js | 26 +++++++++++++++++++++----- test/real-life-examples.spec.js | 5 +++++ 4 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/fragment.js b/src/fragment.js index a9d73aa..c0a9987 100644 --- a/src/fragment.js +++ b/src/fragment.js @@ -16,7 +16,9 @@ export default class extends UrlPart { get sanitize_replacements () { return [ - {substring: /\*/g, replacement: '.*'} + {substring: /\*/g, replacement: '.*'}, + {substring: /\?/g, replacement: '\\\?'}, + {substring: /\//g, replacement: '\\\/'} ]; } diff --git a/src/pattern.js b/src/pattern.js index ad431fe..66a8914 100644 --- a/src/pattern.js +++ b/src/pattern.js @@ -11,11 +11,11 @@ const split_re = new RegExp( '([a-z]+|\\*)*' + // (1) scheme '://' + // scheme separator '(.+@)*' + // (2) username and/or password - '([\\w\\*\\.\\-]+)*' + // (3) host - '(\\:\\d+)*' + // (4) port number - '(/([^\\?\\#]*))*' + // (5) path, (6) excluding slash - '(\\?([^\\#]*))*' + // (7) params, (8) excluding question mark - '(\\#(.*))*' // (9) fragment, (10) excluding hash + '([\\w\\*\\.\\-]+)*' + // (3) host + '(\\:\\d+)*' + // (4) port number + '(/([^\\?\\#]*))*' + // (5) path, (6) excluding slash + '(\\?([^\\#]*))*' + // (7) params, (8) excluding question mark + '(\\#(.*))*' // (9) fragment, (10) excluding hash ); diff --git a/test/fragment.spec.js b/test/fragment.spec.js index e29123a..c17fd16 100644 --- a/test/fragment.spec.js +++ b/test/fragment.spec.js @@ -19,7 +19,7 @@ describe('Fragment', function() { expect(fragment.validate('#')).toBe(false); }); - it('should validate on combination of characters and asterixes', function() { + it('should validate on combination of characters and asterisks', function() { expect(fragment.validate('aaa*bbb*ccc')).toBe(true); }); @@ -41,6 +41,10 @@ describe('Fragment', function() { expect(fragment.sanitize('aaa*bbb*ccc')).toEqual(/^aaa.*bbb.*ccc$/); }); + it('should escape question mark', function() { + expect(fragment.sanitize('aaa*bbb*ccc')).toEqual(/^aaa.*bbb.*ccc$/); + }); + }); describe('test', function() { @@ -61,31 +65,43 @@ describe('Fragment', function() { expect(fragment.test('aaa', pattern)).toBe(true); }); - it('should match fragment with single asterix', function() { + it('should match fragment with single asterisk', function() { const pattern = fragment.sanitize('aaa*'); expect(fragment.test('aaa', pattern)).toBe(true); expect(fragment.test('aaabbb', pattern)).toBe(true); }); - it('should not match invalid fragment with single asterix', function() { + it('should not match invalid fragment with single asterisk', function() { const pattern = fragment.sanitize('aaa*'); expect(fragment.test('bbb', pattern)).toBe(false); expect(fragment.test('bbbaaa', pattern)).toBe(false); }); - it('should match fragment with multiple asterixes', function() { + it('should match fragment with multiple asterisks', function() { const pattern = fragment.sanitize('aaa*bbb*'); expect(fragment.test('aaabbb', pattern)).toBe(true); expect(fragment.test('aaaxxxbbbxxx', pattern)).toBe(true); }); - it('should not match invalid fragment with multiple asterixes', function() { + it('should not match invalid fragment with multiple asterisks', function() { const pattern = fragment.sanitize('aaa*bbb*'); expect(fragment.test('xxx', pattern)).toBe(false); expect(fragment.test('xxxaaa', pattern)).toBe(false); expect(fragment.test('xxxaaabbb', pattern)).toBe(false); }); + it('should match fragment with question mark', function () { + const pattern = fragment.sanitize('aaa?bbb'); + console.log('x', pattern); + expect(fragment.test('aaa?bbb', pattern)).toBe(true); + }); + + it('should match fragment with slash', function () { + const pattern = fragment.sanitize('aaa/bbb'); + console.log('x', pattern); + expect(fragment.test('aaa/bbb', pattern)).toBe(true); + }); + }); }); diff --git a/test/real-life-examples.spec.js b/test/real-life-examples.spec.js index 6cc2704..7e9c7d8 100644 --- a/test/real-life-examples.spec.js +++ b/test/real-life-examples.spec.js @@ -90,4 +90,9 @@ describe('Real life examples', function() { expect(my_match.test('http://localhost:3000/aaa/bbb')).toBe(true); }); + it('should match path commonly used by SPAs', function () { + const my_match = new UrlMatch('*://*/#/aaa?bbb=ccc'); + expect(my_match.test('http://www.aaa.com/#/aaa?bbb=ccc')).toBe(true); + }); + });