Skip to content

[1.x] .data() keys do not match HTML5 data specification when two dashes are used #2070

@kevin-brown

Description

@kevin-brown

jQuery has provided the .data() method since 1.4, and in 1.6 there was a change that made it conform to the HTML5 data specification. According to the specification, the key nested-single should be converted into nestedSingle, and nested--double should be converted into nested-Double.

The .data() method allows for no arguments to be provided, which the docs says should return all of the values as an object

Calling .data() with no parameters retrieves all of the values as a JavaScript object.

In jQuery 2.x, this works correctly and $(element).data() matches element.dataset as one would expect.

In jQuery 1.x, data attributes with two consecutive dashes (--) are converted the same way as data attributes containing a single dash. This means that nested-single and nested--single are treated in the same way, both being converted to nestedSingle.

Special notes

In 1.6 through 1.6.3, attributes containing two dashes in a row are not included in the returned object. This appeared to be fixed in 8e8fa6d, which makes 1.6.4 the first version where this bug appears in 1.x.

Tests

HTML code used for testing

<div data-one="none" data-nested-single="single" data-nested--double="double"></div>

jQuery .data test

var data = $('div').data();

assert.equal(data['one'], 'none');
assert.equal(data['nestedSingle'], 'single');
assert.equal(data['nested-Double'], 'double');

This will pass in 2.x, but fail in 1.x.

HTML5 dataset test

var data = $('div')[0].dataset;

assert.equal(data['one'], 'none');
assert.equal(data['nestedSingle'], 'single');
assert.equal(data['nested-Double'], 'double');

This passes, as expected.


/refs select2/select2#2969
/cc @loic

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions