Skip to content

Commit

Permalink
Select2 now clears the internal ID when it is destroyed (#5587)
Browse files Browse the repository at this point in the history
This fixes a bug where if you cloned a Select2, the internal ID used
for mapping elements (specifically the `<select>`) to the in-memory
data store would be cloned as well, causing issues when you tried to
initialize Select2 on the cloned element. This was because we did not
properly clear all of the internal data and all of the internal
attributes that Select2 uses when we destroyed it. The internal
`data-select2-id` attribute was not being cleared, and this was the
attribute being used for the internal mapping.

Now we properly clear the `data-select2-id` attribute from the element
when we call `RemoveData` on the element. This aligns with what we
were trying to do, since we previously cleared out the internal store
for that ID, and fixes the issue we were seeing when cloning.

Fixes #5247
  • Loading branch information
kevin-brown committed Jul 28, 2019
1 parent 1f3eceb commit 6645ffd
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/js/select2/utils.js
Expand Up @@ -332,6 +332,8 @@ define([
if (Utils.__cache[id] != null) {
delete Utils.__cache[id];
}

element.removeAttribute('data-select2-id');
};

return Utils;
Expand Down
1 change: 1 addition & 0 deletions tests/unit-jq1.html
Expand Up @@ -94,6 +94,7 @@
<script src="selection/single-tests.js" type="text/javascript"></script>
<script src="selection/stopPropagation-tests.js" type="text/javascript"></script>

<script src="utils/data-tests.js" type="text/javascript"></script>
<script src="utils/decorator-tests.js" type="text/javascript"></script>
<script src="utils/escapeMarkup-tests.js" type="text/javascript"></script>
</body>
Expand Down
1 change: 1 addition & 0 deletions tests/unit-jq2.html
Expand Up @@ -94,6 +94,7 @@
<script src="selection/single-tests.js" type="text/javascript"></script>
<script src="selection/stopPropagation-tests.js" type="text/javascript"></script>

<script src="utils/data-tests.js" type="text/javascript"></script>
<script src="utils/decorator-tests.js" type="text/javascript"></script>
<script src="utils/escapeMarkup-tests.js" type="text/javascript"></script>
</body>
Expand Down
1 change: 1 addition & 0 deletions tests/unit-jq3.html
Expand Up @@ -94,6 +94,7 @@
<script src="selection/single-tests.js" type="text/javascript"></script>
<script src="selection/stopPropagation-tests.js" type="text/javascript"></script>

<script src="utils/data-tests.js" type="text/javascript"></script>
<script src="utils/decorator-tests.js" type="text/javascript"></script>
<script src="utils/escapeMarkup-tests.js" type="text/javascript"></script>
</body>
Expand Down
36 changes: 36 additions & 0 deletions tests/utils/data-tests.js
@@ -0,0 +1,36 @@
module('Utils - RemoveData');

var $ = require('jquery');
var Utils = require('select2/utils');

test('The data-select2-id attribute is removed', function (assert) {
var $element = $('<select data-select2-id="test"></select>');

Utils.RemoveData($element[0]);

assert.notEqual(
$element.attr('data-select2-id'),
'test',
'The internal attribute was not removed when the data was cleared'
);
});

test('The internal cache for the element is cleared', function (assert) {
var $element = $('<select data-select2-id="test"></select>');

Utils.__cache.test = {
'foo': 'bar'
};

Utils.RemoveData($element[0]);

assert.equal(Utils.__cache.test, null, 'The cache should now be empty');
});

test('Calling it on an element without data works', function (assert) {
assert.expect(0);

var $element = $('<select></select>');

Utils.RemoveData($element[0]);
});

0 comments on commit 6645ffd

Please sign in to comment.