(function( jQuery ) {
-var rbrace = /^(?:\{.*\}|\[.*\])$/;
+var rbrace = /^(?:\{.*\}|\[.*\])$/,
+ rmultiDash = /([a-z])([A-Z])/g;
jQuery.extend({
cache: {},
data = jQuery.data( this[0] );
if ( this[0].nodeType === 1 ) {
- var attr = this[0].attributes, name;
+ var attr = this[0].attributes, name;
for ( var i = 0, l = attr.length; i < l; i++ ) {
name = attr[i].name;
if ( name.indexOf( "data-" ) === 0 ) {
- name = name.substr( 5 );
+ name = jQuery.camelCase( name.substring(5) );
+
dataAttr( this[0], name, data[ name ] );
}
}
// If nothing was found internally, try to fetch any
// data from the HTML5 data-* attribute
if ( data === undefined && elem.nodeType === 1 ) {
- data = elem.getAttribute( "data-" + key );
+ name = "data-" + key.replace( rmultiDash, "$1-$2" ).toLowerCase();
+
+ data = elem.getAttribute( name );
if ( typeof data === "string" ) {
try {
equals( JSON.stringify(obj), '{"foo":"bar"}', "Expando is hidden from JSON.stringify" );
});
-}
\ No newline at end of file
+}
+
+test("jQuery.data should follow html5 specification regarding camel casing", function() {
+ expect(6);
+
+ var div = jQuery("<div id='myObject' data-foo='a' data-foo-bar='b' data-foo-bar-baz='c'></div>")
+ .prependTo("body");
+
+ equals(div.data().foo, "a", "Verify single word data-* key");
+ equals(div.data().fooBar, "b", "Verify multiple word data-* key");
+ equals(div.data().fooBarBaz, "c", "Verify multiple word data-* key");
+
+ equals(div.data("foo"), "a", "Verify single word data-* key");
+ equals(div.data("fooBar"), "b", "Verify multiple word data-* key");
+ equals(div.data("fooBarBaz"), "c", "Verify multiple word data-* key");
+
+ div.remove();
+});
\ No newline at end of file