Skip to content

Commit 5a6eaca

Browse files
author
Tomas Kirda
committed
Track hintValue and signal hint only when hintValue changes.
1 parent adfbf7a commit 5a6eaca

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/jquery.autocomplete.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,14 @@
2626
var
2727
utils = (function () {
2828
return {
29-
30-
extend: function (target, source) {
31-
return $.extend(target, source);
29+
escapeRegExChars: function (value) {
30+
return value.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
3231
},
33-
3432
createNode: function (html) {
3533
var div = document.createElement('div');
3634
div.innerHTML = html;
3735
return div.firstChild;
3836
}
39-
4037
};
4138
}()),
4239

@@ -102,6 +99,7 @@
10299
suggestion: 'autocomplete-suggestion'
103100
};
104101
that.hint = null;
102+
that.hintValue = '';
105103

106104
// Initialize and set options:
107105
that.initialize();
@@ -113,8 +111,7 @@
113111
$.Autocomplete = Autocomplete;
114112

115113
Autocomplete.formatResult = function (suggestion, currentValue) {
116-
var reEscape = new RegExp('(\\' + ['/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\'].join('|\\') + ')', 'g'),
117-
pattern = '(' + currentValue.replace(reEscape, '\\$1') + ')';
114+
var pattern = '(' + utils.escapeRegExChars(currentValue) + ')';
118115

119116
return suggestion.value.replace(new RegExp(pattern, 'gi'), '<strong>$1<\/strong>');
120117
};
@@ -191,7 +188,7 @@
191188
var that = this,
192189
options = that.options;
193190

194-
utils.extend(options, suppliedOptions);
191+
$.extend(options, suppliedOptions);
195192

196193
that.isLocal = $.isArray(options.lookup);
197194

@@ -521,7 +518,8 @@
521518
if (suggestion) {
522519
hintValue = that.currentValue + suggestion.value.substr(that.currentValue.length);
523520
}
524-
if (that.hint !== suggestion) {
521+
if (that.hintValue !== hintValue) {
522+
that.hintValue = hintValue;
525523
that.hint = suggestion;
526524
(this.options.onHint || $.noop)(hintValue);
527525
}

0 commit comments

Comments
 (0)