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