Data: always camelCase keys in .data()

- This effectively implements our "Embrace HTML5" option
- Related: http://goo.gl/GcQAtn

Fixes gh-2257
This commit is contained in:
Timmy Willison
2015-05-04 09:36:58 -04:00
parent 2862a07af6
commit 0e790985a7
3 changed files with 64 additions and 61 deletions

View File

@@ -587,6 +587,34 @@ test(".data should not miss attr() set data-* with hyphenated property names", f
deepEqual( b.data("long-param"), { a: 2 }, "data with property long-param was found, 2" );
});
test(".data always sets data with the camelCased key (gh-2257)", function() {
expect( 18 );
var div = jQuery("<div>").appendTo("#qunit-fixture"),
datas = {
"non-empty": "a string",
"empty-string": "",
"one-value": 1,
"zero-value": 0,
"an-array": [],
"an-object": {},
"bool-true": true,
"bool-false": false,
// JSHint enforces double quotes,
// but JSON strings need double quotes to parse
// so we need escaped double quotes here
"some-json": "{ \"foo\": \"bar\" }"
};
jQuery.each( datas, function( key, val ) {
div.data( key, val );
var allData = div.data();
equal( allData[ key ], undefined, ".data does not store with hyphenated keys" );
equal( allData[ jQuery.camelCase( key ) ], val, ".data stores the camelCased key" );
});
});
test(".data supports interoperable hyphenated/camelCase get/set of properties with arbitrary non-null|NaN|undefined values", function() {
var div = jQuery("<div/>", { id: "hyphened" }).appendTo("#qunit-fixture"),
@@ -679,7 +707,7 @@ test(".data supports interoperable removal of properties SET TWICE #13850", func
});
});
test( ".removeData supports removal of hyphenated properties via array (#12786)", function() {
test( ".removeData supports removal of hyphenated properties via array (#12786, gh-2257)", function() {
expect( 4 );
var div, plain, compare;
@@ -687,11 +715,10 @@ test( ".removeData supports removal of hyphenated properties via array (#12786)"
div = jQuery("<div>").appendTo("#qunit-fixture");
plain = jQuery({});
// When data is batch assigned (via plain object), the properties
// are not camel cased as they are with (property, value) calls
// Properties should always be camelCased
compare = {
// From batch assignment .data({ "a-a": 1 })
"a-a": 1,
"aA": 1,
// From property, value assignment .data( "b-b", 1 )
"bB": 1
};
@@ -706,7 +733,6 @@ test( ".removeData supports removal of hyphenated properties via array (#12786)"
div.removeData([ "a-a", "b-b" ]);
plain.removeData([ "a-a", "b-b" ]);
// NOTE: Timo's proposal for "propEqual" (or similar) would be nice here
deepEqual( div.data(), {}, "Data is empty. (div)" );
deepEqual( plain.data(), {}, "Data is empty. (plain)" );
});