Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make sure that .data() (no args) returns a list of all the data- prop…

…erties 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.
  • Loading branch information...
commit 8a5df39045292397a06d08b4fab2ad29819b5d44 1 parent a7d0b0b
@jeresig jeresig authored
Showing with 66 additions and 23 deletions.
  1. +45 −21 src/data.js
  2. +21 −2 test/unit/data.js
View
66 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;
@jitter
jitter added a note

JSLint doesn't like this. It now complains on Line 166 of src/data.js that data is already defined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+ 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 );
View
23 test/unit/data.js
@@ -184,14 +184,17 @@ test(".data(String) and .data(String, Object)", function() {
});
test("data-* attributes", function() {
- expect(27);
+ expect(33);
var div = jQuery("<div>"),
- child = jQuery("<div data-myobj='old data' data-ignored=\"DOM\"></div>");
+ child = jQuery("<div data-myobj='old data' data-ignored=\"DOM\" data-other='test'></div>");
equals( div.data("attr"), undefined, "Check for non-existing data-attr attribute" );
div.attr("data-attr", "exists");
equals( div.data("attr"), "exists", "Check for existing data-attr attribute" );
+
+ div.attr("data-attr", "exists2");
+ equals( div.data("attr"), "exists", "Check that updates to data- don't update .data()" );
div.data("attr", "internal").attr("data-attr", "external");
equals( div.data("attr"), "internal", "Check for .data('attr') precedence (internal > external data-* attribute)" );
@@ -205,6 +208,22 @@ test("data-* attributes", function() {
child.data("ignored", "cache");
equals( child.data("ignored"), "cache", "Cached data used before DOM data-* fallback");
+ var obj = child.data(), check = [ "myobj", "ignored", "other" ], num = 0;
+
+ for ( var i = 0, l = check.length; i < l; i++ ) {
+ ok( obj[ check[i] ], "Make sure data- property exists when calling data-." );
+ }
+
+ for ( var prop in obj ) {
+ num++;
+ }
+
+ equals( num, check.length, "Make sure that the right number of properties came through." );
+
+ child.attr("data-other", "newvalue");
+
+ equals( child.data("other"), "test", "Make sure value was pulled in properly from a .data()." );
+
child
.attr("data-true", "true")
.attr("data-false", "false")
Please sign in to comment.
Something went wrong with that request. Please try again.