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