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 // TODO: Figure out why each(wrapAll) is not equivalent to wrapAll
143 // test("wrapAll(Function)", function() {
144 // testWrapAll(functionReturningObj);
147 var testWrapInner = function(val) {
149 var num = jQuery("#first").children().length;
150 var result = jQuery('#first').wrapInner('<div class="red"><div id="tmp"></div></div>');
151 equals( jQuery("#first").children().length, 1, "Only one child" );
152 ok( jQuery("#first").children().is(".red"), "Verify Right Element" );
153 equals( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" );
156 var num = jQuery("#first").children().length;
157 var result = jQuery('#first').wrapInner(document.getElementById('empty'));
158 equals( jQuery("#first").children().length, 1, "Only one child" );
159 ok( jQuery("#first").children().is("#empty"), "Verify Right Element" );
160 equals( jQuery("#first").children().children().length, num, "Verify Elements Intact" );
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("#sap").append(valueObj( document.getElementById('form') ));
236 equals( jQuery("#sap>form").size(), 1, "Check for appending a form" ); // Bug #910
241 jQuery( jQuery("#iframe")[0].contentWindow.document.body ).append(valueObj( "<div>test</div>" ));
246 ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
249 jQuery('<fieldset/>').appendTo('#form').append(valueObj( '<legend id="legend">test</legend>' ));
250 t( 'Append legend', '#legend', ['legend'] );
253 jQuery('#select1').append(valueObj( '<OPTION>Test</OPTION>' ));
254 equals( jQuery('#select1 option:last').text(), "Test", "Appending <OPTION> (all caps)" );
256 jQuery('#table').append(valueObj( '<colgroup></colgroup>' ));
257 ok( jQuery('#table colgroup').length, "Append colgroup" );
259 jQuery('#table colgroup').append(valueObj( '<col/>' ));
260 ok( jQuery('#table colgroup col').length, "Append col" );
263 jQuery('#table').append(valueObj( '<caption></caption>' ));
264 ok( jQuery('#table caption').length, "Append caption" );
268 .append(valueObj( '<select id="appendSelect1"></select>' ))
269 .append(valueObj( '<select id="appendSelect2"><option>Test</option></select>' ));
271 t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );
273 equals( "Two nodes", jQuery('<div />').append("Two", " nodes").text(), "Appending two text nodes (#4011)" );
275 // using contents will get comments regular, text, and comment nodes
276 var j = jQuery("#nonnodes").contents();
277 var d = jQuery("<div/>").appendTo("#nonnodes").append(j);
278 equals( jQuery("#nonnodes").length, 1, "Check node,textnode,comment append moved leaving just the div" );
279 ok( d.contents().length >= 2, "Check node,textnode,comment append works" );
280 d.contents().appendTo("#nonnodes");
282 ok( jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment append cleanup worked" );
285 test("append(String|Element|Array<Element>|jQuery)", function() {
289 test("append(Function)", function() {
290 testAppend(functionReturningObj);
293 test("append(Function) with incoming value", function() {
296 var defaultText = 'Try them out:', old = jQuery("#first").html();
298 var result = jQuery('#first').append(function(i, val){
299 equals( val, old, "Make sure the incoming value is correct." );
300 return '<b>buga</b>';
302 equals( result.text(), defaultText + 'buga', 'Check if text appending works' );
304 var select = jQuery('#select3');
307 equals( select.append(function(i, val){
308 equals( val, old, "Make sure the incoming value is correct." );
309 return '<option value="appendTest">Append Test</option>';
310 }).find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
313 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
314 old = jQuery("#sap").html();
316 jQuery('#sap').append(function(i, val){
317 equals( val, old, "Make sure the incoming value is correct." );
318 return document.getElementById('first');
320 equals( expected, jQuery('#sap').text(), "Check for appending of element" );
323 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
324 old = jQuery("#sap").html();
326 jQuery('#sap').append(function(i, val){
327 equals( val, old, "Make sure the incoming value is correct." );
328 return [document.getElementById('first'), document.getElementById('yahoo')];
330 equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
333 expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
334 old = jQuery("#sap").html();
336 jQuery('#sap').append(function(i, val){
337 equals( val, old, "Make sure the incoming value is correct." );
338 return jQuery("#first, #yahoo");
340 equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
343 old = jQuery("#sap").html();
345 jQuery("#sap").append(function(i, val){
346 equals( val, old, "Make sure the incoming value is correct." );
349 ok( jQuery("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
354 test("appendTo(String|Element|Array<Element>|jQuery)", function() {
356 var defaultText = 'Try them out:'
357 jQuery('<b>buga</b>').appendTo('#first');
358 equals( jQuery("#first").text(), defaultText + 'buga', 'Check if text appending works' );
359 equals( jQuery('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
362 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
363 jQuery(document.getElementById('first')).appendTo('#sap');
364 equals( expected, jQuery('#sap').text(), "Check for appending of element" );
367 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
368 jQuery([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
369 equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
372 ok( jQuery(document.createElement("script")).appendTo("body").length, "Make sure a disconnected script can be appended." );
375 expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
376 jQuery("#first, #yahoo").appendTo('#sap');
377 equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
380 jQuery('#select1').appendTo('#foo');
381 t( 'Append select', '#foo select', ['select1'] );
384 var div = jQuery("<div/>").click(function(){
385 ok(true, "Running a cloned click.");
387 div.appendTo("#main, #moretests");
389 jQuery("#main div:last").click();
390 jQuery("#moretests div:last").click();
393 var div = jQuery("<div/>").appendTo("#main, #moretests");
395 equals( div.length, 2, "appendTo returns the inserted elements" );
397 div.addClass("test");
399 ok( jQuery("#main div:last").hasClass("test"), "appendTo element was modified after the insertion" );
400 ok( jQuery("#moretests div:last").hasClass("test"), "appendTo element was modified after the insertion" );
405 var testPrepend = function(val) {
407 var defaultText = 'Try them out:'
408 var result = jQuery('#first').prepend(val( '<b>buga</b>' ));
409 equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
410 equals( jQuery('#select3').prepend(val( '<option value="prependTest">Prepend Test</option>' )).find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
413 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
414 jQuery('#sap').prepend(val( document.getElementById('first') ));
415 equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
418 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
419 jQuery('#sap').prepend(val( [document.getElementById('first'), document.getElementById('yahoo')] ));
420 equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
423 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
424 jQuery('#sap').prepend(val( jQuery("#first, #yahoo") ));
425 equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
428 test("prepend(String|Element|Array<Element>|jQuery)", function() {
429 testPrepend(bareObj);
432 test("prepend(Function)", function() {
433 testPrepend(functionReturningObj);
436 test("prepend(Function) with incoming value", function() {
439 var defaultText = 'Try them out:', old = jQuery('#first').html();
440 var result = jQuery('#first').prepend(function(i, val) {
441 equals( val, old, "Make sure the incoming value is correct." );
442 return '<b>buga</b>';
444 equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
446 old = jQuery("#select3").html();
448 equals( jQuery('#select3').prepend(function(i, val) {
449 equals( val, old, "Make sure the incoming value is correct." );
450 return '<option value="prependTest">Prepend Test</option>';
451 }).find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
454 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
455 old = jQuery('#sap').html();
457 jQuery('#sap').prepend(function(i, val) {
458 equals( val, old, "Make sure the incoming value is correct." );
459 return document.getElementById('first');
462 equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
465 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
466 old = jQuery('#sap').html();
468 jQuery('#sap').prepend(function(i, val) {
469 equals( val, old, "Make sure the incoming value is correct." );
470 return [document.getElementById('first'), document.getElementById('yahoo')];
473 equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
476 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
477 old = jQuery('#sap').html();
479 jQuery('#sap').prepend(function(i, val) {
480 equals( val, old, "Make sure the incoming value is correct." );
481 return jQuery("#first, #yahoo");
484 equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
487 test("prependTo(String|Element|Array<Element>|jQuery)", function() {
489 var defaultText = 'Try them out:'
490 jQuery('<b>buga</b>').prependTo('#first');
491 equals( jQuery('#first').text(), 'buga' + defaultText, 'Check if text prepending works' );
492 equals( jQuery('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
495 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
496 jQuery(document.getElementById('first')).prependTo('#sap');
497 equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
500 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
501 jQuery([document.getElementById('first'), document.getElementById('yahoo')]).prependTo('#sap');
502 equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
505 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
506 jQuery("#first, #yahoo").prependTo('#sap');
507 equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
510 jQuery('<select id="prependSelect1"></select>').prependTo('form:last');
511 jQuery('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');
513 t( "Prepend Select", "#prependSelect2, #prependSelect1", ["prependSelect2", "prependSelect1"] );
516 var testBefore = function(val) {
518 var expected = 'This is a normal link: bugaYahoo';
519 jQuery('#yahoo').before(val( '<b>buga</b>' ));
520 equals( expected, jQuery('#en').text(), 'Insert String before' );
523 expected = "This is a normal link: Try them out:Yahoo";
524 jQuery('#yahoo').before(val( document.getElementById('first') ));
525 equals( expected, jQuery('#en').text(), "Insert element before" );
528 expected = "This is a normal link: Try them out:diveintomarkYahoo";
529 jQuery('#yahoo').before(val( [document.getElementById('first'), document.getElementById('mark')] ));
530 equals( expected, jQuery('#en').text(), "Insert array of elements before" );
533 expected = "This is a normal link: diveintomarkTry them out:Yahoo";
534 jQuery('#yahoo').before(val( jQuery("#first, #mark") ));
535 equals( expected, jQuery('#en').text(), "Insert jQuery before" );
537 var set = jQuery("<div/>").before("<span>test</span>");
538 equals( set[0].nodeName.toLowerCase(), "span", "Insert the element before the disconnected node." );
539 equals( set.length, 2, "Insert the element before the disconnected node." );
542 test("before(String|Element|Array<Element>|jQuery)", function() {
546 test("before(Function)", function() {
547 testBefore(functionReturningObj);
550 test("insertBefore(String|Element|Array<Element>|jQuery)", function() {
552 var expected = 'This is a normal link: bugaYahoo';
553 jQuery('<b>buga</b>').insertBefore('#yahoo');
554 equals( expected, jQuery('#en').text(), 'Insert String before' );
557 expected = "This is a normal link: Try them out:Yahoo";
558 jQuery(document.getElementById('first')).insertBefore('#yahoo');
559 equals( expected, jQuery('#en').text(), "Insert element before" );
562 expected = "This is a normal link: Try them out:diveintomarkYahoo";
563 jQuery([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
564 equals( expected, jQuery('#en').text(), "Insert array of elements before" );
567 expected = "This is a normal link: diveintomarkTry them out:Yahoo";
568 jQuery("#first, #mark").insertBefore('#yahoo');
569 equals( expected, jQuery('#en').text(), "Insert jQuery before" );
572 var testAfter = function(val) {
574 var expected = 'This is a normal link: Yahoobuga';
575 jQuery('#yahoo').after(val( '<b>buga</b>' ));
576 equals( expected, jQuery('#en').text(), 'Insert String after' );
579 expected = "This is a normal link: YahooTry them out:";
580 jQuery('#yahoo').after(val( document.getElementById('first') ));
581 equals( expected, jQuery('#en').text(), "Insert element after" );
584 expected = "This is a normal link: YahooTry them out:diveintomark";
585 jQuery('#yahoo').after(val( [document.getElementById('first'), document.getElementById('mark')] ));
586 equals( expected, jQuery('#en').text(), "Insert array of elements after" );
589 expected = "This is a normal link: YahoodiveintomarkTry them out:";
590 jQuery('#yahoo').after(val( jQuery("#first, #mark") ));
591 equals( expected, jQuery('#en').text(), "Insert jQuery after" );
593 var set = jQuery("<div/>").after("<span>test</span>");
594 equals( set[1].nodeName.toLowerCase(), "span", "Insert the element after the disconnected node." );
595 equals( set.length, 2, "Insert the element after the disconnected node." );
598 test("after(String|Element|Array<Element>|jQuery)", function() {
602 test("after(Function)", function() {
603 testAfter(functionReturningObj);
606 test("insertAfter(String|Element|Array<Element>|jQuery)", function() {
608 var expected = 'This is a normal link: Yahoobuga';
609 jQuery('<b>buga</b>').insertAfter('#yahoo');
610 equals( expected, jQuery('#en').text(), 'Insert String after' );
613 expected = "This is a normal link: YahooTry them out:";
614 jQuery(document.getElementById('first')).insertAfter('#yahoo');
615 equals( expected, jQuery('#en').text(), "Insert element after" );
618 expected = "This is a normal link: YahooTry them out:diveintomark";
619 jQuery([document.getElementById('first'), document.getElementById('mark')]).insertAfter('#yahoo');
620 equals( expected, jQuery('#en').text(), "Insert array of elements after" );
623 expected = "This is a normal link: YahoodiveintomarkTry them out:";
624 jQuery("#first, #mark").insertAfter('#yahoo');
625 equals( expected, jQuery('#en').text(), "Insert jQuery after" );
628 var testReplaceWith = function(val) {
630 jQuery('#yahoo').replaceWith(val( '<b id="replace">buga</b>' ));
631 ok( jQuery("#replace")[0], 'Replace element with string' );
632 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
635 jQuery('#yahoo').replaceWith(val( document.getElementById('first') ));
636 ok( jQuery("#first")[0], 'Replace element with element' );
637 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
640 jQuery('#yahoo').replaceWith(val( [document.getElementById('first'), document.getElementById('mark')] ));
641 ok( jQuery("#first")[0], 'Replace element with array of elements' );
642 ok( jQuery("#mark")[0], 'Replace element with array of elements' );
643 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
646 jQuery('#yahoo').replaceWith(val( jQuery("#first, #mark") ));
647 ok( jQuery("#first")[0], 'Replace element with set of elements' );
648 ok( jQuery("#mark")[0], 'Replace element with set of elements' );
649 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
652 var tmp = jQuery("<div/>").appendTo("body").click(function(){ ok(true, "Newly bound click run." ); });
653 var y = jQuery('#yahoo').click(function(){ ok(true, "Previously bound click run." ); });
654 var child = y.append("<b>test</b>").find("b").click(function(){ ok(true, "Child bound click run." ); return false; });
656 y.replaceWith( tmp );
664 var set = jQuery("<div/>").replaceWith(val("<span>test</span>"));
665 equals( set[0].nodeName.toLowerCase(), "span", "Replace the disconnected node." );
666 equals( set.length, 1, "Replace the disconnected node." );
668 var $div = jQuery("<div class='replacewith'></div>").appendTo("body");
669 $div.replaceWith("<div class='replacewith'></div><script>" +
670 "equals(jQuery('.replacewith').length, 1, 'Check number of elements in page.');" +
672 equals(jQuery('.replacewith').length, 1, 'Check number of elements in page.');
673 jQuery('.replacewith').remove();
676 test("replaceWith(String|Element|Array<Element>|jQuery)", function() {
677 testReplaceWith(bareObj);
680 test("replaceWith(Function)", function() {
681 testReplaceWith(functionReturningObj);
684 test("replaceAll(String|Element|Array<Element>|jQuery)", function() {
686 jQuery('<b id="replace">buga</b>').replaceAll("#yahoo");
687 ok( jQuery("#replace")[0], 'Replace element with string' );
688 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
691 jQuery(document.getElementById('first')).replaceAll("#yahoo");
692 ok( jQuery("#first")[0], 'Replace element with element' );
693 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
696 jQuery([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo");
697 ok( jQuery("#first")[0], 'Replace element with array of elements' );
698 ok( jQuery("#mark")[0], 'Replace element with array of elements' );
699 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
702 jQuery("#first, #mark").replaceAll("#yahoo");
703 ok( jQuery("#first")[0], 'Replace element with set of elements' );
704 ok( jQuery("#mark")[0], 'Replace element with set of elements' );
705 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
708 test("clone()", function() {
710 equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Assert text for #en' );
711 var clone = jQuery('#yahoo').clone();
712 equals( 'Try them out:Yahoo', jQuery('#first').append(clone).text(), 'Check for clone' );
713 equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Reassert text for #en' );
716 "<table/>", "<tr/>", "<td/>", "<div/>",
717 "<button/>", "<ul/>", "<ol/>", "<li/>",
718 "<input type='checkbox' />", "<select/>", "<option/>", "<textarea/>",
719 "<tbody/>", "<thead/>", "<tfoot/>", "<iframe/>"
721 for (var i = 0; i < cloneTags.length; i++) {
722 var j = jQuery(cloneTags[i]);
723 equals( j[0].tagName, j.clone()[0].tagName, 'Clone a <' + cloneTags[i].substring(1));
726 // using contents will get comments regular, text, and comment nodes
727 var cl = jQuery("#nonnodes").contents().clone();
728 ok( cl.length >= 2, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
730 var div = jQuery("<div><ul><li>test</li></ul></div>").click(function(){
731 ok( true, "Bound event still exists." );
734 div = div.clone(true).clone(true);
735 equals( div.length, 1, "One element cloned" );
736 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
737 div.trigger("click");
739 div = jQuery("<div/>").append([ document.createElement("table"), document.createElement("table") ]);
740 div.find("table").click(function(){
741 ok( true, "Bound event still exists." );
744 div = div.clone(true);
745 equals( div.length, 1, "One element cloned" );
746 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
747 div.find("table:last").trigger("click");
749 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>');
751 div = div.clone(true);
752 equals( div.length, 1, "One element cloned" );
753 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
755 div = jQuery("<div/>").data({ a: true, b: true });
756 div = div.clone(true);
757 equals( div.data("a"), true, "Data cloned." );
758 equals( div.data("b"), true, "Data cloned." );
762 test("clone() on XML nodes", function() {
765 jQuery.get("data/dashboard.xml", function (xml) {
766 var root = jQuery(xml.documentElement).clone();
767 var origTab = jQuery("tab", xml).eq(0);
768 var cloneTab = jQuery("tab", root).eq(0);
769 origTab.text("origval");
770 cloneTab.text("cloneval");
771 equals(origTab.text(), "origval", "Check original XML node was correctly set");
772 equals(cloneTab.text(), "cloneval", "Check cloned XML node was correctly set");
778 var testHtml = function(valueObj) {
781 jQuery.scriptorder = 0;
783 var div = jQuery("#main > div");
784 div.html(valueObj("<b>test</b>"));
786 for ( var i = 0; i < div.size(); i++ ) {
787 if ( div.get(i).childNodes.length != 1 ) pass = false;
789 ok( pass, "Set HTML" );
792 // using contents will get comments regular, text, and comment nodes
793 var j = jQuery("#nonnodes").contents();
794 j.html(valueObj("<b>bold</b>"));
796 // this is needed, or the expando added by jQuery unique will yield a different html
797 j.find('b').removeData();
798 equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
800 jQuery("#main").html(valueObj("<select/>"));
801 jQuery("#main select").html(valueObj("<option>O1</option><option selected='selected'>O2</option><option>O3</option>"));
802 equals( jQuery("#main select").val(), "O2", "Selected option correct" );
804 var $div = jQuery('<div />');
805 equals( $div.html(valueObj( 5 )).html(), '5', 'Setting a number as html' );
806 equals( $div.html(valueObj( 0 )).html(), '0', 'Setting a zero as html' );
808 var $div2 = jQuery('<div/>'), insert = "<div>hello1</div>";
809 equals( $div2.html(insert).html(), insert, "Verify escaped insertion." );
810 equals( $div2.html("x" + insert).html(), "x" + insert, "Verify escaped insertion." );
811 equals( $div2.html(" " + insert).html(), " " + insert, "Verify escaped insertion." );
813 var map = jQuery("<map/>").html(valueObj("<area id='map01' shape='rect' coords='50,50,150,150' href='http://www.jquery.com/' alt='jQuery'>"));
815 equals( map[0].childNodes.length, 1, "The area was inserted." );
816 equals( map[0].firstChild.nodeName.toLowerCase(), "area", "The area was inserted." );
820 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>'));
824 jQuery("#main").html(valueObj('<script type="text/javascript">ok( true, "jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975 (1)" );</script>'));
826 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>'));
828 // 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
829 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>"));
831 setTimeout( start, 100 );
834 test("html(String)", function() {
838 test("html(Function)", function() {
839 testHtml(functionReturningObj);
842 test("html(Function) with incoming value", function() {
845 var div = jQuery("#main > div"), old = div.map(function(){ return jQuery(this).html() });
847 div.html(function(i, val) {
848 equals( val, old[i], "Make sure the incoming value is correct." );
849 return "<b>test</b>";
854 if ( this.childNodes.length !== 1 ) {
858 ok( pass, "Set HTML" );
861 // using contents will get comments regular, text, and comment nodes
862 var j = jQuery("#nonnodes").contents();
863 old = j.map(function(){ return jQuery(this).html(); });
865 j.html(function(i, val) {
866 equals( val, old[i], "Make sure the incoming value is correct." );
867 return "<b>bold</b>";
870 j.find('b').removeData();
871 equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
873 var $div = jQuery('<div />');
875 equals( $div.html(function(i, val) {
876 equals( val, "", "Make sure the incoming value is correct." );
878 }).html(), '5', 'Setting a number as html' );
880 equals( $div.html(function(i, val) {
881 equals( val, "5", "Make sure the incoming value is correct." );
883 }).html(), '0', 'Setting a zero as html' );
885 var $div2 = jQuery('<div/>'), insert = "<div>hello1</div>";
886 equals( $div2.html(function(i, val) {
887 equals( val, "", "Make sure the incoming value is correct." );
889 }).html(), insert, "Verify escaped insertion." );
891 equals( $div2.html(function(i, val) {
892 equals( val, insert, "Make sure the incoming value is correct." );
894 }).html(), "x" + insert, "Verify escaped insertion." );
896 equals( $div2.html(function(i, val) {
897 equals( val, "x" + insert, "Make sure the incoming value is correct." );
899 }).html(), " " + insert, "Verify escaped insertion." );
902 var testRemove = function(method) {
905 var first = jQuery("#ap").children(":first");
906 first.data("foo", "bar");
908 jQuery("#ap").children()[method]();
909 ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
910 equals( jQuery("#ap").children().length, 0, "Check remove" );
912 equals( first.data("foo"), method == "remove" ? null : "bar" );
915 jQuery("#ap").children()[method]("a");
916 ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
917 equals( jQuery("#ap").children().length, 1, "Check filtered remove" );
919 jQuery("#ap").children()[method]("a, code");
920 equals( jQuery("#ap").children().length, 0, "Check multi-filtered remove" );
922 // using contents will get comments regular, text, and comment nodes
923 equals( jQuery("#nonnodes").contents().length, 3, "Check node,textnode,comment remove works" );
924 jQuery("#nonnodes").contents()[method]();
925 equals( jQuery("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" );
930 var first = jQuery("#ap").children(":first");
931 var cleanUp = first.click(function() { count++ })[method]().appendTo("body").click();
933 equals( method == "remove" ? 0 : 1, count );
938 test("remove()", function() {
939 testRemove("remove");
942 test("detach()", function() {
943 testRemove("detach");
946 test("empty()", function() {
948 equals( jQuery("#ap").children().empty().text().length, 0, "Check text is removed" );
949 equals( jQuery("#ap").children().length, 4, "Check elements are not removed" );
951 // using contents will get comments regular, text, and comment nodes
952 var j = jQuery("#nonnodes").contents();
954 equals( j.html(), "", "Check node,textnode,comment empty works" );