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