Update cloneCopyEvent so that it does not create superfluous data objects when cloning elements. Exposes a new method, $.hasData. Fixes #7165. Thanks to DaveMethvin and iliakan for their help.

This commit is contained in:
Colin Snover 2010-12-22 18:19:30 -06:00
commit f28c774f2c
4 changed files with 64 additions and 25 deletions

View file

@ -9,7 +9,7 @@ jQuery.extend({
// Please use with caution // Please use with caution
uuid: 0, uuid: 0,
// Unique for each copy of jQuery on the page // Unique for each copy of jQuery on the page
expando: "jQuery" + jQuery.now(), expando: "jQuery" + jQuery.now(),
// The following elements throw uncatchable exceptions if you // The following elements throw uncatchable exceptions if you
@ -21,6 +21,14 @@ jQuery.extend({
"applet": true "applet": true
}, },
hasData: function( elem ) {
if ( elem.nodeType ) {
elem = jQuery.cache[ elem[jQuery.expando] ];
}
return !!elem && !jQuery.isEmptyObject(elem);
},
data: function( elem, name, data ) { data: function( elem, name, data ) {
if ( !jQuery.acceptData( elem ) ) { if ( !jQuery.acceptData( elem ) ) {
return; return;
@ -144,7 +152,7 @@ jQuery.fn.extend({
var attr = this[0].attributes, name; var attr = this[0].attributes, name;
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;
if ( name.indexOf( "data-" ) === 0 ) { if ( name.indexOf( "data-" ) === 0 ) {
name = name.substr( 5 ); name = name.substr( 5 );
dataAttr( this[0], name, data[ name ] ); dataAttr( this[0], name, data[ name ] );

View file

@ -370,14 +370,18 @@ function root( elem, cur ) {
} }
function cloneCopyEvent(orig, ret) { function cloneCopyEvent(orig, ret) {
var i = 0; ret.each(function (nodeIndex) {
if ( this.nodeType !== 1 || !jQuery.hasData(orig[nodeIndex]) ) {
ret.each(function() {
if ( this.nodeType !== 1 || this.nodeName !== (orig[i] && orig[i].nodeName) ) {
return; return;
} }
var oldData = jQuery.data( orig[i++] ), // XXX remove for 1.5 RC or merge back in if there is actually a reason for this check that has been
// unexposed by unit tests
if ( this.nodeName !== (orig[nodeIndex] && orig[nodeIndex].nodeName) ) {
throw "Cloned data mismatch";
}
var oldData = jQuery.data( orig[nodeIndex] ),
curData = jQuery.data( this, oldData ), curData = jQuery.data( this, oldData ),
events = oldData && oldData.events; events = oldData && oldData.events;

View file

@ -78,6 +78,21 @@ test("jQuery.data", function() {
ok( jQuery.data( window, "BAD" ), "Make sure that the value was set." ); ok( jQuery.data( window, "BAD" ), "Make sure that the value was set." );
}); });
test("jQuery.hasData", function() {
expect(6);
function testData(obj) {
equals( jQuery.hasData(obj), false, "No data exists" );
jQuery.data( obj, "foo", "bar" );
equals( jQuery.hasData(obj), true, "Data exists" );
jQuery.removeData( obj, "foo" );
equals( jQuery.hasData(obj), false, "Data was removed" );
}
testData(document.createElement('div'));
testData({});
});
test(".data()", function() { test(".data()", function() {
expect(5); expect(5);
@ -180,7 +195,7 @@ test(".data(String) and .data(String, Object)", function() {
equals( $elem.data('emptyString','').data('emptyString'), '', "Empty strings are preserved"); equals( $elem.data('emptyString','').data('emptyString'), '', "Empty strings are preserved");
equals( $elem.data('false',false).data('false'), false, "false's are preserved"); equals( $elem.data('false',false).data('false'), false, "false's are preserved");
equals( $elem.data('exists'), true, "Existing data is returned" ); equals( $elem.data('exists'), true, "Existing data is returned" );
// Clean up // Clean up
$elem.removeData(); $elem.removeData();
ok( jQuery.isEmptyObject( $elem[0] ), "removeData clears the object" ); ok( jQuery.isEmptyObject( $elem[0] ), "removeData clears the object" );
@ -191,7 +206,7 @@ test("data-* attributes", function() {
var div = jQuery("<div>"), var div = jQuery("<div>"),
child = jQuery("<div data-myobj='old data' data-ignored=\"DOM\" data-other='test'></div>"), child = jQuery("<div data-myobj='old data' data-ignored=\"DOM\" data-other='test'></div>"),
dummy = jQuery("<div data-myobj='old data' data-ignored=\"DOM\" data-other='test'></div>"); dummy = 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" ); equals( div.data("attr"), undefined, "Check for non-existing data-attr attribute" );
div.attr("data-attr", "exists"); div.attr("data-attr", "exists");
@ -199,10 +214,10 @@ test("data-* attributes", function() {
div.attr("data-attr", "exists2"); div.attr("data-attr", "exists2");
equals( div.data("attr"), "exists", "Check that updates to data- don't update .data()" ); equals( div.data("attr"), "exists", "Check that updates to data- don't update .data()" );
div.data("attr", "internal").attr("data-attr", "external"); div.data("attr", "internal").attr("data-attr", "external");
equals( div.data("attr"), "internal", "Check for .data('attr') precedence (internal > external data-* attribute)" ); equals( div.data("attr"), "internal", "Check for .data('attr') precedence (internal > external data-* attribute)" );
child.appendTo('#main'); child.appendTo('#main');
equals( child.data("myobj"), "old data", "Value accessed from data-* attribute"); equals( child.data("myobj"), "old data", "Value accessed from data-* attribute");
@ -249,7 +264,7 @@ test("data-* attributes", function() {
.attr("data-space", " ") .attr("data-space", " ")
.attr("data-null", "null") .attr("data-null", "null")
.attr("data-string", "test"); .attr("data-string", "test");
strictEqual( child.data('true'), true, "Primitive true read from attribute"); strictEqual( child.data('true'), true, "Primitive true read from attribute");
strictEqual( child.data('false'), false, "Primitive false read from attribute"); strictEqual( child.data('false'), false, "Primitive false read from attribute");
strictEqual( child.data('five'), 5, "Primitive number read from attribute"); strictEqual( child.data('five'), 5, "Primitive number read from attribute");
@ -265,7 +280,7 @@ test("data-* attributes", function() {
strictEqual( child.data('string'), "test", "Typical string read from attribute"); strictEqual( child.data('string'), "test", "Typical string read from attribute");
child.remove(); child.remove();
// tests from metadata plugin // tests from metadata plugin
function testData(index, elem) { function testData(index, elem) {
switch (index) { switch (index) {
@ -289,10 +304,10 @@ test("data-* attributes", function() {
ok(false, ["Assertion failed on index ", index, ", with data ", data].join('')); ok(false, ["Assertion failed on index ", index, ", with data ", data].join(''));
} }
} }
var metadata = '<ol><li class="test test2" data-foo="bar" data-bar="baz" data-arr="[1,2]">Some stuff</li><li class="test test2" data-test="bar" data-bar="baz">Some stuff</li><li class="test test2" data-zoooo="bar" data-bar=\'{"test":"baz"}\'>Some stuff</li><li class="test test2" data-number=true data-stuff="[2,8]">Some stuff</li></ol>', var metadata = '<ol><li class="test test2" data-foo="bar" data-bar="baz" data-arr="[1,2]">Some stuff</li><li class="test test2" data-test="bar" data-bar="baz">Some stuff</li><li class="test test2" data-zoooo="bar" data-bar=\'{"test":"baz"}\'>Some stuff</li><li class="test test2" data-number=true data-stuff="[2,8]">Some stuff</li></ol>',
elem = jQuery(metadata).appendTo('#main'); elem = jQuery(metadata).appendTo('#main');
elem.find("li").each(testData); elem.find("li").each(testData);
elem.remove(); elem.remove();
}); });
@ -305,12 +320,12 @@ test(".data(Object)", function() {
div.data({ "test": "in", "test2": "in2" }); div.data({ "test": "in", "test2": "in2" });
equals( div.data("test"), "in", "Verify setting an object in data" ); equals( div.data("test"), "in", "Verify setting an object in data" );
equals( div.data("test2"), "in2", "Verify setting an object in data" ); equals( div.data("test2"), "in2", "Verify setting an object in data" );
var obj = {test:"unset"}, var obj = {test:"unset"},
jqobj = jQuery(obj); jqobj = jQuery(obj);
jqobj.data({ "test": "in", "test2": "in2" }); jqobj.data({ "test": "in", "test2": "in2" });
equals( obj.test, "in", "Verify setting an object on an object extends the object" ); equals( obj.test, "in", "Verify setting an object on an object extends the object" );
equals( obj.test2, "in2", "Verify setting an object on an object extends the object" ); equals( obj.test2, "in2", "Verify setting an object on an object extends the object" );
}); });
test("jQuery.removeData", function() { test("jQuery.removeData", function() {
@ -324,13 +339,13 @@ test("jQuery.removeData", function() {
jQuery.removeData( div ); jQuery.removeData( div );
ok( !jQuery.data(div, "test2"), "Make sure that the data property no longer exists." ); ok( !jQuery.data(div, "test2"), "Make sure that the data property no longer exists." );
ok( !div[ jQuery.expando ], "Make sure the expando no longer exists, as well." ); ok( !div[ jQuery.expando ], "Make sure the expando no longer exists, as well." );
var obj = {}; var obj = {};
jQuery.data(obj, "test", "testing"); jQuery.data(obj, "test", "testing");
equals( obj.test, "testing", "verify data on plain object"); equals( obj.test, "testing", "verify data on plain object");
jQuery.removeData(obj, "test"); jQuery.removeData(obj, "test");
equals( jQuery.data(obj, "test"), undefined, "Check removal of data on plain object" ); equals( jQuery.data(obj, "test"), undefined, "Check removal of data on plain object" );
equals( obj.test, undefined, "Check removal of data directly from plain object" ); equals( obj.test, undefined, "Check removal of data directly from plain object" );
jQuery.data( window, "BAD", true ); jQuery.data( window, "BAD", true );
jQuery.removeData( window, "BAD" ); jQuery.removeData( window, "BAD" );

View file

@ -51,7 +51,7 @@ test("text(Function) with incoming value", function() {
}); });
var testWrap = function(val) { var testWrap = function(val) {
expect(18); expect(19);
var defaultText = 'Try them out:' var defaultText = 'Try them out:'
var result = jQuery('#first').wrap(val( '<div class="red"><span></span></div>' )).text(); var result = jQuery('#first').wrap(val( '<div class="red"><span></span></div>' )).text();
equals( defaultText, result, 'Check for wrapping of on-the-fly html' ); equals( defaultText, result, 'Check for wrapping of on-the-fly html' );
@ -80,10 +80,20 @@ var testWrap = function(val) {
equals( jQuery("#nonnodes > i").text(), j.text(), "Check node,textnode,comment wraps doesn't hurt text" ); equals( jQuery("#nonnodes > i").text(), j.text(), "Check node,textnode,comment wraps doesn't hurt text" );
// Try wrapping a disconnected node // Try wrapping a disconnected node
var cacheLength = 0;
for (var i in jQuery.cache) {
cacheLength++;
}
j = jQuery("<label/>").wrap(val( "<li/>" )); j = jQuery("<label/>").wrap(val( "<li/>" ));
equals( j[0].nodeName.toUpperCase(), "LABEL", "Element is a label" ); equals( j[0].nodeName.toUpperCase(), "LABEL", "Element is a label" );
equals( j[0].parentNode.nodeName.toUpperCase(), "LI", "Element has been wrapped" ); equals( j[0].parentNode.nodeName.toUpperCase(), "LI", "Element has been wrapped" );
for (i in jQuery.cache) {
cacheLength--;
}
equals(cacheLength, 0, "No memory leak in jQuery.cache (bug #7165)");
// Wrap an element containing a text node // Wrap an element containing a text node
j = jQuery("<span/>").wrap("<div>test</div>"); j = jQuery("<span/>").wrap("<div>test</div>");
equals( j[0].previousSibling.nodeType, 3, "Make sure the previous node is a text element" ); equals( j[0].previousSibling.nodeType, 3, "Make sure the previous node is a text element" );
@ -856,7 +866,7 @@ test("replaceAll(String|Element|Array&lt;Element&gt;|jQuery)", function() {
}); });
test("clone()", function() { test("clone()", function() {
expect(36); expect(37);
equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Assert text for #en' ); equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Assert text for #en' );
var clone = jQuery('#yahoo').clone(); var clone = jQuery('#yahoo').clone();
equals( 'Try them out:Yahoo', jQuery('#first').append(clone).text(), 'Check for clone' ); equals( 'Try them out:Yahoo', jQuery('#first').append(clone).text(), 'Check for clone' );
@ -914,10 +924,12 @@ test("clone()", function() {
equals( clone.html(), div.html(), "Element contents cloned" ); equals( clone.html(), div.html(), "Element contents cloned" );
equals( clone[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" ); equals( clone[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
div = jQuery("<div/>").data({ a: true, b: true }); div = jQuery("<div/>").data({ a: true });
div = div.clone(true); var div2 = div.clone(true);
equals( div.data("a"), true, "Data cloned." ); equals( div2.data("a"), true, "Data cloned." );
equals( div.data("b"), true, "Data cloned." ); div2.data("a", false);
equals( div2.data("a"), false, "Ensure cloned element data object was correctly modified" );
equals( div.data("a"), true, "Ensure cloned element data object is copied, not referenced" );
var form = document.createElement("form"); var form = document.createElement("form");
form.action = "/test/"; form.action = "/test/";