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