Merge SlexAxton/jquery:master into jquery/jquery:master.
authorColin Snover <github.com@zetafleet.com>
Mon, 27 Dec 2010 04:08:41 +0000 (22:08 -0600)
committerColin Snover <github.com@zetafleet.com>
Mon, 27 Dec 2010 04:12:56 +0000 (22:12 -0600)
1  2 
src/data.js

diff --combined src/data.js
@@@ -10,7 -10,8 +10,8 @@@ jQuery.extend(
        uuid: 0,
  
        // Unique for each copy of jQuery on the page
-       expando: "jQuery" + jQuery.now(),
+       // Non-digits removed to match rinlinejQuery
+       expando: "jQuery" + ( jQuery.fn.jquery + Math.random() ).replace( /\D/g, "" ),
  
        // The following elements throw uncatchable exceptions if you
        // attempt to add expando properties to them.
                "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;
  
  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() {
                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] ?
  
                } 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 );
        }
  });
  
 +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 );