From: Colin Snover Date: Mon, 27 Dec 2010 04:08:41 +0000 (-0600) Subject: Merge SlexAxton/jquery:master into jquery/jquery:master. X-Git-Url: http://git.asbjorn.it/?a=commitdiff_plain;h=01cba2ecaaedb5351f668bb09026db3e8bb69082;hp=-c;p=jquery.git Merge SlexAxton/jquery:master into jquery/jquery: --- 01cba2ecaaedb5351f668bb09026db3e8bb69082 diff --combined src/data.js index 21b7543,bb7febf..4d1d1bd --- a/src/data.js +++ b/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. @@@ -21,14 -22,6 +22,14 @@@ "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; @@@ -142,26 -135,8 +143,26 @@@ 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() { @@@ -173,12 -148,31 +174,12 @@@ 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] ? @@@ -187,8 -181,7 +188,8 @@@ } 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 ); @@@ -204,31 -197,4 +205,31 @@@ } }); +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 );