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