From 84d066ff7ce38a301fe5cf76a1a79b3a1e754947 Mon Sep 17 00:00:00 2001 From: Rick Waldron Date: Sun, 10 Jul 2011 21:42:40 -0400 Subject: [PATCH] Correct non-null|undefined evaluation of data property values. Fixes #9794 --- src/data.js | 26 +++++++++++++++++++++----- test/unit/data.js | 26 ++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/data.js b/src/data.js index f69d9decd..353a0918c 100644 --- a/src/data.js +++ b/src/data.js @@ -33,7 +33,9 @@ jQuery.extend({ return; } - var internalKey = jQuery.expando, getByName = typeof name === "string", thisCache, + var thisCache, ret, + internalKey = jQuery.expando, + getByName = typeof name === "string", // We have to handle DOM nodes and JS objects differently because IE6-7 // can't GC object references properly across the DOM-JS boundary @@ -108,10 +110,24 @@ jQuery.extend({ return thisCache[ internalKey ] && thisCache[ internalKey ].events; } - return getByName ? - // Check for both converted-to-camel and non-converted data property names - thisCache[ jQuery.camelCase( name ) ] || thisCache[ name ] : - thisCache; + // Check for both converted-to-camel and non-converted data property names + // If a data property was specified + if ( getByName ) { + + // First try to find the camelCased property + ret = thisCache[ jQuery.camelCase( name ) ]; + + // Test for null|undefined property data was found + if ( ret == null ) { + + // Try to find as-is property data + ret = thisCache[ name ]; + } + } else { + ret = thisCache; + } + + return ret; }, removeData: function( elem, name, pvt /* Internal Use Only */ ) { diff --git a/test/unit/data.js b/test/unit/data.js index 87a3de339..c62bd1cc1 100644 --- a/test/unit/data.js +++ b/test/unit/data.js @@ -525,3 +525,29 @@ test("jQuery.data should not miss data with preset hyphenated property names", f equal( div.data(k), k, "data with property '"+k+"' was correctly found"); }); }); + +test("jQuery.data supports interoperable hyphenated/camelCase get/set of properties with arbitrary non-null|NaN|undefined values", function() { + + var div = jQuery("
", { id: "hyphened" }).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, + "some-json": '{ "foo": "bar" }' + }; + + expect( 18 ); + + jQuery.each( datas, function( key, val ) { + div.data( key, val ); + + deepEqual( div.data( key ), val, "get: " + key ); + deepEqual( div.data( jQuery.camelCase( key ) ), val, "get: " + jQuery.camelCase( key ) ); + }); +}); + -- 2.39.5