Skip to content

Commit ea26634

Browse files
committed
Merge pull request select2#2665 from bor0/master
Items with spaces can be added infinite times on multiselects
2 parents 073f4cb + 8644ccb commit ea26634

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

select2.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,16 @@ the specific language governing permissions and limitations under the Apache Lic
160160
}
161161

162162
/**
163-
* Splits the string into an array of values, trimming each value. An empty array is returned for nulls or empty
163+
* Splits the string into an array of values, transforming each value. An empty array is returned for nulls or empty
164164
* strings
165165
* @param string
166166
* @param separator
167167
*/
168-
function splitVal(string, separator) {
168+
function splitVal(string, separator, transform) {
169169
var val, i, l;
170170
if (string === null || string.length < 1) return [];
171171
val = string.split(separator);
172-
for (i = 0, l = val.length; i < l; i = i + 1) val[i] = $.trim(val[i]);
172+
for (i = 0, l = val.length; i < l; i = i + 1) val[i] = transform(val[i]);
173173
return val;
174174
}
175175

@@ -1063,7 +1063,7 @@ the specific language governing permissions and limitations under the Apache Lic
10631063
if (opts.initSelection === undefined) {
10641064
opts.initSelection = function (element, callback) {
10651065
var data = [];
1066-
$(splitVal(element.val(), opts.separator)).each(function () {
1066+
$(splitVal(element.val(), opts.separator, opts.transformVal)).each(function () {
10671067
var obj = { id: this, text: this },
10681068
tags = opts.tags;
10691069
if ($.isFunction(tags)) tags=tags();
@@ -2595,7 +2595,6 @@ the specific language governing permissions and limitations under the Apache Lic
25952595
self=this;
25962596

25972597
// TODO validate placeholder is a string if specified
2598-
25992598
if (opts.element.get(0).tagName.toLowerCase() === "select") {
26002599
// install the selection initializer
26012600
opts.initSelection = function (element, callback) {
@@ -2610,7 +2609,7 @@ the specific language governing permissions and limitations under the Apache Lic
26102609
} else if ("data" in opts) {
26112610
// install default initSelection when applied to hidden input and data is local
26122611
opts.initSelection = opts.initSelection || function (element, callback) {
2613-
var ids = splitVal(element.val(), opts.separator);
2612+
var ids = splitVal(element.val(), opts.separator, opts.transformVal);
26142613
//search in data by array of ids, storing matching items in a list
26152614
var matches = [];
26162615
opts.query({
@@ -3213,7 +3212,7 @@ the specific language governing permissions and limitations under the Apache Lic
32133212
return val === null ? [] : val;
32143213
} else {
32153214
val = this.opts.element.val();
3216-
return splitVal(val, this.opts.separator);
3215+
return splitVal(val, this.opts.separator, this.opts.transformVal);
32173216
}
32183217
},
32193218

@@ -3429,6 +3428,9 @@ the specific language governing permissions and limitations under the Apache Lic
34293428
markMatch(this.text(result), query.term, markup, escapeMarkup);
34303429
return markup.join("");
34313430
},
3431+
transformVal: function(val) {
3432+
return $.trim(val);
3433+
},
34323434
formatSelection: function (data, container, escapeMarkup) {
34333435
return data ? escapeMarkup(this.text(data)) : undefined;
34343436
},

0 commit comments

Comments
 (0)