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("#first, #yahoo")));
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 jQuery( jQuery("#iframe")[0].contentWindow.document.body ).append(valueObj( "<div>test</div>" ));
270 ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
273 jQuery('<fieldset/>').appendTo('#form').append(valueObj( '<legend id="legend">test</legend>' ));
274 t( 'Append legend', '#legend', ['legend'] );
277 jQuery('#select1').append(valueObj( '<OPTION>Test</OPTION>' ));
278 equals( jQuery('#select1 option:last').text(), "Test", "Appending <OPTION> (all caps)" );
280 jQuery('#table').append(valueObj( '<colgroup></colgroup>' ));
281 ok( jQuery('#table colgroup').length, "Append colgroup" );
283 jQuery('#table colgroup').append(valueObj( '<col/>' ));
284 ok( jQuery('#table colgroup col').length, "Append col" );
287 jQuery('#table').append(valueObj( '<caption></caption>' ));
288 ok( jQuery('#table caption').length, "Append caption" );
292 .append(valueObj( '<select id="appendSelect1"></select>' ))
293 .append(valueObj( '<select id="appendSelect2"><option>Test</option></select>' ));
295 t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );
297 equals( "Two nodes", jQuery('<div />').append("Two", " nodes").text(), "Appending two text nodes (#4011)" );
299 // using contents will get comments regular, text, and comment nodes
300 var j = jQuery("#nonnodes").contents();
301 var d = jQuery("<div/>").appendTo("#nonnodes").append(j);
302 equals( jQuery("#nonnodes").length, 1, "Check node,textnode,comment append moved leaving just the div" );
303 ok( d.contents().length >= 2, "Check node,textnode,comment append works" );
304 d.contents().appendTo("#nonnodes");
306 ok( jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment append cleanup worked" );
309 test("append(String|Element|Array<Element>|jQuery)", function() {
313 test("append(Function)", function() {
314 testAppend(functionReturningObj);
317 test("append(Function) with incoming value", function() {
320 var defaultText = 'Try them out:', old = jQuery("#first").html();
322 var result = jQuery('#first').append(function(i, val){
323 equals( val, old, "Make sure the incoming value is correct." );
324 return '<b>buga</b>';
326 equals( result.text(), defaultText + 'buga', 'Check if text appending works' );
328 var select = jQuery('#select3');
331 equals( select.append(function(i, val){
332 equals( val, old, "Make sure the incoming value is correct." );
333 return '<option value="appendTest">Append Test</option>';
334 }).find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
337 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
338 old = jQuery("#sap").html();
340 jQuery('#sap').append(function(i, val){
341 equals( val, old, "Make sure the incoming value is correct." );
342 return document.getElementById('first');
344 equals( expected, jQuery('#sap').text(), "Check for appending of element" );
347 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
348 old = jQuery("#sap").html();
350 jQuery('#sap').append(function(i, val){
351 equals( val, old, "Make sure the incoming value is correct." );
352 return [document.getElementById('first'), document.getElementById('yahoo')];
354 equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
357 expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
358 old = jQuery("#sap").html();
360 jQuery('#sap').append(function(i, val){
361 equals( val, old, "Make sure the incoming value is correct." );
362 return jQuery("#first, #yahoo");
364 equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
367 old = jQuery("#sap").html();
369 jQuery("#sap").append(function(i, val){
370 equals( val, old, "Make sure the incoming value is correct." );
373 ok( jQuery("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
378 test("appendTo(String|Element|Array<Element>|jQuery)", function() {
381 var defaultText = 'Try them out:'
382 jQuery('<b>buga</b>').appendTo('#first');
383 equals( jQuery("#first").text(), defaultText + 'buga', 'Check if text appending works' );
384 equals( jQuery('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
387 var l = jQuery("#first").children().length + 3;
388 jQuery("body").map(function(){
389 return jQuery("<strong></strong><b></b><b></b>").get();
391 equals( jQuery("#first").children().length, l, "Make sure the elements were inserted." );
392 equals( jQuery("#first").children().last()[0].nodeName.toLowerCase(), "b", "Verify the last element." );
395 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
396 jQuery(document.getElementById('first')).appendTo('#sap');
397 equals( expected, jQuery('#sap').text(), "Check for appending of element" );
400 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
401 jQuery([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
402 equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
405 ok( jQuery(document.createElement("script")).appendTo("body").length, "Make sure a disconnected script can be appended." );
408 expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
409 jQuery("#first, #yahoo").appendTo('#sap');
410 equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
413 jQuery('#select1').appendTo('#foo');
414 t( 'Append select', '#foo select', ['select1'] );
417 var div = jQuery("<div/>").click(function(){
418 ok(true, "Running a cloned click.");
420 div.appendTo("#main, #moretests");
422 jQuery("#main div:last").click();
423 jQuery("#moretests div:last").click();
426 var div = jQuery("<div/>").appendTo("#main, #moretests");
428 equals( div.length, 2, "appendTo returns the inserted elements" );
430 div.addClass("test");
432 ok( jQuery("#main div:last").hasClass("test"), "appendTo element was modified after the insertion" );
433 ok( jQuery("#moretests div:last").hasClass("test"), "appendTo element was modified after the insertion" );
437 div = jQuery("<div/>");
438 jQuery("<span>a</span><b>b</b>").filter("span").appendTo( div );
440 equals( div.children().length, 1, "Make sure the right number of children were inserted." );
442 div = jQuery("#moretests div");
444 var num = jQuery("#main div").length;
445 div.remove().appendTo("#main");
447 equals( jQuery("#main div").length, num, "Make sure all the removed divs were inserted." );
452 var testPrepend = function(val) {
454 var defaultText = 'Try them out:'
455 var result = jQuery('#first').prepend(val( '<b>buga</b>' ));
456 equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
457 equals( jQuery('#select3').prepend(val( '<option value="prependTest">Prepend Test</option>' )).find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
460 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
461 jQuery('#sap').prepend(val( 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 jQuery('#sap').prepend(val( [document.getElementById('first'), document.getElementById('yahoo')] ));
467 equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
470 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
471 jQuery('#sap').prepend(val( jQuery("#first, #yahoo") ));
472 equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
475 test("prepend(String|Element|Array<Element>|jQuery)", function() {
476 testPrepend(bareObj);
479 test("prepend(Function)", function() {
480 testPrepend(functionReturningObj);
483 test("prepend(Function) with incoming value", function() {
486 var defaultText = 'Try them out:', old = jQuery('#first').html();
487 var result = jQuery('#first').prepend(function(i, val) {
488 equals( val, old, "Make sure the incoming value is correct." );
489 return '<b>buga</b>';
491 equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
493 old = jQuery("#select3").html();
495 equals( jQuery('#select3').prepend(function(i, val) {
496 equals( val, old, "Make sure the incoming value is correct." );
497 return '<option value="prependTest">Prepend Test</option>';
498 }).find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
501 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
502 old = jQuery('#sap').html();
504 jQuery('#sap').prepend(function(i, val) {
505 equals( val, old, "Make sure the incoming value is correct." );
506 return document.getElementById('first');
509 equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
512 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
513 old = jQuery('#sap').html();
515 jQuery('#sap').prepend(function(i, val) {
516 equals( val, old, "Make sure the incoming value is correct." );
517 return [document.getElementById('first'), document.getElementById('yahoo')];
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 old = jQuery('#sap').html();
526 jQuery('#sap').prepend(function(i, val) {
527 equals( val, old, "Make sure the incoming value is correct." );
528 return jQuery("#first, #yahoo");
531 equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
534 test("prependTo(String|Element|Array<Element>|jQuery)", function() {
536 var defaultText = 'Try them out:'
537 jQuery('<b>buga</b>').prependTo('#first');
538 equals( jQuery('#first').text(), 'buga' + defaultText, 'Check if text prepending works' );
539 equals( jQuery('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
542 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
543 jQuery(document.getElementById('first')).prependTo('#sap');
544 equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
547 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
548 jQuery([document.getElementById('first'), document.getElementById('yahoo')]).prependTo('#sap');
549 equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
552 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
553 jQuery("#first, #yahoo").prependTo('#sap');
554 equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
557 jQuery('<select id="prependSelect1"></select>').prependTo('form:last');
558 jQuery('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');
560 t( "Prepend Select", "#prependSelect2, #prependSelect1", ["prependSelect2", "prependSelect1"] );
563 var testBefore = function(val) {
565 var expected = 'This is a normal link: bugaYahoo';
566 jQuery('#yahoo').before(val( '<b>buga</b>' ));
567 equals( expected, jQuery('#en').text(), 'Insert String before' );
570 expected = "This is a normal link: Try them out:Yahoo";
571 jQuery('#yahoo').before(val( document.getElementById('first') ));
572 equals( expected, jQuery('#en').text(), "Insert element before" );
575 expected = "This is a normal link: Try them out:diveintomarkYahoo";
576 jQuery('#yahoo').before(val( [document.getElementById('first'), document.getElementById('mark')] ));
577 equals( expected, jQuery('#en').text(), "Insert array of elements before" );
580 expected = "This is a normal link: diveintomarkTry them out:Yahoo";
581 jQuery('#yahoo').before(val( jQuery("#first, #mark") ));
582 equals( expected, jQuery('#en').text(), "Insert jQuery before" );
584 var set = jQuery("<div/>").before("<span>test</span>");
585 equals( set[0].nodeName.toLowerCase(), "span", "Insert the element before the disconnected node." );
586 equals( set.length, 2, "Insert the element before the disconnected node." );
589 test("before(String|Element|Array<Element>|jQuery)", function() {
593 test("before(Function)", function() {
594 testBefore(functionReturningObj);
597 test("insertBefore(String|Element|Array<Element>|jQuery)", function() {
599 var expected = 'This is a normal link: bugaYahoo';
600 jQuery('<b>buga</b>').insertBefore('#yahoo');
601 equals( expected, jQuery('#en').text(), 'Insert String before' );
604 expected = "This is a normal link: Try them out:Yahoo";
605 jQuery(document.getElementById('first')).insertBefore('#yahoo');
606 equals( expected, jQuery('#en').text(), "Insert element before" );
609 expected = "This is a normal link: Try them out:diveintomarkYahoo";
610 jQuery([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
611 equals( expected, jQuery('#en').text(), "Insert array of elements before" );
614 expected = "This is a normal link: diveintomarkTry them out:Yahoo";
615 jQuery("#first, #mark").insertBefore('#yahoo');
616 equals( expected, jQuery('#en').text(), "Insert jQuery before" );
619 var testAfter = function(val) {
621 var expected = 'This is a normal link: Yahoobuga';
622 jQuery('#yahoo').after(val( '<b>buga</b>' ));
623 equals( expected, jQuery('#en').text(), 'Insert String after' );
626 expected = "This is a normal link: YahooTry them out:";
627 jQuery('#yahoo').after(val( document.getElementById('first') ));
628 equals( expected, jQuery('#en').text(), "Insert element after" );
631 expected = "This is a normal link: YahooTry them out:diveintomark";
632 jQuery('#yahoo').after(val( [document.getElementById('first'), document.getElementById('mark')] ));
633 equals( expected, jQuery('#en').text(), "Insert array of elements after" );
636 expected = "This is a normal link: YahoodiveintomarkTry them out:";
637 jQuery('#yahoo').after(val( jQuery("#first, #mark") ));
638 equals( expected, jQuery('#en').text(), "Insert jQuery after" );
640 var set = jQuery("<div/>").after("<span>test</span>");
641 equals( set[1].nodeName.toLowerCase(), "span", "Insert the element after the disconnected node." );
642 equals( set.length, 2, "Insert the element after the disconnected node." );
645 test("after(String|Element|Array<Element>|jQuery)", function() {
649 test("after(Function)", function() {
650 testAfter(functionReturningObj);
653 test("insertAfter(String|Element|Array<Element>|jQuery)", function() {
655 var expected = 'This is a normal link: Yahoobuga';
656 jQuery('<b>buga</b>').insertAfter('#yahoo');
657 equals( expected, jQuery('#en').text(), 'Insert String after' );
660 expected = "This is a normal link: YahooTry them out:";
661 jQuery(document.getElementById('first')).insertAfter('#yahoo');
662 equals( expected, jQuery('#en').text(), "Insert element after" );
665 expected = "This is a normal link: YahooTry them out:diveintomark";
666 jQuery([document.getElementById('first'), document.getElementById('mark')]).insertAfter('#yahoo');
667 equals( expected, jQuery('#en').text(), "Insert array of elements after" );
670 expected = "This is a normal link: YahoodiveintomarkTry them out:";
671 jQuery("#first, #mark").insertAfter('#yahoo');
672 equals( expected, jQuery('#en').text(), "Insert jQuery after" );
675 var testReplaceWith = function(val) {
677 jQuery('#yahoo').replaceWith(val( '<b id="replace">buga</b>' ));
678 ok( jQuery("#replace")[0], 'Replace element with string' );
679 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
682 jQuery('#yahoo').replaceWith(val( document.getElementById('first') ));
683 ok( jQuery("#first")[0], 'Replace element with element' );
684 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
687 jQuery("#main").append('<div id="bar"><div id="baz">Foo</div></div>');
688 jQuery('#baz').replaceWith("Baz");
689 equals( jQuery("#bar").text(),"Baz", 'Replace element with text' );
690 ok( !jQuery("#baz")[0], 'Verify that original element is gone, after element' );
693 jQuery('#yahoo').replaceWith(val( [document.getElementById('first'), document.getElementById('mark')] ));
694 ok( jQuery("#first")[0], 'Replace element with array of elements' );
695 ok( jQuery("#mark")[0], 'Replace element with array of elements' );
696 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
699 jQuery('#yahoo').replaceWith(val( jQuery("#first, #mark") ));
700 ok( jQuery("#first")[0], 'Replace element with set of elements' );
701 ok( jQuery("#mark")[0], 'Replace element with set of elements' );
702 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
705 var tmp = jQuery("<div/>").appendTo("body").click(function(){ ok(true, "Newly bound click run." ); });
706 var y = jQuery('<div/>').appendTo("body").click(function(){ ok(true, "Previously bound click run." ); });
707 var child = y.append("<b>test</b>").find("b").click(function(){ ok(true, "Child bound click run." ); return false; });
709 y.replaceWith( tmp );
712 y.click(); // Shouldn't be run
713 child.click(); // Shouldn't be run
721 y = jQuery('<div/>').appendTo("body").click(function(){ ok(true, "Previously bound click run." ); });
722 var child2 = y.append("<u>test</u>").find("u").click(function(){ ok(true, "Child 2 bound click run." ); return false; });
724 y.replaceWith( child2 );
733 var set = jQuery("<div/>").replaceWith(val("<span>test</span>"));
734 equals( set[0].nodeName.toLowerCase(), "span", "Replace the disconnected node." );
735 equals( set.length, 1, "Replace the disconnected node." );
737 var $div = jQuery("<div class='replacewith'></div>").appendTo("body");
738 // TODO: Work on jQuery(...) inline script execution
739 //$div.replaceWith("<div class='replacewith'></div><script>" +
740 //"equals(jQuery('.replacewith').length, 1, 'Check number of elements in page.');" +
742 equals(jQuery('.replacewith').length, 1, 'Check number of elements in page.');
743 jQuery('.replacewith').remove();
746 test("replaceWith(String|Element|Array<Element>|jQuery)", function() {
747 testReplaceWith(bareObj);
750 test("replaceWith(Function)", function() {
751 testReplaceWith(functionReturningObj);
755 var y = jQuery("#yahoo")[0];
757 jQuery(y).replaceWith(function(){
758 equals( this, y, "Make sure the context is coming in correctly." );
764 test("replaceWith(string) for more than one element", function(){
767 equals(jQuery('#foo p').length, 3, 'ensuring that test data has not changed');
769 jQuery('#foo p').replaceWith('<span>bar</span>');
770 equals(jQuery('#foo span').length, 3, 'verify that all the three original element have been replaced');
771 equals(jQuery('#foo p').length, 0, 'verify that all the three original element have been replaced');
774 test("replaceAll(String|Element|Array<Element>|jQuery)", function() {
776 jQuery('<b id="replace">buga</b>').replaceAll("#yahoo");
777 ok( jQuery("#replace")[0], 'Replace element with string' );
778 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
781 jQuery(document.getElementById('first')).replaceAll("#yahoo");
782 ok( jQuery("#first")[0], 'Replace element with element' );
783 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
786 jQuery([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo");
787 ok( jQuery("#first")[0], 'Replace element with array of elements' );
788 ok( jQuery("#mark")[0], 'Replace element with array of elements' );
789 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
792 jQuery("#first, #mark").replaceAll("#yahoo");
793 ok( jQuery("#first")[0], 'Replace element with set of elements' );
794 ok( jQuery("#mark")[0], 'Replace element with set of elements' );
795 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
798 test("clone()", function() {
800 equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Assert text for #en' );
801 var clone = jQuery('#yahoo').clone();
802 equals( 'Try them out:Yahoo', jQuery('#first').append(clone).text(), 'Check for clone' );
803 equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Reassert text for #en' );
806 "<table/>", "<tr/>", "<td/>", "<div/>",
807 "<button/>", "<ul/>", "<ol/>", "<li/>",
808 "<input type='checkbox' />", "<select/>", "<option/>", "<textarea/>",
809 "<tbody/>", "<thead/>", "<tfoot/>", "<iframe/>"
811 for (var i = 0; i < cloneTags.length; i++) {
812 var j = jQuery(cloneTags[i]);
813 equals( j[0].tagName, j.clone()[0].tagName, 'Clone a <' + cloneTags[i].substring(1));
816 // using contents will get comments regular, text, and comment nodes
817 var cl = jQuery("#nonnodes").contents().clone();
818 ok( cl.length >= 2, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
820 var div = jQuery("<div><ul><li>test</li></ul></div>").click(function(){
821 ok( true, "Bound event still exists." );
824 div = div.clone(true).clone(true);
825 equals( div.length, 1, "One element cloned" );
826 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
827 div.trigger("click");
829 div = jQuery("<div/>").append([ document.createElement("table"), document.createElement("table") ]);
830 div.find("table").click(function(){
831 ok( true, "Bound event still exists." );
834 div = div.clone(true);
835 equals( div.length, 1, "One element cloned" );
836 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
837 div.find("table:last").trigger("click");
839 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>');
841 div = div.clone(true);
842 equals( div.length, 1, "One element cloned" );
843 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
845 div = jQuery("<div/>").data({ a: true, b: true });
846 div = div.clone(true);
847 equals( div.data("a"), true, "Data cloned." );
848 equals( div.data("b"), true, "Data cloned." );
850 var form = document.createElement("form");
851 form.action = "/test/";
852 var div = document.createElement("div");
853 div.appendChild( document.createTextNode("test") );
854 form.appendChild( div );
856 equals( jQuery(form).clone().children().length, 1, "Make sure we just get the form back." );
860 test("clone() on XML nodes", function() {
863 jQuery.get("data/dashboard.xml", function (xml) {
864 var root = jQuery(xml.documentElement).clone();
865 var origTab = jQuery("tab", xml).eq(0);
866 var cloneTab = jQuery("tab", root).eq(0);
867 origTab.text("origval");
868 cloneTab.text("cloneval");
869 equals(origTab.text(), "origval", "Check original XML node was correctly set");
870 equals(cloneTab.text(), "cloneval", "Check cloned XML node was correctly set");
876 var testHtml = function(valueObj) {
879 jQuery.scriptorder = 0;
881 var div = jQuery("#main > div");
882 div.html(valueObj("<b>test</b>"));
884 for ( var i = 0; i < div.size(); i++ ) {
885 if ( div.get(i).childNodes.length != 1 ) pass = false;
887 ok( pass, "Set HTML" );
889 div = jQuery("<div/>").html( valueObj('<div id="parent_1"><div id="child_1"/></div><div id="parent_2"/>') );
891 equals( div.children().length, 2, "Make sure two child nodes exist." );
892 equals( div.children().children().length, 1, "Make sure that a grandchild exists." );
894 var space = jQuery("<div/>").html(valueObj(" "))[0].innerHTML;
895 ok( /^\s$|^ $/.test( space ), "Make sure entities are passed through correctly." );
896 equals( jQuery("<div/>").html(valueObj("&"))[0].innerHTML, "&", "Make sure entities are passed through correctly." );
898 jQuery("#main").html(valueObj("<style>.foobar{color:green;}</style>"));
900 equals( jQuery("#main").children().length, 1, "Make sure there is a child element." );
901 equals( jQuery("#main").children()[0].nodeName.toUpperCase(), "STYLE", "And that a style element was inserted." );
904 // using contents will get comments regular, text, and comment nodes
905 var j = jQuery("#nonnodes").contents();
906 j.html(valueObj("<b>bold</b>"));
908 // this is needed, or the expando added by jQuery unique will yield a different html
909 j.find('b').removeData();
910 equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
912 jQuery("#main").html(valueObj("<select/>"));
913 jQuery("#main select").html(valueObj("<option>O1</option><option selected='selected'>O2</option><option>O3</option>"));
914 equals( jQuery("#main select").val(), "O2", "Selected option correct" );
916 var $div = jQuery('<div />');
917 equals( $div.html(valueObj( 5 )).html(), '5', 'Setting a number as html' );
918 equals( $div.html(valueObj( 0 )).html(), '0', 'Setting a zero as html' );
920 var $div2 = jQuery('<div/>'), insert = "<div>hello1</div>";
921 equals( $div2.html(insert).html().replace(/>/g, ">"), insert, "Verify escaped insertion." );
922 equals( $div2.html("x" + insert).html().replace(/>/g, ">"), "x" + insert, "Verify escaped insertion." );
923 equals( $div2.html(" " + insert).html().replace(/>/g, ">"), " " + insert, "Verify escaped insertion." );
925 var map = jQuery("<map/>").html(valueObj("<area id='map01' shape='rect' coords='50,50,150,150' href='http://www.jquery.com/' alt='jQuery'>"));
927 equals( map[0].childNodes.length, 1, "The area was inserted." );
928 equals( map[0].firstChild.nodeName.toLowerCase(), "area", "The area was inserted." );
932 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>'));
934 jQuery("#main").html(valueObj("<script>ok( true, 'Test repeated injection of script.' );</script>"));
935 jQuery("#main").html(valueObj("<script>ok( true, 'Test repeated injection of script.' );</script>"));
936 jQuery("#main").html(valueObj("<script>ok( true, 'Test repeated injection of script.' );</script>"));
938 jQuery("#main").html(valueObj('<script type="text/javascript">ok( true, "jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975 (1)" );</script>'));
940 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>'));
942 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>"));
945 test("html(String)", function() {
949 test("html(Function)", function() {
950 testHtml(functionReturningObj);
953 test("html(Function) with incoming value", function() {
956 var div = jQuery("#main > div"), old = div.map(function(){ return jQuery(this).html() });
958 div.html(function(i, val) {
959 equals( val, old[i], "Make sure the incoming value is correct." );
960 return "<b>test</b>";
965 if ( this.childNodes.length !== 1 ) {
969 ok( pass, "Set HTML" );
972 // using contents will get comments regular, text, and comment nodes
973 var j = jQuery("#nonnodes").contents();
974 old = j.map(function(){ return jQuery(this).html(); });
976 j.html(function(i, val) {
977 equals( val, old[i], "Make sure the incoming value is correct." );
978 return "<b>bold</b>";
981 j.find('b').removeData();
982 equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
984 var $div = jQuery('<div />');
986 equals( $div.html(function(i, val) {
987 equals( val, "", "Make sure the incoming value is correct." );
989 }).html(), '5', 'Setting a number as html' );
991 equals( $div.html(function(i, val) {
992 equals( val, "5", "Make sure the incoming value is correct." );
994 }).html(), '0', 'Setting a zero as html' );
996 var $div2 = jQuery('<div/>'), insert = "<div>hello1</div>";
997 equals( $div2.html(function(i, val) {
998 equals( val, "", "Make sure the incoming value is correct." );
1000 }).html().replace(/>/g, ">"), insert, "Verify escaped insertion." );
1002 equals( $div2.html(function(i, val) {
1003 equals( val.replace(/>/g, ">"), insert, "Make sure the incoming value is correct." );
1004 return "x" + insert;
1005 }).html().replace(/>/g, ">"), "x" + insert, "Verify escaped insertion." );
1007 equals( $div2.html(function(i, val) {
1008 equals( val.replace(/>/g, ">"), "x" + insert, "Make sure the incoming value is correct." );
1009 return " " + insert;
1010 }).html().replace(/>/g, ">"), " " + insert, "Verify escaped insertion." );
1013 var testRemove = function(method) {
1016 var first = jQuery("#ap").children(":first");
1017 first.data("foo", "bar");
1019 jQuery("#ap").children()[method]();
1020 ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
1021 equals( jQuery("#ap").children().length, 0, "Check remove" );
1023 equals( first.data("foo"), method == "remove" ? null : "bar" );
1026 jQuery("#ap").children()[method]("a");
1027 ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
1028 equals( jQuery("#ap").children().length, 1, "Check filtered remove" );
1030 jQuery("#ap").children()[method]("a, code");
1031 equals( jQuery("#ap").children().length, 0, "Check multi-filtered remove" );
1033 // using contents will get comments regular, text, and comment nodes
1034 equals( jQuery("#nonnodes").contents().length, 3, "Check node,textnode,comment remove works" );
1035 jQuery("#nonnodes").contents()[method]();
1036 equals( jQuery("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" );
1041 var first = jQuery("#ap").children(":first");
1042 var cleanUp = first.click(function() { count++ })[method]().appendTo("body").click();
1044 equals( method == "remove" ? 0 : 1, count );
1049 test("remove()", function() {
1050 testRemove("remove");
1053 test("detach()", function() {
1054 testRemove("detach");
1057 test("empty()", function() {
1059 equals( jQuery("#ap").children().empty().text().length, 0, "Check text is removed" );
1060 equals( jQuery("#ap").children().length, 4, "Check elements are not removed" );
1062 // using contents will get comments regular, text, and comment nodes
1063 var j = jQuery("#nonnodes").contents();
1065 equals( j.html(), "", "Check node,textnode,comment empty works" );
1068 test("jQuery.cleanData", function() {
1071 var type, pos, div, child;
1075 // Should trigger 4 remove event
1076 div = getDiv().remove();
1078 // Should both do nothing
1080 div.trigger("click");
1083 div.children().trigger("click");
1087 child = div.children();
1089 // Should trigger 2 remove event
1094 div.trigger("click");
1096 // Should do nothing
1098 child.trigger("click");
1106 child = div.children();
1108 // Should trigger 2 remove event
1109 div.html("<div></div>");
1113 div.trigger("click");
1115 // Should do nothing
1117 child.trigger("click");
1123 var div = jQuery("<div class='outer'><div class='inner'></div></div>").click(function(){
1124 ok( true, type + " " + pos + " Click event fired." );
1125 }).focus(function(){
1126 ok( true, type + " " + pos + " Focus event fired." );
1127 }).find("div").click(function(){
1128 ok( false, type + " " + pos + " Click event fired." );
1129 }).focus(function(){
1130 ok( false, type + " " + pos + " Focus event fired." );
1131 }).end().appendTo("body");
1133 div[0].detachEvent = div[0].removeEventListener = function(t){
1134 ok( true, type + " Outer " + t + " event unbound" );
1137 div[0].firstChild.detachEvent = div[0].firstChild.removeEventListener = function(t){
1138 ok( true, type + " Inner " + t + " event unbound" );