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