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