diff --git a/src/data.js b/src/data.js
index f1e031ff..21b7543c 100644
--- a/src/data.js
+++ b/src/data.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 ] );
diff --git a/src/manipulation.js b/src/manipulation.js
index 7dea3493..3b9aa146 100644
--- a/src/manipulation.js
+++ b/src/manipulation.js
@@ -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;
diff --git a/test/unit/data.js b/test/unit/data.js
index 1a0f84c1..310cd6bc 100644
--- a/test/unit/data.js
+++ b/test/unit/data.js
@@ -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,7 +206,7 @@ test("data-* attributes", function() {
var div = jQuery("
"),
child = jQuery("
"),
dummy = jQuery("
");
-
+
equals( div.data("attr"), undefined, "Check for non-existing data-attr attribute" );
div.attr("data-attr", "exists");
@@ -199,10 +214,10 @@ test("data-* attributes", function() {
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 = '
- Some stuff
- Some stuff
- Some stuff
- Some stuff
',
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" );
diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js
index d49029eb..ba57a6f9 100644
--- a/test/unit/manipulation.js
+++ b/test/unit/manipulation.js
@@ -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( '
' )).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("
").wrap(val( "
" ));
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("
").wrap("
test
");
equals( j[0].previousSibling.nodeType, 3, "Make sure the previous node is a text element" );
@@ -856,7 +866,7 @@ test("replaceAll(String|Element|Array<Element>|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("
").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("
").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/";