Merge branch '8017lint' of https://github.com/rwldrn/jquery into 8017lint
[jquery.git] / test / unit / manipulation.js
index d49029e..739868b 100644 (file)
@@ -1,4 +1,7 @@
-module("manipulation");
+module("manipulation", { teardown: moduleTeardown });
+
+// Ensure that an extended Array prototype doesn't break jQuery
+Array.prototype.arrayProtoFn = function(arg) { throw("arrayProtoFn should not be called"); };
 
 var bareObj = function(value) { return value; };
 var functionReturningObj = function(value) { return (function() { return value; }); };
@@ -51,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' );
@@ -80,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" );
@@ -102,12 +115,19 @@ var testWrap = function(val) {
        // Wrap an element with a jQuery set and event
        result = jQuery("<div></div>").click(function(){
                ok(true, "Event triggered.");
+
+               // Remove handlers on detached elements
+               result.unbind();
+               jQuery(this).unbind();
        });
 
        j = jQuery("<span/>").wrap(result);
        equals( j[0].parentNode.nodeName.toLowerCase(), "div", "Wrapping works." );
 
        j.parent().trigger("click");
+
+       // clean up attached elements
+       QUnit.reset();
 }
 
 test("wrap(String|Element)", function() {
@@ -382,7 +402,7 @@ test("append(Function) with incoming value", function() {
 });
 
 test("append the same fragment with events (Bug #6997, 5566)", function () {
-       expect(4 + (document.fireEvent ? 1 : 0));
+       expect(2 + (document.fireEvent ? 1 : 0));
        stop(1000);
 
        var element;
@@ -395,8 +415,12 @@ test("append the same fragment with events (Bug #6997, 5566)", function () {
                        ok(true, "Event exists on original after being unbound on clone");
                        jQuery(this).unbind('click');
                });
-               element.clone(true).unbind('click')[0].fireEvent('onclick');
+               var clone = element.clone(true).unbind('click');
+               clone[0].fireEvent('onclick');
                element[0].fireEvent('onclick');
+
+               // manually clean up detached elements
+               clone.remove();
        }
 
        element = jQuery("<a class='test6997'></a>").click(function () {
@@ -413,14 +437,6 @@ test("append the same fragment with events (Bug #6997, 5566)", function () {
 
        jQuery("#listWithTabIndex li").before(element);
        jQuery("#listWithTabIndex li.test6997").eq(1).click();
-
-       element = jQuery("<select><option>Foo</option><option selected>Bar</option></select>");
-
-       equals( element.clone().find("option:selected").val(), element.find("option:selected").val(), "Selected option cloned correctly" );
-
-       element = jQuery("<input type='checkbox'>").attr('checked', 'checked');
-
-       equals( element.clone().is(":checked"), element.is(":checked"), "Checked input cloned correctly" );
 });
 
 test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
@@ -855,8 +871,20 @@ test("replaceAll(String|Element|Array&lt;Element&gt;|jQuery)", function() {
        ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
 });
 
+test("jQuery.clone() (#8017)", function() {
+
+       expect(2);
+
+       ok( jQuery.clone && jQuery.isFunction( jQuery.clone ) , "jQuery.clone() utility exists and is a function.");
+
+       var main = jQuery("#main")[0],
+                       clone = jQuery.clone( main );
+
+       equals( main.childNodes.length, clone.childNodes.length, "Simple child length to ensure a large dom tree copies correctly" );
+});
+
 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' );
@@ -881,20 +909,36 @@ test("clone()", function() {
                ok( true, "Bound event still exists." );
        });
 
-       div = div.clone(true).clone(true);
+       clone = div.clone(true);
+
+       // manually clean up detached elements
+       div.remove();
+
+       div = clone.clone(true);
+
+       // manually clean up detached elements
+       clone.remove();
+
        equals( div.length, 1, "One element cloned" );
        equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
        div.trigger("click");
 
+       // manually clean up detached elements
+       div.remove();
+
        div = jQuery("<div/>").append([ document.createElement("table"), document.createElement("table") ]);
        div.find("table").click(function(){
                ok( true, "Bound event still exists." );
        });
 
-       div = div.clone(true);
-       equals( div.length, 1, "One element cloned" );
-       equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
-       div.find("table:last").trigger("click");
+       clone = div.clone(true);
+       equals( clone.length, 1, "One element cloned" );
+       equals( clone[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
+       clone.find("table:last").trigger("click");
+
+       // manually clean up detached elements
+       div.remove();
+       clone.remove();
 
        // this is technically an invalid object, but because of the special
        // classid instantiation it is the only kind that IE has trouble with,
@@ -914,10 +958,16 @@ 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 });
+       clone = div.clone(true);
+       equals( clone.data("a"), true, "Data cloned." );
+       clone.data("a", false);
+       equals( clone.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" );
+
+       // manually clean up detached elements
+       div.remove();
+       clone.remove();
 
        var form = document.createElement("form");
        form.action = "/test/";
@@ -930,6 +980,28 @@ test("clone()", function() {
        equal( jQuery("body").clone().children()[0].id, "qunit-header", "Make sure cloning body works" );
 });
 
+test("clone(form element) (Bug #3879, #6655)", function() {
+       expect(6);
+       element = jQuery("<select><option>Foo</option><option selected>Bar</option></select>");
+
+       equals( element.clone().find("option:selected").val(), element.find("option:selected").val(), "Selected option cloned correctly" );
+
+       element = jQuery("<input type='checkbox' value='foo'>").attr('checked', 'checked');
+       clone = element.clone();
+
+       equals( clone.is(":checked"), element.is(":checked"), "Checked input cloned correctly" );
+       equals( clone[0].defaultValue, "foo", "Checked input defaultValue cloned correctly" );
+       equals( clone[0].defaultChecked, !jQuery.support.noCloneEvent, "Checked input defaultChecked cloned correctly" );
+
+       element = jQuery("<input type='text' value='foo'>");
+       clone = element.clone();
+       equals( clone[0].defaultValue, "foo", "Text input defaultValue cloned correctly" );
+
+       element = jQuery("<textarea>foo</textarea>");
+       clone = element.clone();
+       equals( clone[0].defaultValue, "foo", "Textarea defaultValue cloned correctly" );
+});
+
 if (!isLocal) {
 test("clone() on XML nodes", function() {
        expect(2);
@@ -1126,15 +1198,21 @@ var testRemove = function(method) {
        jQuery("#nonnodes").contents()[method]();
        equals( jQuery("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" );
 
+       // manually clean up detached elements
+       if (method === "detach") {
+               first.remove();
+       }
+
        QUnit.reset();
 
        var count = 0;
        var first = jQuery("#ap").children(":first");
-       var cleanUp = first.click(function() { count++ })[method]().appendTo("body").click();
+       var cleanUp = first.click(function() { count++ })[method]().appendTo("#main").click();
 
        equals( method == "remove" ? 0 : 1, count );
 
-       cleanUp.detach();
+       // manually clean up detached elements
+       cleanUp.remove();
 };
 
 test("remove()", function() {
@@ -1232,3 +1310,20 @@ test("jQuery.cleanData", function() {
                return div;
        }
 });
+
+test("jQuery.buildFragment - no plain-text caching (Bug #6779)", function() {
+       expect(1);
+
+       // DOM manipulation fails if added text matches an Object method
+       var $f = jQuery( "<div />" ).appendTo( "#main" ),
+               bad = [ "start-", "toString", "hasOwnProperty", "append", "here&there!", "-end" ];
+
+       for ( var i=0; i < bad.length; i++ ) {
+               try {
+                       $f.append( bad[i] );
+               }
+               catch(e) {}
+       }
+    equals($f.text(), bad.join(''), "Cached strings that match Object properties");
+       $f.remove();
+});