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