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