Skip to content

Commit 55f995e

Browse files
committed
Fixed data-ajax-url fallback
This fixes the fallback path for the `data-ajax-url` attribute on elements. As this attribute was previously supported in Select2, the attribute has been migrated to the new, nested format of the url and triggers a deprecation warning when it is used. Because of a fix to the `data-*` attribute parsing made in a9f6d64 that allowed for nested attributes to be parsed correctly in modern browsers under jQuery 1.x, the deprecation warning would be triggered but the attribute would no longer actually be used. This also fixes some of the `.data` calls to use the camel cased version of the key instead of the dashed version, which is the preferred key and will be enforced in future versions of jQuery as the only way to access data attributes. Now in situations where the `dataset` attribute is used by Select2, it combines the results of both `$e.data()` and `e.dataset` when generating the object containing all of the options. This will the `dataset` fix to still be used, while also still relying on jQuery to do additional parsing on any options that it can. The `dataset` fix is now only used on jQuery 1.x, as that is the only version of jQuery affected by the dash issue. This is done using version number parsing on the `$.fn.jquery` property that is defined by jQuery. As this property is not defined in Zepto and many other jQuery compatible checks, we only include the fallback if the property is available. This assumes that any jQuery compatible libraries that are in use will not include the same dash issue, which we believe is a safe assumption given that it did not match the HTML `dataset` specification. This also adds a few tests to ensure that the deprecated attributes still continue to function. This closes select2#3086.
1 parent 3630385 commit 55f995e

8 files changed

Lines changed: 100 additions & 28 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4103,7 +4103,7 @@ define('select2/options',[
41034103
$e.prop('disabled', this.options.disabled);
41044104
$e.prop('multiple', this.options.multiple);
41054105

4106-
if ($e.data('select2-tags')) {
4106+
if ($e.data('select2Tags')) {
41074107
if (window.console && console.warn) {
41084108
console.warn(
41094109
'Select2: The `data-select2-tags` attribute has been changed to ' +
@@ -4112,11 +4112,11 @@ define('select2/options',[
41124112
);
41134113
}
41144114

4115-
$e.data('data', $e.data('select2-tags'));
4115+
$e.data('data', $e.data('select2Tags'));
41164116
$e.data('tags', true);
41174117
}
41184118

4119-
if ($e.data('ajax-url')) {
4119+
if ($e.data('ajaxUrl')) {
41204120
if (window.console && console.warn) {
41214121
console.warn(
41224122
'Select2: The `data-ajax-url` attribute has been changed to ' +
@@ -4125,12 +4125,20 @@ define('select2/options',[
41254125
);
41264126
}
41274127

4128-
$e.data('ajax--url', $e.data('ajax-url'));
4128+
$e.data('ajax-Url', $e.data('ajaxUrl'));
41294129
}
41304130

4131+
var dataset = {};
4132+
41314133
// Prefer the element's `dataset` attribute if it exists
41324134
// jQuery 1.x does not correctly handle data attributes with multiple dashes
4133-
var data = $.extend(true, {}, $e[0].dataset || $e.data());
4135+
if ($.fn.jquery && $.fn.jquery.substr(0, 2) == '1.' && $e[0].dataset) {
4136+
dataset = $.extend(true, {}, $e[0].dataset, $e.data());
4137+
} else {
4138+
dataset = $e.data();
4139+
}
4140+
4141+
var data = $.extend(true, {}, dataset);
41344142

41354143
data = Utils._convertData(data);
41364144

dist/js/select2.amd.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4103,7 +4103,7 @@ define('select2/options',[
41034103
$e.prop('disabled', this.options.disabled);
41044104
$e.prop('multiple', this.options.multiple);
41054105

4106-
if ($e.data('select2-tags')) {
4106+
if ($e.data('select2Tags')) {
41074107
if (window.console && console.warn) {
41084108
console.warn(
41094109
'Select2: The `data-select2-tags` attribute has been changed to ' +
@@ -4112,11 +4112,11 @@ define('select2/options',[
41124112
);
41134113
}
41144114

4115-
$e.data('data', $e.data('select2-tags'));
4115+
$e.data('data', $e.data('select2Tags'));
41164116
$e.data('tags', true);
41174117
}
41184118

4119-
if ($e.data('ajax-url')) {
4119+
if ($e.data('ajaxUrl')) {
41204120
if (window.console && console.warn) {
41214121
console.warn(
41224122
'Select2: The `data-ajax-url` attribute has been changed to ' +
@@ -4125,12 +4125,20 @@ define('select2/options',[
41254125
);
41264126
}
41274127

4128-
$e.data('ajax--url', $e.data('ajax-url'));
4128+
$e.data('ajax-Url', $e.data('ajaxUrl'));
41294129
}
41304130

4131+
var dataset = {};
4132+
41314133
// Prefer the element's `dataset` attribute if it exists
41324134
// jQuery 1.x does not correctly handle data attributes with multiple dashes
4133-
var data = $.extend(true, {}, $e[0].dataset || $e.data());
4135+
if ($.fn.jquery && $.fn.jquery.substr(0, 2) == '1.' && $e[0].dataset) {
4136+
dataset = $.extend(true, {}, $e[0].dataset, $e.data());
4137+
} else {
4138+
dataset = $e.data();
4139+
}
4140+
4141+
var data = $.extend(true, {}, dataset);
41344142

41354143
data = Utils._convertData(data);
41364144

dist/js/select2.full.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4542,7 +4542,7 @@ define('select2/options',[
45424542
$e.prop('disabled', this.options.disabled);
45434543
$e.prop('multiple', this.options.multiple);
45444544

4545-
if ($e.data('select2-tags')) {
4545+
if ($e.data('select2Tags')) {
45464546
if (window.console && console.warn) {
45474547
console.warn(
45484548
'Select2: The `data-select2-tags` attribute has been changed to ' +
@@ -4551,11 +4551,11 @@ define('select2/options',[
45514551
);
45524552
}
45534553

4554-
$e.data('data', $e.data('select2-tags'));
4554+
$e.data('data', $e.data('select2Tags'));
45554555
$e.data('tags', true);
45564556
}
45574557

4558-
if ($e.data('ajax-url')) {
4558+
if ($e.data('ajaxUrl')) {
45594559
if (window.console && console.warn) {
45604560
console.warn(
45614561
'Select2: The `data-ajax-url` attribute has been changed to ' +
@@ -4564,12 +4564,20 @@ define('select2/options',[
45644564
);
45654565
}
45664566

4567-
$e.data('ajax--url', $e.data('ajax-url'));
4567+
$e.data('ajax-Url', $e.data('ajaxUrl'));
45684568
}
45694569

4570+
var dataset = {};
4571+
45704572
// Prefer the element's `dataset` attribute if it exists
45714573
// jQuery 1.x does not correctly handle data attributes with multiple dashes
4572-
var data = $.extend(true, {}, $e[0].dataset || $e.data());
4574+
if ($.fn.jquery && $.fn.jquery.substr(0, 2) == '1.' && $e[0].dataset) {
4575+
dataset = $.extend(true, {}, $e[0].dataset, $e.data());
4576+
} else {
4577+
dataset = $e.data();
4578+
}
4579+
4580+
var data = $.extend(true, {}, dataset);
45734581

45744582
data = Utils._convertData(data);
45754583

dist/js/select2.full.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/select2.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4542,7 +4542,7 @@ define('select2/options',[
45424542
$e.prop('disabled', this.options.disabled);
45434543
$e.prop('multiple', this.options.multiple);
45444544

4545-
if ($e.data('select2-tags')) {
4545+
if ($e.data('select2Tags')) {
45464546
if (window.console && console.warn) {
45474547
console.warn(
45484548
'Select2: The `data-select2-tags` attribute has been changed to ' +
@@ -4551,11 +4551,11 @@ define('select2/options',[
45514551
);
45524552
}
45534553

4554-
$e.data('data', $e.data('select2-tags'));
4554+
$e.data('data', $e.data('select2Tags'));
45554555
$e.data('tags', true);
45564556
}
45574557

4558-
if ($e.data('ajax-url')) {
4558+
if ($e.data('ajaxUrl')) {
45594559
if (window.console && console.warn) {
45604560
console.warn(
45614561
'Select2: The `data-ajax-url` attribute has been changed to ' +
@@ -4564,12 +4564,20 @@ define('select2/options',[
45644564
);
45654565
}
45664566

4567-
$e.data('ajax--url', $e.data('ajax-url'));
4567+
$e.data('ajax-Url', $e.data('ajaxUrl'));
45684568
}
45694569

4570+
var dataset = {};
4571+
45704572
// Prefer the element's `dataset` attribute if it exists
45714573
// jQuery 1.x does not correctly handle data attributes with multiple dashes
4572-
var data = $.extend(true, {}, $e[0].dataset || $e.data());
4574+
if ($.fn.jquery && $.fn.jquery.substr(0, 2) == '1.' && $e[0].dataset) {
4575+
dataset = $.extend(true, {}, $e[0].dataset, $e.data());
4576+
} else {
4577+
dataset = $e.data();
4578+
}
4579+
4580+
var data = $.extend(true, {}, dataset);
45734581

45744582
data = Utils._convertData(data);
45754583

dist/js/select2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/select2/options.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ define([
5454
$e.prop('disabled', this.options.disabled);
5555
$e.prop('multiple', this.options.multiple);
5656

57-
if ($e.data('select2-tags')) {
57+
if ($e.data('select2Tags')) {
5858
if (window.console && console.warn) {
5959
console.warn(
6060
'Select2: The `data-select2-tags` attribute has been changed to ' +
@@ -63,11 +63,11 @@ define([
6363
);
6464
}
6565

66-
$e.data('data', $e.data('select2-tags'));
66+
$e.data('data', $e.data('select2Tags'));
6767
$e.data('tags', true);
6868
}
6969

70-
if ($e.data('ajax-url')) {
70+
if ($e.data('ajaxUrl')) {
7171
if (window.console && console.warn) {
7272
console.warn(
7373
'Select2: The `data-ajax-url` attribute has been changed to ' +
@@ -76,12 +76,20 @@ define([
7676
);
7777
}
7878

79-
$e.data('ajax--url', $e.data('ajax-url'));
79+
$e.data('ajax-Url', $e.data('ajaxUrl'));
8080
}
8181

82+
var dataset = {};
83+
8284
// Prefer the element's `dataset` attribute if it exists
8385
// jQuery 1.x does not correctly handle data attributes with multiple dashes
84-
var data = $.extend(true, {}, $e[0].dataset || $e.data());
86+
if ($.fn.jquery && $.fn.jquery.substr(0, 2) == '1.' && $e[0].dataset) {
87+
dataset = $.extend(true, {}, $e[0].dataset, $e.data());
88+
} else {
89+
dataset = $e.data();
90+
}
91+
92+
var data = $.extend(true, {}, dataset);
8593

8694
data = Utils._convertData(data);
8795

tests/options/deprecated-tests.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,35 @@ test('converted into dataAdapter.query automatically', function (assert) {
216216

217217
assert.ok(called, 'The query function should have been called');
218218
});
219+
220+
module('Options - deprecated - data-ajax-url');
221+
222+
test('converted ajax-url to ajax--url automatically', function (assert) {
223+
var $test = $('<select data-ajax-url="test://url"></select>');
224+
var options = new Options({}, $test);
225+
226+
assert.ok(
227+
options.get('ajax'),
228+
'The `ajax` key was automatically created'
229+
);
230+
assert.equal(
231+
options.get('ajax').url,
232+
'test://url',
233+
'The `url` property for the `ajax` option was filled in correctly'
234+
);
235+
});
236+
237+
test('converted select2-tags to data/tags automatically', function (assert) {
238+
var $test = $('<select data-select2-tags="original data"></select>');
239+
var options = new Options({}, $test);
240+
241+
assert.ok(
242+
options.get('tags'),
243+
'The `tags` key is automatically set to true'
244+
);
245+
assert.equal(
246+
options.get('data'),
247+
'original data',
248+
'The `data` key is created with the original data'
249+
);
250+
});

0 commit comments

Comments
 (0)