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