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