Skip to content
Permalink
Browse files

Only try to get data attributes for a jQuery-wrapped object if it is …

…actually an Element node. Fixes #7524.
  • Loading branch information...
csnover committed Dec 3, 2010
1 parent e1d6edf commit 11e52bdaeae769dec384ca0eb286901122f35501
Showing with 13 additions and 8 deletions.
  1. +9 −7 src/data.js
  2. +4 −1 test/unit/data.js
@@ -138,15 +138,17 @@ jQuery.fn.extend({

if ( typeof key === "undefined" ) {
if ( this.length ) {
var attr = this[0].attributes, name;
data = jQuery.data( this[0] );

for ( var i = 0, l = attr.length; i < l; i++ ) {
name = attr[i].name;

if ( name.indexOf( "data-" ) === 0 ) {
name = name.substr( 5 );
dataAttr( this[0], name, data[ name ] );
if ( this[0].nodeType === 1 ) {
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 );
dataAttr( this[0], name, data[ name ] );
}
}
}
}
@@ -79,7 +79,7 @@ test("jQuery.data", function() {
});

test(".data()", function() {
expect(4);
expect(5);

var div = jQuery("#foo");
strictEqual( div.data("foo"), undefined, "Make sure that missing result is undefined" );
@@ -90,6 +90,9 @@ test(".data()", function() {

var nodiv = jQuery("#unfound");
equals( nodiv.data(), null, "data() on empty set returns null" );

var obj = { foo: "bar" };
equals( jQuery(obj).data(), obj, "Retrieve data object from a wrapped JS object (#7524)" );
})

test(".data(String) and .data(String, Object)", function() {

0 comments on commit 11e52bd

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.