1
0
mirror of synced 2025-02-03 21:59:24 +03:00

Strip whitespace by default for tags

This strips whitespace in tags by default, so multiple tags cannot
be created with only whitespace as the difference in the id.

A test has been added to ensure that this remains fixed in the future.

This closes https://github.com/select2/select2/issues/3076.
This commit is contained in:
Kevin Brown 2015-03-11 18:23:10 -04:00
parent 95be140760
commit 610381be4a
8 changed files with 73 additions and 17 deletions

View File

@ -2845,7 +2845,7 @@ define('select2/data/tags',[
this._removeOldTags();
if (params.term == null || params.term === '' || params.page != null) {
if (params.term == null || params.page != null) {
decorated.call(this, params, callback);
return;
}
@ -2901,9 +2901,15 @@ define('select2/data/tags',[
};
Tags.prototype.createTag = function (decorated, params) {
var term = $.trim(params.term);
if (term === '') {
return null;
}
return {
id: params.term,
text: params.term
id: term,
text: term
};
};

View File

@ -2845,7 +2845,7 @@ define('select2/data/tags',[
this._removeOldTags();
if (params.term == null || params.term === '' || params.page != null) {
if (params.term == null || params.page != null) {
decorated.call(this, params, callback);
return;
}
@ -2901,9 +2901,15 @@ define('select2/data/tags',[
};
Tags.prototype.createTag = function (decorated, params) {
var term = $.trim(params.term);
if (term === '') {
return null;
}
return {
id: params.term,
text: params.term
id: term,
text: term
};
};

View File

@ -3284,7 +3284,7 @@ define('select2/data/tags',[
this._removeOldTags();
if (params.term == null || params.term === '' || params.page != null) {
if (params.term == null || params.page != null) {
decorated.call(this, params, callback);
return;
}
@ -3340,9 +3340,15 @@ define('select2/data/tags',[
};
Tags.prototype.createTag = function (decorated, params) {
var term = $.trim(params.term);
if (term === '') {
return null;
}
return {
id: params.term,
text: params.term
id: term,
text: term
};
};

File diff suppressed because one or more lines are too long

12
dist/js/select2.js vendored
View File

@ -3284,7 +3284,7 @@ define('select2/data/tags',[
this._removeOldTags();
if (params.term == null || params.term === '' || params.page != null) {
if (params.term == null || params.page != null) {
decorated.call(this, params, callback);
return;
}
@ -3340,9 +3340,15 @@ define('select2/data/tags',[
};
Tags.prototype.createTag = function (decorated, params) {
var term = $.trim(params.term);
if (term === '') {
return null;
}
return {
id: params.term,
text: params.term
id: term,
text: term
};
};

File diff suppressed because one or more lines are too long

View File

@ -29,7 +29,7 @@ define([
this._removeOldTags();
if (params.term == null || params.term === '' || params.page != null) {
if (params.term == null || params.page != null) {
decorated.call(this, params, callback);
return;
}
@ -85,9 +85,15 @@ define([
};
Tags.prototype.createTag = function (decorated, params) {
var term = $.trim(params.term);
if (term === '') {
return null;
}
return {
id: params.term,
text: params.term
id: term,
text: term
};
};

View File

@ -38,6 +38,32 @@ test('does not trigger on blank or null terms', function (assert) {
});
});
test('white space is trimmed by default', function (assert) {
var data = new SelectTags($('#qunit-fixture .single'), options);
data.query({
term: ' '
}, function (data) {
assert.equal(data.results.length, 1);
var item = data.results[0];
assert.equal(item.id, 'One');
assert.equal(item.text, 'One');
});
data.query({
term: ' One '
}, function (data) {
assert.equal(data.results.length, 1);
var item = data.results[0];
assert.equal(item.id, 'One');
assert.equal(item.text, 'One');
});
});
test('does not trigger for additional pages', function (assert) {
var data = new SelectTags($('#qunit-fixture .single'), options);