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