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

@@ -111,7 +111,7 @@ jQuery.fn.extend({
}
return access( this, function( value ) {
var data, camelKey;
var data;
// The calling jQuery object (element matches) is not empty
// (and therefore has an element appears at this[ 0 ]) and the
@@ -119,24 +119,17 @@ jQuery.fn.extend({
// will result in `undefined` for elem = this[ 0 ] which will
// throw an exception if an attempt to read a data cache is made.
if ( elem && value === undefined ) {
// Attempt to get data from the cache
// with the key as-is
data = dataUser.get( elem, key );
if ( data !== undefined ) {
return data;
}
camelKey = jQuery.camelCase( key );
// Attempt to get data from the cache
// with the key camelized
data = dataUser.get( elem, camelKey );
// The key will always be camelCased in Data
data = dataUser.get( elem, key );
if ( data !== undefined ) {
return data;
}
// Attempt to "discover" the data in
// HTML5 custom data-* attrs
data = dataAttr( elem, camelKey, undefined );
data = dataAttr( elem, key );
if ( data !== undefined ) {
return data;
}
@@ -146,23 +139,10 @@ jQuery.fn.extend({
}
// Set the data...
camelKey = jQuery.camelCase( key );
this.each(function() {
// First, attempt to store a copy or reference of any
// data that might've been store with a camelCased key.
var data = dataUser.get( this, camelKey );
// For HTML5 data-* attribute interop, we have to
// store property names with dashes in a camelCase form.
// This might not apply to all properties...*
dataUser.set( this, camelKey, value );
// *... In the case of properties that might _actually_
// have dashes, we need to also store a copy of that
// unchanged property.
if ( key.indexOf("-") > -1 && data !== undefined ) {
dataUser.set( this, key, value );
}
// We always store the camelCased key
dataUser.set( this, key, value );
});
}, null, value, arguments.length > 1, null, true );
},