Skip to content

Commit 25425e7

Browse files
committed
Fixes bug affecting values that match "$\d+" (selectize#172).
1 parent ebcfa51 commit 25425e7

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/selectize.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,10 +1802,10 @@ $.extend(Selectize.prototype, {
18021802
}
18031803
if (templateName === 'optgroup') {
18041804
id = data[self.settings.optgroupValueField] || '';
1805-
html = html.replace(regex_tag, '<$1 data-group="' + escape_html(id) + '"');
1805+
html = html.replace(regex_tag, '<$1 data-group="' + escape_replace(escape_html(id)) + '"');
18061806
}
18071807
if (templateName === 'option' || templateName === 'item') {
1808-
html = html.replace(regex_tag, '<$1 data-value="' + escape_html(value || '') + '"');
1808+
html = html.replace(regex_tag, '<$1 data-value="' + escape_replace(escape_html(value || '')) + '"');
18091809
}
18101810

18111811
// update cache

src/utils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ var escape_html = function(str) {
4444
.replace(/"/g, '&quot;');
4545
};
4646

47+
/**
48+
* Escapes "$" characters in replacement strings.
49+
*
50+
* @param {string} str
51+
* @returns {string}
52+
*/
53+
var escape_replace = function(str) {
54+
return (str + '').replace(/\$/g, '$$$$');
55+
};
56+
4757
var hook = {};
4858

4959
/**

test/api.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@
195195
{value: 'a'},
196196
{value: 'b'},
197197
{value: 'c'},
198+
{value: '$1'},
198199
{value: '\''},
199200
{value: '"'},
200201
{value: '\\\''},
@@ -231,6 +232,16 @@
231232
it('should update DOM', function() {
232233
test.selectize.addItem('c');
233234
expect(test.selectize.$control.find('[data-value=c]').length).to.be.equal(1);
235+
236+
test.selectize.addItem('$1');
237+
var found = false;
238+
test.selectize.$control.children().each(function() {
239+
if (this.getAttribute('data-value') === '$1') {
240+
found = true;
241+
return false;
242+
}
243+
});
244+
expect(found).to.be.equal(true);
234245
});
235246
});
236247

0 commit comments

Comments
 (0)