Fixed the case where HTML that contained entities was being inserted as text strings...
[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 testUnwrap = function() {
120         expect(9);
121
122         jQuery("body").append('  <div id="unwrap" style="display: none;"> <div id="unwrap1"> <span class="unwrap">a</span> <span class="unwrap">b</span> </div> <div id="unwrap2"> <span class="unwrap">c</span> <span class="unwrap">d</span> </div> <div id="unwrap3"> <b><span class="unwrap unwrap3">e</span></b> <b><span class="unwrap unwrap3">f</span></b> </div> </div>');
123
124         var abcd = jQuery('#unwrap1 > span, #unwrap2 > span').get(),
125                 abcdef = jQuery('#unwrap span').get();
126
127         equals( jQuery('#unwrap1 span, #unwrap2 span:first').unwrap().length, 3, 'make #unwrap1 and #unwrap2 go away' );
128         same( jQuery('#unwrap > span').get(), abcd, 'all four spans should still exist' );
129
130         same( jQuery('#unwrap3 span').unwrap().get(), jQuery('#unwrap3 > span').get(), 'make all b in #unwrap3 go away' );
131
132         same( jQuery('#unwrap3 span').unwrap().get(), jQuery('#unwrap > span.unwrap3').get(), 'make #unwrap3 go away' );
133
134         same( jQuery('#unwrap').children().get(), abcdef, '#unwrap only contains 6 child spans' );
135
136         same( jQuery('#unwrap > span').unwrap().get(), jQuery('body > span.unwrap').get(), 'make the 6 spans become children of body' );
137
138         same( jQuery('body > span.unwrap').unwrap().get(), jQuery('body > span.unwrap').get(), 'can\'t unwrap children of body' );
139         same( jQuery('body > span.unwrap').unwrap().get(), abcdef, 'can\'t unwrap children of body' );
140
141         same( jQuery('body > span.unwrap').get(), abcdef, 'body contains 6 .unwrap child spans' );
142
143         jQuery('body > span.unwrap').remove();
144 }
145
146 test("unwrap()", function() {
147         testUnwrap();
148 });
149
150 var testAppend = function(valueObj) {
151         expect(21);
152         var defaultText = 'Try them out:'
153         var result = jQuery('#first').append(valueObj('<b>buga</b>'));
154         equals( result.text(), defaultText + 'buga', 'Check if text appending works' );
155         equals( jQuery('#select3').append(valueObj('<option value="appendTest">Append Test</option>')).find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
156
157         reset();
158         var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
159         jQuery('#sap').append(valueObj(document.getElementById('first')));
160         equals( expected, jQuery('#sap').text(), "Check for appending of element" );
161
162         reset();
163         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
164         jQuery('#sap').append(valueObj([document.getElementById('first'), document.getElementById('yahoo')]));
165         equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
166
167         reset();
168         expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
169         jQuery('#sap').append(valueObj(jQuery("#first, #yahoo")));
170         equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
171
172         reset();
173         jQuery("#sap").append(valueObj( 5 ));
174         ok( jQuery("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
175
176         reset();
177         jQuery("#sap").append(valueObj( " text with spaces " ));
178         ok( jQuery("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );
179
180         reset();
181         ok( jQuery("#sap").append(valueObj( [] )), "Check for appending an empty array." );
182         ok( jQuery("#sap").append(valueObj( "" )), "Check for appending an empty string." );
183         ok( jQuery("#sap").append(valueObj( document.getElementsByTagName("foo") )), "Check for appending an empty nodelist." );
184
185         reset();
186         jQuery("#sap").append(valueObj( document.getElementById('form') ));
187         equals( jQuery("#sap>form").size(), 1, "Check for appending a form" ); // Bug #910
188
189         reset();
190         var pass = true;
191         try {
192                 jQuery( jQuery("#iframe")[0].contentWindow.document.body ).append(valueObj( "<div>test</div>" ));
193         } catch(e) {
194                 pass = false;
195         }
196
197         ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
198
199         reset();
200         jQuery('<fieldset/>').appendTo('#form').append(valueObj( '<legend id="legend">test</legend>' ));
201         t( 'Append legend', '#legend', ['legend'] );
202
203         reset();
204         jQuery('#select1').append(valueObj( '<OPTION>Test</OPTION>' ));
205         equals( jQuery('#select1 option:last').text(), "Test", "Appending &lt;OPTION&gt; (all caps)" );
206
207         jQuery('#table').append(valueObj( '<colgroup></colgroup>' ));
208         ok( jQuery('#table colgroup').length, "Append colgroup" );
209
210         jQuery('#table colgroup').append(valueObj( '<col/>' ));
211         ok( jQuery('#table colgroup col').length, "Append col" );
212
213         reset();
214         jQuery('#table').append(valueObj( '<caption></caption>' ));
215         ok( jQuery('#table caption').length, "Append caption" );
216
217         reset();
218         jQuery('form:last')
219                 .append(valueObj( '<select id="appendSelect1"></select>' ))
220                 .append(valueObj( '<select id="appendSelect2"><option>Test</option></select>' ));
221
222         t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );
223
224         // using contents will get comments regular, text, and comment nodes
225         var j = jQuery("#nonnodes").contents();
226         var d = jQuery("<div/>").appendTo("#nonnodes").append(j);
227         equals( jQuery("#nonnodes").length, 1, "Check node,textnode,comment append moved leaving just the div" );
228         ok( d.contents().length >= 2, "Check node,textnode,comment append works" );
229         d.contents().appendTo("#nonnodes");
230         d.remove();
231         ok( jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment append cleanup worked" );
232 }
233
234 test("append(String|Element|Array&lt;Element&gt;|jQuery)", function() {
235   testAppend(bareObj);
236 });
237
238 test("append(Function)", function() {
239         testAppend(functionReturningObj);
240 })
241
242 test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
243         expect(12);
244         var defaultText = 'Try them out:'
245         jQuery('<b>buga</b>').appendTo('#first');
246         equals( jQuery("#first").text(), defaultText + 'buga', 'Check if text appending works' );
247         equals( jQuery('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
248
249         reset();
250         var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
251         jQuery(document.getElementById('first')).appendTo('#sap');
252         equals( expected, jQuery('#sap').text(), "Check for appending of element" );
253
254         reset();
255         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
256         jQuery([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
257         equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
258
259         reset();
260         ok( jQuery(document.createElement("script")).appendTo("body").length, "Make sure a disconnected script can be appended." );
261
262         reset();
263         expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
264         jQuery("#first, #yahoo").appendTo('#sap');
265         equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
266
267         reset();
268         jQuery('#select1').appendTo('#foo');
269         t( 'Append select', '#foo select', ['select1'] );
270
271         reset();
272         var div = jQuery("<div/>").click(function(){
273                 ok(true, "Running a cloned click.");
274         });
275         div.appendTo("#main, #moretests");
276
277         jQuery("#main div:last").click();
278         jQuery("#moretests div:last").click();
279
280         reset();
281         var div = jQuery("<div/>").appendTo("#main, #moretests");
282
283         equals( div.length, 2, "appendTo returns the inserted elements" );
284
285         div.addClass("test");
286
287         ok( jQuery("#main div:last").hasClass("test"), "appendTo element was modified after the insertion" );
288         ok( jQuery("#moretests div:last").hasClass("test"), "appendTo element was modified after the insertion" );
289
290         reset();
291 });
292
293 var testPrepend = function(val) {
294         expect(5);
295         var defaultText = 'Try them out:'
296         var result = jQuery('#first').prepend(val( '<b>buga</b>' ));
297         equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
298         equals( jQuery('#select3').prepend(val( '<option value="prependTest">Prepend Test</option>' )).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('#sap').prepend(val( document.getElementById('first') ));
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('#sap').prepend(val( [document.getElementById('first'), document.getElementById('yahoo')] ));
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('#sap').prepend(val( jQuery("#first, #yahoo") ));
313         equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
314 }
315
316 test("prepend(String|Element|Array&lt;Element&gt;|jQuery)", function() {
317         testPrepend(bareObj);
318 });
319
320 test("prepend(Function)", function() {
321         testPrepend(functionReturningObj);
322 })
323
324 test("prependTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
325         expect(6);
326         var defaultText = 'Try them out:'
327         jQuery('<b>buga</b>').prependTo('#first');
328         equals( jQuery('#first').text(), 'buga' + defaultText, 'Check if text prepending works' );
329         equals( jQuery('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
330
331         reset();
332         var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
333         jQuery(document.getElementById('first')).prependTo('#sap');
334         equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
335
336         reset();
337         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
338         jQuery([document.getElementById('first'), document.getElementById('yahoo')]).prependTo('#sap');
339         equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
340
341         reset();
342         expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
343         jQuery("#first, #yahoo").prependTo('#sap');
344         equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
345
346         reset();
347         jQuery('<select id="prependSelect1"></select>').prependTo('form:last');
348         jQuery('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');
349
350         t( "Prepend Select", "#prependSelect2, #prependSelect1", ["prependSelect2", "prependSelect1"] );
351 });
352
353 var testBefore = function(val) {
354         expect(6);
355         var expected = 'This is a normal link: bugaYahoo';
356         jQuery('#yahoo').before(val( '<b>buga</b>' ));
357         equals( expected, jQuery('#en').text(), 'Insert String before' );
358
359         reset();
360         expected = "This is a normal link: Try them out:Yahoo";
361         jQuery('#yahoo').before(val( document.getElementById('first') ));
362         equals( expected, jQuery('#en').text(), "Insert element before" );
363
364         reset();
365         expected = "This is a normal link: Try them out:diveintomarkYahoo";
366         jQuery('#yahoo').before(val( [document.getElementById('first'), document.getElementById('mark')] ));
367         equals( expected, jQuery('#en').text(), "Insert array of elements before" );
368
369         reset();
370         expected = "This is a normal link: diveintomarkTry them out:Yahoo";
371         jQuery('#yahoo').before(val( jQuery("#first, #mark") ));
372         equals( expected, jQuery('#en').text(), "Insert jQuery before" );
373
374         var set = jQuery("<div/>").before("<span>test</span>");
375         equals( set[0].nodeName.toLowerCase(), "span", "Insert the element before the disconnected node." );
376         equals( set.length, 2, "Insert the element before the disconnected node." );
377 }
378
379 test("before(String|Element|Array&lt;Element&gt;|jQuery)", function() {
380         testBefore(bareObj);
381 });
382
383 test("before(Function)", function() {
384         testBefore(functionReturningObj);
385 })
386
387 test("insertBefore(String|Element|Array&lt;Element&gt;|jQuery)", function() {
388         expect(4);
389         var expected = 'This is a normal link: bugaYahoo';
390         jQuery('<b>buga</b>').insertBefore('#yahoo');
391         equals( expected, jQuery('#en').text(), 'Insert String before' );
392
393         reset();
394         expected = "This is a normal link: Try them out:Yahoo";
395         jQuery(document.getElementById('first')).insertBefore('#yahoo');
396         equals( expected, jQuery('#en').text(), "Insert element before" );
397
398         reset();
399         expected = "This is a normal link: Try them out:diveintomarkYahoo";
400         jQuery([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
401         equals( expected, jQuery('#en').text(), "Insert array of elements before" );
402
403         reset();
404         expected = "This is a normal link: diveintomarkTry them out:Yahoo";
405         jQuery("#first, #mark").insertBefore('#yahoo');
406         equals( expected, jQuery('#en').text(), "Insert jQuery before" );
407 });
408
409 var testAfter = function(val) {
410         expect(6);
411         var expected = 'This is a normal link: Yahoobuga';
412         jQuery('#yahoo').after(val( '<b>buga</b>' ));
413         equals( expected, jQuery('#en').text(), 'Insert String after' );
414
415         reset();
416         expected = "This is a normal link: YahooTry them out:";
417         jQuery('#yahoo').after(val( document.getElementById('first') ));
418         equals( expected, jQuery('#en').text(), "Insert element after" );
419
420         reset();
421         expected = "This is a normal link: YahooTry them out:diveintomark";
422         jQuery('#yahoo').after(val( [document.getElementById('first'), document.getElementById('mark')] ));
423         equals( expected, jQuery('#en').text(), "Insert array of elements after" );
424
425         reset();
426         expected = "This is a normal link: YahoodiveintomarkTry them out:";
427         jQuery('#yahoo').after(val( jQuery("#first, #mark") ));
428         equals( expected, jQuery('#en').text(), "Insert jQuery after" );
429
430         var set = jQuery("<div/>").after("<span>test</span>");
431         equals( set[1].nodeName.toLowerCase(), "span", "Insert the element after the disconnected node." );
432         equals( set.length, 2, "Insert the element after the disconnected node." );
433 };
434
435 test("after(String|Element|Array&lt;Element&gt;|jQuery)", function() {
436         testAfter(bareObj);
437 });
438
439 test("after(Function)", function() {
440         testAfter(functionReturningObj);
441 })
442
443 test("insertAfter(String|Element|Array&lt;Element&gt;|jQuery)", function() {
444         expect(4);
445         var expected = 'This is a normal link: Yahoobuga';
446         jQuery('<b>buga</b>').insertAfter('#yahoo');
447         equals( expected, jQuery('#en').text(), 'Insert String after' );
448
449         reset();
450         expected = "This is a normal link: YahooTry them out:";
451         jQuery(document.getElementById('first')).insertAfter('#yahoo');
452         equals( expected, jQuery('#en').text(), "Insert element after" );
453
454         reset();
455         expected = "This is a normal link: YahooTry them out:diveintomark";
456         jQuery([document.getElementById('first'), document.getElementById('mark')]).insertAfter('#yahoo');
457         equals( expected, jQuery('#en').text(), "Insert array of elements after" );
458
459         reset();
460         expected = "This is a normal link: YahoodiveintomarkTry them out:";
461         jQuery("#first, #mark").insertAfter('#yahoo');
462         equals( expected, jQuery('#en').text(), "Insert jQuery after" );
463 });
464
465 var testReplaceWith = function(val) {
466         expect(12);
467         jQuery('#yahoo').replaceWith(val( '<b id="replace">buga</b>' ));
468         ok( jQuery("#replace")[0], 'Replace element with string' );
469         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
470
471         reset();
472         jQuery('#yahoo').replaceWith(val( document.getElementById('first') ));
473         ok( jQuery("#first")[0], 'Replace element with element' );
474         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
475
476         reset();
477         jQuery('#yahoo').replaceWith(val( [document.getElementById('first'), document.getElementById('mark')] ));
478         ok( jQuery("#first")[0], 'Replace element with array of elements' );
479         ok( jQuery("#mark")[0], 'Replace element with array of elements' );
480         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
481
482         reset();
483         jQuery('#yahoo').replaceWith(val( jQuery("#first, #mark") ));
484         ok( jQuery("#first")[0], 'Replace element with set of elements' );
485         ok( jQuery("#mark")[0], 'Replace element with set of elements' );
486         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
487
488         var set = jQuery("<div/>").replaceWith(val("<span>test</span>"));
489         equals( set[0].nodeName.toLowerCase(), "span", "Replace the disconnected node." );
490         equals( set.length, 1, "Replace the disconnected node." );
491 }
492
493 test("replaceWith(String|Element|Array&lt;Element&gt;|jQuery)", function() {
494         testReplaceWith(bareObj);
495 });
496
497 test("replaceWith(Function)", function() {
498         testReplaceWith(functionReturningObj);
499 })
500
501 test("replaceAll(String|Element|Array&lt;Element&gt;|jQuery)", function() {
502         expect(10);
503         jQuery('<b id="replace">buga</b>').replaceAll("#yahoo");
504         ok( jQuery("#replace")[0], 'Replace element with string' );
505         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
506
507         reset();
508         jQuery(document.getElementById('first')).replaceAll("#yahoo");
509         ok( jQuery("#first")[0], 'Replace element with element' );
510         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
511
512         reset();
513         jQuery([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo");
514         ok( jQuery("#first")[0], 'Replace element with array of elements' );
515         ok( jQuery("#mark")[0], 'Replace element with array of elements' );
516         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
517
518         reset();
519         jQuery("#first, #mark").replaceAll("#yahoo");
520         ok( jQuery("#first")[0], 'Replace element with set of elements' );
521         ok( jQuery("#mark")[0], 'Replace element with set of elements' );
522         ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
523 });
524
525 test("clone()", function() {
526         expect(28);
527         equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Assert text for #en' );
528         var clone = jQuery('#yahoo').clone();
529         equals( 'Try them out:Yahoo', jQuery('#first').append(clone).text(), 'Check for clone' );
530         equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Reassert text for #en' );
531
532         var cloneTags = [
533                 "<table/>", "<tr/>", "<td/>", "<div/>",
534                 "<button/>", "<ul/>", "<ol/>", "<li/>",
535                 "<input type='checkbox' />", "<select/>", "<option/>", "<textarea/>",
536                 "<tbody/>", "<thead/>", "<tfoot/>", "<iframe/>"
537         ];
538         for (var i = 0; i < cloneTags.length; i++) {
539                 var j = jQuery(cloneTags[i]);
540                 equals( j[0].tagName, j.clone()[0].tagName, 'Clone a &lt;' + cloneTags[i].substring(1));
541         }
542
543         // using contents will get comments regular, text, and comment nodes
544         var cl = jQuery("#nonnodes").contents().clone();
545         ok( cl.length >= 2, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
546
547         var div = jQuery("<div><ul><li>test</li></ul></div>").click(function(){
548                 ok( true, "Bound event still exists." );
549         });
550
551         div = div.clone(true).clone(true);
552         equals( div.length, 1, "One element cloned" );
553         equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
554         div.trigger("click");
555
556         div = jQuery("<div/>").append([ document.createElement("table"), document.createElement("table") ]);
557         div.find("table").click(function(){
558                 ok( true, "Bound event still exists." );
559         });
560
561         div = div.clone(true);
562         equals( div.length, 1, "One element cloned" );
563         equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
564         div.find("table:last").trigger("click");
565
566         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>');
567
568         div = div.clone(true);
569         equals( div.length, 1, "One element cloned" );
570         equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
571 });
572
573 if (!isLocal) {
574 test("clone() on XML nodes", function() {
575         expect(2);
576         stop();
577         jQuery.get("data/dashboard.xml", function (xml) {
578                 var root = jQuery(xml.documentElement).clone();
579                 var origTab = jQuery("tab", xml).eq(0);
580                 var cloneTab = jQuery("tab", root).eq(0);
581                 origTab.text("origval");
582                 cloneTab.text("cloneval");
583                 equals(origTab.text(), "origval", "Check original XML node was correctly set");
584                 equals(cloneTab.text(), "cloneval", "Check cloned XML node was correctly set");
585                 start();
586         });
587 });
588 }
589
590 test("val()", function() {
591         expect(9);
592
593         document.getElementById('text1').value = "bla";
594         equals( jQuery("#text1").val(), "bla", "Check for modified value of input element" );
595
596         reset();
597
598         equals( jQuery("#text1").val(), "Test", "Check for value of input element" );
599         // ticket #1714 this caused a JS error in IE
600         equals( jQuery("#first").val(), "", "Check a paragraph element to see if it has a value" );
601         ok( jQuery([]).val() === undefined, "Check an empty jQuery object will return undefined from val" );
602
603         equals( jQuery('#select2').val(), '3', 'Call val() on a single="single" select' );
604
605         same( jQuery('#select3').val(), ['1', '2'], 'Call val() on a multiple="multiple" select' );
606
607         equals( jQuery('#option3c').val(), '2', 'Call val() on a option element with value' );
608
609         equals( jQuery('#option3a').val(), '', 'Call val() on a option element with empty value' );
610
611         equals( jQuery('#option3e').val(), 'no value', 'Call val() on a option element with no value attribute' );
612
613 });
614
615 var testVal = function(valueObj) {
616         expect(5);
617
618         jQuery("#text1").val(valueObj( 'test' ));
619         equals( document.getElementById('text1').value, "test", "Check for modified (via val(String)) value of input element" );
620
621         jQuery("#text1").val(valueObj( 67 ));
622         equals( document.getElementById('text1').value, "67", "Check for modified (via val(Number)) value of input element" );
623
624         jQuery("#select1").val(valueObj( "3" ));
625         equals( jQuery("#select1").val(), "3", "Check for modified (via val(String)) value of select element" );
626
627         jQuery("#select1").val(valueObj( 2 ));
628         equals( jQuery("#select1").val(), "2", "Check for modified (via val(Number)) value of select element" );
629
630         // using contents will get comments regular, text, and comment nodes
631         var j = jQuery("#nonnodes").contents();
632         j.val(valueObj( "asdf" ));
633         equals( j.val(), "asdf", "Check node,textnode,comment with val()" );
634         j.removeAttr("value");
635 }
636
637 test("val(String/Number)", function() {
638         testVal(bareObj);
639 });
640
641 test("val(Function)", function() {
642         testVal(functionReturningObj);
643 })
644
645 var testHtml = function(valueObj) {
646         expect(20);
647
648         window.debug = true;
649
650         jQuery.scriptorder = 0;
651
652         var div = jQuery("#main > div");
653         div.html(valueObj("<b>test</b>"));
654         var pass = true;
655         for ( var i = 0; i < div.size(); i++ ) {
656                 if ( div.get(i).childNodes.length != 1 ) pass = false;
657         }
658         ok( pass, "Set HTML" );
659
660         delete window.debug;
661
662         reset();
663         // using contents will get comments regular, text, and comment nodes
664         var j = jQuery("#nonnodes").contents();
665         j.html(valueObj("<b>bold</b>"));
666
667         // this is needed, or the expando added by jQuery unique will yield a different html
668         j.find('b').removeData();
669         equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
670
671         jQuery("#main").html(valueObj("<select/>"));
672         jQuery("#main select").html(valueObj("<option>O1</option><option selected='selected'>O2</option><option>O3</option>"));
673         equals( jQuery("#main select").val(), "O2", "Selected option correct" );
674
675         var $div = jQuery('<div />');
676         equals( $div.html(valueObj( 5 )).html(), '5', 'Setting a number as html' );
677         equals( $div.html(valueObj( 0 )).html(), '0', 'Setting a zero as html' );
678
679         var $div2 = jQuery('<div/>'), insert = "&lt;div&gt;hello1&lt;/div&gt;";
680         equals( $div2.html(insert).html(), insert, "Verify escaped insertion." );
681         equals( $div2.html("x" + insert).html(), "x" + insert, "Verify escaped insertion." );
682         equals( $div2.html(" " + insert).html(), " " + insert, "Verify escaped insertion." );
683
684
685         reset();
686
687         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>'));
688
689         stop();
690
691         jQuery("#main").html(valueObj('<script type="text/javascript">ok( true, "jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975 (1)" );</script>'));
692
693         jQuery("#main").html(valueObj('foo <form><script type="text/javascript">ok( true, "jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975 (2)" );</script></form>'));
694
695         // 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
696         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>"));
697
698         setTimeout( start, 100 );
699 }
700
701 test("html(String)", function() {
702         testHtml(bareObj);
703 });
704
705 test("html(Function)", function() {
706         testHtml(functionReturningObj);
707 })
708
709 var testText = function(valueObj) {
710         expect(4);
711         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" );
712
713         // using contents will get comments regular, text, and comment nodes
714         var j = jQuery("#nonnodes").contents();
715         j.text("hi!");
716         equals( jQuery(j[0]).text(), "hi!", "Check node,textnode,comment with text()" );
717         equals( j[1].nodeValue, " there ", "Check node,textnode,comment with text()" );
718         equals( j[2].nodeType, 8, "Check node,textnode,comment with text()" );
719 }
720
721 test("text(String)", function() {
722         testText(bareObj)
723 });
724
725 test("text(Function)", function() {
726         testText(functionReturningObj);
727 })
728
729 var testRemove = function(method) {
730         expect(9);
731
732         var first = jQuery("#ap").children(":first");
733         first.data("foo", "bar");
734
735         jQuery("#ap").children()[method]();
736         ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
737         equals( jQuery("#ap").children().length, 0, "Check remove" );
738
739         equals( first.data("foo"), method == "remove" ? null : "bar" );
740
741         reset();
742         jQuery("#ap").children()[method]("a");
743         ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
744         equals( jQuery("#ap").children().length, 1, "Check filtered remove" );
745
746         jQuery("#ap").children()[method]("a, code");
747         equals( jQuery("#ap").children().length, 0, "Check multi-filtered remove" );
748
749         // using contents will get comments regular, text, and comment nodes
750         equals( jQuery("#nonnodes").contents().length, 3, "Check node,textnode,comment remove works" );
751         jQuery("#nonnodes").contents()[method]();
752         equals( jQuery("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" );
753
754         reset();
755
756         var count = 0;
757         var first = jQuery("#ap").children(":first");
758         var cleanUp = first.click(function() { count++ })[method]().appendTo("body").click();
759         
760         equals( method == "remove" ? 0 : 1, count );
761         
762         cleanUp.detach();
763 };
764
765 test("remove()", function() {
766         testRemove("remove");
767 });
768
769 test("detach()", function() {
770         testRemove("detach");
771 });
772
773 test("empty()", function() {
774         expect(3);
775         equals( jQuery("#ap").children().empty().text().length, 0, "Check text is removed" );
776         equals( jQuery("#ap").children().length, 4, "Check elements are not removed" );
777
778         // using contents will get comments regular, text, and comment nodes
779         var j = jQuery("#nonnodes").contents();
780         j.empty();
781         equals( j.html(), "", "Check node,textnode,comment empty works" );
782 });
783