Skip to content

Commit

Permalink
Merge pull request #77 from jharding/76-escape-regex-characters
Browse files Browse the repository at this point in the history
Escape regex characters when updating hint
  • Loading branch information
jharding committed Mar 3, 2013
2 parents b904ff8 + 720f94c commit ff9d74e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
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)',
'[email protected]'
]
});
</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

0 comments on commit ff9d74e

Please sign in to comment.