From a5e407cafb9d398b5bf2f209ad0e8a42691bf391 Mon Sep 17 00:00:00 2001 From: John Resig Date: Tue, 10 May 2011 11:56:42 -0400 Subject: [PATCH] Make sure that data properties with hyphens are always accessed/set using camelCase. Fixes #9124. --- src/data.js | 4 ++-- test/unit/data.js | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/data.js b/src/data.js index 9e5d1ab0b9..c019a64e2d 100644 --- a/src/data.js +++ b/src/data.js @@ -98,7 +98,7 @@ jQuery.extend({ } if ( data !== undefined ) { - thisCache[ name ] = data; + thisCache[ jQuery.camelCase( name ) ] = data; } // TODO: This is a hack for 1.5 ONLY. It will be removed in 1.6. Users should @@ -108,7 +108,7 @@ jQuery.extend({ return thisCache[ internalKey ] && thisCache[ internalKey ].events; } - return getByName ? thisCache[ name ] : thisCache; + return getByName ? thisCache[ jQuery.camelCase( name ) ] : thisCache; }, removeData: function( elem, name, pvt /* Internal Use Only */ ) { diff --git a/test/unit/data.js b/test/unit/data.js index 8b5ce96122..1af2077ec4 100644 --- a/test/unit/data.js +++ b/test/unit/data.js @@ -488,7 +488,7 @@ if (window.JSON && window.JSON.stringify) { } test("jQuery.data should follow html5 specification regarding camel casing", function() { - expect(6); + expect(8); var div = jQuery("
") .prependTo("body"); @@ -501,5 +501,10 @@ test("jQuery.data should follow html5 specification regarding camel casing", fun equals(div.data("fooBar"), "b", "Verify multiple word data-* key"); equals(div.data("fooBarBaz"), "c", "Verify multiple word data-* key"); + div.data("foo-bar", "d"); + + equals(div.data("fooBar"), "d", "Verify updated data-* key"); + equals(div.data("foo-bar"), "d", "Verify updated data-* key"); + div.remove(); -}); \ No newline at end of file +});