Don't use for-in loops on Arrays. Fixes #7817. Thanks to dmethvin.
authorColin Snover <github.com@zetafleet.com>
Thu, 23 Dec 2010 00:31:33 +0000 (18:31 -0600)
committerColin Snover <github.com@zetafleet.com>
Thu, 23 Dec 2010 00:32:33 +0000 (18:32 -0600)
Conflicts:
src/manipulation.js

src/data.js
src/manipulation.js
test/unit/data.js
test/unit/manipulation.js

index f1e031f..21b7543 100644 (file)
@@ -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 ] );
index 9d70a7b..5f4b15d 100644 (file)
@@ -370,14 +370,18 @@ function root( elem, cur ) {
 }
 
 function cloneCopyEvent(orig, ret) {
-       var node = 0;
-
-       ret.each(function() {
-               if ( this.nodeType !== 1 || this.nodeName !== (orig[node] && orig[node].nodeName) ) {
+       ret.each(function (nodeIndex) {
+               if ( this.nodeType !== 1 || !jQuery.hasData(orig[nodeIndex]) ) {
                        return;
                }
 
-               var oldData = jQuery.data( orig[node++] ),
+               // 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;
 
index 1a0f84c..310cd6b 100644 (file)
@@ -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("<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");
@@ -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 = '<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" );
index cbc0b77..da16354 100644 (file)
@@ -54,7 +54,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' );
@@ -83,10 +83,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" );
@@ -859,7 +869,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' );
@@ -917,10 +927,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/";