Преглед на файлове

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.
Kevin Brown преди 10 години
родител
ревизия
610381be4a
променени са 8 файла, в които са добавени 71 реда и са изтрити 15 реда
  1. 9 3
      dist/js/select2.amd.full.js
  2. 9 3
      dist/js/select2.amd.js
  3. 9 3
      dist/js/select2.full.js
  4. 0 0
      dist/js/select2.full.min.js
  5. 9 3
      dist/js/select2.js
  6. 0 0
      dist/js/select2.min.js
  7. 9 3
      src/js/select2/data/tags.js
  8. 26 0
      tests/data/tags-tests.js

+ 9 - 3
dist/js/select2.amd.full.js

@@ -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
     };
   };
 

+ 9 - 3
dist/js/select2.amd.js

@@ -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
     };
   };
 

+ 9 - 3
dist/js/select2.full.js

@@ -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
     };
   };
 

Файловите разлики са ограничени, защото са твърде много
+ 0 - 0
dist/js/select2.full.min.js


+ 9 - 3
dist/js/select2.js

@@ -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
     };
   };
 

Файловите разлики са ограничени, защото са твърде много
+ 0 - 0
dist/js/select2.min.js


+ 9 - 3
src/js/select2/data/tags.js

@@ -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
     };
   };
 

+ 26 - 0
tests/data/tags-tests.js

@@ -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);
 

Някои файлове не бяха показани, защото твърде много файлове са промени