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