8ee36882103380c6bfbb382d8160987175a84532
[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("append the same fragment with events (Bug #6997, 5566)", function () {
385         expect(2);
386         stop(1000);
387
388         var element = jQuery("<a class='test6997'></a>").click(function () {
389                 ok(true, "Append second element events work");
390         });
391
392         jQuery("#listWithTabIndex li").append(element)
393                 .find('a.test6997').eq(1).click();
394
395         element = jQuery("<li class='test6997'></li>").click(function () {
396                 ok(true, "Before second element events work");
397                 start();
398         });
399
400         jQuery("#listWithTabIndex li").before(element);
401         jQuery("#listWithTabIndex li.test6997").eq(1).click();
402 });
403
404 test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
405         expect(16);
406
407         var defaultText = 'Try them out:'
408         jQuery('<b>buga</b>').appendTo('#first');
409         equals( jQuery("#first").text(), defaultText + 'buga', 'Check if text appending works' );
410         equals( jQuery('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
411
412         QUnit.reset();
413         var l = jQuery("#first").children().length + 2;
414         jQuery("<strong>test</strong>");
415         jQuery("<strong>test</strong>");
416         jQuery([ jQuery("<strong>test</strong>")[0], jQuery("<strong>test</strong>")[0] ])
417                 .appendTo("#first");
418         equals( jQuery("#first").children().length, l, "Make sure the elements were inserted." );
419         equals( jQuery("#first").children().last()[0].nodeName.toLowerCase(), "strong", "Verify the last element." );
420
421         QUnit.reset();
422         var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
423         jQuery(document.getElementById('first')).appendTo('#sap');
424         equals( jQuery('#sap').text(), expected, "Check for appending of element" );
425
426         QUnit.reset();
427         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
428         jQuery([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
429         equals( jQuery('#sap').text(), expected, "Check for appending of array of elements" );
430
431         QUnit.reset();
432         ok( jQuery(document.createElement("script")).appendTo("body").length, "Make sure a disconnected script can be appended." );
433
434         QUnit.reset();
435         expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
436         jQuery("#yahoo, #first").appendTo('#sap');
437         equals( jQuery('#sap').text(), expected, "Check for appending of jQuery object" );
438
439         QUnit.reset();
440         jQuery('#select1').appendTo('#foo');
441         t( 'Append select', '#foo select', ['select1'] );
442
443         QUnit.reset();
444         var div = jQuery("<div/>").click(function(){
445                 ok(true, "Running a cloned click.");
446         });
447         div.appendTo("#main, #moretests");
448
449         jQuery("#main div:last").click();
450         jQuery("#moretests div:last").click();
451
452         QUnit.reset();
453         var div = jQuery("<div/>").appendTo("#main, #moretests");
454
455         equals( div.length, 2, "appendTo returns the inserted elements" );
456
457         div.addClass("test");
458
459         ok( jQuery("#main div:last").hasClass("test"), "appendTo element was modified after the insertion" );
460         ok( jQuery("#moretests div:last").hasClass("test"), "appendTo element was modified after the insertion" );
461
462         QUnit.reset();
463
464         div = jQuery("<div/>");
465         jQuery("<span>a</span><b>b</b>").filter("span").appendTo( div );
466
467         equals( div.children().length, 1, "Make sure the right number of children were inserted." );
468
469         div = jQuery("#moretests div");
470
471         var num = jQuery("#main div").length;
472         div.remove().appendTo("#main");
473
474         equals( jQuery("#main div").length, num, "Make sure all the removed divs were inserted." );
475
476         QUnit.reset();
477 });
478
479 var testPrepend = function(val) {
480         expect(5);
481         var defaultText = 'Try them out:'
482         var result = jQuery('#first').prepend(val( '<b>buga</b>' ));
483         equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
484         equals( jQuery('#select3').prepend(val( '<option value="prependTest">Prepend Test</option>' )).find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
485
486         QUnit.reset();
487         var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
488         jQuery('#sap').prepend(val( document.getElementById('first') ));
489         equals( jQuery('#sap').text(), expected, "Check for prepending of element" );
490
491         QUnit.reset();
492         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
493         jQuery('#sap').prepend(val( [document.getElementById('first'), document.getElementById('yahoo')] ));
494         equals( jQuery('#sap').text(), expected, "Check for prepending of array of elements" );
495
496         QUnit.reset();
497         expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
498         jQuery('#sap').prepend(val( jQuery("#yahoo, #first") ));
499         equals( jQuery('#sap').text(), expected, "Check for prepending of jQuery object" );
500 };
501
502 test("prepend(String|Element|Array&lt;Element&gt;|jQuery)", function() {
503         testPrepend(bareObj);
504 });
505
506 test("prepend(Function)", function() {
507         testPrepend(functionReturningObj);
508 });
509
510 test("prepend(Function) with incoming value", function() {
511         expect(10);
512
513         var defaultText = 'Try them out:', old = jQuery('#first').html();
514         var result = jQuery('#first').prepend(function(i, val) {
515                 equals( val, old, "Make sure the incoming value is correct." );
516                 return '<b>buga</b>';
517         });
518         equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
519
520         old = jQuery("#select3").html();
521
522         equals( jQuery('#select3').prepend(function(i, val) {
523                 equals( val, old, "Make sure the incoming value is correct." );
524                 return '<option value="prependTest">Prepend Test</option>';
525         }).find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
526
527         QUnit.reset();
528         var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
529         old = jQuery('#sap').html();
530
531         jQuery('#sap').prepend(function(i, val) {
532                 equals( val, old, "Make sure the incoming value is correct." );
533                 return document.getElementById('first');
534         });
535
536         equals( jQuery('#sap').text(), expected, "Check for prepending of element" );
537
538         QUnit.reset();
539         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
540         old = jQuery('#sap').html();
541
542         jQuery('#sap').prepend(function(i, val) {
543                 equals( val, old, "Make sure the incoming value is correct." );
544                 return [document.getElementById('first'), document.getElementById('yahoo')];
545         });
546
547         equals( jQuery('#sap').text(), expected, "Check for prepending of array of elements" );
548
549         QUnit.reset();
550         expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
551         old = jQuery('#sap').html();
552
553         jQuery('#sap').prepend(function(i, val) {
554                 equals( val, old, "Make sure the incoming value is correct." );
555                 return jQuery("#yahoo, #first");
556         });
557
558         equals( jQuery('#sap').text(), expected, "Check for prepending of jQuery object" );
559 });
560
561 test("prependTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
562         expect(6);
563         var defaultText = 'Try them out:'
564         jQuery('<b>buga</b>').prependTo('#first');
565         equals( jQuery('#first').text(), 'buga' + defaultText, 'Check if text prepending works' );
566         equals( jQuery('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
567
568         QUnit.reset();
569         var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
570         jQuery(document.getElementById('first')).prependTo('#sap');
571         equals( jQuery('#sap').text(), expected, "Check for prepending of element" );
572
573         QUnit.reset();
574         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
575         jQuery([document.getElementById('first'), document.getElementById('yahoo')]).prependTo('#sap');
576         equals( jQuery('#sap').text(), expected, "Check for prepending of array of elements" );
577
578         QUnit.reset();
579         expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
580         jQuery("#yahoo, #first").prependTo('#sap');
581         equals( jQuery('#sap').text(), expected, "Check for prepending of jQuery object" );
582
583         QUnit.reset();
584         jQuery('<select id="prependSelect1"></select>').prependTo('form:last');
585         jQuery('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');
586
587         t( "Prepend Select", "#prependSelect2, #prependSelect1", ["prependSelect2", "prependSelect1"] );
588 });
589
590 var testBefore = function(val) {
591         expect(6);
592         var expected = 'This is a normal link: bugaYahoo';
593         jQuery('#yahoo').before(val( '<b>buga</b>' ));
594         equals( jQuery('#en').text(), expected, 'Insert String before' );
595
596         QUnit.reset();
597         expected = "This is a normal link: Try them out:Yahoo";
598         jQuery('#yahoo').before(val( document.getElementById('first') ));
599         equals( jQuery('#en').text(), expected, "Insert element before" );
600
601         QUnit.reset();
602         expected = "This is a normal link: Try them out:diveintomarkYahoo";
603         jQuery('#yahoo').before(val( [document.getElementById('first'), document.getElementById('mark')] ));
604         equals( jQuery('#en').text(), expected, "Insert array of elements before" );
605
606         QUnit.reset();
607         expected = "This is a normal link: diveintomarkTry them out:Yahoo";
608         jQuery('#yahoo').before(val( jQuery("#mark, #first") ));
609         equals( jQuery('#en').text(), expected, "Insert jQuery before" );
610
611         var set = jQuery("<div/>").before("<span>test</span>");
612         equals( set[0].nodeName.toLowerCase(), "span", "Insert the element before the disconnected node." );
613         equals( set.length, 2, "Insert the element before the disconnected node." );
614 }
615
616 test("before(String|Element|Array&lt;Element&gt;|jQuery)", function() {
617         testBefore(bareObj);
618 });
619
620 test("before(Function)", function() {
621         testBefore(functionReturningObj);
622 })
623
624 test("insertBefore(String|Element|Array&lt;Element&gt;|jQuery)", function() {
625         expect(4);
626         var expected = 'This is a normal link: bugaYahoo';
627         jQuery('<b>buga</b>').insertBefore('#yahoo');
628         equals( jQuery('#en').text(), expected, 'Insert String before' );
629
630         QUnit.reset();
631         expected = "This is a normal link: Try them out:Yahoo";
632         jQuery(document.getElementById('first')).insertBefore('#yahoo');
633         equals( jQuery('#en').text(), expected, "Insert element before" );
634
635         QUnit.reset();
636         expected = "This is a normal link: Try them out:diveintomarkYahoo";
637         jQuery([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
638         equals( jQuery('#en').text(), expected, "Insert array of elements before" );
639
640         QUnit.reset();
641         expected = "This is a normal link: diveintomarkTry them out:Yahoo";
642         jQuery("#mark, #first").insertBefore('#yahoo');
643         equals( jQuery('#en').text(), expected, "Insert jQuery before" );
644 });
645
646 var testAfter = function(val) {
647         expect(6);
648         var expected = 'This is a normal link: Yahoobuga';
649         jQuery('#yahoo').after(val( '<b>buga</b>' ));
650         equals( jQuery('#en').text(), expected, 'Insert String after' );
651
652         QUnit.reset();
653         expected = "This is a normal link: YahooTry them out:";
654         jQuery('#yahoo').after(val( document.getElementById('first') ));
655         equals( jQuery('#en').text(), expected, "Insert element after" );
656
657         QUnit.reset();
658         expected = "This is a normal link: YahooTry them out:diveintomark";
659         jQuery('#yahoo').after(val( [document.getElementById('first'), document.getElementById('mark')] ));
660         equals( jQuery('#en').text(), expected, "Insert array of elements after" );
661
662         QUnit.reset();
663         expected = "This is a normal link: YahoodiveintomarkTry them out:";
664         jQuery('#yahoo').after(val( jQuery("#mark, #first") ));
665         equals( jQuery('#en').text(), expected, "Insert jQuery after" );
666
667         var set = jQuery("<div/>").after("<span>test</span>");
668         equals( set[1].nodeName.toLowerCase(), "span", "Insert the element after the disconnected node." );
669         equals( set.length, 2, "Insert the element after the disconnected node." );
670 };
671
672 test("after(String|Element|Array&lt;Element&gt;|jQuery)", function() {
673         testAfter(bareObj);
674 });
675
676 test("after(Function)", function() {
677         testAfter(functionReturningObj);
678 })
679
680 test("insertAfter(String|Element|Array&lt;Element&gt;|jQuery)", function() {
681         expect(4);
682         var expected = 'This is a normal link: Yahoobuga';
683         jQuery('<b>buga</b>').insertAfter('#yahoo');
684         equals( jQuery('#en').text(), expected, 'Insert String after' );
685
686         QUnit.reset();
687         expected = "This is a normal link: YahooTry them out:";
688         jQuery(document.getElementById('first')).insertAfter('#yahoo');
689         equals( jQuery('#en').text(), expected, "Insert element after" );
690
691         QUnit.reset();
692         expected = "This is a normal link: YahooTry them out:diveintomark";
693         jQuery([document.getElementById('first'), document.getElementById('mark')]).insertAfter('#yahoo');
694         equals( jQuery('#en').text(), expected, "Insert array of elements after" );
695
696         QUnit.reset();
697         expected = "This is a normal link: YahoodiveintomarkTry them out:";
698         jQuery("#mark, #first").insertAfter('#yahoo');
699         equals( jQuery('#en').text(), expected, "Insert jQuery after" );
700 });
701
702 var testReplaceWith = function(val) {
703         expect(20);
704         jQuery('#yahoo').replaceWith(val( '<b id="replace">buga</b>' ));
705         ok( jQuery("#replace")[0], 'Replace element with string' );
706         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
707
708         QUnit.reset();
709         jQuery('#yahoo').replaceWith(val( document.getElementById('first') ));
710         ok( jQuery("#first")[0], 'Replace element with element' );
711         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
712
713         QUnit.reset();
714         jQuery("#main").append('<div id="bar"><div id="baz">Foo</div></div>');
715         jQuery('#baz').replaceWith("Baz");
716         equals( jQuery("#bar").text(),"Baz", 'Replace element with text' );
717         ok( !jQuery("#baz")[0], 'Verify that original element is gone, after element' );
718
719         QUnit.reset();
720         jQuery('#yahoo').replaceWith(val( [document.getElementById('first'), document.getElementById('mark')] ));
721         ok( jQuery("#first")[0], 'Replace element with array of elements' );
722         ok( jQuery("#mark")[0], 'Replace element with array of elements' );
723         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
724
725         QUnit.reset();
726         jQuery('#yahoo').replaceWith(val( jQuery("#mark, #first") ));
727         ok( jQuery("#first")[0], 'Replace element with set of elements' );
728         ok( jQuery("#mark")[0], 'Replace element with set of elements' );
729         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
730
731         QUnit.reset();
732         var tmp = jQuery("<div/>").appendTo("body").click(function(){ ok(true, "Newly bound click run." ); });
733         var y = jQuery('<div/>').appendTo("body").click(function(){ ok(true, "Previously bound click run." ); });
734         var child = y.append("<b>test</b>").find("b").click(function(){ ok(true, "Child bound click run." ); return false; });
735
736         y.replaceWith( tmp );
737
738         tmp.click();
739         y.click(); // Shouldn't be run
740         child.click(); // Shouldn't be run
741
742         tmp.remove();
743         y.remove();
744         child.remove();
745
746         QUnit.reset();
747
748         y = jQuery('<div/>').appendTo("body").click(function(){ ok(true, "Previously bound click run." ); });
749         var child2 = y.append("<u>test</u>").find("u").click(function(){ ok(true, "Child 2 bound click run." ); return false; });
750
751         y.replaceWith( child2 );
752
753         child2.click();
754
755         y.remove();
756         child2.remove();
757
758         QUnit.reset();
759
760         var set = jQuery("<div/>").replaceWith(val("<span>test</span>"));
761         equals( set[0].nodeName.toLowerCase(), "span", "Replace the disconnected node." );
762         equals( set.length, 1, "Replace the disconnected node." );
763
764         var $div = jQuery("<div class='replacewith'></div>").appendTo("body");
765         // TODO: Work on jQuery(...) inline script execution
766         //$div.replaceWith("<div class='replacewith'></div><script>" +
767                 //"equals(jQuery('.replacewith').length, 1, 'Check number of elements in page.');" +
768                 //"</script>");
769         equals(jQuery('.replacewith').length, 1, 'Check number of elements in page.');
770         jQuery('.replacewith').remove();
771
772         QUnit.reset();
773
774         jQuery("#main").append("<div id='replaceWith'></div>");
775         equals( jQuery("#main").find("div[id=replaceWith]").length, 1, "Make sure only one div exists." );
776
777         jQuery("#replaceWith").replaceWith( val("<div id='replaceWith'></div>") );
778         equals( jQuery("#main").find("div[id=replaceWith]").length, 1, "Make sure only one div exists." );
779
780         jQuery("#replaceWith").replaceWith( val("<div id='replaceWith'></div>") );
781         equals( jQuery("#main").find("div[id=replaceWith]").length, 1, "Make sure only one div exists." );
782 }
783
784 test("replaceWith(String|Element|Array&lt;Element&gt;|jQuery)", function() {
785         testReplaceWith(bareObj);
786 });
787
788 test("replaceWith(Function)", function() {
789         testReplaceWith(functionReturningObj);
790
791         expect(21);
792
793         var y = jQuery("#yahoo")[0];
794
795         jQuery(y).replaceWith(function(){
796                 equals( this, y, "Make sure the context is coming in correctly." );
797         });
798
799         QUnit.reset();
800 });
801
802 test("replaceWith(string) for more than one element", function(){
803         expect(3);
804
805         equals(jQuery('#foo p').length, 3, 'ensuring that test data has not changed');
806
807         jQuery('#foo p').replaceWith('<span>bar</span>');
808         equals(jQuery('#foo span').length, 3, 'verify that all the three original element have been replaced');
809         equals(jQuery('#foo p').length, 0, 'verify that all the three original element have been replaced');
810 });
811
812 test("replaceAll(String|Element|Array&lt;Element&gt;|jQuery)", function() {
813         expect(10);
814         jQuery('<b id="replace">buga</b>').replaceAll("#yahoo");
815         ok( jQuery("#replace")[0], 'Replace element with string' );
816         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
817
818         QUnit.reset();
819         jQuery(document.getElementById('first')).replaceAll("#yahoo");
820         ok( jQuery("#first")[0], 'Replace element with element' );
821         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
822
823         QUnit.reset();
824         jQuery([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo");
825         ok( jQuery("#first")[0], 'Replace element with array of elements' );
826         ok( jQuery("#mark")[0], 'Replace element with array of elements' );
827         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
828
829         QUnit.reset();
830         jQuery("#mark, #first").replaceAll("#yahoo");
831         ok( jQuery("#first")[0], 'Replace element with set of elements' );
832         ok( jQuery("#mark")[0], 'Replace element with set of elements' );
833         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
834 });
835
836 test("clone()", function() {
837         expect(31);
838         equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Assert text for #en' );
839         var clone = jQuery('#yahoo').clone();
840         equals( 'Try them out:Yahoo', jQuery('#first').append(clone).text(), 'Check for clone' );
841         equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Reassert text for #en' );
842
843         var cloneTags = [
844                 "<table/>", "<tr/>", "<td/>", "<div/>",
845                 "<button/>", "<ul/>", "<ol/>", "<li/>",
846                 "<input type='checkbox' />", "<select/>", "<option/>", "<textarea/>",
847                 "<tbody/>", "<thead/>", "<tfoot/>", "<iframe/>"
848         ];
849         for (var i = 0; i < cloneTags.length; i++) {
850                 var j = jQuery(cloneTags[i]);
851                 equals( j[0].tagName, j.clone()[0].tagName, 'Clone a &lt;' + cloneTags[i].substring(1));
852         }
853
854         // using contents will get comments regular, text, and comment nodes
855         var cl = jQuery("#nonnodes").contents().clone();
856         ok( cl.length >= 2, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
857
858         var div = jQuery("<div><ul><li>test</li></ul></div>").click(function(){
859                 ok( true, "Bound event still exists." );
860         });
861
862         div = div.clone(true).clone(true);
863         equals( div.length, 1, "One element cloned" );
864         equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
865         div.trigger("click");
866
867         div = jQuery("<div/>").append([ document.createElement("table"), document.createElement("table") ]);
868         div.find("table").click(function(){
869                 ok( true, "Bound event still exists." );
870         });
871
872         div = div.clone(true);
873         equals( div.length, 1, "One element cloned" );
874         equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
875         div.find("table:last").trigger("click");
876
877         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>');
878
879         div = div.clone(true);
880         equals( div.length, 1, "One element cloned" );
881         equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
882
883         div = jQuery("<div/>").data({ a: true, b: true });
884         div = div.clone(true);
885         equals( div.data("a"), true, "Data cloned." );
886         equals( div.data("b"), true, "Data cloned." );
887
888         var form = document.createElement("form");
889         form.action = "/test/";
890         var div = document.createElement("div");
891         div.appendChild( document.createTextNode("test") );
892         form.appendChild( div );
893
894         equals( jQuery(form).clone().children().length, 1, "Make sure we just get the form back." );
895 });
896
897 if (!isLocal) {
898 test("clone() on XML nodes", function() {
899         expect(2);
900         stop();
901         jQuery.get("data/dashboard.xml", function (xml) {
902                 var root = jQuery(xml.documentElement).clone();
903                 var origTab = jQuery("tab", xml).eq(0);
904                 var cloneTab = jQuery("tab", root).eq(0);
905                 origTab.text("origval");
906                 cloneTab.text("cloneval");
907                 equals(origTab.text(), "origval", "Check original XML node was correctly set");
908                 equals(cloneTab.text(), "cloneval", "Check cloned XML node was correctly set");
909                 start();
910         });
911 });
912 }
913
914 var testHtml = function(valueObj) {
915         expect(31);
916
917         jQuery.scriptorder = 0;
918
919         var div = jQuery("#main > div");
920         div.html(valueObj("<b>test</b>"));
921         var pass = true;
922         for ( var i = 0; i < div.size(); i++ ) {
923                 if ( div.get(i).childNodes.length != 1 ) pass = false;
924         }
925         ok( pass, "Set HTML" );
926
927         div = jQuery("<div/>").html( valueObj('<div id="parent_1"><div id="child_1"/></div><div id="parent_2"/>') );
928
929         equals( div.children().length, 2, "Make sure two child nodes exist." );
930         equals( div.children().children().length, 1, "Make sure that a grandchild exists." );
931
932         var space = jQuery("<div/>").html(valueObj("&#160;"))[0].innerHTML;
933         ok( /^\xA0$|^&nbsp;$/.test( space ), "Make sure entities are passed through correctly." );
934         equals( jQuery("<div/>").html(valueObj("&amp;"))[0].innerHTML, "&amp;", "Make sure entities are passed through correctly." );
935
936         jQuery("#main").html(valueObj("<style>.foobar{color:green;}</style>"));
937
938         equals( jQuery("#main").children().length, 1, "Make sure there is a child element." );
939         equals( jQuery("#main").children()[0].nodeName.toUpperCase(), "STYLE", "And that a style element was inserted." );
940
941         QUnit.reset();
942         // using contents will get comments regular, text, and comment nodes
943         var j = jQuery("#nonnodes").contents();
944         j.html(valueObj("<b>bold</b>"));
945
946         // this is needed, or the expando added by jQuery unique will yield a different html
947         j.find('b').removeData();
948         equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
949
950         jQuery("#main").html(valueObj("<select/>"));
951         jQuery("#main select").html(valueObj("<option>O1</option><option selected='selected'>O2</option><option>O3</option>"));
952         equals( jQuery("#main select").val(), "O2", "Selected option correct" );
953
954         var $div = jQuery('<div />');
955         equals( $div.html(valueObj( 5 )).html(), '5', 'Setting a number as html' );
956         equals( $div.html(valueObj( 0 )).html(), '0', 'Setting a zero as html' );
957
958         var $div2 = jQuery('<div/>'), insert = "&lt;div&gt;hello1&lt;/div&gt;";
959         equals( $div2.html(insert).html().replace(/>/g, "&gt;"), insert, "Verify escaped insertion." );
960         equals( $div2.html("x" + insert).html().replace(/>/g, "&gt;"), "x" + insert, "Verify escaped insertion." );
961         equals( $div2.html(" " + insert).html().replace(/>/g, "&gt;"), " " + insert, "Verify escaped insertion." );
962
963         var map = jQuery("<map/>").html(valueObj("<area id='map01' shape='rect' coords='50,50,150,150' href='http://www.jquery.com/' alt='jQuery'>"));
964
965         equals( map[0].childNodes.length, 1, "The area was inserted." );
966         equals( map[0].firstChild.nodeName.toLowerCase(), "area", "The area was inserted." );
967
968         QUnit.reset();
969
970         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>'));
971
972         jQuery("#main").html(valueObj("<script>ok( true, 'Test repeated injection of script.' );</script>"));
973         jQuery("#main").html(valueObj("<script>ok( true, 'Test repeated injection of script.' );</script>"));
974         jQuery("#main").html(valueObj("<script>ok( true, 'Test repeated injection of script.' );</script>"));
975
976         jQuery("#main").html(valueObj('<script type="text/javascript">ok( true, "jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975 (1)" );</script>'));
977
978         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>'));
979
980         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>"));
981 }
982
983 test("html(String)", function() {
984         testHtml(bareObj);
985 });
986
987 test("html(Function)", function() {
988         testHtml(functionReturningObj);
989
990         expect(33);
991
992         QUnit.reset();
993
994         jQuery("#main").html(function(){
995                 return jQuery(this).text();
996         });
997
998         ok( !/</.test( jQuery("#main").html() ), "Replace html with text." );
999         ok( jQuery("#main").html().length > 0, "Make sure text exists." );
1000 });
1001
1002 test("html(Function) with incoming value", function() {
1003         expect(20);
1004
1005         var div = jQuery("#main > div"), old = div.map(function(){ return jQuery(this).html() });
1006
1007         div.html(function(i, val) {
1008                 equals( val, old[i], "Make sure the incoming value is correct." );
1009                 return "<b>test</b>";
1010         });
1011
1012         var pass = true;
1013         div.each(function(){
1014                 if ( this.childNodes.length !== 1 ) {
1015                         pass = false;
1016                 }
1017         })
1018         ok( pass, "Set HTML" );
1019
1020         QUnit.reset();
1021         // using contents will get comments regular, text, and comment nodes
1022         var j = jQuery("#nonnodes").contents();
1023         old = j.map(function(){ return jQuery(this).html(); });
1024
1025         j.html(function(i, val) {
1026                 equals( val, old[i], "Make sure the incoming value is correct." );
1027                 return "<b>bold</b>";
1028         });
1029
1030         // Handle the case where no comment is in the document
1031         if ( j.length === 2 ) {
1032                 equals( null, null, "Make sure the incoming value is correct." );
1033         }
1034
1035         j.find('b').removeData();
1036         equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
1037
1038         var $div = jQuery('<div />');
1039
1040         equals( $div.html(function(i, val) {
1041                 equals( val, "", "Make sure the incoming value is correct." );
1042                 return 5;
1043         }).html(), '5', 'Setting a number as html' );
1044
1045         equals( $div.html(function(i, val) {
1046                 equals( val, "5", "Make sure the incoming value is correct." );
1047                 return 0;
1048         }).html(), '0', 'Setting a zero as html' );
1049
1050         var $div2 = jQuery('<div/>'), insert = "&lt;div&gt;hello1&lt;/div&gt;";
1051         equals( $div2.html(function(i, val) {
1052                 equals( val, "", "Make sure the incoming value is correct." );
1053                 return insert;
1054         }).html().replace(/>/g, "&gt;"), insert, "Verify escaped insertion." );
1055
1056         equals( $div2.html(function(i, val) {
1057                 equals( val.replace(/>/g, "&gt;"), insert, "Make sure the incoming value is correct." );
1058                 return "x" + insert;
1059         }).html().replace(/>/g, "&gt;"), "x" + insert, "Verify escaped insertion." );
1060
1061         equals( $div2.html(function(i, val) {
1062                 equals( val.replace(/>/g, "&gt;"), "x" + insert, "Make sure the incoming value is correct." );
1063                 return " " + insert;
1064         }).html().replace(/>/g, "&gt;"), " " + insert, "Verify escaped insertion." );
1065 });
1066
1067 var testRemove = function(method) {
1068         expect(9);
1069
1070         var first = jQuery("#ap").children(":first");
1071         first.data("foo", "bar");
1072
1073         jQuery("#ap").children()[method]();
1074         ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
1075         equals( jQuery("#ap").children().length, 0, "Check remove" );
1076
1077         equals( first.data("foo"), method == "remove" ? null : "bar" );
1078
1079         QUnit.reset();
1080         jQuery("#ap").children()[method]("a");
1081         ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
1082         equals( jQuery("#ap").children().length, 1, "Check filtered remove" );
1083
1084         jQuery("#ap").children()[method]("a, code");
1085         equals( jQuery("#ap").children().length, 0, "Check multi-filtered remove" );
1086
1087         // using contents will get comments regular, text, and comment nodes
1088         // Handle the case where no comment is in the document
1089         ok( jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment remove works" );
1090         jQuery("#nonnodes").contents()[method]();
1091         equals( jQuery("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" );
1092
1093         QUnit.reset();
1094
1095         var count = 0;
1096         var first = jQuery("#ap").children(":first");
1097         var cleanUp = first.click(function() { count++ })[method]().appendTo("body").click();
1098
1099         equals( method == "remove" ? 0 : 1, count );
1100
1101         cleanUp.detach();
1102 };
1103
1104 test("remove()", function() {
1105         testRemove("remove");
1106 });
1107
1108 test("detach()", function() {
1109         testRemove("detach");
1110 });
1111
1112 test("empty()", function() {
1113         expect(3);
1114         equals( jQuery("#ap").children().empty().text().length, 0, "Check text is removed" );
1115         equals( jQuery("#ap").children().length, 4, "Check elements are not removed" );
1116
1117         // using contents will get comments regular, text, and comment nodes
1118         var j = jQuery("#nonnodes").contents();
1119         j.empty();
1120         equals( j.html(), "", "Check node,textnode,comment empty works" );
1121 });
1122
1123 test("jQuery.cleanData", function() {
1124         expect(14);
1125
1126         var type, pos, div, child;
1127
1128         type = "remove";
1129
1130         // Should trigger 4 remove event
1131         div = getDiv().remove();
1132
1133         // Should both do nothing
1134         pos = "Outer";
1135         div.trigger("click");
1136
1137         pos = "Inner";
1138         div.children().trigger("click");
1139
1140         type = "empty";
1141         div = getDiv();
1142         child = div.children();
1143
1144         // Should trigger 2 remove event
1145         div.empty();
1146
1147         // Should trigger 1
1148         pos = "Outer";
1149         div.trigger("click");
1150
1151         // Should do nothing
1152         pos = "Inner";
1153         child.trigger("click");
1154
1155         // Should trigger 2
1156         div.remove();
1157
1158         type = "html";
1159
1160         div = getDiv();
1161         child = div.children();
1162
1163         // Should trigger 2 remove event
1164         div.html("<div></div>");
1165
1166         // Should trigger 1
1167         pos = "Outer";
1168         div.trigger("click");
1169
1170         // Should do nothing
1171         pos = "Inner";
1172         child.trigger("click");
1173
1174         // Should trigger 2
1175         div.remove();
1176
1177         function getDiv() {
1178                 var div = jQuery("<div class='outer'><div class='inner'></div></div>").click(function(){
1179                         ok( true, type + " " + pos + " Click event fired." );
1180                 }).focus(function(){
1181                         ok( true, type + " " + pos + " Focus event fired." );
1182                 }).find("div").click(function(){
1183                         ok( false, type + " " + pos + " Click event fired." );
1184                 }).focus(function(){
1185                         ok( false, type + " " + pos + " Focus event fired." );
1186                 }).end().appendTo("body");
1187
1188                 div[0].detachEvent = div[0].removeEventListener = function(t){
1189                         ok( true, type + " Outer " + t + " event unbound" );
1190                 };
1191
1192                 div[0].firstChild.detachEvent = div[0].firstChild.removeEventListener = function(t){
1193                         ok( true, type + " Inner " + t + " event unbound" );
1194                 };
1195
1196                 return div;
1197         }
1198 });