]> source.dussan.org Git - jquery.git/commitdiff
Only try to get data attributes for a jQuery-wrapped object if it is actually an... 119/head
authorColin Snover <github.com@zetafleet.com>
Fri, 3 Dec 2010 07:19:39 +0000 (01:19 -0600)
committerColin Snover <github.com@zetafleet.com>
Fri, 3 Dec 2010 07:19:39 +0000 (01:19 -0600)
src/data.js
test/unit/data.js

index f7f65efeb7d93e6330cbac26da77b79a0542defc..f1e031fff0b351980339d5f758b31571925ff2cb 100644 (file)
@@ -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 ] );
+                                               }
                                        }
                                }
                        }
index 315043ca17c35bd53d95084562cf1b96fd139796..1a0f84c1f7087a48814c5eacb705eed776f53097 100644 (file)
@@ -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() {