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