Added some .text(Function) tests.
[jquery.git] / test / unit / manipulation.js
1 module("manipulation");
2
3 var bareObj = function(value) { return value; };
4 var functionReturningObj = function(value) { return (function() { return value; }); };
5
6 test("text()", function() {
7         expect(2);
8         var expected = "This link has class=\"blog\": Simon Willison's Weblog";
9         equals( jQuery('#sap').text(), expected, 'Check for merged text of more then one element.' );
10
11         // Check serialization of text values
12         equals( jQuery(document.createTextNode("foo")).text(), "foo", "Text node was retreived from .text()." );
13 });
14
15 var testText = function(valueObj) {
16         expect(4);
17         var val = valueObj("<div><b>Hello</b> cruel world!</div>");
18         equals( jQuery("#foo").text(val)[0].innerHTML.replace(/>/g, "&gt;"), "&lt;div&gt;&lt;b&gt;Hello&lt;/b&gt; cruel world!&lt;/div&gt;", "Check escaped text" );
19
20         // using contents will get comments regular, text, and comment nodes
21         var j = jQuery("#nonnodes").contents();
22         j.text(valueObj("hi!"));
23         equals( jQuery(j[0]).text(), "hi!", "Check node,textnode,comment with text()" );
24         equals( j[1].nodeValue, " there ", "Check node,textnode,comment with text()" );
25         equals( j[2].nodeType, 8, "Check node,textnode,comment with text()" );
26 }
27
28 test("text(String)", function() {
29         testText(bareObj)
30 });
31
32 test("text(Function)", function() {
33         testText(functionReturningObj);
34 });
35
36 test("text(Function) with incoming value", function() {
37         expect(2);
38         
39         var old = "This link has class=\"blog\": Simon Willison's Weblog";
40         
41         jQuery('#sap').text(function(i, val) {
42                 equals( val, old, "Make sure the incoming value is correct." );
43                 return "foobar";
44         });
45         
46         equals( jQuery("#sap").text(), "foobar", 'Check for merged text of more then one element.' );
47         
48         reset();
49 });
50
51 var testWrap = function(val) {
52         expect(18);
53         var defaultText = 'Try them out:'
54         var result = jQuery('#first').wrap(val( '<div class="red"><span></span></div>' )).text();
55         equals( defaultText, result, 'Check for wrapping of on-the-fly html' );
56         ok( jQuery('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
57
58         reset();
59         var defaultText = 'Try them out:'
60         var result = jQuery('#first').wrap(val( document.getElementById('empty') )).parent();
61         ok( result.is('ol'), 'Check for element wrapping' );
62         equals( result.text(), defaultText, 'Check for element wrapping' );
63
64         reset();
65         jQuery('#check1').click(function() {
66                 var checkbox = this;
67                 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
68                 jQuery(checkbox).wrap(val( '<div id="c1" style="display:none;"></div>' ));
69                 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
70         }).click();
71
72         // using contents will get comments regular, text, and comment nodes
73         var j = jQuery("#nonnodes").contents();
74         j.wrap(val( "<i></i>" ));
75         equals( jQuery("#nonnodes > i").length, 3, "Check node,textnode,comment wraps ok" );
76         equals( jQuery("#nonnodes > i").text(), j.text(), "Check node,textnode,comment wraps doesn't hurt text" );
77
78         // Try wrapping a disconnected node
79         j = jQuery("<label/>").wrap(val( "<li/>" ));
80         equals( j[0].nodeName.toUpperCase(), "LABEL", "Element is a label" );
81         equals( j[0].parentNode.nodeName.toUpperCase(), "LI", "Element has been wrapped" );
82
83         // Wrap an element containing a text node
84         j = jQuery("<span/>").wrap("<div>test</div>");
85         equals( j[0].previousSibling.nodeType, 3, "Make sure the previous node is a text element" );
86         equals( j[0].parentNode.nodeName.toUpperCase(), "DIV", "And that we're in the div element." );
87
88         // Try to wrap an element with multiple elements (should fail)
89         j = jQuery("<div><span></span></div>").children().wrap("<p></p><div></div>");
90         equals( j[0].parentNode.parentNode.childNodes.length, 1, "There should only be one element wrapping." );
91         equals( j.length, 1, "There should only be one element (no cloning)." );
92         equals( j[0].parentNode.nodeName.toUpperCase(), "P", "The span should be in the paragraph." );
93
94         // Wrap an element with a jQuery set
95         j = jQuery("<span/>").wrap(jQuery("<div></div>"));
96         equals( j[0].parentNode.nodeName.toLowerCase(), "div", "Wrapping works." );
97
98         // Wrap an element with a jQuery set and event
99         result = jQuery("<div></div>").click(function(){
100                 ok(true, "Event triggered.");
101         });
102
103         j = jQuery("<span/>").wrap(result);
104         equals( j[0].parentNode.nodeName.toLowerCase(), "div", "Wrapping works." );
105
106         j.parent().trigger("click");
107 }
108
109 test("wrap(String|Element)", function() {
110         testWrap(bareObj);
111 });
112
113 test("wrap(Function)", function() {
114         testWrap(functionReturningObj);
115 })
116
117 var testWrapAll = function(val) {
118         expect(8);
119         var prev = jQuery("#firstp")[0].previousSibling;
120         var p = jQuery("#firstp,#first")[0].parentNode;
121
122         var result = jQuery('#firstp,#first').wrapAll(val( '<div class="red"><div class="tmp"></div></div>' ));
123         equals( result.parent().length, 1, 'Check for wrapping of on-the-fly html' );
124         ok( jQuery('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
125         ok( jQuery('#firstp').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
126         equals( jQuery("#first").parent().parent()[0].previousSibling, prev, "Correct Previous Sibling" );
127         equals( jQuery("#first").parent().parent()[0].parentNode, p, "Correct Parent" );
128
129         reset();
130         var prev = jQuery("#firstp")[0].previousSibling;
131         var p = jQuery("#first")[0].parentNode;
132         var result = jQuery('#firstp,#first').wrapAll(val( document.getElementById('empty') ));
133         equals( jQuery("#first").parent()[0], jQuery("#firstp").parent()[0], "Same Parent" );
134         equals( jQuery("#first").parent()[0].previousSibling, prev, "Correct Previous Sibling" );
135         equals( jQuery("#first").parent()[0].parentNode, p, "Correct Parent" );
136 }
137
138 test("wrapAll(String|Element)", function() {
139         testWrapAll(bareObj);
140 });
141
142 // TODO: Figure out why each(wrapAll) is not equivalent to wrapAll
143 // test("wrapAll(Function)", function() {
144 //      testWrapAll(functionReturningObj);
145 // })
146
147 var testWrapInner = function(val) {
148         expect(6);
149         var num = jQuery("#first").children().length;
150         var result = jQuery('#first').wrapInner('<div class="red"><div id="tmp"></div></div>');
151         equals( jQuery("#first").children().length, 1, "Only one child" );
152         ok( jQuery("#first").children().is(".red"), "Verify Right Element" );
153         equals( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" );
154
155         reset();
156         var num = jQuery("#first").children().length;
157         var result = jQuery('#first').wrapInner(document.getElementById('empty'));
158         equals( jQuery("#first").children().length, 1, "Only one child" );
159         ok( jQuery("#first").children().is("#empty"), "Verify Right Element" );
160         equals( jQuery("#first").children().children().length, num, "Verify Elements Intact" );
161 }
162
163 test("wrapInner(String|Element)", function() {
164         testWrapInner(bareObj);
165 });
166
167 // TODO: wrapInner uses wrapAll -- get wrapAll working with Function
168 // test("wrapInner(Function)", function() {
169 //      testWrapInner(functionReturningObj)
170 // })
171
172 test("unwrap()", function() {
173         expect(9);
174
175         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>');
176
177         var abcd = jQuery('#unwrap1 > span, #unwrap2 > span').get(),
178                 abcdef = jQuery('#unwrap span').get();
179
180         equals( jQuery('#unwrap1 span').add('#unwrap2 span:first').unwrap().length, 3, 'make #unwrap1 and #unwrap2 go away' );
181         same( jQuery('#unwrap > span').get(), abcd, 'all four spans should still exist' );
182
183         same( jQuery('#unwrap3 span').unwrap().get(), jQuery('#unwrap3 > span').get(), 'make all b in #unwrap3 go away' );
184
185         same( jQuery('#unwrap3 span').unwrap().get(), jQuery('#unwrap > span.unwrap3').get(), 'make #unwrap3 go away' );
186
187         same( jQuery('#unwrap').children().get(), abcdef, '#unwrap only contains 6 child spans' );
188
189         same( jQuery('#unwrap > span').unwrap().get(), jQuery('body > span.unwrap').get(), 'make the 6 spans become children of body' );
190
191         same( jQuery('body > span.unwrap').unwrap().get(), jQuery('body > span.unwrap').get(), 'can\'t unwrap children of body' );
192         same( jQuery('body > span.unwrap').unwrap().get(), abcdef, 'can\'t unwrap children of body' );
193
194         same( jQuery('body > span.unwrap').get(), abcdef, 'body contains 6 .unwrap child spans' );
195
196         jQuery('body > span.unwrap').remove();
197 });
198
199 var testAppend = function(valueObj) {
200         expect(22);
201         var defaultText = 'Try them out:'
202         var result = jQuery('#first').append(valueObj('<b>buga</b>'));
203         equals( result.text(), defaultText + 'buga', 'Check if text appending works' );
204         equals( jQuery('#select3').append(valueObj('<option value="appendTest">Append Test</option>')).find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
205
206         reset();
207         var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
208         jQuery('#sap').append(valueObj(document.getElementById('first')));
209         equals( expected, jQuery('#sap').text(), "Check for appending of element" );
210
211         reset();
212         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
213         jQuery('#sap').append(valueObj([document.getElementById('first'), document.getElementById('yahoo')]));
214         equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
215
216         reset();
217         expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
218         jQuery('#sap').append(valueObj(jQuery("#first, #yahoo")));
219         equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
220
221         reset();
222         jQuery("#sap").append(valueObj( 5 ));
223         ok( jQuery("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
224
225         reset();
226         jQuery("#sap").append(valueObj( " text with spaces " ));
227         ok( jQuery("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );
228
229         reset();
230         ok( jQuery("#sap").append(valueObj( [] )), "Check for appending an empty array." );
231         ok( jQuery("#sap").append(valueObj( "" )), "Check for appending an empty string." );
232         ok( jQuery("#sap").append(valueObj( document.getElementsByTagName("foo") )), "Check for appending an empty nodelist." );
233
234         reset();
235         jQuery("#sap").append(valueObj( document.getElementById('form') ));
236         equals( jQuery("#sap>form").size(), 1, "Check for appending a form" ); // Bug #910
237
238         reset();
239         var pass = true;
240         try {
241                 jQuery( jQuery("#iframe")[0].contentWindow.document.body ).append(valueObj( "<div>test</div>" ));
242         } catch(e) {
243                 pass = false;
244         }
245
246         ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
247
248         reset();
249         jQuery('<fieldset/>').appendTo('#form').append(valueObj( '<legend id="legend">test</legend>' ));
250         t( 'Append legend', '#legend', ['legend'] );
251
252         reset();
253         jQuery('#select1').append(valueObj( '<OPTION>Test</OPTION>' ));
254         equals( jQuery('#select1 option:last').text(), "Test", "Appending &lt;OPTION&gt; (all caps)" );
255
256         jQuery('#table').append(valueObj( '<colgroup></colgroup>' ));
257         ok( jQuery('#table colgroup').length, "Append colgroup" );
258
259         jQuery('#table colgroup').append(valueObj( '<col/>' ));
260         ok( jQuery('#table colgroup col').length, "Append col" );
261
262         reset();
263         jQuery('#table').append(valueObj( '<caption></caption>' ));
264         ok( jQuery('#table caption').length, "Append caption" );
265
266         reset();
267         jQuery('form:last')
268                 .append(valueObj( '<select id="appendSelect1"></select>' ))
269                 .append(valueObj( '<select id="appendSelect2"><option>Test</option></select>' ));
270
271         t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );
272
273         equals( "Two nodes", jQuery('<div />').append("Two", " nodes").text(), "Appending two text nodes (#4011)" );
274
275         // using contents will get comments regular, text, and comment nodes
276         var j = jQuery("#nonnodes").contents();
277         var d = jQuery("<div/>").appendTo("#nonnodes").append(j);
278         equals( jQuery("#nonnodes").length, 1, "Check node,textnode,comment append moved leaving just the div" );
279         ok( d.contents().length >= 2, "Check node,textnode,comment append works" );
280         d.contents().appendTo("#nonnodes");
281         d.remove();
282         ok( jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment append cleanup worked" );
283 }
284
285 test("append(String|Element|Array&lt;Element&gt;|jQuery)", function() {
286         testAppend(bareObj);
287 });
288
289 test("append(Function)", function() {
290         testAppend(functionReturningObj);
291 })
292
293 test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
294         expect(12);
295         var defaultText = 'Try them out:'
296         jQuery('<b>buga</b>').appendTo('#first');
297         equals( jQuery("#first").text(), defaultText + 'buga', 'Check if text appending works' );
298         equals( jQuery('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
299
300         reset();
301         var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
302         jQuery(document.getElementById('first')).appendTo('#sap');
303         equals( expected, jQuery('#sap').text(), "Check for appending of element" );
304
305         reset();
306         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
307         jQuery([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
308         equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
309
310         reset();
311         ok( jQuery(document.createElement("script")).appendTo("body").length, "Make sure a disconnected script can be appended." );
312
313         reset();
314         expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
315         jQuery("#first, #yahoo").appendTo('#sap');
316         equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
317
318         reset();
319         jQuery('#select1').appendTo('#foo');
320         t( 'Append select', '#foo select', ['select1'] );
321
322         reset();
323         var div = jQuery("<div/>").click(function(){
324                 ok(true, "Running a cloned click.");
325         });
326         div.appendTo("#main, #moretests");
327
328         jQuery("#main div:last").click();
329         jQuery("#moretests div:last").click();
330
331         reset();
332         var div = jQuery("<div/>").appendTo("#main, #moretests");
333
334         equals( div.length, 2, "appendTo returns the inserted elements" );
335
336         div.addClass("test");
337
338         ok( jQuery("#main div:last").hasClass("test"), "appendTo element was modified after the insertion" );
339         ok( jQuery("#moretests div:last").hasClass("test"), "appendTo element was modified after the insertion" );
340
341         reset();
342 });
343
344 var testPrepend = function(val) {
345         expect(5);
346         var defaultText = 'Try them out:'
347         var result = jQuery('#first').prepend(val( '<b>buga</b>' ));
348         equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
349         equals( jQuery('#select3').prepend(val( '<option value="prependTest">Prepend Test</option>' )).find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
350
351         reset();
352         var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
353         jQuery('#sap').prepend(val( document.getElementById('first') ));
354         equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
355
356         reset();
357         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
358         jQuery('#sap').prepend(val( [document.getElementById('first'), document.getElementById('yahoo')] ));
359         equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
360
361         reset();
362         expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
363         jQuery('#sap').prepend(val( jQuery("#first, #yahoo") ));
364         equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
365 }
366
367 test("prepend(String|Element|Array&lt;Element&gt;|jQuery)", function() {
368         testPrepend(bareObj);
369 });
370
371 test("prepend(Function)", function() {
372         testPrepend(functionReturningObj);
373 })
374
375 test("prependTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
376         expect(6);
377         var defaultText = 'Try them out:'
378         jQuery('<b>buga</b>').prependTo('#first');
379         equals( jQuery('#first').text(), 'buga' + defaultText, 'Check if text prepending works' );
380         equals( jQuery('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
381
382         reset();
383         var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
384         jQuery(document.getElementById('first')).prependTo('#sap');
385         equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
386
387         reset();
388         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
389         jQuery([document.getElementById('first'), document.getElementById('yahoo')]).prependTo('#sap');
390         equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
391
392         reset();
393         expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
394         jQuery("#first, #yahoo").prependTo('#sap');
395         equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
396
397         reset();
398         jQuery('<select id="prependSelect1"></select>').prependTo('form:last');
399         jQuery('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');
400
401         t( "Prepend Select", "#prependSelect2, #prependSelect1", ["prependSelect2", "prependSelect1"] );
402 });
403
404 var testBefore = function(val) {
405         expect(6);
406         var expected = 'This is a normal link: bugaYahoo';
407         jQuery('#yahoo').before(val( '<b>buga</b>' ));
408         equals( expected, jQuery('#en').text(), 'Insert String before' );
409
410         reset();
411         expected = "This is a normal link: Try them out:Yahoo";
412         jQuery('#yahoo').before(val( document.getElementById('first') ));
413         equals( expected, jQuery('#en').text(), "Insert element before" );
414
415         reset();
416         expected = "This is a normal link: Try them out:diveintomarkYahoo";
417         jQuery('#yahoo').before(val( [document.getElementById('first'), document.getElementById('mark')] ));
418         equals( expected, jQuery('#en').text(), "Insert array of elements before" );
419
420         reset();
421         expected = "This is a normal link: diveintomarkTry them out:Yahoo";
422         jQuery('#yahoo').before(val( jQuery("#first, #mark") ));
423         equals( expected, jQuery('#en').text(), "Insert jQuery before" );
424
425         var set = jQuery("<div/>").before("<span>test</span>");
426         equals( set[0].nodeName.toLowerCase(), "span", "Insert the element before the disconnected node." );
427         equals( set.length, 2, "Insert the element before the disconnected node." );
428 }
429
430 test("before(String|Element|Array&lt;Element&gt;|jQuery)", function() {
431         testBefore(bareObj);
432 });
433
434 test("before(Function)", function() {
435         testBefore(functionReturningObj);
436 })
437
438 test("insertBefore(String|Element|Array&lt;Element&gt;|jQuery)", function() {
439         expect(4);
440         var expected = 'This is a normal link: bugaYahoo';
441         jQuery('<b>buga</b>').insertBefore('#yahoo');
442         equals( expected, jQuery('#en').text(), 'Insert String before' );
443
444         reset();
445         expected = "This is a normal link: Try them out:Yahoo";
446         jQuery(document.getElementById('first')).insertBefore('#yahoo');
447         equals( expected, jQuery('#en').text(), "Insert element before" );
448
449         reset();
450         expected = "This is a normal link: Try them out:diveintomarkYahoo";
451         jQuery([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
452         equals( expected, jQuery('#en').text(), "Insert array of elements before" );
453
454         reset();
455         expected = "This is a normal link: diveintomarkTry them out:Yahoo";
456         jQuery("#first, #mark").insertBefore('#yahoo');
457         equals( expected, jQuery('#en').text(), "Insert jQuery before" );
458 });
459
460 var testAfter = function(val) {
461         expect(6);
462         var expected = 'This is a normal link: Yahoobuga';
463         jQuery('#yahoo').after(val( '<b>buga</b>' ));
464         equals( expected, jQuery('#en').text(), 'Insert String after' );
465
466         reset();
467         expected = "This is a normal link: YahooTry them out:";
468         jQuery('#yahoo').after(val( document.getElementById('first') ));
469         equals( expected, jQuery('#en').text(), "Insert element after" );
470
471         reset();
472         expected = "This is a normal link: YahooTry them out:diveintomark";
473         jQuery('#yahoo').after(val( [document.getElementById('first'), document.getElementById('mark')] ));
474         equals( expected, jQuery('#en').text(), "Insert array of elements after" );
475
476         reset();
477         expected = "This is a normal link: YahoodiveintomarkTry them out:";
478         jQuery('#yahoo').after(val( jQuery("#first, #mark") ));
479         equals( expected, jQuery('#en').text(), "Insert jQuery after" );
480
481         var set = jQuery("<div/>").after("<span>test</span>");
482         equals( set[1].nodeName.toLowerCase(), "span", "Insert the element after the disconnected node." );
483         equals( set.length, 2, "Insert the element after the disconnected node." );
484 };
485
486 test("after(String|Element|Array&lt;Element&gt;|jQuery)", function() {
487         testAfter(bareObj);
488 });
489
490 test("after(Function)", function() {
491         testAfter(functionReturningObj);
492 })
493
494 test("insertAfter(String|Element|Array&lt;Element&gt;|jQuery)", function() {
495         expect(4);
496         var expected = 'This is a normal link: Yahoobuga';
497         jQuery('<b>buga</b>').insertAfter('#yahoo');
498         equals( expected, jQuery('#en').text(), 'Insert String after' );
499
500         reset();
501         expected = "This is a normal link: YahooTry them out:";
502         jQuery(document.getElementById('first')).insertAfter('#yahoo');
503         equals( expected, jQuery('#en').text(), "Insert element after" );
504
505         reset();
506         expected = "This is a normal link: YahooTry them out:diveintomark";
507         jQuery([document.getElementById('first'), document.getElementById('mark')]).insertAfter('#yahoo');
508         equals( expected, jQuery('#en').text(), "Insert array of elements after" );
509
510         reset();
511         expected = "This is a normal link: YahoodiveintomarkTry them out:";
512         jQuery("#first, #mark").insertAfter('#yahoo');
513         equals( expected, jQuery('#en').text(), "Insert jQuery after" );
514 });
515
516 var testReplaceWith = function(val) {
517         expect(14);
518         jQuery('#yahoo').replaceWith(val( '<b id="replace">buga</b>' ));
519         ok( jQuery("#replace")[0], 'Replace element with string' );
520         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
521
522         reset();
523         jQuery('#yahoo').replaceWith(val( document.getElementById('first') ));
524         ok( jQuery("#first")[0], 'Replace element with element' );
525         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
526
527         reset();
528         jQuery('#yahoo').replaceWith(val( [document.getElementById('first'), document.getElementById('mark')] ));
529         ok( jQuery("#first")[0], 'Replace element with array of elements' );
530         ok( jQuery("#mark")[0], 'Replace element with array of elements' );
531         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
532
533         reset();
534         jQuery('#yahoo').replaceWith(val( jQuery("#first, #mark") ));
535         ok( jQuery("#first")[0], 'Replace element with set of elements' );
536         ok( jQuery("#mark")[0], 'Replace element with set of elements' );
537         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
538
539         var set = jQuery("<div/>").replaceWith(val("<span>test</span>"));
540         equals( set[0].nodeName.toLowerCase(), "span", "Replace the disconnected node." );
541         equals( set.length, 1, "Replace the disconnected node." );
542
543         var $div = jQuery("<div class='replacewith'></div>").appendTo("body");
544         $div.replaceWith("<div class='replacewith'></div><script>" +
545                 "equals(jQuery('.replacewith').length, 1, 'Check number of elements in page.');" +
546                 "</script>");
547         equals(jQuery('.replacewith').length, 1, 'Check number of elements in page.');
548         jQuery('.replacewith').remove();
549 }
550
551 test("replaceWith(String|Element|Array&lt;Element&gt;|jQuery)", function() {
552         testReplaceWith(bareObj);
553 });
554
555 test("replaceWith(Function)", function() {
556         testReplaceWith(functionReturningObj);
557 })
558
559 test("replaceAll(String|Element|Array&lt;Element&gt;|jQuery)", function() {
560         expect(10);
561         jQuery('<b id="replace">buga</b>').replaceAll("#yahoo");
562         ok( jQuery("#replace")[0], 'Replace element with string' );
563         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
564
565         reset();
566         jQuery(document.getElementById('first')).replaceAll("#yahoo");
567         ok( jQuery("#first")[0], 'Replace element with element' );
568         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
569
570         reset();
571         jQuery([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo");
572         ok( jQuery("#first")[0], 'Replace element with array of elements' );
573         ok( jQuery("#mark")[0], 'Replace element with array of elements' );
574         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
575
576         reset();
577         jQuery("#first, #mark").replaceAll("#yahoo");
578         ok( jQuery("#first")[0], 'Replace element with set of elements' );
579         ok( jQuery("#mark")[0], 'Replace element with set of elements' );
580         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
581 });
582
583 test("clone()", function() {
584         expect(30);
585         equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Assert text for #en' );
586         var clone = jQuery('#yahoo').clone();
587         equals( 'Try them out:Yahoo', jQuery('#first').append(clone).text(), 'Check for clone' );
588         equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Reassert text for #en' );
589
590         var cloneTags = [
591                 "<table/>", "<tr/>", "<td/>", "<div/>",
592                 "<button/>", "<ul/>", "<ol/>", "<li/>",
593                 "<input type='checkbox' />", "<select/>", "<option/>", "<textarea/>",
594                 "<tbody/>", "<thead/>", "<tfoot/>", "<iframe/>"
595         ];
596         for (var i = 0; i < cloneTags.length; i++) {
597                 var j = jQuery(cloneTags[i]);
598                 equals( j[0].tagName, j.clone()[0].tagName, 'Clone a &lt;' + cloneTags[i].substring(1));
599         }
600
601         // using contents will get comments regular, text, and comment nodes
602         var cl = jQuery("#nonnodes").contents().clone();
603         ok( cl.length >= 2, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
604
605         var div = jQuery("<div><ul><li>test</li></ul></div>").click(function(){
606                 ok( true, "Bound event still exists." );
607         });
608
609         div = div.clone(true).clone(true);
610         equals( div.length, 1, "One element cloned" );
611         equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
612         div.trigger("click");
613
614         div = jQuery("<div/>").append([ document.createElement("table"), document.createElement("table") ]);
615         div.find("table").click(function(){
616                 ok( true, "Bound event still exists." );
617         });
618
619         div = div.clone(true);
620         equals( div.length, 1, "One element cloned" );
621         equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
622         div.find("table:last").trigger("click");
623
624         div = jQuery("<div/>").html('<object height="355" width="425">  <param name="movie" value="http://www.youtube.com/v/JikaHBDoV3k&amp;hl=en">  <param name="wmode" value="transparent"> </object>');
625
626         div = div.clone(true);
627         equals( div.length, 1, "One element cloned" );
628         equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
629
630         div = jQuery("<div/>").data({ a: true, b: true });
631         div = div.clone(true);
632         equals( div.data("a"), true, "Data cloned." );
633         equals( div.data("b"), true, "Data cloned." );
634 });
635
636 if (!isLocal) {
637 test("clone() on XML nodes", function() {
638         expect(2);
639         stop();
640         jQuery.get("data/dashboard.xml", function (xml) {
641                 var root = jQuery(xml.documentElement).clone();
642                 var origTab = jQuery("tab", xml).eq(0);
643                 var cloneTab = jQuery("tab", root).eq(0);
644                 origTab.text("origval");
645                 cloneTab.text("cloneval");
646                 equals(origTab.text(), "origval", "Check original XML node was correctly set");
647                 equals(cloneTab.text(), "cloneval", "Check cloned XML node was correctly set");
648                 start();
649         });
650 });
651 }
652
653 var testHtml = function(valueObj) {
654         expect(22);
655
656         jQuery.scriptorder = 0;
657
658         var div = jQuery("#main > div");
659         div.html(valueObj("<b>test</b>"));
660         var pass = true;
661         for ( var i = 0; i < div.size(); i++ ) {
662                 if ( div.get(i).childNodes.length != 1 ) pass = false;
663         }
664         ok( pass, "Set HTML" );
665
666         reset();
667         // using contents will get comments regular, text, and comment nodes
668         var j = jQuery("#nonnodes").contents();
669         j.html(valueObj("<b>bold</b>"));
670
671         // this is needed, or the expando added by jQuery unique will yield a different html
672         j.find('b').removeData();
673         equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
674
675         jQuery("#main").html(valueObj("<select/>"));
676         jQuery("#main select").html(valueObj("<option>O1</option><option selected='selected'>O2</option><option>O3</option>"));
677         equals( jQuery("#main select").val(), "O2", "Selected option correct" );
678
679         var $div = jQuery('<div />');
680         equals( $div.html(valueObj( 5 )).html(), '5', 'Setting a number as html' );
681         equals( $div.html(valueObj( 0 )).html(), '0', 'Setting a zero as html' );
682
683         var $div2 = jQuery('<div/>'), insert = "&lt;div&gt;hello1&lt;/div&gt;";
684         equals( $div2.html(insert).html(), insert, "Verify escaped insertion." );
685         equals( $div2.html("x" + insert).html(), "x" + insert, "Verify escaped insertion." );
686         equals( $div2.html(" " + insert).html(), " " + insert, "Verify escaped insertion." );
687
688         var map = jQuery("<map/>").html(valueObj("<area id='map01' shape='rect' coords='50,50,150,150' href='http://www.jquery.com/' alt='jQuery'>"));
689
690         equals( map[0].childNodes.length, 1, "The area was inserted." );
691         equals( map[0].firstChild.nodeName.toLowerCase(), "area", "The area was inserted." );
692
693         reset();
694
695         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>'));
696
697         stop();
698
699         jQuery("#main").html(valueObj('<script type="text/javascript">ok( true, "jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975 (1)" );</script>'));
700
701         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>'));
702
703         // it was decided that waiting to execute ALL scripts makes sense since nested ones have to wait anyway so this test case is changed, see #1959
704         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>"));
705
706         setTimeout( start, 100 );
707 }
708
709 test("html(String)", function() {
710         testHtml(bareObj);
711 });
712
713 test("html(Function)", function() {
714         testHtml(functionReturningObj);
715 });
716
717 var testRemove = function(method) {
718         expect(9);
719
720         var first = jQuery("#ap").children(":first");
721         first.data("foo", "bar");
722
723         jQuery("#ap").children()[method]();
724         ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
725         equals( jQuery("#ap").children().length, 0, "Check remove" );
726
727         equals( first.data("foo"), method == "remove" ? null : "bar" );
728
729         reset();
730         jQuery("#ap").children()[method]("a");
731         ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
732         equals( jQuery("#ap").children().length, 1, "Check filtered remove" );
733
734         jQuery("#ap").children()[method]("a, code");
735         equals( jQuery("#ap").children().length, 0, "Check multi-filtered remove" );
736
737         // using contents will get comments regular, text, and comment nodes
738         equals( jQuery("#nonnodes").contents().length, 3, "Check node,textnode,comment remove works" );
739         jQuery("#nonnodes").contents()[method]();
740         equals( jQuery("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" );
741
742         reset();
743
744         var count = 0;
745         var first = jQuery("#ap").children(":first");
746         var cleanUp = first.click(function() { count++ })[method]().appendTo("body").click();
747         
748         equals( method == "remove" ? 0 : 1, count );
749         
750         cleanUp.detach();
751 };
752
753 test("remove()", function() {
754         testRemove("remove");
755 });
756
757 test("detach()", function() {
758         testRemove("detach");
759 });
760
761 test("empty()", function() {
762         expect(3);
763         equals( jQuery("#ap").children().empty().text().length, 0, "Check text is removed" );
764         equals( jQuery("#ap").children().length, 4, "Check elements are not removed" );
765
766         // using contents will get comments regular, text, and comment nodes
767         var j = jQuery("#nonnodes").contents();
768         j.empty();
769         equals( j.html(), "", "Check node,textnode,comment empty works" );
770 });
771