Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape regex characters when updating hint #77

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/js/typeahead_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ var TypeaheadView = (function() {
hint = dataForFirstSuggestion ? dataForFirstSuggestion.value : null,
inputValue,
query,
escapedQuery,
beginsWithQuery,
match;

Expand All @@ -103,8 +104,9 @@ var TypeaheadView = (function() {
query = inputValue
.replace(/\s{2,}/g, ' ') // condense whitespace
.replace(/^\s+/g, ''); // strip leading whitespace
escapedQuery = utils.escapeRegExChars(query);

beginsWithQuery = new RegExp('^(?:' + query + ')(.*$)', 'i');
beginsWithQuery = new RegExp('^(?:' + escapedQuery + ')(.*$)', 'i');
match = beginsWithQuery.exec(hint);

this.inputView.setHintValue(inputValue + (match ? match[1] : ''));
Expand Down
5 changes: 5 additions & 0 deletions src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ var utils = {

isBlankString: function(str) { return !str || /^\s*$/.test(str); },

// http://stackoverflow.com/a/6969486
escapeRegExChars: function(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
},

isString: function(obj) { return typeof obj === 'string'; },

isNumber: function(obj) { return typeof obj === 'number'; },
Expand Down
14 changes: 14 additions & 0 deletions test/playground.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
<br>
<br>
<input class="bad-tokens" type="text" placeholder="bad tokens">
<br>
<br>
<input class="regex-symbols" type="text" placeholder="regex symbols">
</div>

<script>
Expand Down Expand Up @@ -110,6 +113,17 @@
}
]
});

$('.regex-symbols').typeahead({
local: [
'*.js',
'[Tt]ypeahead.js',
'^typeahead.js$',
'typeahead.js(0.8.2)',
'typeahead.js(@\\d.\\d.\\d)',
'typeahead.js@0.8.2'
]
});
</script>
</body>
</html>
16 changes: 15 additions & 1 deletion test/typeahead_view_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,11 @@ describe('TypeaheadView', function() {
});

describe('if top suggestion\'s value begins with query', function() {
it('should show hint', function() {
beforeEach(function() {
this.dropdownView.isOpen.andReturn(true);
});

it('should show hint', function() {
this.inputView.getInputValue.andReturn('san ');
this.dropdownView.getFirstSuggestion
.andReturn({ value: 'san francisco' });
Expand All @@ -534,6 +537,17 @@ describe('TypeaheadView', function() {
expect(this.inputView.setHintValue)
.toHaveBeenCalledWith('san francisco');
});

it('should escape regex characters', function() {
this.inputView.getInputValue.andReturn('*.js(v');
this.dropdownView.getFirstSuggestion
.andReturn({ value: '*.js(v\\d.\\d.\\d)' });

this[view].trigger(eventType);

expect(this.inputView.setHintValue)
.toHaveBeenCalledWith('*.js(v\\d.\\d.\\d)');
});
});

describe('if top suggestion\'s value does not begin with query',
Expand Down