]> source.dussan.org Git - jquery.git/commitdiff
Fixes #7328. When getting data- attributes, after-cap any embedded dashes per the...
authorAlexis Abril <alexis.abril@gmail.com>
Sun, 10 Apr 2011 19:17:00 +0000 (15:17 -0400)
committerDave Methvin <dave.methvin@gmail.com>
Sun, 10 Apr 2011 19:17:00 +0000 (15:17 -0400)
src/data.js
test/unit/data.js

index 2d53a710485e80979eceaa4cdf4e18b28bf7982c..c2fd558f05a7dd2ba39c31f04de836219c67b896 100644 (file)
@@ -1,6 +1,7 @@
 (function( jQuery ) {
 
-var rbrace = /^(?:\{.*\}|\[.*\])$/;
+var rbrace = /^(?:\{.*\}|\[.*\])$/,
+       rmultiDash = /([a-z])([A-Z])/g;
 
 jQuery.extend({
        cache: {},
@@ -223,12 +224,13 @@ jQuery.fn.extend({
                                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 ] );
                                                }
                                        }
@@ -282,7 +284,9 @@ function dataAttr( elem, key, data ) {
        // 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 {
index 8fb7f35adefcdc1fe15c965449ad1578461b61be..94fa2a018558b3b5b565fe8d38360ce212c34b9e 100644 (file)
@@ -485,4 +485,21 @@ if (window.JSON && window.JSON.stringify) {
 
                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