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