Replace usage of the removed global reset() method with QUnit.reset().
[jquery.git] / test / unit / manipulation.js
1 module("manipulation");
2
3 var bareObj = function(value) { return value; };
4 var functionReturningObj = function(value) { return (function() { return value; }); };
5
6 test("text()", function() {
7         expect(2);
8         var expected = "This link has class=\"blog\": Simon Willison's Weblog";
9         equals( jQuery('#sap').text(), expected, 'Check for merged text of more then one element.' );
10
11         // Check serialization of text values
12         equals( jQuery(document.createTextNode("foo")).text(), "foo", "Text node was retreived from .text()." );
13 });
14
15 var testText = function(valueObj) {
16         expect(4);
17         var val = valueObj("<div><b>Hello</b> cruel world!</div>");
18         equals( jQuery("#foo").text(val)[0].innerHTML.replace(/>/g, "&gt;"), "&lt;div&gt;&lt;b&gt;Hello&lt;/b&gt; cruel world!&lt;/div&gt;", "Check escaped text" );
19
20         // using contents will get comments regular, text, and comment nodes
21         var j = jQuery("#nonnodes").contents();
22         j.text(valueObj("hi!"));
23         equals( jQuery(j[0]).text(), "hi!", "Check node,textnode,comment with text()" );
24         equals( j[1].nodeValue, " there ", "Check node,textnode,comment with text()" );
25         equals( j[2].nodeType, 8, "Check node,textnode,comment with text()" );
26 }
27
28 test("text(String)", function() {
29         testText(bareObj)
30 });
31
32 test("text(Function)", function() {
33         testText(functionReturningObj);
34 });
35
36 test("text(Function) with incoming value", function() {
37         expect(2);
38         
39         var old = "This link has class=\"blog\": Simon Willison's Weblog";
40         
41         jQuery('#sap').text(function(i, val) {
42                 equals( val, old, "Make sure the incoming value is correct." );
43                 return "foobar";
44         });
45         
46         equals( jQuery("#sap").text(), "foobar", 'Check for merged text of more then one element.' );
47         
48         QUnit.reset();
49 });
50
51 var testWrap = function(val) {
52         expect(18);
53         var defaultText = 'Try them out:'
54         var result = jQuery('#first').wrap(val( '<div class="red"><span></span></div>' )).text();
55         equals( defaultText, result, 'Check for wrapping of on-the-fly html' );
56         ok( jQuery('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
57
58         QUnit.reset();
59         var defaultText = 'Try them out:'
60         var result = jQuery('#first').wrap(val( document.getElementById('empty') )).parent();
61         ok( result.is('ol'), 'Check for element wrapping' );
62         equals( result.text(), defaultText, 'Check for element wrapping' );
63
64         QUnit.reset();
65         jQuery('#check1').click(function() {
66                 var checkbox = this;
67                 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
68                 jQuery(checkbox).wrap(val( '<div id="c1" style="display:none;"></div>' ));
69                 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
70         }).click();
71
72         // using contents will get comments regular, text, and comment nodes
73         var j = jQuery("#nonnodes").contents();
74         j.wrap(val( "<i></i>" ));
75         equals( jQuery("#nonnodes > i").length, 3, "Check node,textnode,comment wraps ok" );
76         equals( jQuery("#nonnodes > i").text(), j.text(), "Check node,textnode,comment wraps doesn't hurt text" );
77
78         // Try wrapping a disconnected node
79         j = jQuery("<label/>").wrap(val( "<li/>" ));
80         equals( j[0].nodeName.toUpperCase(), "LABEL", "Element is a label" );
81         equals( j[0].parentNode.nodeName.toUpperCase(), "LI", "Element has been wrapped" );
82
83         // Wrap an element containing a text node
84         j = jQuery("<span/>").wrap("<div>test</div>");
85         equals( j[0].previousSibling.nodeType, 3, "Make sure the previous node is a text element" );
86         equals( j[0].parentNode.nodeName.toUpperCase(), "DIV", "And that we're in the div element." );
87
88         // Try to wrap an element with multiple elements (should fail)
89         j = jQuery("<div><span></span></div>").children().wrap("<p></p><div></div>");
90         equals( j[0].parentNode.parentNode.childNodes.length, 1, "There should only be one element wrapping." );
91         equals( j.length, 1, "There should only be one element (no cloning)." );
92         equals( j[0].parentNode.nodeName.toUpperCase(), "P", "The span should be in the paragraph." );
93
94         // Wrap an element with a jQuery set
95         j = jQuery("<span/>").wrap(jQuery("<div></div>"));
96         equals( j[0].parentNode.nodeName.toLowerCase(), "div", "Wrapping works." );
97
98         // Wrap an element with a jQuery set and event
99         result = jQuery("<div></div>").click(function(){
100                 ok(true, "Event triggered.");
101         });
102
103         j = jQuery("<span/>").wrap(result);
104         equals( j[0].parentNode.nodeName.toLowerCase(), "div", "Wrapping works." );
105
106         j.parent().trigger("click");
107 }
108
109 test("wrap(String|Element)", function() {
110         testWrap(bareObj);
111 });
112
113 test("wrap(Function)", function() {
114         testWrap(functionReturningObj);
115 })
116
117 var testWrapAll = function(val) {
118         expect(8);
119         var prev = jQuery("#firstp")[0].previousSibling;
120         var p = jQuery("#firstp,#first")[0].parentNode;
121
122         var result = jQuery('#firstp,#first').wrapAll(val( '<div class="red"><div class="tmp"></div></div>' ));
123         equals( result.parent().length, 1, 'Check for wrapping of on-the-fly html' );
124         ok( jQuery('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
125         ok( jQuery('#firstp').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
126         equals( jQuery("#first").parent().parent()[0].previousSibling, prev, "Correct Previous Sibling" );
127         equals( jQuery("#first").parent().parent()[0].parentNode, p, "Correct Parent" );
128
129         QUnit.reset();
130         var prev = jQuery("#firstp")[0].previousSibling;
131         var p = jQuery("#first")[0].parentNode;
132         var result = jQuery('#firstp,#first').wrapAll(val( document.getElementById('empty') ));
133         equals( jQuery("#first").parent()[0], jQuery("#firstp").parent()[0], "Same Parent" );
134         equals( jQuery("#first").parent()[0].previousSibling, prev, "Correct Previous Sibling" );
135         equals( jQuery("#first").parent()[0].parentNode, p, "Correct Parent" );
136 }
137
138 test("wrapAll(String|Element)", function() {
139         testWrapAll(bareObj);
140 });
141
142 var testWrapInner = function(val) {
143         expect(11);
144         var num = jQuery("#first").children().length;
145         var result = jQuery('#first').wrapInner(val('<div class="red"><div id="tmp"></div></div>'));
146         equals( jQuery("#first").children().length, 1, "Only one child" );
147         ok( jQuery("#first").children().is(".red"), "Verify Right Element" );
148         equals( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" );
149
150         QUnit.reset();
151         var num = jQuery("#first").html("foo<div>test</div><div>test2</div>").children().length;
152         var result = jQuery('#first').wrapInner(val('<div class="red"><div id="tmp"></div></div>'));
153         equals( jQuery("#first").children().length, 1, "Only one child" );
154         ok( jQuery("#first").children().is(".red"), "Verify Right Element" );
155         equals( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" );
156
157         QUnit.reset();
158         var num = jQuery("#first").children().length;
159         var result = jQuery('#first').wrapInner(val(document.getElementById('empty')));
160         equals( jQuery("#first").children().length, 1, "Only one child" );
161         ok( jQuery("#first").children().is("#empty"), "Verify Right Element" );
162         equals( jQuery("#first").children().children().length, num, "Verify Elements Intact" );
163
164         var div = jQuery("<div/>");
165         div.wrapInner(val("<span></span>"));
166         equals(div.children().length, 1, "The contents were wrapped.");
167         equals(div.children()[0].nodeName.toLowerCase(), "span", "A span was inserted.");
168 }
169
170 test("wrapInner(String|Element)", function() {
171         testWrapInner(bareObj);
172 });
173
174 test("wrapInner(Function)", function() {
175         testWrapInner(functionReturningObj)
176 });
177
178 test("unwrap()", function() {
179         expect(9);
180
181         jQuery("body").append('  <div id="unwrap" style="display: none;"> <div id="unwrap1"> <span class="unwrap">a</span> <span class="unwrap">b</span> </div> <div id="unwrap2"> <span class="unwrap">c</span> <span class="unwrap">d</span> </div> <div id="unwrap3"> <b><span class="unwrap unwrap3">e</span></b> <b><span class="unwrap unwrap3">f</span></b> </div> </div>');
182
183         var abcd = jQuery('#unwrap1 > span, #unwrap2 > span').get(),
184                 abcdef = jQuery('#unwrap span').get();
185
186         equals( jQuery('#unwrap1 span').add('#unwrap2 span:first').unwrap().length, 3, 'make #unwrap1 and #unwrap2 go away' );
187         same( jQuery('#unwrap > span').get(), abcd, 'all four spans should still exist' );
188
189         same( jQuery('#unwrap3 span').unwrap().get(), jQuery('#unwrap3 > span').get(), 'make all b in #unwrap3 go away' );
190
191         same( jQuery('#unwrap3 span').unwrap().get(), jQuery('#unwrap > span.unwrap3').get(), 'make #unwrap3 go away' );
192
193         same( jQuery('#unwrap').children().get(), abcdef, '#unwrap only contains 6 child spans' );
194
195         same( jQuery('#unwrap > span').unwrap().get(), jQuery('body > span.unwrap').get(), 'make the 6 spans become children of body' );
196
197         same( jQuery('body > span.unwrap').unwrap().get(), jQuery('body > span.unwrap').get(), 'can\'t unwrap children of body' );
198         same( jQuery('body > span.unwrap').unwrap().get(), abcdef, 'can\'t unwrap children of body' );
199
200         same( jQuery('body > span.unwrap').get(), abcdef, 'body contains 6 .unwrap child spans' );
201
202         jQuery('body > span.unwrap').remove();
203 });
204
205 var testAppend = function(valueObj) {
206         expect(37);
207         var defaultText = 'Try them out:'
208         var result = jQuery('#first').append(valueObj('<b>buga</b>'));
209         equals( result.text(), defaultText + 'buga', 'Check if text appending works' );
210         equals( jQuery('#select3').append(valueObj('<option value="appendTest">Append Test</option>')).find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
211
212         QUnit.reset();
213         var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
214         jQuery('#sap').append(valueObj(document.getElementById('first')));
215         equals( expected, jQuery('#sap').text(), "Check for appending of element" );
216
217         QUnit.reset();
218         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
219         jQuery('#sap').append(valueObj([document.getElementById('first'), document.getElementById('yahoo')]));
220         equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
221
222         QUnit.reset();
223         expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
224         jQuery('#sap').append(valueObj(jQuery("#first, #yahoo")));
225         equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
226
227         QUnit.reset();
228         jQuery("#sap").append(valueObj( 5 ));
229         ok( jQuery("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
230
231         QUnit.reset();
232         jQuery("#sap").append(valueObj( " text with spaces " ));
233         ok( jQuery("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );
234
235         QUnit.reset();
236         ok( jQuery("#sap").append(valueObj( [] )), "Check for appending an empty array." );
237         ok( jQuery("#sap").append(valueObj( "" )), "Check for appending an empty string." );
238         ok( jQuery("#sap").append(valueObj( document.getElementsByTagName("foo") )), "Check for appending an empty nodelist." );
239         
240         QUnit.reset();
241         jQuery("form").append(valueObj('<input name="radiotest" type="radio" checked="checked" />'));
242         jQuery("form input[name=radiotest]").each(function(){
243                 ok( jQuery(this).is(':checked'), "Append checked radio");
244         }).remove();
245
246         QUnit.reset();
247         jQuery("form").append(valueObj('<input name="radiotest" type="radio" checked    =   \'checked\' />'));
248         jQuery("form input[name=radiotest]").each(function(){
249                 ok( jQuery(this).is(':checked'), "Append alternately formated checked radio");
250         }).remove();
251
252         QUnit.reset();
253         jQuery("form").append(valueObj('<input name="radiotest" type="radio" checked />'));
254         jQuery("form input[name=radiotest]").each(function(){
255                 ok( jQuery(this).is(':checked'), "Append HTML5-formated checked radio");
256         }).remove();
257
258         QUnit.reset();
259         jQuery("#sap").append(valueObj( document.getElementById('form') ));
260         equals( jQuery("#sap>form").size(), 1, "Check for appending a form" ); // Bug #910
261
262         QUnit.reset();
263         var pass = true;
264         try {
265                 jQuery( jQuery("#iframe")[0].contentWindow.document.body ).append(valueObj( "<div>test</div>" ));
266         } catch(e) {
267                 pass = false;
268         }
269
270         ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
271
272         QUnit.reset();
273         jQuery('<fieldset/>').appendTo('#form').append(valueObj( '<legend id="legend">test</legend>' ));
274         t( 'Append legend', '#legend', ['legend'] );
275
276         QUnit.reset();
277         jQuery('#select1').append(valueObj( '<OPTION>Test</OPTION>' ));
278         equals( jQuery('#select1 option:last').text(), "Test", "Appending &lt;OPTION&gt; (all caps)" );
279
280         jQuery('#table').append(valueObj( '<colgroup></colgroup>' ));
281         ok( jQuery('#table colgroup').length, "Append colgroup" );
282
283         jQuery('#table colgroup').append(valueObj( '<col/>' ));
284         ok( jQuery('#table colgroup col').length, "Append col" );
285
286         QUnit.reset();
287         jQuery('#table').append(valueObj( '<caption></caption>' ));
288         ok( jQuery('#table caption').length, "Append caption" );
289
290         QUnit.reset();
291         jQuery('form:last')
292                 .append(valueObj( '<select id="appendSelect1"></select>' ))
293                 .append(valueObj( '<select id="appendSelect2"><option>Test</option></select>' ));
294
295         t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );
296
297         equals( "Two nodes", jQuery('<div />').append("Two", " nodes").text(), "Appending two text nodes (#4011)" );
298
299         // using contents will get comments regular, text, and comment nodes
300         var j = jQuery("#nonnodes").contents();
301         var d = jQuery("<div/>").appendTo("#nonnodes").append(j);
302         equals( jQuery("#nonnodes").length, 1, "Check node,textnode,comment append moved leaving just the div" );
303         ok( d.contents().length >= 2, "Check node,textnode,comment append works" );
304         d.contents().appendTo("#nonnodes");
305         d.remove();
306         ok( jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment append cleanup worked" );
307 }
308
309 test("append(String|Element|Array&lt;Element&gt;|jQuery)", function() {
310         testAppend(bareObj);
311 });
312
313 test("append(Function)", function() {
314         testAppend(functionReturningObj);
315 });
316
317 test("append(Function) with incoming value", function() {
318         expect(12);
319         
320         var defaultText = 'Try them out:', old = jQuery("#first").html();
321         
322         var result = jQuery('#first').append(function(i, val){
323                 equals( val, old, "Make sure the incoming value is correct." );
324                 return '<b>buga</b>';
325         });
326         equals( result.text(), defaultText + 'buga', 'Check if text appending works' );
327         
328         var select = jQuery('#select3');
329         old = select.html();
330         
331         equals( select.append(function(i, val){
332                 equals( val, old, "Make sure the incoming value is correct." );
333                 return '<option value="appendTest">Append Test</option>';
334         }).find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
335
336         QUnit.reset();
337         var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
338         old = jQuery("#sap").html();
339         
340         jQuery('#sap').append(function(i, val){
341                 equals( val, old, "Make sure the incoming value is correct." );
342                 return document.getElementById('first');
343         });
344         equals( expected, jQuery('#sap').text(), "Check for appending of element" );
345
346         QUnit.reset();
347         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
348         old = jQuery("#sap").html();
349         
350         jQuery('#sap').append(function(i, val){
351                 equals( val, old, "Make sure the incoming value is correct." );
352                 return [document.getElementById('first'), document.getElementById('yahoo')];
353         });
354         equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
355
356         QUnit.reset();
357         expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
358         old = jQuery("#sap").html();
359         
360         jQuery('#sap').append(function(i, val){
361                 equals( val, old, "Make sure the incoming value is correct." );
362                 return jQuery("#first, #yahoo");
363         });
364         equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
365
366         QUnit.reset();
367         old = jQuery("#sap").html();
368         
369         jQuery("#sap").append(function(i, val){
370                 equals( val, old, "Make sure the incoming value is correct." );
371                 return 5;
372         });
373         ok( jQuery("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );        
374         
375         QUnit.reset();
376 });
377
378 test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
379         expect(16);
380
381         var defaultText = 'Try them out:'
382         jQuery('<b>buga</b>').appendTo('#first');
383         equals( jQuery("#first").text(), defaultText + 'buga', 'Check if text appending works' );
384         equals( jQuery('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
385
386         QUnit.reset();
387         var l = jQuery("#first").children().length + 2;
388         jQuery("<strong>test</strong>");
389         jQuery("<strong>test</strong>");
390         jQuery([ jQuery("<strong>test</strong>")[0], jQuery("<strong>test</strong>")[0] ])
391                 .appendTo("#first");
392         equals( jQuery("#first").children().length, l, "Make sure the elements were inserted." );
393         equals( jQuery("#first").children().last()[0].nodeName.toLowerCase(), "strong", "Verify the last element." );
394
395         QUnit.reset();
396         var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
397         jQuery(document.getElementById('first')).appendTo('#sap');
398         equals( expected, jQuery('#sap').text(), "Check for appending of element" );
399
400         QUnit.reset();
401         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
402         jQuery([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
403         equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
404
405         QUnit.reset();
406         ok( jQuery(document.createElement("script")).appendTo("body").length, "Make sure a disconnected script can be appended." );
407
408         QUnit.reset();
409         expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
410         jQuery("#first, #yahoo").appendTo('#sap');
411         equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
412
413         QUnit.reset();
414         jQuery('#select1').appendTo('#foo');
415         t( 'Append select', '#foo select', ['select1'] );
416
417         QUnit.reset();
418         var div = jQuery("<div/>").click(function(){
419                 ok(true, "Running a cloned click.");
420         });
421         div.appendTo("#main, #moretests");
422
423         jQuery("#main div:last").click();
424         jQuery("#moretests div:last").click();
425
426         QUnit.reset();
427         var div = jQuery("<div/>").appendTo("#main, #moretests");
428
429         equals( div.length, 2, "appendTo returns the inserted elements" );
430
431         div.addClass("test");
432
433         ok( jQuery("#main div:last").hasClass("test"), "appendTo element was modified after the insertion" );
434         ok( jQuery("#moretests div:last").hasClass("test"), "appendTo element was modified after the insertion" );
435
436         QUnit.reset();
437
438         div = jQuery("<div/>");
439         jQuery("<span>a</span><b>b</b>").filter("span").appendTo( div );
440
441         equals( div.children().length, 1, "Make sure the right number of children were inserted." );
442
443         div = jQuery("#moretests div");
444
445         var num = jQuery("#main div").length;
446         div.remove().appendTo("#main");
447
448         equals( jQuery("#main div").length, num, "Make sure all the removed divs were inserted." );
449
450         QUnit.reset();
451 });
452
453 var testPrepend = function(val) {
454         expect(5);
455         var defaultText = 'Try them out:'
456         var result = jQuery('#first').prepend(val( '<b>buga</b>' ));
457         equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
458         equals( jQuery('#select3').prepend(val( '<option value="prependTest">Prepend Test</option>' )).find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
459
460         QUnit.reset();
461         var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
462         jQuery('#sap').prepend(val( document.getElementById('first') ));
463         equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
464
465         QUnit.reset();
466         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
467         jQuery('#sap').prepend(val( [document.getElementById('first'), document.getElementById('yahoo')] ));
468         equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
469
470         QUnit.reset();
471         expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
472         jQuery('#sap').prepend(val( jQuery("#first, #yahoo") ));
473         equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
474 };
475
476 test("prepend(String|Element|Array&lt;Element&gt;|jQuery)", function() {
477         testPrepend(bareObj);
478 });
479
480 test("prepend(Function)", function() {
481         testPrepend(functionReturningObj);
482 });
483
484 test("prepend(Function) with incoming value", function() {
485         expect(10);
486         
487         var defaultText = 'Try them out:', old = jQuery('#first').html();
488         var result = jQuery('#first').prepend(function(i, val) {
489                 equals( val, old, "Make sure the incoming value is correct." );
490                 return '<b>buga</b>';
491         });
492         equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
493         
494         old = jQuery("#select3").html();
495         
496         equals( jQuery('#select3').prepend(function(i, val) {
497                 equals( val, old, "Make sure the incoming value is correct." );
498                 return '<option value="prependTest">Prepend Test</option>';
499         }).find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
500
501         QUnit.reset();
502         var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
503         old = jQuery('#sap').html();
504         
505         jQuery('#sap').prepend(function(i, val) {
506                 equals( val, old, "Make sure the incoming value is correct." );
507                 return document.getElementById('first');
508         });
509         
510         equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
511
512         QUnit.reset();
513         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
514         old = jQuery('#sap').html();
515         
516         jQuery('#sap').prepend(function(i, val) {
517                 equals( val, old, "Make sure the incoming value is correct." );
518                 return [document.getElementById('first'), document.getElementById('yahoo')];
519         });
520         
521         equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
522
523         QUnit.reset();
524         expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
525         old = jQuery('#sap').html();
526         
527         jQuery('#sap').prepend(function(i, val) {
528                 equals( val, old, "Make sure the incoming value is correct." );
529                 return jQuery("#first, #yahoo");
530         });
531         
532         equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );     
533 });
534
535 test("prependTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
536         expect(6);
537         var defaultText = 'Try them out:'
538         jQuery('<b>buga</b>').prependTo('#first');
539         equals( jQuery('#first').text(), 'buga' + defaultText, 'Check if text prepending works' );
540         equals( jQuery('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
541
542         QUnit.reset();
543         var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
544         jQuery(document.getElementById('first')).prependTo('#sap');
545         equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
546
547         QUnit.reset();
548         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
549         jQuery([document.getElementById('first'), document.getElementById('yahoo')]).prependTo('#sap');
550         equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
551
552         QUnit.reset();
553         expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
554         jQuery("#first, #yahoo").prependTo('#sap');
555         equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
556
557         QUnit.reset();
558         jQuery('<select id="prependSelect1"></select>').prependTo('form:last');
559         jQuery('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');
560
561         t( "Prepend Select", "#prependSelect2, #prependSelect1", ["prependSelect2", "prependSelect1"] );
562 });
563
564 var testBefore = function(val) {
565         expect(6);
566         var expected = 'This is a normal link: bugaYahoo';
567         jQuery('#yahoo').before(val( '<b>buga</b>' ));
568         equals( expected, jQuery('#en').text(), 'Insert String before' );
569
570         QUnit.reset();
571         expected = "This is a normal link: Try them out:Yahoo";
572         jQuery('#yahoo').before(val( document.getElementById('first') ));
573         equals( expected, jQuery('#en').text(), "Insert element before" );
574
575         QUnit.reset();
576         expected = "This is a normal link: Try them out:diveintomarkYahoo";
577         jQuery('#yahoo').before(val( [document.getElementById('first'), document.getElementById('mark')] ));
578         equals( expected, jQuery('#en').text(), "Insert array of elements before" );
579
580         QUnit.reset();
581         expected = "This is a normal link: diveintomarkTry them out:Yahoo";
582         jQuery('#yahoo').before(val( jQuery("#first, #mark") ));
583         equals( expected, jQuery('#en').text(), "Insert jQuery before" );
584
585         var set = jQuery("<div/>").before("<span>test</span>");
586         equals( set[0].nodeName.toLowerCase(), "span", "Insert the element before the disconnected node." );
587         equals( set.length, 2, "Insert the element before the disconnected node." );
588 }
589
590 test("before(String|Element|Array&lt;Element&gt;|jQuery)", function() {
591         testBefore(bareObj);
592 });
593
594 test("before(Function)", function() {
595         testBefore(functionReturningObj);
596 })
597
598 test("insertBefore(String|Element|Array&lt;Element&gt;|jQuery)", function() {
599         expect(4);
600         var expected = 'This is a normal link: bugaYahoo';
601         jQuery('<b>buga</b>').insertBefore('#yahoo');
602         equals( expected, jQuery('#en').text(), 'Insert String before' );
603
604         QUnit.reset();
605         expected = "This is a normal link: Try them out:Yahoo";
606         jQuery(document.getElementById('first')).insertBefore('#yahoo');
607         equals( expected, jQuery('#en').text(), "Insert element before" );
608
609         QUnit.reset();
610         expected = "This is a normal link: Try them out:diveintomarkYahoo";
611         jQuery([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
612         equals( expected, jQuery('#en').text(), "Insert array of elements before" );
613
614         QUnit.reset();
615         expected = "This is a normal link: diveintomarkTry them out:Yahoo";
616         jQuery("#first, #mark").insertBefore('#yahoo');
617         equals( expected, jQuery('#en').text(), "Insert jQuery before" );
618 });
619
620 var testAfter = function(val) {
621         expect(6);
622         var expected = 'This is a normal link: Yahoobuga';
623         jQuery('#yahoo').after(val( '<b>buga</b>' ));
624         equals( expected, jQuery('#en').text(), 'Insert String after' );
625
626         QUnit.reset();
627         expected = "This is a normal link: YahooTry them out:";
628         jQuery('#yahoo').after(val( document.getElementById('first') ));
629         equals( expected, jQuery('#en').text(), "Insert element after" );
630
631         QUnit.reset();
632         expected = "This is a normal link: YahooTry them out:diveintomark";
633         jQuery('#yahoo').after(val( [document.getElementById('first'), document.getElementById('mark')] ));
634         equals( expected, jQuery('#en').text(), "Insert array of elements after" );
635
636         QUnit.reset();
637         expected = "This is a normal link: YahoodiveintomarkTry them out:";
638         jQuery('#yahoo').after(val( jQuery("#first, #mark") ));
639         equals( expected, jQuery('#en').text(), "Insert jQuery after" );
640
641         var set = jQuery("<div/>").after("<span>test</span>");
642         equals( set[1].nodeName.toLowerCase(), "span", "Insert the element after the disconnected node." );
643         equals( set.length, 2, "Insert the element after the disconnected node." );
644 };
645
646 test("after(String|Element|Array&lt;Element&gt;|jQuery)", function() {
647         testAfter(bareObj);
648 });
649
650 test("after(Function)", function() {
651         testAfter(functionReturningObj);
652 })
653
654 test("insertAfter(String|Element|Array&lt;Element&gt;|jQuery)", function() {
655         expect(4);
656         var expected = 'This is a normal link: Yahoobuga';
657         jQuery('<b>buga</b>').insertAfter('#yahoo');
658         equals( expected, jQuery('#en').text(), 'Insert String after' );
659
660         QUnit.reset();
661         expected = "This is a normal link: YahooTry them out:";
662         jQuery(document.getElementById('first')).insertAfter('#yahoo');
663         equals( expected, jQuery('#en').text(), "Insert element after" );
664
665         QUnit.reset();
666         expected = "This is a normal link: YahooTry them out:diveintomark";
667         jQuery([document.getElementById('first'), document.getElementById('mark')]).insertAfter('#yahoo');
668         equals( expected, jQuery('#en').text(), "Insert array of elements after" );
669
670         QUnit.reset();
671         expected = "This is a normal link: YahoodiveintomarkTry them out:";
672         jQuery("#first, #mark").insertAfter('#yahoo');
673         equals( expected, jQuery('#en').text(), "Insert jQuery after" );
674 });
675
676 var testReplaceWith = function(val) {
677         expect(17);
678         jQuery('#yahoo').replaceWith(val( '<b id="replace">buga</b>' ));
679         ok( jQuery("#replace")[0], 'Replace element with string' );
680         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
681
682         QUnit.reset();
683         jQuery('#yahoo').replaceWith(val( document.getElementById('first') ));
684         ok( jQuery("#first")[0], 'Replace element with element' );
685         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
686
687         QUnit.reset();
688         jQuery("#main").append('<div id="bar"><div id="baz">Foo</div></div>');
689         jQuery('#baz').replaceWith("Baz");
690         equals( jQuery("#bar").text(),"Baz", 'Replace element with text' );
691         ok( !jQuery("#baz")[0], 'Verify that original element is gone, after element' );
692
693         QUnit.reset();
694         jQuery('#yahoo').replaceWith(val( [document.getElementById('first'), document.getElementById('mark')] ));
695         ok( jQuery("#first")[0], 'Replace element with array of elements' );
696         ok( jQuery("#mark")[0], 'Replace element with array of elements' );
697         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
698
699         QUnit.reset();
700         jQuery('#yahoo').replaceWith(val( jQuery("#first, #mark") ));
701         ok( jQuery("#first")[0], 'Replace element with set of elements' );
702         ok( jQuery("#mark")[0], 'Replace element with set of elements' );
703         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
704
705         QUnit.reset();
706         var tmp = jQuery("<div/>").appendTo("body").click(function(){ ok(true, "Newly bound click run." ); });
707         var y = jQuery('<div/>').appendTo("body").click(function(){ ok(true, "Previously bound click run." ); });
708         var child = y.append("<b>test</b>").find("b").click(function(){ ok(true, "Child bound click run." ); return false; });
709
710         y.replaceWith( tmp );
711
712         tmp.click();
713         y.click(); // Shouldn't be run
714         child.click(); // Shouldn't be run
715
716         tmp.remove();
717         y.remove();
718         child.remove();
719
720         QUnit.reset();
721
722         y = jQuery('<div/>').appendTo("body").click(function(){ ok(true, "Previously bound click run." ); });
723         var child2 = y.append("<u>test</u>").find("u").click(function(){ ok(true, "Child 2 bound click run." ); return false; });
724
725         y.replaceWith( child2 );
726
727         child2.click();
728
729         y.remove();
730         child2.remove();
731
732         QUnit.reset();
733
734         var set = jQuery("<div/>").replaceWith(val("<span>test</span>"));
735         equals( set[0].nodeName.toLowerCase(), "span", "Replace the disconnected node." );
736         equals( set.length, 1, "Replace the disconnected node." );
737
738         var $div = jQuery("<div class='replacewith'></div>").appendTo("body");
739         // TODO: Work on jQuery(...) inline script execution
740         //$div.replaceWith("<div class='replacewith'></div><script>" +
741                 //"equals(jQuery('.replacewith').length, 1, 'Check number of elements in page.');" +
742                 //"</script>");
743         equals(jQuery('.replacewith').length, 1, 'Check number of elements in page.');
744         jQuery('.replacewith').remove();
745 }
746
747 test("replaceWith(String|Element|Array&lt;Element&gt;|jQuery)", function() {
748         testReplaceWith(bareObj);
749 });
750
751 test("replaceWith(Function)", function() {
752         testReplaceWith(functionReturningObj);
753
754         expect(18);
755
756         var y = jQuery("#yahoo")[0];
757
758         jQuery(y).replaceWith(function(){
759                 equals( this, y, "Make sure the context is coming in correctly." );
760         });
761
762         QUnit.reset();
763 });
764
765 test("replaceWith(string) for more than one element", function(){
766         expect(3);
767
768         equals(jQuery('#foo p').length, 3, 'ensuring that test data has not changed');
769
770         jQuery('#foo p').replaceWith('<span>bar</span>');
771         equals(jQuery('#foo span').length, 3, 'verify that all the three original element have been replaced');
772         equals(jQuery('#foo p').length, 0, 'verify that all the three original element have been replaced');
773 });
774
775 test("replaceAll(String|Element|Array&lt;Element&gt;|jQuery)", function() {
776         expect(10);
777         jQuery('<b id="replace">buga</b>').replaceAll("#yahoo");
778         ok( jQuery("#replace")[0], 'Replace element with string' );
779         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
780
781         QUnit.reset();
782         jQuery(document.getElementById('first')).replaceAll("#yahoo");
783         ok( jQuery("#first")[0], 'Replace element with element' );
784         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
785
786         QUnit.reset();
787         jQuery([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo");
788         ok( jQuery("#first")[0], 'Replace element with array of elements' );
789         ok( jQuery("#mark")[0], 'Replace element with array of elements' );
790         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
791
792         QUnit.reset();
793         jQuery("#first, #mark").replaceAll("#yahoo");
794         ok( jQuery("#first")[0], 'Replace element with set of elements' );
795         ok( jQuery("#mark")[0], 'Replace element with set of elements' );
796         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
797 });
798
799 test("clone()", function() {
800         expect(31);
801         equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Assert text for #en' );
802         var clone = jQuery('#yahoo').clone();
803         equals( 'Try them out:Yahoo', jQuery('#first').append(clone).text(), 'Check for clone' );
804         equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Reassert text for #en' );
805
806         var cloneTags = [
807                 "<table/>", "<tr/>", "<td/>", "<div/>",
808                 "<button/>", "<ul/>", "<ol/>", "<li/>",
809                 "<input type='checkbox' />", "<select/>", "<option/>", "<textarea/>",
810                 "<tbody/>", "<thead/>", "<tfoot/>", "<iframe/>"
811         ];
812         for (var i = 0; i < cloneTags.length; i++) {
813                 var j = jQuery(cloneTags[i]);
814                 equals( j[0].tagName, j.clone()[0].tagName, 'Clone a &lt;' + cloneTags[i].substring(1));
815         }
816
817         // using contents will get comments regular, text, and comment nodes
818         var cl = jQuery("#nonnodes").contents().clone();
819         ok( cl.length >= 2, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
820
821         var div = jQuery("<div><ul><li>test</li></ul></div>").click(function(){
822                 ok( true, "Bound event still exists." );
823         });
824
825         div = div.clone(true).clone(true);
826         equals( div.length, 1, "One element cloned" );
827         equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
828         div.trigger("click");
829
830         div = jQuery("<div/>").append([ document.createElement("table"), document.createElement("table") ]);
831         div.find("table").click(function(){
832                 ok( true, "Bound event still exists." );
833         });
834
835         div = div.clone(true);
836         equals( div.length, 1, "One element cloned" );
837         equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
838         div.find("table:last").trigger("click");
839
840         div = jQuery("<div/>").html('<object height="355" width="425">  <param name="movie" value="http://www.youtube.com/v/JikaHBDoV3k&amp;hl=en">  <param name="wmode" value="transparent"> </object>');
841
842         div = div.clone(true);
843         equals( div.length, 1, "One element cloned" );
844         equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
845
846         div = jQuery("<div/>").data({ a: true, b: true });
847         div = div.clone(true);
848         equals( div.data("a"), true, "Data cloned." );
849         equals( div.data("b"), true, "Data cloned." );
850
851         var form = document.createElement("form");
852         form.action = "/test/";
853         var div = document.createElement("div");
854         div.appendChild( document.createTextNode("test") );
855         form.appendChild( div );
856
857         equals( jQuery(form).clone().children().length, 1, "Make sure we just get the form back." );
858 });
859
860 if (!isLocal) {
861 test("clone() on XML nodes", function() {
862         expect(2);
863         stop();
864         jQuery.get("data/dashboard.xml", function (xml) {
865                 var root = jQuery(xml.documentElement).clone();
866                 var origTab = jQuery("tab", xml).eq(0);
867                 var cloneTab = jQuery("tab", root).eq(0);
868                 origTab.text("origval");
869                 cloneTab.text("cloneval");
870                 equals(origTab.text(), "origval", "Check original XML node was correctly set");
871                 equals(cloneTab.text(), "cloneval", "Check cloned XML node was correctly set");
872                 start();
873         });
874 });
875 }
876
877 var testHtml = function(valueObj) {
878         expect(31);
879
880         jQuery.scriptorder = 0;
881
882         var div = jQuery("#main > div");
883         div.html(valueObj("<b>test</b>"));
884         var pass = true;
885         for ( var i = 0; i < div.size(); i++ ) {
886                 if ( div.get(i).childNodes.length != 1 ) pass = false;
887         }
888         ok( pass, "Set HTML" );
889
890         div = jQuery("<div/>").html( valueObj('<div id="parent_1"><div id="child_1"/></div><div id="parent_2"/>') );
891
892         equals( div.children().length, 2, "Make sure two child nodes exist." );
893         equals( div.children().children().length, 1, "Make sure that a grandchild exists." );
894
895         var space = jQuery("<div/>").html(valueObj("&#160;"))[0].innerHTML;
896         ok( /^\s$|^&nbsp;$/.test( space ), "Make sure entities are passed through correctly." );
897         equals( jQuery("<div/>").html(valueObj("&amp;"))[0].innerHTML, "&amp;", "Make sure entities are passed through correctly." );
898
899         jQuery("#main").html(valueObj("<style>.foobar{color:green;}</style>"));
900
901         equals( jQuery("#main").children().length, 1, "Make sure there is a child element." );
902         equals( jQuery("#main").children()[0].nodeName.toUpperCase(), "STYLE", "And that a style element was inserted." );
903
904         QUnit.reset();
905         // using contents will get comments regular, text, and comment nodes
906         var j = jQuery("#nonnodes").contents();
907         j.html(valueObj("<b>bold</b>"));
908
909         // this is needed, or the expando added by jQuery unique will yield a different html
910         j.find('b').removeData();
911         equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
912
913         jQuery("#main").html(valueObj("<select/>"));
914         jQuery("#main select").html(valueObj("<option>O1</option><option selected='selected'>O2</option><option>O3</option>"));
915         equals( jQuery("#main select").val(), "O2", "Selected option correct" );
916
917         var $div = jQuery('<div />');
918         equals( $div.html(valueObj( 5 )).html(), '5', 'Setting a number as html' );
919         equals( $div.html(valueObj( 0 )).html(), '0', 'Setting a zero as html' );
920
921         var $div2 = jQuery('<div/>'), insert = "&lt;div&gt;hello1&lt;/div&gt;";
922         equals( $div2.html(insert).html().replace(/>/g, "&gt;"), insert, "Verify escaped insertion." );
923         equals( $div2.html("x" + insert).html().replace(/>/g, "&gt;"), "x" + insert, "Verify escaped insertion." );
924         equals( $div2.html(" " + insert).html().replace(/>/g, "&gt;"), " " + insert, "Verify escaped insertion." );
925
926         var map = jQuery("<map/>").html(valueObj("<area id='map01' shape='rect' coords='50,50,150,150' href='http://www.jquery.com/' alt='jQuery'>"));
927
928         equals( map[0].childNodes.length, 1, "The area was inserted." );
929         equals( map[0].firstChild.nodeName.toLowerCase(), "area", "The area was inserted." );
930
931         QUnit.reset();
932
933         jQuery("#main").html(valueObj('<script type="something/else">ok( false, "Non-script evaluated." );</script><script type="text/javascript">ok( true, "text/javascript is evaluated." );</script><script>ok( true, "No type is evaluated." );</script><div><script type="text/javascript">ok( true, "Inner text/javascript is evaluated." );</script><script>ok( true, "Inner No type is evaluated." );</script><script type="something/else">ok( false, "Non-script evaluated." );</script></div>'));
934
935         jQuery("#main").html(valueObj("<script>ok( true, 'Test repeated injection of script.' );</script>"));
936         jQuery("#main").html(valueObj("<script>ok( true, 'Test repeated injection of script.' );</script>"));
937         jQuery("#main").html(valueObj("<script>ok( true, 'Test repeated injection of script.' );</script>"));
938
939         jQuery("#main").html(valueObj('<script type="text/javascript">ok( true, "jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975 (1)" );</script>'));
940
941         jQuery("#main").html(valueObj('foo <form><script type="text/javascript">ok( true, "jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975 (2)" );</script></form>'));
942
943         jQuery("#main").html(valueObj("<script>equals(jQuery.scriptorder++, 0, 'Script is executed in order');equals(jQuery('#scriptorder').length, 1,'Execute after html (even though appears before)')<\/script><span id='scriptorder'><script>equals(jQuery.scriptorder++, 1, 'Script (nested) is executed in order');equals(jQuery('#scriptorder').length, 1,'Execute after html')<\/script></span><script>equals(jQuery.scriptorder++, 2, 'Script (unnested) is executed in order');equals(jQuery('#scriptorder').length, 1,'Execute after html')<\/script>"));
944 }
945
946 test("html(String)", function() {
947         testHtml(bareObj);
948 });
949
950 test("html(Function)", function() {
951         testHtml(functionReturningObj);
952 });
953
954 test("html(Function) with incoming value", function() {
955         expect(20);
956         
957         var div = jQuery("#main > div"), old = div.map(function(){ return jQuery(this).html() });
958         
959         div.html(function(i, val) {
960                 equals( val, old[i], "Make sure the incoming value is correct." );
961                 return "<b>test</b>";
962         });
963         
964         var pass = true;
965         div.each(function(){
966                 if ( this.childNodes.length !== 1 ) {
967                         pass = false;
968                 }
969         })
970         ok( pass, "Set HTML" );
971
972         QUnit.reset();
973         // using contents will get comments regular, text, and comment nodes
974         var j = jQuery("#nonnodes").contents();
975         old = j.map(function(){ return jQuery(this).html(); });
976         
977         j.html(function(i, val) {
978                 equals( val, old[i], "Make sure the incoming value is correct." );
979                 return "<b>bold</b>";
980         });
981         
982         j.find('b').removeData();
983         equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
984         
985         var $div = jQuery('<div />');
986         
987         equals( $div.html(function(i, val) {
988                 equals( val, "", "Make sure the incoming value is correct." );
989                 return 5;
990         }).html(), '5', 'Setting a number as html' );
991         
992         equals( $div.html(function(i, val) {
993                 equals( val, "5", "Make sure the incoming value is correct." );
994                 return 0;
995         }).html(), '0', 'Setting a zero as html' );
996
997         var $div2 = jQuery('<div/>'), insert = "&lt;div&gt;hello1&lt;/div&gt;";
998         equals( $div2.html(function(i, val) {
999                 equals( val, "", "Make sure the incoming value is correct." );
1000                 return insert;
1001         }).html().replace(/>/g, "&gt;"), insert, "Verify escaped insertion." );
1002         
1003         equals( $div2.html(function(i, val) {
1004                 equals( val.replace(/>/g, "&gt;"), insert, "Make sure the incoming value is correct." );
1005                 return "x" + insert;
1006         }).html().replace(/>/g, "&gt;"), "x" + insert, "Verify escaped insertion." );
1007         
1008         equals( $div2.html(function(i, val) {
1009                 equals( val.replace(/>/g, "&gt;"), "x" + insert, "Make sure the incoming value is correct." );
1010                 return " " + insert;
1011         }).html().replace(/>/g, "&gt;"), " " + insert, "Verify escaped insertion." );   
1012 });
1013
1014 var testRemove = function(method) {
1015         expect(9);
1016
1017         var first = jQuery("#ap").children(":first");
1018         first.data("foo", "bar");
1019
1020         jQuery("#ap").children()[method]();
1021         ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
1022         equals( jQuery("#ap").children().length, 0, "Check remove" );
1023
1024         equals( first.data("foo"), method == "remove" ? null : "bar" );
1025
1026         QUnit.reset();
1027         jQuery("#ap").children()[method]("a");
1028         ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
1029         equals( jQuery("#ap").children().length, 1, "Check filtered remove" );
1030
1031         jQuery("#ap").children()[method]("a, code");
1032         equals( jQuery("#ap").children().length, 0, "Check multi-filtered remove" );
1033
1034         // using contents will get comments regular, text, and comment nodes
1035         equals( jQuery("#nonnodes").contents().length, 3, "Check node,textnode,comment remove works" );
1036         jQuery("#nonnodes").contents()[method]();
1037         equals( jQuery("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" );
1038
1039         QUnit.reset();
1040
1041         var count = 0;
1042         var first = jQuery("#ap").children(":first");
1043         var cleanUp = first.click(function() { count++ })[method]().appendTo("body").click();
1044         
1045         equals( method == "remove" ? 0 : 1, count );
1046         
1047         cleanUp.detach();
1048 };
1049
1050 test("remove()", function() {
1051         testRemove("remove");
1052 });
1053
1054 test("detach()", function() {
1055         testRemove("detach");
1056 });
1057
1058 test("empty()", function() {
1059         expect(3);
1060         equals( jQuery("#ap").children().empty().text().length, 0, "Check text is removed" );
1061         equals( jQuery("#ap").children().length, 4, "Check elements are not removed" );
1062
1063         // using contents will get comments regular, text, and comment nodes
1064         var j = jQuery("#nonnodes").contents();
1065         j.empty();
1066         equals( j.html(), "", "Check node,textnode,comment empty works" );
1067 });
1068
1069 test("jQuery.cleanData", function() {
1070         expect(14);
1071         
1072         var type, pos, div, child;
1073         
1074         type = "remove";
1075         
1076         // Should trigger 4 remove event
1077         div = getDiv().remove();
1078         
1079         // Should both do nothing
1080         pos = "Outer";
1081         div.trigger("click");
1082         
1083         pos = "Inner";
1084         div.children().trigger("click");
1085         
1086         type = "empty";
1087         div = getDiv();
1088         child = div.children();
1089         
1090         // Should trigger 2 remove event
1091         div.empty();
1092         
1093         // Should trigger 1
1094         pos = "Outer";
1095         div.trigger("click");
1096         
1097         // Should do nothing
1098         pos = "Inner";
1099         child.trigger("click");
1100
1101         // Should trigger 2
1102         div.remove();
1103         
1104         type = "html";
1105         
1106         div = getDiv();
1107         child = div.children();
1108         
1109         // Should trigger 2 remove event
1110         div.html("<div></div>");
1111         
1112         // Should trigger 1
1113         pos = "Outer";
1114         div.trigger("click");
1115         
1116         // Should do nothing
1117         pos = "Inner";
1118         child.trigger("click");
1119
1120         // Should trigger 2
1121         div.remove();
1122         
1123         function getDiv() {
1124                 var div = jQuery("<div class='outer'><div class='inner'></div></div>").click(function(){
1125                         ok( true, type + " " + pos + " Click event fired." );
1126                 }).focus(function(){
1127                         ok( true, type + " " + pos + " Focus event fired." );
1128                 }).find("div").click(function(){
1129                         ok( false, type + " " + pos + " Click event fired." );
1130                 }).focus(function(){
1131                         ok( false, type + " " + pos + " Focus event fired." );
1132                 }).end().appendTo("body");
1133                 
1134                 div[0].detachEvent = div[0].removeEventListener = function(t){
1135                         ok( true, type + " Outer " + t + " event unbound" );
1136                 };
1137                 
1138                 div[0].firstChild.detachEvent = div[0].firstChild.removeEventListener = function(t){
1139                         ok( true, type + " Inner " + t + " event unbound" );
1140                 };
1141                 
1142                 return div;
1143         }
1144 });