Skip to content

Commit 37254ba

Browse files
committed
Merge pull request jquery#455 from rwldrn/9413
Supports interoperable removal of hyphenated/camelCase properties. Fixes #9413
2 parents caa47c3 + 2ce5e95 commit 37254ba

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/data.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,12 @@ jQuery.extend({
135135
return;
136136
}
137137

138-
var internalKey = jQuery.expando, isNode = elem.nodeType,
138+
var thisCache,
139+
140+
// Reference to internal data cache key
141+
internalKey = jQuery.expando,
142+
143+
isNode = elem.nodeType,
139144

140145
// See jQuery.data for more information
141146
cache = isNode ? jQuery.cache : elem,
@@ -150,9 +155,16 @@ jQuery.extend({
150155
}
151156

152157
if ( name ) {
153-
var thisCache = pvt ? cache[ id ][ internalKey ] : cache[ id ];
158+
159+
thisCache = pvt ? cache[ id ][ internalKey ] : cache[ id ];
154160

155161
if ( thisCache ) {
162+
163+
// Support interoperable removal of hyphenated or camelcased keys
164+
if ( !thisCache[ name ] ) {
165+
name = jQuery.camelCase( name );
166+
}
167+
156168
delete thisCache[ name ];
157169

158170
// If there is no data left in the cache, we want to continue

test/unit/data.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,3 +551,31 @@ test("jQuery.data supports interoperable hyphenated/camelCase get/set of propert
551551
});
552552
});
553553

554+
test("jQuery.data supports interoperable removal of hyphenated/camelCase properties", function() {
555+
var div = jQuery("<div/>", { id: "hyphened" }).appendTo("#qunit-fixture"),
556+
datas = {
557+
"non-empty": "a string",
558+
"empty-string": "",
559+
"one-value": 1,
560+
"zero-value": 0,
561+
"an-array": [],
562+
"an-object": {},
563+
"bool-true": true,
564+
"bool-false": false,
565+
"some-json": '{ "foo": "bar" }'
566+
};
567+
568+
expect( 27 );
569+
570+
jQuery.each( datas, function( key, val ) {
571+
div.data( key, val );
572+
573+
deepEqual( div.data( key ), val, "get: " + key );
574+
deepEqual( div.data( jQuery.camelCase( key ) ), val, "get: " + jQuery.camelCase( key ) );
575+
576+
div.removeData( key );
577+
578+
equal( div.data( key ), undefined, "get: " + key );
579+
580+
});
581+
});

0 commit comments

Comments
 (0)