Made some additional tests for #5785.
[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("append(Function) with incoming value", function() {
294         expect(12);
295         
296         var defaultText = 'Try them out:', old = jQuery("#first").html();
297         
298         var result = jQuery('#first').append(function(i, val){
299                 equals( val, old, "Make sure the incoming value is correct." );
300                 return '<b>buga</b>';
301         });
302         equals( result.text(), defaultText + 'buga', 'Check if text appending works' );
303         
304         var select = jQuery('#select3');
305         old = select.html();
306         
307         equals( select.append(function(i, val){
308                 equals( val, old, "Make sure the incoming value is correct." );
309                 return '<option value="appendTest">Append Test</option>';
310         }).find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
311
312         reset();
313         var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
314         old = jQuery("#sap").html();
315         
316         jQuery('#sap').append(function(i, val){
317                 equals( val, old, "Make sure the incoming value is correct." );
318                 return document.getElementById('first');
319         });
320         equals( expected, jQuery('#sap').text(), "Check for appending of element" );
321
322         reset();
323         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
324         old = jQuery("#sap").html();
325         
326         jQuery('#sap').append(function(i, val){
327                 equals( val, old, "Make sure the incoming value is correct." );
328                 return [document.getElementById('first'), document.getElementById('yahoo')];
329         });
330         equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
331
332         reset();
333         expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
334         old = jQuery("#sap").html();
335         
336         jQuery('#sap').append(function(i, val){
337                 equals( val, old, "Make sure the incoming value is correct." );
338                 return jQuery("#first, #yahoo");
339         });
340         equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
341
342         reset();
343         old = jQuery("#sap").html();
344         
345         jQuery("#sap").append(function(i, val){
346                 equals( val, old, "Make sure the incoming value is correct." );
347                 return 5;
348         });
349         ok( jQuery("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );        
350         
351         reset();
352 });
353
354 test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
355         expect(12);
356         var defaultText = 'Try them out:'
357         jQuery('<b>buga</b>').appendTo('#first');
358         equals( jQuery("#first").text(), defaultText + 'buga', 'Check if text appending works' );
359         equals( jQuery('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
360
361         reset();
362         var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
363         jQuery(document.getElementById('first')).appendTo('#sap');
364         equals( expected, jQuery('#sap').text(), "Check for appending of element" );
365
366         reset();
367         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
368         jQuery([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
369         equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
370
371         reset();
372         ok( jQuery(document.createElement("script")).appendTo("body").length, "Make sure a disconnected script can be appended." );
373
374         reset();
375         expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
376         jQuery("#first, #yahoo").appendTo('#sap');
377         equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
378
379         reset();
380         jQuery('#select1').appendTo('#foo');
381         t( 'Append select', '#foo select', ['select1'] );
382
383         reset();
384         var div = jQuery("<div/>").click(function(){
385                 ok(true, "Running a cloned click.");
386         });
387         div.appendTo("#main, #moretests");
388
389         jQuery("#main div:last").click();
390         jQuery("#moretests div:last").click();
391
392         reset();
393         var div = jQuery("<div/>").appendTo("#main, #moretests");
394
395         equals( div.length, 2, "appendTo returns the inserted elements" );
396
397         div.addClass("test");
398
399         ok( jQuery("#main div:last").hasClass("test"), "appendTo element was modified after the insertion" );
400         ok( jQuery("#moretests div:last").hasClass("test"), "appendTo element was modified after the insertion" );
401
402         reset();
403 });
404
405 var testPrepend = function(val) {
406         expect(5);
407         var defaultText = 'Try them out:'
408         var result = jQuery('#first').prepend(val( '<b>buga</b>' ));
409         equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
410         equals( jQuery('#select3').prepend(val( '<option value="prependTest">Prepend Test</option>' )).find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
411
412         reset();
413         var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
414         jQuery('#sap').prepend(val( document.getElementById('first') ));
415         equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
416
417         reset();
418         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
419         jQuery('#sap').prepend(val( [document.getElementById('first'), document.getElementById('yahoo')] ));
420         equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
421
422         reset();
423         expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
424         jQuery('#sap').prepend(val( jQuery("#first, #yahoo") ));
425         equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
426 };
427
428 test("prepend(String|Element|Array&lt;Element&gt;|jQuery)", function() {
429         testPrepend(bareObj);
430 });
431
432 test("prepend(Function)", function() {
433         testPrepend(functionReturningObj);
434 });
435
436 test("prepend(Function) with incoming value", function() {
437         expect(10);
438         
439         var defaultText = 'Try them out:', old = jQuery('#first').html();
440         var result = jQuery('#first').prepend(function(i, val) {
441                 equals( val, old, "Make sure the incoming value is correct." );
442                 return '<b>buga</b>';
443         });
444         equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
445         
446         old = jQuery("#select3").html();
447         
448         equals( jQuery('#select3').prepend(function(i, val) {
449                 equals( val, old, "Make sure the incoming value is correct." );
450                 return '<option value="prependTest">Prepend Test</option>';
451         }).find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
452
453         reset();
454         var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
455         old = jQuery('#sap').html();
456         
457         jQuery('#sap').prepend(function(i, val) {
458                 equals( val, old, "Make sure the incoming value is correct." );
459                 return document.getElementById('first');
460         });
461         
462         equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
463
464         reset();
465         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
466         old = jQuery('#sap').html();
467         
468         jQuery('#sap').prepend(function(i, val) {
469                 equals( val, old, "Make sure the incoming value is correct." );
470                 return [document.getElementById('first'), document.getElementById('yahoo')];
471         });
472         
473         equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
474
475         reset();
476         expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
477         old = jQuery('#sap').html();
478         
479         jQuery('#sap').prepend(function(i, val) {
480                 equals( val, old, "Make sure the incoming value is correct." );
481                 return jQuery("#first, #yahoo");
482         });
483         
484         equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );     
485 });
486
487 test("prependTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
488         expect(6);
489         var defaultText = 'Try them out:'
490         jQuery('<b>buga</b>').prependTo('#first');
491         equals( jQuery('#first').text(), 'buga' + defaultText, 'Check if text prepending works' );
492         equals( jQuery('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
493
494         reset();
495         var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
496         jQuery(document.getElementById('first')).prependTo('#sap');
497         equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
498
499         reset();
500         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
501         jQuery([document.getElementById('first'), document.getElementById('yahoo')]).prependTo('#sap');
502         equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
503
504         reset();
505         expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
506         jQuery("#first, #yahoo").prependTo('#sap');
507         equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
508
509         reset();
510         jQuery('<select id="prependSelect1"></select>').prependTo('form:last');
511         jQuery('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');
512
513         t( "Prepend Select", "#prependSelect2, #prependSelect1", ["prependSelect2", "prependSelect1"] );
514 });
515
516 var testBefore = function(val) {
517         expect(6);
518         var expected = 'This is a normal link: bugaYahoo';
519         jQuery('#yahoo').before(val( '<b>buga</b>' ));
520         equals( expected, jQuery('#en').text(), 'Insert String before' );
521
522         reset();
523         expected = "This is a normal link: Try them out:Yahoo";
524         jQuery('#yahoo').before(val( document.getElementById('first') ));
525         equals( expected, jQuery('#en').text(), "Insert element before" );
526
527         reset();
528         expected = "This is a normal link: Try them out:diveintomarkYahoo";
529         jQuery('#yahoo').before(val( [document.getElementById('first'), document.getElementById('mark')] ));
530         equals( expected, jQuery('#en').text(), "Insert array of elements before" );
531
532         reset();
533         expected = "This is a normal link: diveintomarkTry them out:Yahoo";
534         jQuery('#yahoo').before(val( jQuery("#first, #mark") ));
535         equals( expected, jQuery('#en').text(), "Insert jQuery before" );
536
537         var set = jQuery("<div/>").before("<span>test</span>");
538         equals( set[0].nodeName.toLowerCase(), "span", "Insert the element before the disconnected node." );
539         equals( set.length, 2, "Insert the element before the disconnected node." );
540 }
541
542 test("before(String|Element|Array&lt;Element&gt;|jQuery)", function() {
543         testBefore(bareObj);
544 });
545
546 test("before(Function)", function() {
547         testBefore(functionReturningObj);
548 })
549
550 test("insertBefore(String|Element|Array&lt;Element&gt;|jQuery)", function() {
551         expect(4);
552         var expected = 'This is a normal link: bugaYahoo';
553         jQuery('<b>buga</b>').insertBefore('#yahoo');
554         equals( expected, jQuery('#en').text(), 'Insert String before' );
555
556         reset();
557         expected = "This is a normal link: Try them out:Yahoo";
558         jQuery(document.getElementById('first')).insertBefore('#yahoo');
559         equals( expected, jQuery('#en').text(), "Insert element before" );
560
561         reset();
562         expected = "This is a normal link: Try them out:diveintomarkYahoo";
563         jQuery([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
564         equals( expected, jQuery('#en').text(), "Insert array of elements before" );
565
566         reset();
567         expected = "This is a normal link: diveintomarkTry them out:Yahoo";
568         jQuery("#first, #mark").insertBefore('#yahoo');
569         equals( expected, jQuery('#en').text(), "Insert jQuery before" );
570 });
571
572 var testAfter = function(val) {
573         expect(6);
574         var expected = 'This is a normal link: Yahoobuga';
575         jQuery('#yahoo').after(val( '<b>buga</b>' ));
576         equals( expected, jQuery('#en').text(), 'Insert String after' );
577
578         reset();
579         expected = "This is a normal link: YahooTry them out:";
580         jQuery('#yahoo').after(val( document.getElementById('first') ));
581         equals( expected, jQuery('#en').text(), "Insert element after" );
582
583         reset();
584         expected = "This is a normal link: YahooTry them out:diveintomark";
585         jQuery('#yahoo').after(val( [document.getElementById('first'), document.getElementById('mark')] ));
586         equals( expected, jQuery('#en').text(), "Insert array of elements after" );
587
588         reset();
589         expected = "This is a normal link: YahoodiveintomarkTry them out:";
590         jQuery('#yahoo').after(val( jQuery("#first, #mark") ));
591         equals( expected, jQuery('#en').text(), "Insert jQuery after" );
592
593         var set = jQuery("<div/>").after("<span>test</span>");
594         equals( set[1].nodeName.toLowerCase(), "span", "Insert the element after the disconnected node." );
595         equals( set.length, 2, "Insert the element after the disconnected node." );
596 };
597
598 test("after(String|Element|Array&lt;Element&gt;|jQuery)", function() {
599         testAfter(bareObj);
600 });
601
602 test("after(Function)", function() {
603         testAfter(functionReturningObj);
604 })
605
606 test("insertAfter(String|Element|Array&lt;Element&gt;|jQuery)", function() {
607         expect(4);
608         var expected = 'This is a normal link: Yahoobuga';
609         jQuery('<b>buga</b>').insertAfter('#yahoo');
610         equals( expected, jQuery('#en').text(), 'Insert String after' );
611
612         reset();
613         expected = "This is a normal link: YahooTry them out:";
614         jQuery(document.getElementById('first')).insertAfter('#yahoo');
615         equals( expected, jQuery('#en').text(), "Insert element after" );
616
617         reset();
618         expected = "This is a normal link: YahooTry them out:diveintomark";
619         jQuery([document.getElementById('first'), document.getElementById('mark')]).insertAfter('#yahoo');
620         equals( expected, jQuery('#en').text(), "Insert array of elements after" );
621
622         reset();
623         expected = "This is a normal link: YahoodiveintomarkTry them out:";
624         jQuery("#first, #mark").insertAfter('#yahoo');
625         equals( expected, jQuery('#en').text(), "Insert jQuery after" );
626 });
627
628 var testReplaceWith = function(val) {
629         expect(17);
630         jQuery('#yahoo').replaceWith(val( '<b id="replace">buga</b>' ));
631         ok( jQuery("#replace")[0], 'Replace element with string' );
632         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
633
634         reset();
635         jQuery('#yahoo').replaceWith(val( document.getElementById('first') ));
636         ok( jQuery("#first")[0], 'Replace element with element' );
637         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
638
639         reset();
640         jQuery('#yahoo').replaceWith(val( [document.getElementById('first'), document.getElementById('mark')] ));
641         ok( jQuery("#first")[0], 'Replace element with array of elements' );
642         ok( jQuery("#mark")[0], 'Replace element with array of elements' );
643         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
644
645         reset();
646         jQuery('#yahoo').replaceWith(val( jQuery("#first, #mark") ));
647         ok( jQuery("#first")[0], 'Replace element with set of elements' );
648         ok( jQuery("#mark")[0], 'Replace element with set of elements' );
649         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
650
651         reset();
652         var tmp = jQuery("<div/>").appendTo("body").click(function(){ ok(true, "Newly bound click run." ); });
653         var y = jQuery('#yahoo').click(function(){ ok(true, "Previously bound click run." ); });
654         var child = y.append("<b>test</b>").find("b").click(function(){ ok(true, "Child bound click run." ); return false; });
655
656         y.replaceWith( tmp );
657
658         tmp.click();
659         y.click();
660         child.click();
661
662         reset();
663
664         var set = jQuery("<div/>").replaceWith(val("<span>test</span>"));
665         equals( set[0].nodeName.toLowerCase(), "span", "Replace the disconnected node." );
666         equals( set.length, 1, "Replace the disconnected node." );
667
668         var $div = jQuery("<div class='replacewith'></div>").appendTo("body");
669         $div.replaceWith("<div class='replacewith'></div><script>" +
670                 "equals(jQuery('.replacewith').length, 1, 'Check number of elements in page.');" +
671                 "</script>");
672         equals(jQuery('.replacewith').length, 1, 'Check number of elements in page.');
673         jQuery('.replacewith').remove();
674 }
675
676 test("replaceWith(String|Element|Array&lt;Element&gt;|jQuery)", function() {
677         testReplaceWith(bareObj);
678 });
679
680 test("replaceWith(Function)", function() {
681         testReplaceWith(functionReturningObj);
682 })
683
684 test("replaceAll(String|Element|Array&lt;Element&gt;|jQuery)", function() {
685         expect(10);
686         jQuery('<b id="replace">buga</b>').replaceAll("#yahoo");
687         ok( jQuery("#replace")[0], 'Replace element with string' );
688         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
689
690         reset();
691         jQuery(document.getElementById('first')).replaceAll("#yahoo");
692         ok( jQuery("#first")[0], 'Replace element with element' );
693         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
694
695         reset();
696         jQuery([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo");
697         ok( jQuery("#first")[0], 'Replace element with array of elements' );
698         ok( jQuery("#mark")[0], 'Replace element with array of elements' );
699         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
700
701         reset();
702         jQuery("#first, #mark").replaceAll("#yahoo");
703         ok( jQuery("#first")[0], 'Replace element with set of elements' );
704         ok( jQuery("#mark")[0], 'Replace element with set of elements' );
705         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
706 });
707
708 test("clone()", function() {
709         expect(30);
710         equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Assert text for #en' );
711         var clone = jQuery('#yahoo').clone();
712         equals( 'Try them out:Yahoo', jQuery('#first').append(clone).text(), 'Check for clone' );
713         equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Reassert text for #en' );
714
715         var cloneTags = [
716                 "<table/>", "<tr/>", "<td/>", "<div/>",
717                 "<button/>", "<ul/>", "<ol/>", "<li/>",
718                 "<input type='checkbox' />", "<select/>", "<option/>", "<textarea/>",
719                 "<tbody/>", "<thead/>", "<tfoot/>", "<iframe/>"
720         ];
721         for (var i = 0; i < cloneTags.length; i++) {
722                 var j = jQuery(cloneTags[i]);
723                 equals( j[0].tagName, j.clone()[0].tagName, 'Clone a &lt;' + cloneTags[i].substring(1));
724         }
725
726         // using contents will get comments regular, text, and comment nodes
727         var cl = jQuery("#nonnodes").contents().clone();
728         ok( cl.length >= 2, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
729
730         var div = jQuery("<div><ul><li>test</li></ul></div>").click(function(){
731                 ok( true, "Bound event still exists." );
732         });
733
734         div = div.clone(true).clone(true);
735         equals( div.length, 1, "One element cloned" );
736         equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
737         div.trigger("click");
738
739         div = jQuery("<div/>").append([ document.createElement("table"), document.createElement("table") ]);
740         div.find("table").click(function(){
741                 ok( true, "Bound event still exists." );
742         });
743
744         div = div.clone(true);
745         equals( div.length, 1, "One element cloned" );
746         equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
747         div.find("table:last").trigger("click");
748
749         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>');
750
751         div = div.clone(true);
752         equals( div.length, 1, "One element cloned" );
753         equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
754
755         div = jQuery("<div/>").data({ a: true, b: true });
756         div = div.clone(true);
757         equals( div.data("a"), true, "Data cloned." );
758         equals( div.data("b"), true, "Data cloned." );
759 });
760
761 if (!isLocal) {
762 test("clone() on XML nodes", function() {
763         expect(2);
764         stop();
765         jQuery.get("data/dashboard.xml", function (xml) {
766                 var root = jQuery(xml.documentElement).clone();
767                 var origTab = jQuery("tab", xml).eq(0);
768                 var cloneTab = jQuery("tab", root).eq(0);
769                 origTab.text("origval");
770                 cloneTab.text("cloneval");
771                 equals(origTab.text(), "origval", "Check original XML node was correctly set");
772                 equals(cloneTab.text(), "cloneval", "Check cloned XML node was correctly set");
773                 start();
774         });
775 });
776 }
777
778 var testHtml = function(valueObj) {
779         expect(22);
780
781         jQuery.scriptorder = 0;
782
783         var div = jQuery("#main > div");
784         div.html(valueObj("<b>test</b>"));
785         var pass = true;
786         for ( var i = 0; i < div.size(); i++ ) {
787                 if ( div.get(i).childNodes.length != 1 ) pass = false;
788         }
789         ok( pass, "Set HTML" );
790
791         reset();
792         // using contents will get comments regular, text, and comment nodes
793         var j = jQuery("#nonnodes").contents();
794         j.html(valueObj("<b>bold</b>"));
795
796         // this is needed, or the expando added by jQuery unique will yield a different html
797         j.find('b').removeData();
798         equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
799
800         jQuery("#main").html(valueObj("<select/>"));
801         jQuery("#main select").html(valueObj("<option>O1</option><option selected='selected'>O2</option><option>O3</option>"));
802         equals( jQuery("#main select").val(), "O2", "Selected option correct" );
803
804         var $div = jQuery('<div />');
805         equals( $div.html(valueObj( 5 )).html(), '5', 'Setting a number as html' );
806         equals( $div.html(valueObj( 0 )).html(), '0', 'Setting a zero as html' );
807
808         var $div2 = jQuery('<div/>'), insert = "&lt;div&gt;hello1&lt;/div&gt;";
809         equals( $div2.html(insert).html(), insert, "Verify escaped insertion." );
810         equals( $div2.html("x" + insert).html(), "x" + insert, "Verify escaped insertion." );
811         equals( $div2.html(" " + insert).html(), " " + insert, "Verify escaped insertion." );
812
813         var map = jQuery("<map/>").html(valueObj("<area id='map01' shape='rect' coords='50,50,150,150' href='http://www.jquery.com/' alt='jQuery'>"));
814
815         equals( map[0].childNodes.length, 1, "The area was inserted." );
816         equals( map[0].firstChild.nodeName.toLowerCase(), "area", "The area was inserted." );
817
818         reset();
819
820         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>'));
821
822         stop();
823
824         jQuery("#main").html(valueObj('<script type="text/javascript">ok( true, "jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975 (1)" );</script>'));
825
826         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>'));
827
828         // 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
829         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>"));
830
831         setTimeout( start, 100 );
832 }
833
834 test("html(String)", function() {
835         testHtml(bareObj);
836 });
837
838 test("html(Function)", function() {
839         testHtml(functionReturningObj);
840 });
841
842 test("html(Function) with incoming value", function() {
843         expect(20);
844         
845         var div = jQuery("#main > div"), old = div.map(function(){ return jQuery(this).html() });
846         
847         div.html(function(i, val) {
848                 equals( val, old[i], "Make sure the incoming value is correct." );
849                 return "<b>test</b>";
850         });
851         
852         var pass = true;
853         div.each(function(){
854                 if ( this.childNodes.length !== 1 ) {
855                         pass = false;
856                 }
857         })
858         ok( pass, "Set HTML" );
859
860         reset();
861         // using contents will get comments regular, text, and comment nodes
862         var j = jQuery("#nonnodes").contents();
863         old = j.map(function(){ return jQuery(this).html(); });
864         
865         j.html(function(i, val) {
866                 equals( val, old[i], "Make sure the incoming value is correct." );
867                 return "<b>bold</b>";
868         });
869         
870         j.find('b').removeData();
871         equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
872         
873         var $div = jQuery('<div />');
874         
875         equals( $div.html(function(i, val) {
876                 equals( val, "", "Make sure the incoming value is correct." );
877                 return 5;
878         }).html(), '5', 'Setting a number as html' );
879         
880         equals( $div.html(function(i, val) {
881                 equals( val, "5", "Make sure the incoming value is correct." );
882                 return 0;
883         }).html(), '0', 'Setting a zero as html' );
884
885         var $div2 = jQuery('<div/>'), insert = "&lt;div&gt;hello1&lt;/div&gt;";
886         equals( $div2.html(function(i, val) {
887                 equals( val, "", "Make sure the incoming value is correct." );
888                 return insert;
889         }).html(), insert, "Verify escaped insertion." );
890         
891         equals( $div2.html(function(i, val) {
892                 equals( val, insert, "Make sure the incoming value is correct." );
893                 return "x" + insert;
894         }).html(), "x" + insert, "Verify escaped insertion." );
895         
896         equals( $div2.html(function(i, val) {
897                 equals( val, "x" + insert, "Make sure the incoming value is correct." );
898                 return " " + insert;
899         }).html(), " " + insert, "Verify escaped insertion." ); 
900 });
901
902 var testRemove = function(method) {
903         expect(9);
904
905         var first = jQuery("#ap").children(":first");
906         first.data("foo", "bar");
907
908         jQuery("#ap").children()[method]();
909         ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
910         equals( jQuery("#ap").children().length, 0, "Check remove" );
911
912         equals( first.data("foo"), method == "remove" ? null : "bar" );
913
914         reset();
915         jQuery("#ap").children()[method]("a");
916         ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
917         equals( jQuery("#ap").children().length, 1, "Check filtered remove" );
918
919         jQuery("#ap").children()[method]("a, code");
920         equals( jQuery("#ap").children().length, 0, "Check multi-filtered remove" );
921
922         // using contents will get comments regular, text, and comment nodes
923         equals( jQuery("#nonnodes").contents().length, 3, "Check node,textnode,comment remove works" );
924         jQuery("#nonnodes").contents()[method]();
925         equals( jQuery("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" );
926
927         reset();
928
929         var count = 0;
930         var first = jQuery("#ap").children(":first");
931         var cleanUp = first.click(function() { count++ })[method]().appendTo("body").click();
932         
933         equals( method == "remove" ? 0 : 1, count );
934         
935         cleanUp.detach();
936 };
937
938 test("remove()", function() {
939         testRemove("remove");
940 });
941
942 test("detach()", function() {
943         testRemove("detach");
944 });
945
946 test("empty()", function() {
947         expect(3);
948         equals( jQuery("#ap").children().empty().text().length, 0, "Check text is removed" );
949         equals( jQuery("#ap").children().length, 4, "Check elements are not removed" );
950
951         // using contents will get comments regular, text, and comment nodes
952         var j = jQuery("#nonnodes").contents();
953         j.empty();
954         equals( j.html(), "", "Check node,textnode,comment empty works" );
955 });
956