Skip to content

Commit 8944d6b

Browse files
committed
Fixed creation of RegExp with special characters.
Changers done include the following: - Before creating a new regular expression, escaping the text which might contain special characters. - The property to be matched and picked up can now be an array as well. - Optimized the code while marking all items. Fetching only the items that matter.
1 parent 4bdb805 commit 8944d6b

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

src/paginated.dropdown.widget.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
valueProperty: null,
2121
itemsPerPage: 10,
2222
dropdownWidth: null,
23-
elementType: 'text',
23+
elementType: "text",
2424
persistState: false,
2525
closeBtnRequired: true,
2626
footerRequired: true,
@@ -111,7 +111,7 @@
111111
var self = this,
112112
el = self.element;
113113

114-
if(self.options.elementType && self.options.elementType == 'text') {
114+
if(self.options.elementType && self.options.elementType == "text") {
115115
el.keydown(function( event ) {
116116
self._keyNavigations( event );
117117
});
@@ -138,7 +138,7 @@
138138
self._fetchData( $.trim(el.val()) );
139139
});
140140
}
141-
else if(self.options.elementType && self.options.elementType == 'link') {
141+
else if(self.options.elementType && self.options.elementType == "link") {
142142
el.click(function( event ) {
143143
self._fetchData();
144144
});
@@ -169,7 +169,7 @@
169169
var el = this.element;
170170
if(this.options.valueProperty) {
171171
if($.type(o) == "object") {
172-
el.val( o[this.options.valueProperty] ).focus();
172+
el.val( $.type(this.options.valueProperty) == "array" ? o[this.options.valueProperty[0]] : o[this.options.valueProperty] ).focus();
173173
}
174174
else {
175175
el.val( o ).focus();
@@ -290,6 +290,7 @@
290290
this._bindDropdownComponentEvents();
291291
this._highlightItem(this._getWidgetState().highlightedIndex);
292292
this._markAllItems();
293+
this.element.parent().find( "." + this.wp.widgetBaseClass + this.wp.hyphen + this.wp.containerLabel ).show();
293294
},
294295
_constructHtmlUsingTemplate: function() {
295296
var self = this,
@@ -298,7 +299,7 @@
298299
widgetState = self._getWidgetState(),
299300
matchedText = null,
300301
underlinedText = null,
301-
highlightRegExp = new RegExp(widgetState.query, "i"),
302+
highlightRegExp = new RegExp($.ui.autocomplete.escapeRegex(widgetState.query), "i"),
302303
highlightQueryText = function(text) {
303304
underlinedText = "";
304305
matchedText = text.match(highlightRegExp);
@@ -323,16 +324,21 @@
323324
if($.type(item) == "object") {
324325
$.each(item, function(k, v) {
325326
var templateKey = "{@:" + k + "}",
326-
regEx = new RegExp(templateKey, "g");
327+
regEx = new RegExp($.ui.autocomplete.escapeRegex(templateKey), "g");
327328

328329
if(self.options.itemTemplate.indexOf( templateKey ) != -1) {
329-
templateClone = templateClone.replace( regEx, (k == self.options.valueProperty) ? highlightQueryText(v) : v );
330+
if($.type(self.options.valueProperty) == "array") {
331+
templateClone = templateClone.replace( regEx, (self.options.valueProperty.indexOf(k) != -1) ? highlightQueryText(v) : v );
332+
}
333+
else {
334+
templateClone = templateClone.replace( regEx, (k == self.options.valueProperty) ? highlightQueryText(v) : v );
335+
}
330336
}
331337
});
332338
}
333339
else {
334340
var templateKey = "{@:" + self.options.valueProperty + "}",
335-
regEx = new RegExp(templateKey, "g");
341+
regEx = new RegExp($.ui.autocomplete.escapeRegex(templateKey), "g");
336342

337343
if(self.options.itemTemplate.indexOf( templateKey ) != -1) {
338344
templateClone = templateClone.replace( regEx, (k == self.options.valueProperty) ? highlightQueryText(item) : item );
@@ -427,6 +433,8 @@
427433
parent = self.element.parent();
428434
// bind footer events only if footer is required.
429435
if(self.options.footerRequired) {
436+
// TODO: If customisable footer is provided, then we should be able to specify elements to attach events too,
437+
// rather than creating a generic CSS definition to attach the events to.
430438
parent.find( "." + self.wp.widgetBaseClass + self.wp.hyphen + self.wp.footerLabel ).bind("click dblclick", function( e ) {
431439
self.element.focus();
432440
});
@@ -466,11 +474,9 @@
466474
},
467475
_markAllItems: function() {
468476
var self = this,
469-
items = this.element.siblings( "." + this.wp.widgetBaseClass + this.wp.hyphen + this.wp.containerLabel ).children();
477+
items = this.element.siblings( "." + this.wp.widgetBaseClass + this.wp.hyphen + this.wp.containerLabel ).children().not( "." + this.wp.widgetBaseClass + this.wp.hyphen + this.wp.footerLabel );
470478
items.each(function(index, item) {
471-
if(!$( item ).hasClass( self.wp.widgetBaseClass + self.wp.hyphen + self.wp.footerLabel )) {
472-
$( item ).addClass( self.wp.widgetBaseClass + self.wp.hyphen + self.wp.itemLabel );
473-
}
479+
$( item ).addClass( self.wp.widgetBaseClass + self.wp.hyphen + self.wp.itemLabel );
474480
});
475481
},
476482
_removeItemHighlight: function(selectedItem) {

0 commit comments

Comments
 (0)