jQuery.fn.extend({
data: function( key, value ) {
- var data = null;
+ var parts, attr, name,
+ data = null;
if ( typeof key === "undefined" ) {
if ( this.length ) {
data = jQuery.data( this[0] );
- if ( this[0].nodeType === 1 ) {
- var attr = this[0].attributes, name;
+ if ( this[0].nodeType === 1 && !jQuery._data( this[0], "parsedAttrs" ) ) {
+ attr = this[0].attributes;
for ( var i = 0, l = attr.length; i < l; i++ ) {
name = attr[i].name;
dataAttr( this[0], name, data[ name ] );
}
}
+ jQuery._data( this[0], "parsedAttrs", true );
}
}
});
}
- var parts = key.split(".");
+ parts = key.split(".");
parts[1] = parts[1] ? "." + parts[1] : "";
if ( value === undefined ) {
// change the url to trigger unload
frame.attr("src", "data/iframe.html?param=true");
});
+
+test( "Only check element attributes once when calling .data() - #8909", function() {
+ expect( 2 );
+ var testing = {
+ test: "testing",
+ test2: "testing"
+ },
+ element = jQuery( "<div data-test='testing'>" ),
+ node = element[ 0 ];
+
+ // set an attribute using attr to ensure it
+ node.setAttribute( "data-test2", "testing" );
+ deepEqual( element.data(), testing, "Sanity Check" );
+
+ node.setAttribute( "data-test3", "testing" );
+ deepEqual( element.data(), testing, "The data didn't change even though the data-* attrs did" );
+
+ // clean up data cache
+ element.remove();
+
+});