Skip to content
Permalink
Browse files

Update cloneCopyEvent so that it does not create superfluous data obj…

…ects when cloning elements. Exposes a new method, $.hasData. Fixes #7165. Thanks to DaveMethvin and iliakan for their help.
  • Loading branch information...
csnover committed Dec 23, 2010
2 parents 291b1ed + a2bf7ab commit f28c774f2cde004a33aefc28306d995c599d963b
Showing with 64 additions and 25 deletions.
  1. +10 −2 src/data.js
  2. +9 −5 src/manipulation.js
  3. +27 −12 test/unit/data.js
  4. +18 −6 test/unit/manipulation.js
@@ -9,7 +9,7 @@ jQuery.extend({
// Please use with caution
uuid: 0,

// Unique for each copy of jQuery on the page
// Unique for each copy of jQuery on the page
expando: "jQuery" + jQuery.now(),

// The following elements throw uncatchable exceptions if you
@@ -21,6 +21,14 @@ jQuery.extend({
"applet": true
},

hasData: function( elem ) {
if ( elem.nodeType ) {
elem = jQuery.cache[ elem[jQuery.expando] ];
}

return !!elem && !jQuery.isEmptyObject(elem);
},

data: function( elem, name, data ) {
if ( !jQuery.acceptData( elem ) ) {
return;
@@ -144,7 +152,7 @@ jQuery.fn.extend({
var attr = this[0].attributes, name;
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 ] );
@@ -370,14 +370,18 @@ function root( elem, cur ) {
}

function cloneCopyEvent(orig, ret) {
var i = 0;

ret.each(function() {
if ( this.nodeType !== 1 || this.nodeName !== (orig[i] && orig[i].nodeName) ) {
ret.each(function (nodeIndex) {
if ( this.nodeType !== 1 || !jQuery.hasData(orig[nodeIndex]) ) {
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 ),
events = oldData && oldData.events;

@@ -78,6 +78,21 @@ test("jQuery.data", function() {
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() {
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('false',false).data('false'), false, "false's are preserved");
equals( $elem.data('exists'), true, "Existing data is returned" );

// Clean up
$elem.removeData();
ok( jQuery.isEmptyObject( $elem[0] ), "removeData clears the object" );
@@ -191,18 +206,18 @@ test("data-* attributes", function() {
var div = jQuery("<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>");

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)" );

child.appendTo('#main');
equals( child.data("myobj"), "old data", "Value accessed from data-* attribute");

@@ -249,7 +264,7 @@ test("data-* attributes", function() {
.attr("data-space", " ")
.attr("data-null", "null")
.attr("data-string", "test");

strictEqual( child.data('true'), true, "Primitive true read from attribute");
strictEqual( child.data('false'), false, "Primitive false 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");

child.remove();

// tests from metadata plugin
function testData(index, elem) {
switch (index) {
@@ -289,10 +304,10 @@ test("data-* attributes", function() {
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>',
elem = jQuery(metadata).appendTo('#main');

elem.find("li").each(testData);
elem.remove();
});
@@ -305,12 +320,12 @@ test(".data(Object)", function() {
div.data({ "test": "in", "test2": "in2" });
equals( div.data("test"), "in", "Verify setting an object in data" );
equals( div.data("test2"), "in2", "Verify setting an object in data" );

var obj = {test:"unset"},
jqobj = jQuery(obj);
jqobj.data({ "test": "in", "test2": "in2" });
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() {
@@ -324,13 +339,13 @@ test("jQuery.removeData", function() {
jQuery.removeData( div );
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." );

var obj = {};
jQuery.data(obj, "test", "testing");
equals( obj.test, "testing", "verify data on plain object");
jQuery.removeData(obj, "test");
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.removeData( window, "BAD" );
@@ -51,7 +51,7 @@ test("text(Function) with incoming value", function() {
});

var testWrap = function(val) {
expect(18);
expect(19);
var defaultText = 'Try them out:'
var result = jQuery('#first').wrap(val( '<div class="red"><span></span></div>' )).text();
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" );

// Try wrapping a disconnected node
var cacheLength = 0;
for (var i in jQuery.cache) {
cacheLength++;
}

j = jQuery("<label/>").wrap(val( "<li/>" ));
equals( j[0].nodeName.toUpperCase(), "LABEL", "Element is a label" );
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
j = jQuery("<span/>").wrap("<div>test</div>");
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() {
expect(36);
expect(37);
equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Assert text for #en' );
var clone = jQuery('#yahoo').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[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );

div = jQuery("<div/>").data({ a: true, b: true });
div = div.clone(true);
equals( div.data("a"), true, "Data cloned." );
equals( div.data("b"), true, "Data cloned." );
div = jQuery("<div/>").data({ a: true });
var div2 = div.clone(true);
equals( div2.data("a"), 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");
form.action = "/test/";

0 comments on commit f28c774

Please sign in to comment.
You can’t perform that action at this time.