1 module("manipulation");
3 var bareObj = function(value) { return value; };
4 var functionReturningObj = function(value) { return (function() { return value; }); };
6 test("text()", function() {
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.' );
11 // Check serialization of text values
12 equals( jQuery(document.createTextNode("foo")).text(), "foo", "Text node was retreived from .text()." );
15 var testText = function(valueObj) {
17 var val = valueObj("<div><b>Hello</b> cruel world!</div>");
18 equals( jQuery("#foo").text(val)[0].innerHTML.replace(/>/g, ">"), "<div><b>Hello</b> cruel world!</div>", "Check escaped text" );
20 // using contents will get comments regular, text, and comment nodes
21 var j = jQuery("#nonnodes").contents();
22 j.text(valueObj("hi!"));
23 equals( jQuery(j[0]).text(), "hi!", "Check node,textnode,comment with text()" );
24 equals( j[1].nodeValue, " there ", "Check node,textnode,comment with text()" );
25 equals( j[2].nodeType, 8, "Check node,textnode,comment with text()" );
28 test("text(String)", function() {
32 test("text(Function)", function() {
33 testText(functionReturningObj);
36 test("text(Function) with incoming value", function() {
39 var old = "This link has class=\"blog\": Simon Willison's Weblog";
41 jQuery('#sap').text(function(i, val) {
42 equals( val, old, "Make sure the incoming value is correct." );
46 equals( jQuery("#sap").text(), "foobar", 'Check for merged text of more then one element.' );
51 var testWrap = function(val) {
53 var defaultText = 'Try them out:'
54 var result = jQuery('#first').wrap(val( '<div class="red"><span></span></div>' )).text();
55 equals( defaultText, result, 'Check for wrapping of on-the-fly html' );
56 ok( jQuery('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
59 var defaultText = 'Try them out:'
60 var result = jQuery('#first').wrap(val( document.getElementById('empty') )).parent();
61 ok( result.is('ol'), 'Check for element wrapping' );
62 equals( result.text(), defaultText, 'Check for element wrapping' );
65 jQuery('#check1').click(function() {
67 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
68 jQuery(checkbox).wrap(val( '<div id="c1" style="display:none;"></div>' ));
69 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
72 // using contents will get comments regular, text, and comment nodes
73 var j = jQuery("#nonnodes").contents();
74 j.wrap(val( "<i></i>" ));
75 equals( jQuery("#nonnodes > i").length, 3, "Check node,textnode,comment wraps ok" );
76 equals( jQuery("#nonnodes > i").text(), j.text(), "Check node,textnode,comment wraps doesn't hurt text" );
78 // Try wrapping a disconnected node
79 j = jQuery("<label/>").wrap(val( "<li/>" ));
80 equals( j[0].nodeName.toUpperCase(), "LABEL", "Element is a label" );
81 equals( j[0].parentNode.nodeName.toUpperCase(), "LI", "Element has been wrapped" );
83 // Wrap an element containing a text node
84 j = jQuery("<span/>").wrap("<div>test</div>");
85 equals( j[0].previousSibling.nodeType, 3, "Make sure the previous node is a text element" );
86 equals( j[0].parentNode.nodeName.toUpperCase(), "DIV", "And that we're in the div element." );
88 // Try to wrap an element with multiple elements (should fail)
89 j = jQuery("<div><span></span></div>").children().wrap("<p></p><div></div>");
90 equals( j[0].parentNode.parentNode.childNodes.length, 1, "There should only be one element wrapping." );
91 equals( j.length, 1, "There should only be one element (no cloning)." );
92 equals( j[0].parentNode.nodeName.toUpperCase(), "P", "The span should be in the paragraph." );
94 // Wrap an element with a jQuery set
95 j = jQuery("<span/>").wrap(jQuery("<div></div>"));
96 equals( j[0].parentNode.nodeName.toLowerCase(), "div", "Wrapping works." );
98 // Wrap an element with a jQuery set and event
99 result = jQuery("<div></div>").click(function(){
100 ok(true, "Event triggered.");
103 j = jQuery("<span/>").wrap(result);
104 equals( j[0].parentNode.nodeName.toLowerCase(), "div", "Wrapping works." );
106 j.parent().trigger("click");
109 test("wrap(String|Element)", function() {
113 test("wrap(Function)", function() {
114 testWrap(functionReturningObj);
117 var testWrapAll = function(val) {
119 var prev = jQuery("#firstp")[0].previousSibling;
120 var p = jQuery("#firstp,#first")[0].parentNode;
122 var result = jQuery('#firstp,#first').wrapAll(val( '<div class="red"><div class="tmp"></div></div>' ));
123 equals( result.parent().length, 1, 'Check for wrapping of on-the-fly html' );
124 ok( jQuery('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
125 ok( jQuery('#firstp').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
126 equals( jQuery("#first").parent().parent()[0].previousSibling, prev, "Correct Previous Sibling" );
127 equals( jQuery("#first").parent().parent()[0].parentNode, p, "Correct Parent" );
130 var prev = jQuery("#firstp")[0].previousSibling;
131 var p = jQuery("#first")[0].parentNode;
132 var result = jQuery('#firstp,#first').wrapAll(val( document.getElementById('empty') ));
133 equals( jQuery("#first").parent()[0], jQuery("#firstp").parent()[0], "Same Parent" );
134 equals( jQuery("#first").parent()[0].previousSibling, prev, "Correct Previous Sibling" );
135 equals( jQuery("#first").parent()[0].parentNode, p, "Correct Parent" );
138 test("wrapAll(String|Element)", function() {
139 testWrapAll(bareObj);
142 var testWrapInner = function(val) {
144 var num = jQuery("#first").children().length;
145 var result = jQuery('#first').wrapInner('<div class="red"><div id="tmp"></div></div>');
146 equals( jQuery("#first").children().length, 1, "Only one child" );
147 ok( jQuery("#first").children().is(".red"), "Verify Right Element" );
148 equals( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" );
151 var num = jQuery("#first").children().length;
152 var result = jQuery('#first').wrapInner(document.getElementById('empty'));
153 equals( jQuery("#first").children().length, 1, "Only one child" );
154 ok( jQuery("#first").children().is("#empty"), "Verify Right Element" );
155 equals( jQuery("#first").children().children().length, num, "Verify Elements Intact" );
157 var div = jQuery("<div/>");
158 div.wrapInner("<span></span>");
159 equals(div.children().length, 1, "The contents were wrapped.");
160 equals(div.children()[0].nodeName.toLowerCase(), "span", "A span was inserted.");
163 test("wrapInner(String|Element)", function() {
164 testWrapInner(bareObj);
167 // TODO: wrapInner uses wrapAll -- get wrapAll working with Function
168 // test("wrapInner(Function)", function() {
169 // testWrapInner(functionReturningObj)
172 test("unwrap()", function() {
175 jQuery("body").append(' <div id="unwrap" style="display: none;"> <div id="unwrap1"> <span class="unwrap">a</span> <span class="unwrap">b</span> </div> <div id="unwrap2"> <span class="unwrap">c</span> <span class="unwrap">d</span> </div> <div id="unwrap3"> <b><span class="unwrap unwrap3">e</span></b> <b><span class="unwrap unwrap3">f</span></b> </div> </div>');
177 var abcd = jQuery('#unwrap1 > span, #unwrap2 > span').get(),
178 abcdef = jQuery('#unwrap span').get();
180 equals( jQuery('#unwrap1 span').add('#unwrap2 span:first').unwrap().length, 3, 'make #unwrap1 and #unwrap2 go away' );
181 same( jQuery('#unwrap > span').get(), abcd, 'all four spans should still exist' );
183 same( jQuery('#unwrap3 span').unwrap().get(), jQuery('#unwrap3 > span').get(), 'make all b in #unwrap3 go away' );
185 same( jQuery('#unwrap3 span').unwrap().get(), jQuery('#unwrap > span.unwrap3').get(), 'make #unwrap3 go away' );
187 same( jQuery('#unwrap').children().get(), abcdef, '#unwrap only contains 6 child spans' );
189 same( jQuery('#unwrap > span').unwrap().get(), jQuery('body > span.unwrap').get(), 'make the 6 spans become children of body' );
191 same( jQuery('body > span.unwrap').unwrap().get(), jQuery('body > span.unwrap').get(), 'can\'t unwrap children of body' );
192 same( jQuery('body > span.unwrap').unwrap().get(), abcdef, 'can\'t unwrap children of body' );
194 same( jQuery('body > span.unwrap').get(), abcdef, 'body contains 6 .unwrap child spans' );
196 jQuery('body > span.unwrap').remove();
199 var testAppend = function(valueObj) {
201 var defaultText = 'Try them out:'
202 var result = jQuery('#first').append(valueObj('<b>buga</b>'));
203 equals( result.text(), defaultText + 'buga', 'Check if text appending works' );
204 equals( jQuery('#select3').append(valueObj('<option value="appendTest">Append Test</option>')).find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
207 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
208 jQuery('#sap').append(valueObj(document.getElementById('first')));
209 equals( expected, jQuery('#sap').text(), "Check for appending of element" );
212 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
213 jQuery('#sap').append(valueObj([document.getElementById('first'), document.getElementById('yahoo')]));
214 equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
217 expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
218 jQuery('#sap').append(valueObj(jQuery("#first, #yahoo")));
219 equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
222 jQuery("#sap").append(valueObj( 5 ));
223 ok( jQuery("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
226 jQuery("#sap").append(valueObj( " text with spaces " ));
227 ok( jQuery("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );
230 ok( jQuery("#sap").append(valueObj( [] )), "Check for appending an empty array." );
231 ok( jQuery("#sap").append(valueObj( "" )), "Check for appending an empty string." );
232 ok( jQuery("#sap").append(valueObj( document.getElementsByTagName("foo") )), "Check for appending an empty nodelist." );
235 jQuery("form").append(valueObj('<input name="radiotest" type="radio" checked="checked" />'));
236 jQuery("form input[name=radiotest]").each(function(){
237 ok( jQuery(this).is(':checked'), "Append checked radio");
241 jQuery("form").append(valueObj('<input name="radiotest" type="radio" checked = \'checked\' />'));
242 jQuery("form input[name=radiotest]").each(function(){
243 ok( jQuery(this).is(':checked'), "Append alternately formated checked radio");
247 jQuery("form").append(valueObj('<input name="radiotest" type="radio" checked />'));
248 jQuery("form input[name=radiotest]").each(function(){
249 ok( jQuery(this).is(':checked'), "Append HTML5-formated checked radio");
253 jQuery("#sap").append(valueObj( document.getElementById('form') ));
254 equals( jQuery("#sap>form").size(), 1, "Check for appending a form" ); // Bug #910
259 jQuery( jQuery("#iframe")[0].contentWindow.document.body ).append(valueObj( "<div>test</div>" ));
264 ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
267 jQuery('<fieldset/>').appendTo('#form').append(valueObj( '<legend id="legend">test</legend>' ));
268 t( 'Append legend', '#legend', ['legend'] );
271 jQuery('#select1').append(valueObj( '<OPTION>Test</OPTION>' ));
272 equals( jQuery('#select1 option:last').text(), "Test", "Appending <OPTION> (all caps)" );
274 jQuery('#table').append(valueObj( '<colgroup></colgroup>' ));
275 ok( jQuery('#table colgroup').length, "Append colgroup" );
277 jQuery('#table colgroup').append(valueObj( '<col/>' ));
278 ok( jQuery('#table colgroup col').length, "Append col" );
281 jQuery('#table').append(valueObj( '<caption></caption>' ));
282 ok( jQuery('#table caption').length, "Append caption" );
286 .append(valueObj( '<select id="appendSelect1"></select>' ))
287 .append(valueObj( '<select id="appendSelect2"><option>Test</option></select>' ));
289 t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );
291 equals( "Two nodes", jQuery('<div />').append("Two", " nodes").text(), "Appending two text nodes (#4011)" );
293 // using contents will get comments regular, text, and comment nodes
294 var j = jQuery("#nonnodes").contents();
295 var d = jQuery("<div/>").appendTo("#nonnodes").append(j);
296 equals( jQuery("#nonnodes").length, 1, "Check node,textnode,comment append moved leaving just the div" );
297 ok( d.contents().length >= 2, "Check node,textnode,comment append works" );
298 d.contents().appendTo("#nonnodes");
300 ok( jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment append cleanup worked" );
303 test("append(String|Element|Array<Element>|jQuery)", function() {
307 test("append(Function)", function() {
308 testAppend(functionReturningObj);
311 test("append(Function) with incoming value", function() {
314 var defaultText = 'Try them out:', old = jQuery("#first").html();
316 var result = jQuery('#first').append(function(i, val){
317 equals( val, old, "Make sure the incoming value is correct." );
318 return '<b>buga</b>';
320 equals( result.text(), defaultText + 'buga', 'Check if text appending works' );
322 var select = jQuery('#select3');
325 equals( select.append(function(i, val){
326 equals( val, old, "Make sure the incoming value is correct." );
327 return '<option value="appendTest">Append Test</option>';
328 }).find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
331 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
332 old = jQuery("#sap").html();
334 jQuery('#sap').append(function(i, val){
335 equals( val, old, "Make sure the incoming value is correct." );
336 return document.getElementById('first');
338 equals( expected, jQuery('#sap').text(), "Check for appending of element" );
341 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
342 old = jQuery("#sap").html();
344 jQuery('#sap').append(function(i, val){
345 equals( val, old, "Make sure the incoming value is correct." );
346 return [document.getElementById('first'), document.getElementById('yahoo')];
348 equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
351 expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
352 old = jQuery("#sap").html();
354 jQuery('#sap').append(function(i, val){
355 equals( val, old, "Make sure the incoming value is correct." );
356 return jQuery("#first, #yahoo");
358 equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
361 old = jQuery("#sap").html();
363 jQuery("#sap").append(function(i, val){
364 equals( val, old, "Make sure the incoming value is correct." );
367 ok( jQuery("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
372 test("appendTo(String|Element|Array<Element>|jQuery)", function() {
374 var defaultText = 'Try them out:'
375 jQuery('<b>buga</b>').appendTo('#first');
376 equals( jQuery("#first").text(), defaultText + 'buga', 'Check if text appending works' );
377 equals( jQuery('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
380 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
381 jQuery(document.getElementById('first')).appendTo('#sap');
382 equals( expected, jQuery('#sap').text(), "Check for appending of element" );
385 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
386 jQuery([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
387 equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
390 ok( jQuery(document.createElement("script")).appendTo("body").length, "Make sure a disconnected script can be appended." );
393 expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
394 jQuery("#first, #yahoo").appendTo('#sap');
395 equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
398 jQuery('#select1').appendTo('#foo');
399 t( 'Append select', '#foo select', ['select1'] );
402 var div = jQuery("<div/>").click(function(){
403 ok(true, "Running a cloned click.");
405 div.appendTo("#main, #moretests");
407 jQuery("#main div:last").click();
408 jQuery("#moretests div:last").click();
411 var div = jQuery("<div/>").appendTo("#main, #moretests");
413 equals( div.length, 2, "appendTo returns the inserted elements" );
415 div.addClass("test");
417 ok( jQuery("#main div:last").hasClass("test"), "appendTo element was modified after the insertion" );
418 ok( jQuery("#moretests div:last").hasClass("test"), "appendTo element was modified after the insertion" );
423 var testPrepend = function(val) {
425 var defaultText = 'Try them out:'
426 var result = jQuery('#first').prepend(val( '<b>buga</b>' ));
427 equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
428 equals( jQuery('#select3').prepend(val( '<option value="prependTest">Prepend Test</option>' )).find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
431 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
432 jQuery('#sap').prepend(val( document.getElementById('first') ));
433 equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
436 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
437 jQuery('#sap').prepend(val( [document.getElementById('first'), document.getElementById('yahoo')] ));
438 equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
441 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
442 jQuery('#sap').prepend(val( jQuery("#first, #yahoo") ));
443 equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
446 test("prepend(String|Element|Array<Element>|jQuery)", function() {
447 testPrepend(bareObj);
450 test("prepend(Function)", function() {
451 testPrepend(functionReturningObj);
454 test("prepend(Function) with incoming value", function() {
457 var defaultText = 'Try them out:', old = jQuery('#first').html();
458 var result = jQuery('#first').prepend(function(i, val) {
459 equals( val, old, "Make sure the incoming value is correct." );
460 return '<b>buga</b>';
462 equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
464 old = jQuery("#select3").html();
466 equals( jQuery('#select3').prepend(function(i, val) {
467 equals( val, old, "Make sure the incoming value is correct." );
468 return '<option value="prependTest">Prepend Test</option>';
469 }).find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
472 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
473 old = jQuery('#sap').html();
475 jQuery('#sap').prepend(function(i, val) {
476 equals( val, old, "Make sure the incoming value is correct." );
477 return document.getElementById('first');
480 equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
483 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
484 old = jQuery('#sap').html();
486 jQuery('#sap').prepend(function(i, val) {
487 equals( val, old, "Make sure the incoming value is correct." );
488 return [document.getElementById('first'), document.getElementById('yahoo')];
491 equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
494 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
495 old = jQuery('#sap').html();
497 jQuery('#sap').prepend(function(i, val) {
498 equals( val, old, "Make sure the incoming value is correct." );
499 return jQuery("#first, #yahoo");
502 equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
505 test("prependTo(String|Element|Array<Element>|jQuery)", function() {
507 var defaultText = 'Try them out:'
508 jQuery('<b>buga</b>').prependTo('#first');
509 equals( jQuery('#first').text(), 'buga' + defaultText, 'Check if text prepending works' );
510 equals( jQuery('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
513 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
514 jQuery(document.getElementById('first')).prependTo('#sap');
515 equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
518 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
519 jQuery([document.getElementById('first'), document.getElementById('yahoo')]).prependTo('#sap');
520 equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
523 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
524 jQuery("#first, #yahoo").prependTo('#sap');
525 equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
528 jQuery('<select id="prependSelect1"></select>').prependTo('form:last');
529 jQuery('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');
531 t( "Prepend Select", "#prependSelect2, #prependSelect1", ["prependSelect2", "prependSelect1"] );
534 var testBefore = function(val) {
536 var expected = 'This is a normal link: bugaYahoo';
537 jQuery('#yahoo').before(val( '<b>buga</b>' ));
538 equals( expected, jQuery('#en').text(), 'Insert String before' );
541 expected = "This is a normal link: Try them out:Yahoo";
542 jQuery('#yahoo').before(val( document.getElementById('first') ));
543 equals( expected, jQuery('#en').text(), "Insert element before" );
546 expected = "This is a normal link: Try them out:diveintomarkYahoo";
547 jQuery('#yahoo').before(val( [document.getElementById('first'), document.getElementById('mark')] ));
548 equals( expected, jQuery('#en').text(), "Insert array of elements before" );
551 expected = "This is a normal link: diveintomarkTry them out:Yahoo";
552 jQuery('#yahoo').before(val( jQuery("#first, #mark") ));
553 equals( expected, jQuery('#en').text(), "Insert jQuery before" );
555 var set = jQuery("<div/>").before("<span>test</span>");
556 equals( set[0].nodeName.toLowerCase(), "span", "Insert the element before the disconnected node." );
557 equals( set.length, 2, "Insert the element before the disconnected node." );
560 test("before(String|Element|Array<Element>|jQuery)", function() {
564 test("before(Function)", function() {
565 testBefore(functionReturningObj);
568 test("insertBefore(String|Element|Array<Element>|jQuery)", function() {
570 var expected = 'This is a normal link: bugaYahoo';
571 jQuery('<b>buga</b>').insertBefore('#yahoo');
572 equals( expected, jQuery('#en').text(), 'Insert String before' );
575 expected = "This is a normal link: Try them out:Yahoo";
576 jQuery(document.getElementById('first')).insertBefore('#yahoo');
577 equals( expected, jQuery('#en').text(), "Insert element before" );
580 expected = "This is a normal link: Try them out:diveintomarkYahoo";
581 jQuery([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
582 equals( expected, jQuery('#en').text(), "Insert array of elements before" );
585 expected = "This is a normal link: diveintomarkTry them out:Yahoo";
586 jQuery("#first, #mark").insertBefore('#yahoo');
587 equals( expected, jQuery('#en').text(), "Insert jQuery before" );
590 var testAfter = function(val) {
592 var expected = 'This is a normal link: Yahoobuga';
593 jQuery('#yahoo').after(val( '<b>buga</b>' ));
594 equals( expected, jQuery('#en').text(), 'Insert String after' );
597 expected = "This is a normal link: YahooTry them out:";
598 jQuery('#yahoo').after(val( document.getElementById('first') ));
599 equals( expected, jQuery('#en').text(), "Insert element after" );
602 expected = "This is a normal link: YahooTry them out:diveintomark";
603 jQuery('#yahoo').after(val( [document.getElementById('first'), document.getElementById('mark')] ));
604 equals( expected, jQuery('#en').text(), "Insert array of elements after" );
607 expected = "This is a normal link: YahoodiveintomarkTry them out:";
608 jQuery('#yahoo').after(val( jQuery("#first, #mark") ));
609 equals( expected, jQuery('#en').text(), "Insert jQuery after" );
611 var set = jQuery("<div/>").after("<span>test</span>");
612 equals( set[1].nodeName.toLowerCase(), "span", "Insert the element after the disconnected node." );
613 equals( set.length, 2, "Insert the element after the disconnected node." );
616 test("after(String|Element|Array<Element>|jQuery)", function() {
620 test("after(Function)", function() {
621 testAfter(functionReturningObj);
624 test("insertAfter(String|Element|Array<Element>|jQuery)", function() {
626 var expected = 'This is a normal link: Yahoobuga';
627 jQuery('<b>buga</b>').insertAfter('#yahoo');
628 equals( expected, jQuery('#en').text(), 'Insert String after' );
631 expected = "This is a normal link: YahooTry them out:";
632 jQuery(document.getElementById('first')).insertAfter('#yahoo');
633 equals( expected, jQuery('#en').text(), "Insert element after" );
636 expected = "This is a normal link: YahooTry them out:diveintomark";
637 jQuery([document.getElementById('first'), document.getElementById('mark')]).insertAfter('#yahoo');
638 equals( expected, jQuery('#en').text(), "Insert array of elements after" );
641 expected = "This is a normal link: YahoodiveintomarkTry them out:";
642 jQuery("#first, #mark").insertAfter('#yahoo');
643 equals( expected, jQuery('#en').text(), "Insert jQuery after" );
646 var testReplaceWith = function(val) {
648 jQuery('#yahoo').replaceWith(val( '<b id="replace">buga</b>' ));
649 ok( jQuery("#replace")[0], 'Replace element with string' );
650 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
653 jQuery('#yahoo').replaceWith(val( document.getElementById('first') ));
654 ok( jQuery("#first")[0], 'Replace element with element' );
655 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
658 jQuery('#yahoo').replaceWith(val( [document.getElementById('first'), document.getElementById('mark')] ));
659 ok( jQuery("#first")[0], 'Replace element with array of elements' );
660 ok( jQuery("#mark")[0], 'Replace element with array of elements' );
661 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
664 jQuery('#yahoo').replaceWith(val( jQuery("#first, #mark") ));
665 ok( jQuery("#first")[0], 'Replace element with set of elements' );
666 ok( jQuery("#mark")[0], 'Replace element with set of elements' );
667 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
670 var tmp = jQuery("<div/>").appendTo("body").click(function(){ ok(true, "Newly bound click run." ); });
671 var y = jQuery('<div/>').appendTo("body").click(function(){ ok(true, "Previously bound click run." ); });
672 var child = y.append("<b>test</b>").find("b").click(function(){ ok(true, "Child bound click run." ); return false; });
674 y.replaceWith( tmp );
677 y.click(); // Shouldn't be run
678 child.click(); // Shouldn't be run
686 y = jQuery('<div/>').appendTo("body").click(function(){ ok(true, "Previously bound click run." ); });
687 var child2 = y.append("<u>test</u>").find("u").click(function(){ ok(true, "Child 2 bound click run." ); return false; });
689 y.replaceWith( child2 );
698 var set = jQuery("<div/>").replaceWith(val("<span>test</span>"));
699 equals( set[0].nodeName.toLowerCase(), "span", "Replace the disconnected node." );
700 equals( set.length, 1, "Replace the disconnected node." );
702 var $div = jQuery("<div class='replacewith'></div>").appendTo("body");
703 // TODO: Work on jQuery(...) inline script execution
704 //$div.replaceWith("<div class='replacewith'></div><script>" +
705 //"equals(jQuery('.replacewith').length, 1, 'Check number of elements in page.');" +
707 equals(jQuery('.replacewith').length, 1, 'Check number of elements in page.');
708 jQuery('.replacewith').remove();
711 test("replaceWith(String|Element|Array<Element>|jQuery)", function() {
712 testReplaceWith(bareObj);
715 test("replaceWith(Function)", function() {
716 testReplaceWith(functionReturningObj);
719 test("replaceAll(String|Element|Array<Element>|jQuery)", function() {
721 jQuery('<b id="replace">buga</b>').replaceAll("#yahoo");
722 ok( jQuery("#replace")[0], 'Replace element with string' );
723 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
726 jQuery(document.getElementById('first')).replaceAll("#yahoo");
727 ok( jQuery("#first")[0], 'Replace element with element' );
728 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
731 jQuery([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo");
732 ok( jQuery("#first")[0], 'Replace element with array of elements' );
733 ok( jQuery("#mark")[0], 'Replace element with array of elements' );
734 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
737 jQuery("#first, #mark").replaceAll("#yahoo");
738 ok( jQuery("#first")[0], 'Replace element with set of elements' );
739 ok( jQuery("#mark")[0], 'Replace element with set of elements' );
740 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
743 test("clone()", function() {
745 equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Assert text for #en' );
746 var clone = jQuery('#yahoo').clone();
747 equals( 'Try them out:Yahoo', jQuery('#first').append(clone).text(), 'Check for clone' );
748 equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Reassert text for #en' );
751 "<table/>", "<tr/>", "<td/>", "<div/>",
752 "<button/>", "<ul/>", "<ol/>", "<li/>",
753 "<input type='checkbox' />", "<select/>", "<option/>", "<textarea/>",
754 "<tbody/>", "<thead/>", "<tfoot/>", "<iframe/>"
756 for (var i = 0; i < cloneTags.length; i++) {
757 var j = jQuery(cloneTags[i]);
758 equals( j[0].tagName, j.clone()[0].tagName, 'Clone a <' + cloneTags[i].substring(1));
761 // using contents will get comments regular, text, and comment nodes
762 var cl = jQuery("#nonnodes").contents().clone();
763 ok( cl.length >= 2, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
765 var div = jQuery("<div><ul><li>test</li></ul></div>").click(function(){
766 ok( true, "Bound event still exists." );
769 div = div.clone(true).clone(true);
770 equals( div.length, 1, "One element cloned" );
771 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
772 div.trigger("click");
774 div = jQuery("<div/>").append([ document.createElement("table"), document.createElement("table") ]);
775 div.find("table").click(function(){
776 ok( true, "Bound event still exists." );
779 div = div.clone(true);
780 equals( div.length, 1, "One element cloned" );
781 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
782 div.find("table:last").trigger("click");
784 div = jQuery("<div/>").html('<object height="355" width="425"> <param name="movie" value="http://www.youtube.com/v/JikaHBDoV3k&hl=en"> <param name="wmode" value="transparent"> </object>');
786 div = div.clone(true);
787 equals( div.length, 1, "One element cloned" );
788 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
790 div = jQuery("<div/>").data({ a: true, b: true });
791 div = div.clone(true);
792 equals( div.data("a"), true, "Data cloned." );
793 equals( div.data("b"), true, "Data cloned." );
797 test("clone() on XML nodes", function() {
800 jQuery.get("data/dashboard.xml", function (xml) {
801 var root = jQuery(xml.documentElement).clone();
802 var origTab = jQuery("tab", xml).eq(0);
803 var cloneTab = jQuery("tab", root).eq(0);
804 origTab.text("origval");
805 cloneTab.text("cloneval");
806 equals(origTab.text(), "origval", "Check original XML node was correctly set");
807 equals(cloneTab.text(), "cloneval", "Check cloned XML node was correctly set");
813 var testHtml = function(valueObj) {
816 jQuery.scriptorder = 0;
818 var div = jQuery("#main > div");
819 div.html(valueObj("<b>test</b>"));
821 for ( var i = 0; i < div.size(); i++ ) {
822 if ( div.get(i).childNodes.length != 1 ) pass = false;
824 ok( pass, "Set HTML" );
826 div = jQuery("<div/>").html( valueObj('<div id="parent_1"><div id="child_1"/></div><div id="parent_2"/>') );
828 equals( div.children().length, 2, "Make sure two child nodes exist." );
829 equals( div.children().children().length, 1, "Make sure that a grandchild exists." );
832 // using contents will get comments regular, text, and comment nodes
833 var j = jQuery("#nonnodes").contents();
834 j.html(valueObj("<b>bold</b>"));
836 // this is needed, or the expando added by jQuery unique will yield a different html
837 j.find('b').removeData();
838 equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
840 jQuery("#main").html(valueObj("<select/>"));
841 jQuery("#main select").html(valueObj("<option>O1</option><option selected='selected'>O2</option><option>O3</option>"));
842 equals( jQuery("#main select").val(), "O2", "Selected option correct" );
844 var $div = jQuery('<div />');
845 equals( $div.html(valueObj( 5 )).html(), '5', 'Setting a number as html' );
846 equals( $div.html(valueObj( 0 )).html(), '0', 'Setting a zero as html' );
848 var $div2 = jQuery('<div/>'), insert = "<div>hello1</div>";
849 equals( $div2.html(insert).html().replace(/>/g, ">"), insert, "Verify escaped insertion." );
850 equals( $div2.html("x" + insert).html().replace(/>/g, ">"), "x" + insert, "Verify escaped insertion." );
851 equals( $div2.html(" " + insert).html().replace(/>/g, ">"), " " + insert, "Verify escaped insertion." );
853 var map = jQuery("<map/>").html(valueObj("<area id='map01' shape='rect' coords='50,50,150,150' href='http://www.jquery.com/' alt='jQuery'>"));
855 equals( map[0].childNodes.length, 1, "The area was inserted." );
856 equals( map[0].firstChild.nodeName.toLowerCase(), "area", "The area was inserted." );
860 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>'));
864 jQuery("#main").html(valueObj('<script type="text/javascript">ok( true, "jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975 (1)" );</script>'));
866 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>'));
868 // it was decided that waiting to execute ALL scripts makes sense since nested ones have to wait anyway so this test case is changed, see #1959
869 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>"));
871 setTimeout( start, 100 );
874 test("html(String)", function() {
878 test("html(Function)", function() {
879 testHtml(functionReturningObj);
882 test("html(Function) with incoming value", function() {
885 var div = jQuery("#main > div"), old = div.map(function(){ return jQuery(this).html() });
887 div.html(function(i, val) {
888 equals( val, old[i], "Make sure the incoming value is correct." );
889 return "<b>test</b>";
894 if ( this.childNodes.length !== 1 ) {
898 ok( pass, "Set HTML" );
901 // using contents will get comments regular, text, and comment nodes
902 var j = jQuery("#nonnodes").contents();
903 old = j.map(function(){ return jQuery(this).html(); });
905 j.html(function(i, val) {
906 equals( val, old[i], "Make sure the incoming value is correct." );
907 return "<b>bold</b>";
910 j.find('b').removeData();
911 equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
913 var $div = jQuery('<div />');
915 equals( $div.html(function(i, val) {
916 equals( val, "", "Make sure the incoming value is correct." );
918 }).html(), '5', 'Setting a number as html' );
920 equals( $div.html(function(i, val) {
921 equals( val, "5", "Make sure the incoming value is correct." );
923 }).html(), '0', 'Setting a zero as html' );
925 var $div2 = jQuery('<div/>'), insert = "<div>hello1</div>";
926 equals( $div2.html(function(i, val) {
927 equals( val, "", "Make sure the incoming value is correct." );
929 }).html().replace(/>/g, ">"), insert, "Verify escaped insertion." );
931 equals( $div2.html(function(i, val) {
932 equals( val.replace(/>/g, ">"), insert, "Make sure the incoming value is correct." );
934 }).html().replace(/>/g, ">"), "x" + insert, "Verify escaped insertion." );
936 equals( $div2.html(function(i, val) {
937 equals( val.replace(/>/g, ">"), "x" + insert, "Make sure the incoming value is correct." );
939 }).html().replace(/>/g, ">"), " " + insert, "Verify escaped insertion." );
942 var testRemove = function(method) {
945 var first = jQuery("#ap").children(":first");
946 first.data("foo", "bar");
948 jQuery("#ap").children()[method]();
949 ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
950 equals( jQuery("#ap").children().length, 0, "Check remove" );
952 equals( first.data("foo"), method == "remove" ? null : "bar" );
955 jQuery("#ap").children()[method]("a");
956 ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
957 equals( jQuery("#ap").children().length, 1, "Check filtered remove" );
959 jQuery("#ap").children()[method]("a, code");
960 equals( jQuery("#ap").children().length, 0, "Check multi-filtered remove" );
962 // using contents will get comments regular, text, and comment nodes
963 equals( jQuery("#nonnodes").contents().length, 3, "Check node,textnode,comment remove works" );
964 jQuery("#nonnodes").contents()[method]();
965 equals( jQuery("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" );
970 var first = jQuery("#ap").children(":first");
971 var cleanUp = first.click(function() { count++ })[method]().appendTo("body").click();
973 equals( method == "remove" ? 0 : 1, count );
978 test("remove()", function() {
979 testRemove("remove");
982 test("detach()", function() {
983 testRemove("detach");
986 test("empty()", function() {
988 equals( jQuery("#ap").children().empty().text().length, 0, "Check text is removed" );
989 equals( jQuery("#ap").children().length, 4, "Check elements are not removed" );
991 // using contents will get comments regular, text, and comment nodes
992 var j = jQuery("#nonnodes").contents();
994 equals( j.html(), "", "Check node,textnode,comment empty works" );
997 test("jQuery.cleanData", function() {
1000 var type, pos, div, child;
1004 // Should trigger 4 remove event
1005 div = getDiv().remove();
1007 // Should both do nothing
1009 div.trigger("click");
1012 div.children().trigger("click");
1016 child = div.children();
1018 // Should trigger 2 remove event
1023 div.trigger("click");
1025 // Should do nothing
1027 child.trigger("click");
1035 child = div.children();
1037 // Should trigger 2 remove event
1038 div.html("<div></div>");
1042 div.trigger("click");
1044 // Should do nothing
1046 child.trigger("click");
1052 var div = jQuery("<div class='outer'><div class='inner'></div></div>").click(function(){
1053 ok( true, type + " " + pos + " Click event fired." );
1054 }).focus(function(){
1055 ok( true, type + " " + pos + " Focus event fired." );
1056 }).find("div").click(function(){
1057 ok( false, type + " " + pos + " Click event fired." );
1058 }).focus(function(){
1059 ok( false, type + " " + pos + " Focus event fired." );
1060 }).end().appendTo("body");
1062 div[0].detachEvent = div[0].removeEventListener = function(t){
1063 ok( true, type + " Outer " + t + " event unbound" );
1066 div[0].firstChild.detachEvent = div[0].firstChild.removeEventListener = function(t){
1067 ok( true, type + " Inner " + t + " event unbound" );