Commit 2831cfd0 authored by Corey Frang's avatar Corey Frang Committed by timmywil

Landing pull request 503. 1.7 data: set a flag in the private data cache to...

Landing pull request 503. 1.7 data: set a flag in the private data cache to avoid having to scan attributes multiple times - Fixes #8909.

More Details:
 - https://github.com/jquery/jquery/pull/503
 - http://bugs.jquery.com/ticket/8909
parent d5f144a7
...@@ -231,14 +231,15 @@ jQuery.extend({ ...@@ -231,14 +231,15 @@ jQuery.extend({
jQuery.fn.extend({ jQuery.fn.extend({
data: function( key, value ) { data: function( key, value ) {
var data = null; var parts, attr, name,
data = null;
if ( typeof key === "undefined" ) { if ( typeof key === "undefined" ) {
if ( this.length ) { if ( this.length ) {
data = jQuery.data( this[0] ); data = jQuery.data( this[0] );
if ( this[0].nodeType === 1 ) { if ( this[0].nodeType === 1 && !jQuery._data( this[0], "parsedAttrs" ) ) {
var attr = this[0].attributes, name; attr = this[0].attributes;
for ( var i = 0, l = attr.length; i < l; i++ ) { for ( var i = 0, l = attr.length; i < l; i++ ) {
name = attr[i].name; name = attr[i].name;
...@@ -248,6 +249,7 @@ jQuery.fn.extend({ ...@@ -248,6 +249,7 @@ jQuery.fn.extend({
dataAttr( this[0], name, data[ name ] ); dataAttr( this[0], name, data[ name ] );
} }
} }
jQuery._data( this[0], "parsedAttrs", true );
} }
} }
...@@ -259,7 +261,7 @@ jQuery.fn.extend({ ...@@ -259,7 +261,7 @@ jQuery.fn.extend({
}); });
} }
var parts = key.split("."); parts = key.split(".");
parts[1] = parts[1] ? "." + parts[1] : ""; parts[1] = parts[1] ? "." + parts[1] : "";
if ( value === undefined ) { if ( value === undefined ) {
......
...@@ -599,3 +599,24 @@ test("Triggering the removeData should not throw exceptions. (#10080)", function ...@@ -599,3 +599,24 @@ test("Triggering the removeData should not throw exceptions. (#10080)", function
// change the url to trigger unload // change the url to trigger unload
frame.attr("src", "data/iframe.html?param=true"); 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();
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment