Skip to content

Commit

Permalink
fix: match patterns containing special characters in fragment
Browse files Browse the repository at this point in the history
refs InlineManual/player#702
  • Loading branch information
fczbkk committed Jul 28, 2016
1 parent ab8b569 commit 4c8b5a1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/fragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '\\\/'}
];
}

Expand Down
10 changes: 5 additions & 5 deletions src/pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);


Expand Down
26 changes: 21 additions & 5 deletions test/fragment.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand All @@ -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() {
Expand All @@ -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);
});

});

});
5 changes: 5 additions & 0 deletions test/real-life-examples.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

});

0 comments on commit 4c8b5a1

Please sign in to comment.