Skip to content

Commit

Permalink
Fixes bug affecting values that match "$\d+" (selectize#172).
Browse files Browse the repository at this point in the history
  • Loading branch information
brianreavis committed Nov 3, 2013
1 parent ebcfa51 commit 25425e7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/selectize.js
Original file line number Diff line number Diff line change
Expand Up @@ -1802,10 +1802,10 @@ $.extend(Selectize.prototype, {
}
if (templateName === 'optgroup') {
id = data[self.settings.optgroupValueField] || '';
html = html.replace(regex_tag, '<$1 data-group="' + escape_html(id) + '"');
html = html.replace(regex_tag, '<$1 data-group="' + escape_replace(escape_html(id)) + '"');
}
if (templateName === 'option' || templateName === 'item') {
html = html.replace(regex_tag, '<$1 data-value="' + escape_html(value || '') + '"');
html = html.replace(regex_tag, '<$1 data-value="' + escape_replace(escape_html(value || '')) + '"');
}

// update cache
Expand Down
10 changes: 10 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ var escape_html = function(str) {
.replace(/"/g, '&quot;');
};

/**
* Escapes "$" characters in replacement strings.
*
* @param {string} str
* @returns {string}
*/
var escape_replace = function(str) {
return (str + '').replace(/\$/g, '$$$$');
};

var hook = {};

/**
Expand Down
11 changes: 11 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
{value: 'a'},
{value: 'b'},
{value: 'c'},
{value: '$1'},
{value: '\''},
{value: '"'},
{value: '\\\''},
Expand Down Expand Up @@ -231,6 +232,16 @@
it('should update DOM', function() {
test.selectize.addItem('c');
expect(test.selectize.$control.find('[data-value=c]').length).to.be.equal(1);

test.selectize.addItem('$1');
var found = false;
test.selectize.$control.children().each(function() {
if (this.getAttribute('data-value') === '$1') {
found = true;
return false;
}
});
expect(found).to.be.equal(true);
});
});

Expand Down

0 comments on commit 25425e7

Please sign in to comment.