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