diff options
author | John Resig <jeresig@gmail.com> | 2010-10-17 11:42:53 -0400 |
---|---|---|
committer | John Resig <jeresig@gmail.com> | 2010-10-17 11:42:53 -0400 |
commit | 8a5df39045292397a06d08b4fab2ad29819b5d44 (patch) | |
tree | adb77e81f34b5c1bdae860600d73710e3ea618e0 /src/data.js | |
parent | a7d0b0b9e887782bf1ed795152a845da87729c52 (diff) | |
download | jquery-8a5df39045292397a06d08b4fab2ad29819b5d44.tar.gz jquery-8a5df39045292397a06d08b4fab2ad29819b5d44.zip |
Make sure that .data() (no args) returns a list of all the data- properties as well. Also make sure that accessing a data- property via .data() doesn't cause it to change again at a later time (it should be static). Fixes #7222, #7223.
Diffstat (limited to 'src/data.js')
-rw-r--r-- | src/data.js | 66 |
1 files changed, 45 insertions, 21 deletions
diff --git a/src/data.js b/src/data.js index 732e9233b..31cdc121e 100644 --- a/src/data.js +++ b/src/data.js @@ -135,7 +135,23 @@ jQuery.extend({ jQuery.fn.extend({ data: function( key, value ) { if ( typeof key === "undefined" ) { - return this.length ? jQuery.data( this[0] ) : null; + var data = null; + + 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 ] ); + } + } + } + + return data; } else if ( typeof key === "object" ) { return this.each(function() { @@ -152,26 +168,7 @@ jQuery.fn.extend({ // 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] ? @@ -196,4 +193,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 ); |