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