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(val('<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").html("foo<div>test</div><div>test2</div>").children().length;
152 var result = jQuery('#first').wrapInner(val('<div class="red"><div id="tmp"></div></div>'));
153 equals( jQuery("#first").children().length, 1, "Only one child" );
154 ok( jQuery("#first").children().is(".red"), "Verify Right Element" );
155 equals( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" );
158 var num = jQuery("#first").children().length;
159 var result = jQuery('#first').wrapInner(val(document.getElementById('empty')));
160 equals( jQuery("#first").children().length, 1, "Only one child" );
161 ok( jQuery("#first").children().is("#empty"), "Verify Right Element" );
162 equals( jQuery("#first").children().children().length, num, "Verify Elements Intact" );
164 var div = jQuery("<div/>");
165 div.wrapInner(val("<span></span>"));
166 equals(div.children().length, 1, "The contents were wrapped.");
167 equals(div.children()[0].nodeName.toLowerCase(), "span", "A span was inserted.");
170 test("wrapInner(String|Element)", function() {
171 testWrapInner(bareObj);
174 test("wrapInner(Function)", function() {
175 testWrapInner(functionReturningObj)
178 test("unwrap()", function() {
181 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>');
183 var abcd = jQuery('#unwrap1 > span, #unwrap2 > span').get(),
184 abcdef = jQuery('#unwrap span').get();
186 equals( jQuery('#unwrap1 span').add('#unwrap2 span:first').unwrap().length, 3, 'make #unwrap1 and #unwrap2 go away' );
187 same( jQuery('#unwrap > span').get(), abcd, 'all four spans should still exist' );
189 same( jQuery('#unwrap3 span').unwrap().get(), jQuery('#unwrap3 > span').get(), 'make all b in #unwrap3 go away' );
191 same( jQuery('#unwrap3 span').unwrap().get(), jQuery('#unwrap > span.unwrap3').get(), 'make #unwrap3 go away' );
193 same( jQuery('#unwrap').children().get(), abcdef, '#unwrap only contains 6 child spans' );
195 same( jQuery('#unwrap > span').unwrap().get(), jQuery('body > span.unwrap').get(), 'make the 6 spans become children of body' );
197 same( jQuery('body > span.unwrap').unwrap().get(), jQuery('body > span.unwrap').get(), 'can\'t unwrap children of body' );
198 same( jQuery('body > span.unwrap').unwrap().get(), abcdef, 'can\'t unwrap children of body' );
200 same( jQuery('body > span.unwrap').get(), abcdef, 'body contains 6 .unwrap child spans' );
202 jQuery('body > span.unwrap').remove();
205 var testAppend = function(valueObj) {
207 var defaultText = 'Try them out:'
208 var result = jQuery('#first').append(valueObj('<b>buga</b>'));
209 equals( result.text(), defaultText + 'buga', 'Check if text appending works' );
210 equals( jQuery('#select3').append(valueObj('<option value="appendTest">Append Test</option>')).find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
213 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
214 jQuery('#sap').append(valueObj(document.getElementById('first')));
215 equals( expected, jQuery('#sap').text(), "Check for appending of element" );
218 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
219 jQuery('#sap').append(valueObj([document.getElementById('first'), document.getElementById('yahoo')]));
220 equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
223 expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
224 jQuery('#sap').append(valueObj(jQuery("#yahoo, #first")));
225 equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
228 jQuery("#sap").append(valueObj( 5 ));
229 ok( jQuery("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
232 jQuery("#sap").append(valueObj( " text with spaces " ));
233 ok( jQuery("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );
236 ok( jQuery("#sap").append(valueObj( [] )), "Check for appending an empty array." );
237 ok( jQuery("#sap").append(valueObj( "" )), "Check for appending an empty string." );
238 ok( jQuery("#sap").append(valueObj( document.getElementsByTagName("foo") )), "Check for appending an empty nodelist." );
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 checked radio");
247 jQuery("form").append(valueObj('<input name="radiotest" type="radio" checked = \'checked\' />'));
248 jQuery("form input[name=radiotest]").each(function(){
249 ok( jQuery(this).is(':checked'), "Append alternately formated checked radio");
253 jQuery("form").append(valueObj('<input name="radiotest" type="radio" checked />'));
254 jQuery("form input[name=radiotest]").each(function(){
255 ok( jQuery(this).is(':checked'), "Append HTML5-formated checked radio");
259 jQuery("#sap").append(valueObj( document.getElementById('form') ));
260 equals( jQuery("#sap>form").size(), 1, "Check for appending a form" ); // Bug #910
265 var body = jQuery("#iframe")[0].contentWindow.document.body;
268 jQuery( document.body ).append(valueObj( "<div>test</div>" ));
272 ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
275 jQuery('<fieldset/>').appendTo('#form').append(valueObj( '<legend id="legend">test</legend>' ));
276 t( 'Append legend', '#legend', ['legend'] );
279 jQuery('#select1').append(valueObj( '<OPTION>Test</OPTION>' ));
280 equals( jQuery('#select1 option:last').text(), "Test", "Appending <OPTION> (all caps)" );
282 jQuery('#table').append(valueObj( '<colgroup></colgroup>' ));
283 ok( jQuery('#table colgroup').length, "Append colgroup" );
285 jQuery('#table colgroup').append(valueObj( '<col/>' ));
286 ok( jQuery('#table colgroup col').length, "Append col" );
289 jQuery('#table').append(valueObj( '<caption></caption>' ));
290 ok( jQuery('#table caption').length, "Append caption" );
294 .append(valueObj( '<select id="appendSelect1"></select>' ))
295 .append(valueObj( '<select id="appendSelect2"><option>Test</option></select>' ));
297 t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );
299 equals( "Two nodes", jQuery('<div />').append("Two", " nodes").text(), "Appending two text nodes (#4011)" );
301 // using contents will get comments regular, text, and comment nodes
302 var j = jQuery("#nonnodes").contents();
303 var d = jQuery("<div/>").appendTo("#nonnodes").append(j);
304 equals( jQuery("#nonnodes").length, 1, "Check node,textnode,comment append moved leaving just the div" );
305 ok( d.contents().length >= 2, "Check node,textnode,comment append works" );
306 d.contents().appendTo("#nonnodes");
308 ok( jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment append cleanup worked" );
311 test("append(String|Element|Array<Element>|jQuery)", function() {
315 test("append(Function)", function() {
316 testAppend(functionReturningObj);
319 test("append(Function) with incoming value", function() {
322 var defaultText = 'Try them out:', old = jQuery("#first").html();
324 var result = jQuery('#first').append(function(i, val){
325 equals( val, old, "Make sure the incoming value is correct." );
326 return '<b>buga</b>';
328 equals( result.text(), defaultText + 'buga', 'Check if text appending works' );
330 var select = jQuery('#select3');
333 equals( select.append(function(i, val){
334 equals( val, old, "Make sure the incoming value is correct." );
335 return '<option value="appendTest">Append Test</option>';
336 }).find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
339 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
340 old = jQuery("#sap").html();
342 jQuery('#sap').append(function(i, val){
343 equals( val, old, "Make sure the incoming value is correct." );
344 return document.getElementById('first');
346 equals( expected, jQuery('#sap').text(), "Check for appending of element" );
349 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
350 old = jQuery("#sap").html();
352 jQuery('#sap').append(function(i, val){
353 equals( val, old, "Make sure the incoming value is correct." );
354 return [document.getElementById('first'), document.getElementById('yahoo')];
356 equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
359 expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
360 old = jQuery("#sap").html();
362 jQuery('#sap').append(function(i, val){
363 equals( val, old, "Make sure the incoming value is correct." );
364 return jQuery("#yahoo, #first");
366 equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
369 old = jQuery("#sap").html();
371 jQuery("#sap").append(function(i, val){
372 equals( val, old, "Make sure the incoming value is correct." );
375 ok( jQuery("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
380 test("appendTo(String|Element|Array<Element>|jQuery)", function() {
383 var defaultText = 'Try them out:'
384 jQuery('<b>buga</b>').appendTo('#first');
385 equals( jQuery("#first").text(), defaultText + 'buga', 'Check if text appending works' );
386 equals( jQuery('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
389 var l = jQuery("#first").children().length + 2;
390 jQuery("<strong>test</strong>");
391 jQuery("<strong>test</strong>");
392 jQuery([ jQuery("<strong>test</strong>")[0], jQuery("<strong>test</strong>")[0] ])
394 equals( jQuery("#first").children().length, l, "Make sure the elements were inserted." );
395 equals( jQuery("#first").children().last()[0].nodeName.toLowerCase(), "strong", "Verify the last element." );
398 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
399 jQuery(document.getElementById('first')).appendTo('#sap');
400 equals( expected, jQuery('#sap').text(), "Check for appending of element" );
403 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
404 jQuery([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
405 equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
408 ok( jQuery(document.createElement("script")).appendTo("body").length, "Make sure a disconnected script can be appended." );
411 expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
412 jQuery("#yahoo, #first").appendTo('#sap');
413 equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
416 jQuery('#select1').appendTo('#foo');
417 t( 'Append select', '#foo select', ['select1'] );
420 var div = jQuery("<div/>").click(function(){
421 ok(true, "Running a cloned click.");
423 div.appendTo("#main, #moretests");
425 jQuery("#main div:last").click();
426 jQuery("#moretests div:last").click();
429 var div = jQuery("<div/>").appendTo("#main, #moretests");
431 equals( div.length, 2, "appendTo returns the inserted elements" );
433 div.addClass("test");
435 ok( jQuery("#main div:last").hasClass("test"), "appendTo element was modified after the insertion" );
436 ok( jQuery("#moretests div:last").hasClass("test"), "appendTo element was modified after the insertion" );
440 div = jQuery("<div/>");
441 jQuery("<span>a</span><b>b</b>").filter("span").appendTo( div );
443 equals( div.children().length, 1, "Make sure the right number of children were inserted." );
445 div = jQuery("#moretests div");
447 var num = jQuery("#main div").length;
448 div.remove().appendTo("#main");
450 equals( jQuery("#main div").length, num, "Make sure all the removed divs were inserted." );
455 var testPrepend = function(val) {
457 var defaultText = 'Try them out:'
458 var result = jQuery('#first').prepend(val( '<b>buga</b>' ));
459 equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
460 equals( jQuery('#select3').prepend(val( '<option value="prependTest">Prepend Test</option>' )).find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
463 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
464 jQuery('#sap').prepend(val( document.getElementById('first') ));
465 equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
468 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
469 jQuery('#sap').prepend(val( [document.getElementById('first'), document.getElementById('yahoo')] ));
470 equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
473 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
474 jQuery('#sap').prepend(val( jQuery("#yahoo, #first") ));
475 equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
478 test("prepend(String|Element|Array<Element>|jQuery)", function() {
479 testPrepend(bareObj);
482 test("prepend(Function)", function() {
483 testPrepend(functionReturningObj);
486 test("prepend(Function) with incoming value", function() {
489 var defaultText = 'Try them out:', old = jQuery('#first').html();
490 var result = jQuery('#first').prepend(function(i, val) {
491 equals( val, old, "Make sure the incoming value is correct." );
492 return '<b>buga</b>';
494 equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
496 old = jQuery("#select3").html();
498 equals( jQuery('#select3').prepend(function(i, val) {
499 equals( val, old, "Make sure the incoming value is correct." );
500 return '<option value="prependTest">Prepend Test</option>';
501 }).find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
504 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
505 old = jQuery('#sap').html();
507 jQuery('#sap').prepend(function(i, val) {
508 equals( val, old, "Make sure the incoming value is correct." );
509 return document.getElementById('first');
512 equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
515 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
516 old = jQuery('#sap').html();
518 jQuery('#sap').prepend(function(i, val) {
519 equals( val, old, "Make sure the incoming value is correct." );
520 return [document.getElementById('first'), document.getElementById('yahoo')];
523 equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
526 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
527 old = jQuery('#sap').html();
529 jQuery('#sap').prepend(function(i, val) {
530 equals( val, old, "Make sure the incoming value is correct." );
531 return jQuery("#yahoo, #first");
534 equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
537 test("prependTo(String|Element|Array<Element>|jQuery)", function() {
539 var defaultText = 'Try them out:'
540 jQuery('<b>buga</b>').prependTo('#first');
541 equals( jQuery('#first').text(), 'buga' + defaultText, 'Check if text prepending works' );
542 equals( jQuery('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
545 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
546 jQuery(document.getElementById('first')).prependTo('#sap');
547 equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
550 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
551 jQuery([document.getElementById('first'), document.getElementById('yahoo')]).prependTo('#sap');
552 equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
555 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
556 jQuery("#yahoo, #first").prependTo('#sap');
557 equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
560 jQuery('<select id="prependSelect1"></select>').prependTo('form:last');
561 jQuery('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');
563 t( "Prepend Select", "#prependSelect2, #prependSelect1", ["prependSelect2", "prependSelect1"] );
566 var testBefore = function(val) {
568 var expected = 'This is a normal link: bugaYahoo';
569 jQuery('#yahoo').before(val( '<b>buga</b>' ));
570 equals( expected, jQuery('#en').text(), 'Insert String before' );
573 expected = "This is a normal link: Try them out:Yahoo";
574 jQuery('#yahoo').before(val( document.getElementById('first') ));
575 equals( expected, jQuery('#en').text(), "Insert element before" );
578 expected = "This is a normal link: Try them out:diveintomarkYahoo";
579 jQuery('#yahoo').before(val( [document.getElementById('first'), document.getElementById('mark')] ));
580 equals( expected, jQuery('#en').text(), "Insert array of elements before" );
583 expected = "This is a normal link: diveintomarkTry them out:Yahoo";
584 jQuery('#yahoo').before(val( jQuery("#first, #mark") ));
585 equals( expected, jQuery('#en').text(), "Insert jQuery before" );
587 var set = jQuery("<div/>").before("<span>test</span>");
588 equals( set[0].nodeName.toLowerCase(), "span", "Insert the element before the disconnected node." );
589 equals( set.length, 2, "Insert the element before the disconnected node." );
592 test("before(String|Element|Array<Element>|jQuery)", function() {
596 test("before(Function)", function() {
597 testBefore(functionReturningObj);
600 test("insertBefore(String|Element|Array<Element>|jQuery)", function() {
602 var expected = 'This is a normal link: bugaYahoo';
603 jQuery('<b>buga</b>').insertBefore('#yahoo');
604 equals( expected, jQuery('#en').text(), 'Insert String before' );
607 expected = "This is a normal link: Try them out:Yahoo";
608 jQuery(document.getElementById('first')).insertBefore('#yahoo');
609 equals( expected, jQuery('#en').text(), "Insert element before" );
612 expected = "This is a normal link: Try them out:diveintomarkYahoo";
613 jQuery([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
614 equals( expected, jQuery('#en').text(), "Insert array of elements before" );
617 expected = "This is a normal link: diveintomarkTry them out:Yahoo";
618 jQuery("#first, #mark").insertBefore('#yahoo');
619 equals( expected, jQuery('#en').text(), "Insert jQuery before" );
622 var testAfter = function(val) {
624 var expected = 'This is a normal link: Yahoobuga';
625 jQuery('#yahoo').after(val( '<b>buga</b>' ));
626 equals( expected, jQuery('#en').text(), 'Insert String after' );
629 expected = "This is a normal link: YahooTry them out:";
630 jQuery('#yahoo').after(val( document.getElementById('first') ));
631 equals( expected, jQuery('#en').text(), "Insert element after" );
634 expected = "This is a normal link: YahooTry them out:diveintomark";
635 jQuery('#yahoo').after(val( [document.getElementById('first'), document.getElementById('mark')] ));
636 equals( expected, jQuery('#en').text(), "Insert array of elements after" );
639 expected = "This is a normal link: YahoodiveintomarkTry them out:";
640 jQuery('#yahoo').after(val( jQuery("#first, #mark") ));
641 equals( expected, jQuery('#en').text(), "Insert jQuery after" );
643 var set = jQuery("<div/>").after("<span>test</span>");
644 equals( set[1].nodeName.toLowerCase(), "span", "Insert the element after the disconnected node." );
645 equals( set.length, 2, "Insert the element after the disconnected node." );
648 test("after(String|Element|Array<Element>|jQuery)", function() {
652 test("after(Function)", function() {
653 testAfter(functionReturningObj);
656 test("insertAfter(String|Element|Array<Element>|jQuery)", function() {
658 var expected = 'This is a normal link: Yahoobuga';
659 jQuery('<b>buga</b>').insertAfter('#yahoo');
660 equals( expected, jQuery('#en').text(), 'Insert String after' );
663 expected = "This is a normal link: YahooTry them out:";
664 jQuery(document.getElementById('first')).insertAfter('#yahoo');
665 equals( expected, jQuery('#en').text(), "Insert element after" );
668 expected = "This is a normal link: YahooTry them out:diveintomark";
669 jQuery([document.getElementById('first'), document.getElementById('mark')]).insertAfter('#yahoo');
670 equals( expected, jQuery('#en').text(), "Insert array of elements after" );
673 expected = "This is a normal link: YahoodiveintomarkTry them out:";
674 jQuery("#first, #mark").insertAfter('#yahoo');
675 equals( expected, jQuery('#en').text(), "Insert jQuery after" );
678 var testReplaceWith = function(val) {
680 jQuery('#yahoo').replaceWith(val( '<b id="replace">buga</b>' ));
681 ok( jQuery("#replace")[0], 'Replace element with string' );
682 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
685 jQuery('#yahoo').replaceWith(val( document.getElementById('first') ));
686 ok( jQuery("#first")[0], 'Replace element with element' );
687 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
690 jQuery("#main").append('<div id="bar"><div id="baz">Foo</div></div>');
691 jQuery('#baz').replaceWith("Baz");
692 equals( jQuery("#bar").text(),"Baz", 'Replace element with text' );
693 ok( !jQuery("#baz")[0], 'Verify that original element is gone, after element' );
696 jQuery('#yahoo').replaceWith(val( [document.getElementById('first'), document.getElementById('mark')] ));
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('#yahoo').replaceWith(val( jQuery("#first, #mark") ));
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 var tmp = jQuery("<div/>").appendTo("body").click(function(){ ok(true, "Newly bound click run." ); });
709 var y = jQuery('<div/>').appendTo("body").click(function(){ ok(true, "Previously bound click run." ); });
710 var child = y.append("<b>test</b>").find("b").click(function(){ ok(true, "Child bound click run." ); return false; });
712 y.replaceWith( tmp );
715 y.click(); // Shouldn't be run
716 child.click(); // Shouldn't be run
724 y = jQuery('<div/>').appendTo("body").click(function(){ ok(true, "Previously bound click run." ); });
725 var child2 = y.append("<u>test</u>").find("u").click(function(){ ok(true, "Child 2 bound click run." ); return false; });
727 y.replaceWith( child2 );
736 var set = jQuery("<div/>").replaceWith(val("<span>test</span>"));
737 equals( set[0].nodeName.toLowerCase(), "span", "Replace the disconnected node." );
738 equals( set.length, 1, "Replace the disconnected node." );
740 var $div = jQuery("<div class='replacewith'></div>").appendTo("body");
741 // TODO: Work on jQuery(...) inline script execution
742 //$div.replaceWith("<div class='replacewith'></div><script>" +
743 //"equals(jQuery('.replacewith').length, 1, 'Check number of elements in page.');" +
745 equals(jQuery('.replacewith').length, 1, 'Check number of elements in page.');
746 jQuery('.replacewith').remove();
749 test("replaceWith(String|Element|Array<Element>|jQuery)", function() {
750 testReplaceWith(bareObj);
753 test("replaceWith(Function)", function() {
754 testReplaceWith(functionReturningObj);
758 var y = jQuery("#yahoo")[0];
760 jQuery(y).replaceWith(function(){
761 equals( this, y, "Make sure the context is coming in correctly." );
767 test("replaceWith(string) for more than one element", function(){
770 equals(jQuery('#foo p').length, 3, 'ensuring that test data has not changed');
772 jQuery('#foo p').replaceWith('<span>bar</span>');
773 equals(jQuery('#foo span').length, 3, 'verify that all the three original element have been replaced');
774 equals(jQuery('#foo p').length, 0, 'verify that all the three original element have been replaced');
777 test("replaceAll(String|Element|Array<Element>|jQuery)", function() {
779 jQuery('<b id="replace">buga</b>').replaceAll("#yahoo");
780 ok( jQuery("#replace")[0], 'Replace element with string' );
781 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
784 jQuery(document.getElementById('first')).replaceAll("#yahoo");
785 ok( jQuery("#first")[0], 'Replace element with element' );
786 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
789 jQuery([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo");
790 ok( jQuery("#first")[0], 'Replace element with array of elements' );
791 ok( jQuery("#mark")[0], 'Replace element with array of elements' );
792 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
795 jQuery("#first, #mark").replaceAll("#yahoo");
796 ok( jQuery("#first")[0], 'Replace element with set of elements' );
797 ok( jQuery("#mark")[0], 'Replace element with set of elements' );
798 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
801 test("clone()", function() {
803 equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Assert text for #en' );
804 var clone = jQuery('#yahoo').clone();
805 equals( 'Try them out:Yahoo', jQuery('#first').append(clone).text(), 'Check for clone' );
806 equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Reassert text for #en' );
809 "<table/>", "<tr/>", "<td/>", "<div/>",
810 "<button/>", "<ul/>", "<ol/>", "<li/>",
811 "<input type='checkbox' />", "<select/>", "<option/>", "<textarea/>",
812 "<tbody/>", "<thead/>", "<tfoot/>", "<iframe/>"
814 for (var i = 0; i < cloneTags.length; i++) {
815 var j = jQuery(cloneTags[i]);
816 equals( j[0].tagName, j.clone()[0].tagName, 'Clone a <' + cloneTags[i].substring(1));
819 // using contents will get comments regular, text, and comment nodes
820 var cl = jQuery("#nonnodes").contents().clone();
821 ok( cl.length >= 2, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
823 var div = jQuery("<div><ul><li>test</li></ul></div>").click(function(){
824 ok( true, "Bound event still exists." );
827 div = div.clone(true).clone(true);
828 equals( div.length, 1, "One element cloned" );
829 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
830 div.trigger("click");
832 div = jQuery("<div/>").append([ document.createElement("table"), document.createElement("table") ]);
833 div.find("table").click(function(){
834 ok( true, "Bound event still exists." );
837 div = div.clone(true);
838 equals( div.length, 1, "One element cloned" );
839 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
840 div.find("table:last").trigger("click");
842 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>');
844 div = div.clone(true);
845 equals( div.length, 1, "One element cloned" );
846 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
848 div = jQuery("<div/>").data({ a: true, b: true });
849 div = div.clone(true);
850 equals( div.data("a"), true, "Data cloned." );
851 equals( div.data("b"), true, "Data cloned." );
853 var form = document.createElement("form");
854 form.action = "/test/";
855 var div = document.createElement("div");
856 div.appendChild( document.createTextNode("test") );
857 form.appendChild( div );
859 equals( jQuery(form).clone().children().length, 1, "Make sure we just get the form back." );
863 test("clone() on XML nodes", function() {
866 jQuery.get("data/dashboard.xml", function (xml) {
867 var root = jQuery(xml.documentElement).clone();
868 var origTab = jQuery("tab", xml).eq(0);
869 var cloneTab = jQuery("tab", root).eq(0);
870 origTab.text("origval");
871 cloneTab.text("cloneval");
872 equals(origTab.text(), "origval", "Check original XML node was correctly set");
873 equals(cloneTab.text(), "cloneval", "Check cloned XML node was correctly set");
879 var testHtml = function(valueObj) {
882 jQuery.scriptorder = 0;
884 var div = jQuery("#main > div");
885 div.html(valueObj("<b>test</b>"));
887 for ( var i = 0; i < div.size(); i++ ) {
888 if ( div.get(i).childNodes.length != 1 ) pass = false;
890 ok( pass, "Set HTML" );
892 div = jQuery("<div/>").html( valueObj('<div id="parent_1"><div id="child_1"/></div><div id="parent_2"/>') );
894 equals( div.children().length, 2, "Make sure two child nodes exist." );
895 equals( div.children().children().length, 1, "Make sure that a grandchild exists." );
897 var space = jQuery("<div/>").html(valueObj(" "))[0].innerHTML;
898 ok( /^\s$|^ $/.test( space ), "Make sure entities are passed through correctly." );
899 equals( jQuery("<div/>").html(valueObj("&"))[0].innerHTML, "&", "Make sure entities are passed through correctly." );
901 jQuery("#main").html(valueObj("<style>.foobar{color:green;}</style>"));
903 equals( jQuery("#main").children().length, 1, "Make sure there is a child element." );
904 equals( jQuery("#main").children()[0].nodeName.toUpperCase(), "STYLE", "And that a style element was inserted." );
907 // using contents will get comments regular, text, and comment nodes
908 var j = jQuery("#nonnodes").contents();
909 j.html(valueObj("<b>bold</b>"));
911 // this is needed, or the expando added by jQuery unique will yield a different html
912 j.find('b').removeData();
913 equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
915 jQuery("#main").html(valueObj("<select/>"));
916 jQuery("#main select").html(valueObj("<option>O1</option><option selected='selected'>O2</option><option>O3</option>"));
917 equals( jQuery("#main select").val(), "O2", "Selected option correct" );
919 var $div = jQuery('<div />');
920 equals( $div.html(valueObj( 5 )).html(), '5', 'Setting a number as html' );
921 equals( $div.html(valueObj( 0 )).html(), '0', 'Setting a zero as html' );
923 var $div2 = jQuery('<div/>'), insert = "<div>hello1</div>";
924 equals( $div2.html(insert).html().replace(/>/g, ">"), insert, "Verify escaped insertion." );
925 equals( $div2.html("x" + insert).html().replace(/>/g, ">"), "x" + insert, "Verify escaped insertion." );
926 equals( $div2.html(" " + insert).html().replace(/>/g, ">"), " " + insert, "Verify escaped insertion." );
928 var map = jQuery("<map/>").html(valueObj("<area id='map01' shape='rect' coords='50,50,150,150' href='http://www.jquery.com/' alt='jQuery'>"));
930 equals( map[0].childNodes.length, 1, "The area was inserted." );
931 equals( map[0].firstChild.nodeName.toLowerCase(), "area", "The area was inserted." );
935 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>'));
937 jQuery("#main").html(valueObj("<script>ok( true, 'Test repeated injection of script.' );</script>"));
938 jQuery("#main").html(valueObj("<script>ok( true, 'Test repeated injection of script.' );</script>"));
939 jQuery("#main").html(valueObj("<script>ok( true, 'Test repeated injection of script.' );</script>"));
941 jQuery("#main").html(valueObj('<script type="text/javascript">ok( true, "jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975 (1)" );</script>'));
943 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>'));
945 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>"));
948 test("html(String)", function() {
952 test("html(Function)", function() {
953 testHtml(functionReturningObj);
956 test("html(Function) with incoming value", function() {
959 var div = jQuery("#main > div"), old = div.map(function(){ return jQuery(this).html() });
961 div.html(function(i, val) {
962 equals( val, old[i], "Make sure the incoming value is correct." );
963 return "<b>test</b>";
968 if ( this.childNodes.length !== 1 ) {
972 ok( pass, "Set HTML" );
975 // using contents will get comments regular, text, and comment nodes
976 var j = jQuery("#nonnodes").contents();
977 old = j.map(function(){ return jQuery(this).html(); });
979 j.html(function(i, val) {
980 equals( val, old[i], "Make sure the incoming value is correct." );
981 return "<b>bold</b>";
984 j.find('b').removeData();
985 equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
987 var $div = jQuery('<div />');
989 equals( $div.html(function(i, val) {
990 equals( val, "", "Make sure the incoming value is correct." );
992 }).html(), '5', 'Setting a number as html' );
994 equals( $div.html(function(i, val) {
995 equals( val, "5", "Make sure the incoming value is correct." );
997 }).html(), '0', 'Setting a zero as html' );
999 var $div2 = jQuery('<div/>'), insert = "<div>hello1</div>";
1000 equals( $div2.html(function(i, val) {
1001 equals( val, "", "Make sure the incoming value is correct." );
1003 }).html().replace(/>/g, ">"), insert, "Verify escaped insertion." );
1005 equals( $div2.html(function(i, val) {
1006 equals( val.replace(/>/g, ">"), insert, "Make sure the incoming value is correct." );
1007 return "x" + insert;
1008 }).html().replace(/>/g, ">"), "x" + insert, "Verify escaped insertion." );
1010 equals( $div2.html(function(i, val) {
1011 equals( val.replace(/>/g, ">"), "x" + insert, "Make sure the incoming value is correct." );
1012 return " " + insert;
1013 }).html().replace(/>/g, ">"), " " + insert, "Verify escaped insertion." );
1016 var testRemove = function(method) {
1019 var first = jQuery("#ap").children(":first");
1020 first.data("foo", "bar");
1022 jQuery("#ap").children()[method]();
1023 ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
1024 equals( jQuery("#ap").children().length, 0, "Check remove" );
1026 equals( first.data("foo"), method == "remove" ? null : "bar" );
1029 jQuery("#ap").children()[method]("a");
1030 ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
1031 equals( jQuery("#ap").children().length, 1, "Check filtered remove" );
1033 jQuery("#ap").children()[method]("a, code");
1034 equals( jQuery("#ap").children().length, 0, "Check multi-filtered remove" );
1036 // using contents will get comments regular, text, and comment nodes
1037 equals( jQuery("#nonnodes").contents().length, 3, "Check node,textnode,comment remove works" );
1038 jQuery("#nonnodes").contents()[method]();
1039 equals( jQuery("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" );
1044 var first = jQuery("#ap").children(":first");
1045 var cleanUp = first.click(function() { count++ })[method]().appendTo("body").click();
1047 equals( method == "remove" ? 0 : 1, count );
1052 test("remove()", function() {
1053 testRemove("remove");
1056 test("detach()", function() {
1057 testRemove("detach");
1060 test("empty()", function() {
1062 equals( jQuery("#ap").children().empty().text().length, 0, "Check text is removed" );
1063 equals( jQuery("#ap").children().length, 4, "Check elements are not removed" );
1065 // using contents will get comments regular, text, and comment nodes
1066 var j = jQuery("#nonnodes").contents();
1068 equals( j.html(), "", "Check node,textnode,comment empty works" );
1071 test("jQuery.cleanData", function() {
1074 var type, pos, div, child;
1078 // Should trigger 4 remove event
1079 div = getDiv().remove();
1081 // Should both do nothing
1083 div.trigger("click");
1086 div.children().trigger("click");
1090 child = div.children();
1092 // Should trigger 2 remove event
1097 div.trigger("click");
1099 // Should do nothing
1101 child.trigger("click");
1109 child = div.children();
1111 // Should trigger 2 remove event
1112 div.html("<div></div>");
1116 div.trigger("click");
1118 // Should do nothing
1120 child.trigger("click");
1126 var div = jQuery("<div class='outer'><div class='inner'></div></div>").click(function(){
1127 ok( true, type + " " + pos + " Click event fired." );
1128 }).focus(function(){
1129 ok( true, type + " " + pos + " Focus event fired." );
1130 }).find("div").click(function(){
1131 ok( false, type + " " + pos + " Click event fired." );
1132 }).focus(function(){
1133 ok( false, type + " " + pos + " Focus event fired." );
1134 }).end().appendTo("body");
1136 div[0].detachEvent = div[0].removeEventListener = function(t){
1137 ok( true, type + " Outer " + t + " event unbound" );
1140 div[0].firstChild.detachEvent = div[0].firstChild.removeEventListener = function(t){
1141 ok( true, type + " Inner " + t + " event unbound" );