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