Merge SlexAxton/jquery:master into jquery/jquery:master.
[jquery.git] / src / data.js
index bb7febf..4d1d1bd 100644 (file)
@@ -22,6 +22,14 @@ jQuery.extend({
                "applet": true
        },
 
+       hasData: function( elem ) {
+               if ( elem.nodeType ) {
+                       elem = jQuery.cache[ elem[jQuery.expando] ];
+               }
+
+               return !!elem && !jQuery.isEmptyObject(elem);
+       },
+
        data: function( elem, name, data ) {
                if ( !jQuery.acceptData( elem ) ) {
                        return;
@@ -135,8 +143,26 @@ jQuery.extend({
 
 jQuery.fn.extend({
        data: function( key, value ) {
+               var data = null;
+
                if ( typeof key === "undefined" ) {
-                       return this.length ? jQuery.data( this[0] ) : null;
+                       if ( this.length ) {
+                               data = jQuery.data( this[0] );
+
+                               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 ] );
+                                               }
+                                       }
+                               }
+                       }
+
+                       return data;
 
                } else if ( typeof key === "object" ) {
                        return this.each(function() {
@@ -148,31 +174,12 @@ jQuery.fn.extend({
                parts[1] = parts[1] ? "." + parts[1] : "";
 
                if ( value === undefined ) {
-                       var data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
+                       data = this.triggerHandler("getData" + parts[1] + "!", [parts[0]]);
 
                        // Try to fetch any internally stored data first
                        if ( data === undefined && this.length ) {
                                data = jQuery.data( this[0], key );
-
-                               // If nothing was found internally, try to fetch any
-                               // data from the HTML5 data-* attribute
-                               if ( data === undefined && this[0].nodeType === 1 ) {
-                                       data = this[0].getAttribute( "data-" + key );
-
-                                       if ( typeof data === "string" ) {
-                                               try {
-                                                       data = data === "true" ? true :
-                                                               data === "false" ? false :
-                                                               data === "null" ? null :
-                                                               !jQuery.isNaN( data ) ? parseFloat( data ) :
-                                                               rbrace.test( data ) ? jQuery.parseJSON( data ) :
-                                                               data;
-                                               } catch( e ) {}
-
-                                       } else {
-                                               data = undefined;
-                                       }
-                               }
+                               data = dataAttr( this[0], key, data );
                        }
 
                        return data === undefined && parts[1] ?
@@ -181,7 +188,8 @@ jQuery.fn.extend({
 
                } else {
                        return this.each(function() {
-                               var $this = jQuery( this ), args = [ parts[0], value ];
+                               var $this = jQuery( this ),
+                                       args = [ parts[0], value ];
 
                                $this.triggerHandler( "setData" + parts[1] + "!", args );
                                jQuery.data( this, key, value );
@@ -197,4 +205,31 @@ jQuery.fn.extend({
        }
 });
 
+function dataAttr( elem, key, data ) {
+       // If nothing was found internally, try to fetch any
+       // data from the HTML5 data-* attribute
+       if ( data === undefined && elem.nodeType === 1 ) {
+               data = elem.getAttribute( "data-" + key );
+
+               if ( typeof data === "string" ) {
+                       try {
+                               data = data === "true" ? true :
+                               data === "false" ? false :
+                               data === "null" ? null :
+                               !jQuery.isNaN( data ) ? parseFloat( data ) :
+                                       rbrace.test( data ) ? jQuery.parseJSON( data ) :
+                                       data;
+                       } catch( e ) {}
+
+                       // Make sure we set the data so it isn't changed later
+                       jQuery.data( elem, key, data );
+
+               } else {
+                       data = undefined;
+               }
+       }
+
+       return data;
+}
+
 })( jQuery );