Permalink
Browse files

Merge pull request #432 from rwldrn/9794

Correct non-null|undefined evaluation of data property values. Fixes #9779
  • Loading branch information...
2 parents 2e298d9 + 8fed1e7 commit 59936dc04d4dd2731874fecd6237383a0e41ddd5 @dmethvin dmethvin committed Aug 4, 2011
Showing with 47 additions and 5 deletions.
  1. +21 −5 src/data.js
  2. +26 −0 test/unit/data.js
View
@@ -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 as-is property data
+ ret = thisCache[ name ];
+
+ // Test for null|undefined property data
+ if ( ret == null ) {
+
+ // Try to find the camelCased property
+ ret = thisCache[ jQuery.camelCase( name ) ];
+ }
+ } else {
+ ret = thisCache;
+ }
+
+ return ret;
},
removeData: function( elem, name, pvt /* Internal Use Only */ ) {
View
@@ -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("<div/>", { 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 ) );
+ });
+});
+

0 comments on commit 59936dc

Please sign in to comment.