Skip to content

Commit 6645ffd

Browse files
authored
Select2 now clears the internal ID when it is destroyed (select2#5587)
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 select2#5247
1 parent 1f3eceb commit 6645ffd

5 files changed

Lines changed: 41 additions & 0 deletions

File tree

src/js/select2/utils.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ define([
332332
if (Utils.__cache[id] != null) {
333333
delete Utils.__cache[id];
334334
}
335+
336+
element.removeAttribute('data-select2-id');
335337
};
336338

337339
return Utils;

tests/unit-jq1.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
<script src="selection/single-tests.js" type="text/javascript"></script>
9595
<script src="selection/stopPropagation-tests.js" type="text/javascript"></script>
9696

97+
<script src="utils/data-tests.js" type="text/javascript"></script>
9798
<script src="utils/decorator-tests.js" type="text/javascript"></script>
9899
<script src="utils/escapeMarkup-tests.js" type="text/javascript"></script>
99100
</body>

tests/unit-jq2.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
<script src="selection/single-tests.js" type="text/javascript"></script>
9595
<script src="selection/stopPropagation-tests.js" type="text/javascript"></script>
9696

97+
<script src="utils/data-tests.js" type="text/javascript"></script>
9798
<script src="utils/decorator-tests.js" type="text/javascript"></script>
9899
<script src="utils/escapeMarkup-tests.js" type="text/javascript"></script>
99100
</body>

tests/unit-jq3.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
<script src="selection/single-tests.js" type="text/javascript"></script>
9595
<script src="selection/stopPropagation-tests.js" type="text/javascript"></script>
9696

97+
<script src="utils/data-tests.js" type="text/javascript"></script>
9798
<script src="utils/decorator-tests.js" type="text/javascript"></script>
9899
<script src="utils/escapeMarkup-tests.js" type="text/javascript"></script>
99100
</body>

tests/utils/data-tests.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module('Utils - RemoveData');
2+
3+
var $ = require('jquery');
4+
var Utils = require('select2/utils');
5+
6+
test('The data-select2-id attribute is removed', function (assert) {
7+
var $element = $('<select data-select2-id="test"></select>');
8+
9+
Utils.RemoveData($element[0]);
10+
11+
assert.notEqual(
12+
$element.attr('data-select2-id'),
13+
'test',
14+
'The internal attribute was not removed when the data was cleared'
15+
);
16+
});
17+
18+
test('The internal cache for the element is cleared', function (assert) {
19+
var $element = $('<select data-select2-id="test"></select>');
20+
21+
Utils.__cache.test = {
22+
'foo': 'bar'
23+
};
24+
25+
Utils.RemoveData($element[0]);
26+
27+
assert.equal(Utils.__cache.test, null, 'The cache should now be empty');
28+
});
29+
30+
test('Calling it on an element without data works', function (assert) {
31+
assert.expect(0);
32+
33+
var $element = $('<select></select>');
34+
35+
Utils.RemoveData($element[0]);
36+
});

0 commit comments

Comments
 (0)