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()" );
26 // Blackberry 4.6 doesn't maintain comments in the DOM
27 equals( jQuery("#nonnodes")[0].childNodes.length < 3 ? 8 : j[2].nodeType, 8, "Check node,textnode,comment with text()" );
30 test("text(String)", function() {
34 test("text(Function)", function() {
35 testText(functionReturningObj);
38 test("text(Function) with incoming value", function() {
41 var old = "This link has class=\"blog\": Simon Willison's Weblog";
43 jQuery('#sap').text(function(i, val) {
44 equals( val, old, "Make sure the incoming value is correct." );
48 equals( jQuery("#sap").text(), "foobar", 'Check for merged text of more then one element.' );
53 var testWrap = function(val) {
55 var defaultText = 'Try them out:'
56 var result = jQuery('#first').wrap(val( '<div class="red"><span></span></div>' )).text();
57 equals( defaultText, result, 'Check for wrapping of on-the-fly html' );
58 ok( jQuery('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
61 var defaultText = 'Try them out:'
62 var result = jQuery('#first').wrap(val( document.getElementById('empty') )).parent();
63 ok( result.is('ol'), 'Check for element wrapping' );
64 equals( result.text(), defaultText, 'Check for element wrapping' );
67 jQuery('#check1').click(function() {
69 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
70 jQuery(checkbox).wrap(val( '<div id="c1" style="display:none;"></div>' ));
71 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
74 // using contents will get comments regular, text, and comment nodes
75 var j = jQuery("#nonnodes").contents();
76 j.wrap(val( "<i></i>" ));
78 // Blackberry 4.6 doesn't maintain comments in the DOM
79 equals( jQuery("#nonnodes > i").length, jQuery("#nonnodes")[0].childNodes.length, "Check node,textnode,comment wraps ok" );
80 equals( jQuery("#nonnodes > i").text(), j.text(), "Check node,textnode,comment wraps doesn't hurt text" );
82 // Try wrapping a disconnected node
83 j = jQuery("<label/>").wrap(val( "<li/>" ));
84 equals( j[0].nodeName.toUpperCase(), "LABEL", "Element is a label" );
85 equals( j[0].parentNode.nodeName.toUpperCase(), "LI", "Element has been wrapped" );
87 // Wrap an element containing a text node
88 j = jQuery("<span/>").wrap("<div>test</div>");
89 equals( j[0].previousSibling.nodeType, 3, "Make sure the previous node is a text element" );
90 equals( j[0].parentNode.nodeName.toUpperCase(), "DIV", "And that we're in the div element." );
92 // Try to wrap an element with multiple elements (should fail)
93 j = jQuery("<div><span></span></div>").children().wrap("<p></p><div></div>");
94 equals( j[0].parentNode.parentNode.childNodes.length, 1, "There should only be one element wrapping." );
95 equals( j.length, 1, "There should only be one element (no cloning)." );
96 equals( j[0].parentNode.nodeName.toUpperCase(), "P", "The span should be in the paragraph." );
98 // Wrap an element with a jQuery set
99 j = jQuery("<span/>").wrap(jQuery("<div></div>"));
100 equals( j[0].parentNode.nodeName.toLowerCase(), "div", "Wrapping works." );
102 // Wrap an element with a jQuery set and event
103 result = jQuery("<div></div>").click(function(){
104 ok(true, "Event triggered.");
107 j = jQuery("<span/>").wrap(result);
108 equals( j[0].parentNode.nodeName.toLowerCase(), "div", "Wrapping works." );
110 j.parent().trigger("click");
113 test("wrap(String|Element)", function() {
117 test("wrap(Function)", function() {
118 testWrap(functionReturningObj);
121 var testWrapAll = function(val) {
123 var prev = jQuery("#firstp")[0].previousSibling;
124 var p = jQuery("#firstp,#first")[0].parentNode;
126 var result = jQuery('#firstp,#first').wrapAll(val( '<div class="red"><div class="tmp"></div></div>' ));
127 equals( result.parent().length, 1, 'Check for wrapping of on-the-fly html' );
128 ok( jQuery('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
129 ok( jQuery('#firstp').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
130 equals( jQuery("#first").parent().parent()[0].previousSibling, prev, "Correct Previous Sibling" );
131 equals( jQuery("#first").parent().parent()[0].parentNode, p, "Correct Parent" );
134 var prev = jQuery("#firstp")[0].previousSibling;
135 var p = jQuery("#first")[0].parentNode;
136 var result = jQuery('#firstp,#first').wrapAll(val( document.getElementById('empty') ));
137 equals( jQuery("#first").parent()[0], jQuery("#firstp").parent()[0], "Same Parent" );
138 equals( jQuery("#first").parent()[0].previousSibling, prev, "Correct Previous Sibling" );
139 equals( jQuery("#first").parent()[0].parentNode, p, "Correct Parent" );
142 test("wrapAll(String|Element)", function() {
143 testWrapAll(bareObj);
146 var testWrapInner = function(val) {
148 var num = jQuery("#first").children().length;
149 var result = jQuery('#first').wrapInner(val('<div class="red"><div id="tmp"></div></div>'));
150 equals( jQuery("#first").children().length, 1, "Only one child" );
151 ok( jQuery("#first").children().is(".red"), "Verify Right Element" );
152 equals( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" );
155 var num = jQuery("#first").html("foo<div>test</div><div>test2</div>").children().length;
156 var result = jQuery('#first').wrapInner(val('<div class="red"><div id="tmp"></div></div>'));
157 equals( jQuery("#first").children().length, 1, "Only one child" );
158 ok( jQuery("#first").children().is(".red"), "Verify Right Element" );
159 equals( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" );
162 var num = jQuery("#first").children().length;
163 var result = jQuery('#first').wrapInner(val(document.getElementById('empty')));
164 equals( jQuery("#first").children().length, 1, "Only one child" );
165 ok( jQuery("#first").children().is("#empty"), "Verify Right Element" );
166 equals( jQuery("#first").children().children().length, num, "Verify Elements Intact" );
168 var div = jQuery("<div/>");
169 div.wrapInner(val("<span></span>"));
170 equals(div.children().length, 1, "The contents were wrapped.");
171 equals(div.children()[0].nodeName.toLowerCase(), "span", "A span was inserted.");
174 test("wrapInner(String|Element)", function() {
175 testWrapInner(bareObj);
178 test("wrapInner(Function)", function() {
179 testWrapInner(functionReturningObj)
182 test("unwrap()", function() {
185 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>');
187 var abcd = jQuery('#unwrap1 > span, #unwrap2 > span').get(),
188 abcdef = jQuery('#unwrap span').get();
190 equals( jQuery('#unwrap1 span').add('#unwrap2 span:first').unwrap().length, 3, 'make #unwrap1 and #unwrap2 go away' );
191 same( jQuery('#unwrap > span').get(), abcd, 'all four spans should still exist' );
193 same( jQuery('#unwrap3 span').unwrap().get(), jQuery('#unwrap3 > span').get(), 'make all b in #unwrap3 go away' );
195 same( jQuery('#unwrap3 span').unwrap().get(), jQuery('#unwrap > span.unwrap3').get(), 'make #unwrap3 go away' );
197 same( jQuery('#unwrap').children().get(), abcdef, '#unwrap only contains 6 child spans' );
199 same( jQuery('#unwrap > span').unwrap().get(), jQuery('body > span.unwrap').get(), 'make the 6 spans become children of body' );
201 same( jQuery('body > span.unwrap').unwrap().get(), jQuery('body > span.unwrap').get(), 'can\'t unwrap children of body' );
202 same( jQuery('body > span.unwrap').unwrap().get(), abcdef, 'can\'t unwrap children of body' );
204 same( jQuery('body > span.unwrap').get(), abcdef, 'body contains 6 .unwrap child spans' );
206 jQuery('body > span.unwrap').remove();
209 var testAppend = function(valueObj) {
211 var defaultText = 'Try them out:'
212 var result = jQuery('#first').append(valueObj('<b>buga</b>'));
213 equals( result.text(), defaultText + 'buga', 'Check if text appending works' );
214 equals( jQuery('#select3').append(valueObj('<option value="appendTest">Append Test</option>')).find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
217 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
218 jQuery('#sap').append(valueObj(document.getElementById('first')));
219 equals( jQuery('#sap').text(), expected, "Check for appending of element" );
222 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
223 jQuery('#sap').append(valueObj([document.getElementById('first'), document.getElementById('yahoo')]));
224 equals( jQuery('#sap').text(), expected, "Check for appending of array of elements" );
227 expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
228 jQuery('#sap').append(valueObj(jQuery("#yahoo, #first")));
229 equals( jQuery('#sap').text(), expected, "Check for appending of jQuery object" );
232 jQuery("#sap").append(valueObj( 5 ));
233 ok( jQuery("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
236 jQuery("#sap").append(valueObj( " text with spaces " ));
237 ok( jQuery("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );
240 ok( jQuery("#sap").append(valueObj( [] )), "Check for appending an empty array." );
241 ok( jQuery("#sap").append(valueObj( "" )), "Check for appending an empty string." );
242 ok( jQuery("#sap").append(valueObj( document.getElementsByTagName("foo") )), "Check for appending an empty nodelist." );
245 jQuery("form").append(valueObj('<input name="radiotest" type="radio" checked="checked" />'));
246 jQuery("form input[name=radiotest]").each(function(){
247 ok( jQuery(this).is(':checked'), "Append checked radio");
251 jQuery("form").append(valueObj('<input name="radiotest" type="radio" checked = \'checked\' />'));
252 jQuery("form input[name=radiotest]").each(function(){
253 ok( jQuery(this).is(':checked'), "Append alternately formated checked radio");
257 jQuery("form").append(valueObj('<input name="radiotest" type="radio" checked />'));
258 jQuery("form input[name=radiotest]").each(function(){
259 ok( jQuery(this).is(':checked'), "Append HTML5-formated checked radio");
263 jQuery("#sap").append(valueObj( document.getElementById('form') ));
264 equals( jQuery("#sap>form").size(), 1, "Check for appending a form" ); // Bug #910
269 var body = jQuery("#iframe")[0].contentWindow.document.body;
272 jQuery( body ).append(valueObj( "<div>test</div>" ));
276 ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
279 jQuery('<fieldset/>').appendTo('#form').append(valueObj( '<legend id="legend">test</legend>' ));
280 t( 'Append legend', '#legend', ['legend'] );
283 jQuery('#select1').append(valueObj( '<OPTION>Test</OPTION>' ));
284 equals( jQuery('#select1 option:last').text(), "Test", "Appending <OPTION> (all caps)" );
286 jQuery('#table').append(valueObj( '<colgroup></colgroup>' ));
287 ok( jQuery('#table colgroup').length, "Append colgroup" );
289 jQuery('#table colgroup').append(valueObj( '<col/>' ));
290 ok( jQuery('#table colgroup col').length, "Append col" );
293 jQuery('#table').append(valueObj( '<caption></caption>' ));
294 ok( jQuery('#table caption').length, "Append caption" );
298 .append(valueObj( '<select id="appendSelect1"></select>' ))
299 .append(valueObj( '<select id="appendSelect2"><option>Test</option></select>' ));
301 t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );
303 equals( "Two nodes", jQuery('<div />').append("Two", " nodes").text(), "Appending two text nodes (#4011)" );
305 // using contents will get comments regular, text, and comment nodes
306 var j = jQuery("#nonnodes").contents();
307 var d = jQuery("<div/>").appendTo("#nonnodes").append(j);
308 equals( jQuery("#nonnodes").length, 1, "Check node,textnode,comment append moved leaving just the div" );
309 ok( d.contents().length >= 2, "Check node,textnode,comment append works" );
310 d.contents().appendTo("#nonnodes");
312 ok( jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment append cleanup worked" );
315 test("append(String|Element|Array<Element>|jQuery)", function() {
319 test("append(Function)", function() {
320 testAppend(functionReturningObj);
323 test("append(Function) with incoming value", function() {
326 var defaultText = 'Try them out:', old = jQuery("#first").html();
328 var result = jQuery('#first').append(function(i, val){
329 equals( val, old, "Make sure the incoming value is correct." );
330 return '<b>buga</b>';
332 equals( result.text(), defaultText + 'buga', 'Check if text appending works' );
334 var select = jQuery('#select3');
337 equals( select.append(function(i, val){
338 equals( val, old, "Make sure the incoming value is correct." );
339 return '<option value="appendTest">Append Test</option>';
340 }).find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
343 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
344 old = jQuery("#sap").html();
346 jQuery('#sap').append(function(i, val){
347 equals( val, old, "Make sure the incoming value is correct." );
348 return document.getElementById('first');
350 equals( jQuery('#sap').text(), expected, "Check for appending of element" );
353 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
354 old = jQuery("#sap").html();
356 jQuery('#sap').append(function(i, val){
357 equals( val, old, "Make sure the incoming value is correct." );
358 return [document.getElementById('first'), document.getElementById('yahoo')];
360 equals( jQuery('#sap').text(), expected, "Check for appending of array of elements" );
363 expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
364 old = jQuery("#sap").html();
366 jQuery('#sap').append(function(i, val){
367 equals( val, old, "Make sure the incoming value is correct." );
368 return jQuery("#yahoo, #first");
370 equals( jQuery('#sap').text(), expected, "Check for appending of jQuery object" );
373 old = jQuery("#sap").html();
375 jQuery("#sap").append(function(i, val){
376 equals( val, old, "Make sure the incoming value is correct." );
379 ok( jQuery("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
384 test("appendTo(String|Element|Array<Element>|jQuery)", function() {
387 var defaultText = 'Try them out:'
388 jQuery('<b>buga</b>').appendTo('#first');
389 equals( jQuery("#first").text(), defaultText + 'buga', 'Check if text appending works' );
390 equals( jQuery('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
393 var l = jQuery("#first").children().length + 2;
394 jQuery("<strong>test</strong>");
395 jQuery("<strong>test</strong>");
396 jQuery([ jQuery("<strong>test</strong>")[0], jQuery("<strong>test</strong>")[0] ])
398 equals( jQuery("#first").children().length, l, "Make sure the elements were inserted." );
399 equals( jQuery("#first").children().last()[0].nodeName.toLowerCase(), "strong", "Verify the last element." );
402 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
403 jQuery(document.getElementById('first')).appendTo('#sap');
404 equals( jQuery('#sap').text(), expected, "Check for appending of element" );
407 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
408 jQuery([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
409 equals( jQuery('#sap').text(), expected, "Check for appending of array of elements" );
412 ok( jQuery(document.createElement("script")).appendTo("body").length, "Make sure a disconnected script can be appended." );
415 expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
416 jQuery("#yahoo, #first").appendTo('#sap');
417 equals( jQuery('#sap').text(), expected, "Check for appending of jQuery object" );
420 jQuery('#select1').appendTo('#foo');
421 t( 'Append select', '#foo select', ['select1'] );
424 var div = jQuery("<div/>").click(function(){
425 ok(true, "Running a cloned click.");
427 div.appendTo("#main, #moretests");
429 jQuery("#main div:last").click();
430 jQuery("#moretests div:last").click();
433 var div = jQuery("<div/>").appendTo("#main, #moretests");
435 equals( div.length, 2, "appendTo returns the inserted elements" );
437 div.addClass("test");
439 ok( jQuery("#main div:last").hasClass("test"), "appendTo element was modified after the insertion" );
440 ok( jQuery("#moretests div:last").hasClass("test"), "appendTo element was modified after the insertion" );
444 div = jQuery("<div/>");
445 jQuery("<span>a</span><b>b</b>").filter("span").appendTo( div );
447 equals( div.children().length, 1, "Make sure the right number of children were inserted." );
449 div = jQuery("#moretests div");
451 var num = jQuery("#main div").length;
452 div.remove().appendTo("#main");
454 equals( jQuery("#main div").length, num, "Make sure all the removed divs were inserted." );
459 var testPrepend = function(val) {
461 var defaultText = 'Try them out:'
462 var result = jQuery('#first').prepend(val( '<b>buga</b>' ));
463 equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
464 equals( jQuery('#select3').prepend(val( '<option value="prependTest">Prepend Test</option>' )).find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
467 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
468 jQuery('#sap').prepend(val( document.getElementById('first') ));
469 equals( jQuery('#sap').text(), expected, "Check for prepending of element" );
472 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
473 jQuery('#sap').prepend(val( [document.getElementById('first'), document.getElementById('yahoo')] ));
474 equals( jQuery('#sap').text(), expected, "Check for prepending of array of elements" );
477 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
478 jQuery('#sap').prepend(val( jQuery("#yahoo, #first") ));
479 equals( jQuery('#sap').text(), expected, "Check for prepending of jQuery object" );
482 test("prepend(String|Element|Array<Element>|jQuery)", function() {
483 testPrepend(bareObj);
486 test("prepend(Function)", function() {
487 testPrepend(functionReturningObj);
490 test("prepend(Function) with incoming value", function() {
493 var defaultText = 'Try them out:', old = jQuery('#first').html();
494 var result = jQuery('#first').prepend(function(i, val) {
495 equals( val, old, "Make sure the incoming value is correct." );
496 return '<b>buga</b>';
498 equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
500 old = jQuery("#select3").html();
502 equals( jQuery('#select3').prepend(function(i, val) {
503 equals( val, old, "Make sure the incoming value is correct." );
504 return '<option value="prependTest">Prepend Test</option>';
505 }).find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
508 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
509 old = jQuery('#sap').html();
511 jQuery('#sap').prepend(function(i, val) {
512 equals( val, old, "Make sure the incoming value is correct." );
513 return document.getElementById('first');
516 equals( jQuery('#sap').text(), expected, "Check for prepending of element" );
519 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
520 old = jQuery('#sap').html();
522 jQuery('#sap').prepend(function(i, val) {
523 equals( val, old, "Make sure the incoming value is correct." );
524 return [document.getElementById('first'), document.getElementById('yahoo')];
527 equals( jQuery('#sap').text(), expected, "Check for prepending of array of elements" );
530 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
531 old = jQuery('#sap').html();
533 jQuery('#sap').prepend(function(i, val) {
534 equals( val, old, "Make sure the incoming value is correct." );
535 return jQuery("#yahoo, #first");
538 equals( jQuery('#sap').text(), expected, "Check for prepending of jQuery object" );
541 test("prependTo(String|Element|Array<Element>|jQuery)", function() {
543 var defaultText = 'Try them out:'
544 jQuery('<b>buga</b>').prependTo('#first');
545 equals( jQuery('#first').text(), 'buga' + defaultText, 'Check if text prepending works' );
546 equals( jQuery('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
549 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
550 jQuery(document.getElementById('first')).prependTo('#sap');
551 equals( jQuery('#sap').text(), expected, "Check for prepending of element" );
554 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
555 jQuery([document.getElementById('first'), document.getElementById('yahoo')]).prependTo('#sap');
556 equals( jQuery('#sap').text(), expected, "Check for prepending of array of elements" );
559 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
560 jQuery("#yahoo, #first").prependTo('#sap');
561 equals( jQuery('#sap').text(), expected, "Check for prepending of jQuery object" );
564 jQuery('<select id="prependSelect1"></select>').prependTo('form:last');
565 jQuery('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');
567 t( "Prepend Select", "#prependSelect2, #prependSelect1", ["prependSelect2", "prependSelect1"] );
570 var testBefore = function(val) {
572 var expected = 'This is a normal link: bugaYahoo';
573 jQuery('#yahoo').before(val( '<b>buga</b>' ));
574 equals( jQuery('#en').text(), expected, 'Insert String before' );
577 expected = "This is a normal link: Try them out:Yahoo";
578 jQuery('#yahoo').before(val( document.getElementById('first') ));
579 equals( jQuery('#en').text(), expected, "Insert element before" );
582 expected = "This is a normal link: Try them out:diveintomarkYahoo";
583 jQuery('#yahoo').before(val( [document.getElementById('first'), document.getElementById('mark')] ));
584 equals( jQuery('#en').text(), expected, "Insert array of elements before" );
587 expected = "This is a normal link: diveintomarkTry them out:Yahoo";
588 jQuery('#yahoo').before(val( jQuery("#mark, #first") ));
589 equals( jQuery('#en').text(), expected, "Insert jQuery before" );
591 var set = jQuery("<div/>").before("<span>test</span>");
592 equals( set[0].nodeName.toLowerCase(), "span", "Insert the element before the disconnected node." );
593 equals( set.length, 2, "Insert the element before the disconnected node." );
596 test("before(String|Element|Array<Element>|jQuery)", function() {
600 test("before(Function)", function() {
601 testBefore(functionReturningObj);
604 test("insertBefore(String|Element|Array<Element>|jQuery)", function() {
606 var expected = 'This is a normal link: bugaYahoo';
607 jQuery('<b>buga</b>').insertBefore('#yahoo');
608 equals( jQuery('#en').text(), expected, 'Insert String before' );
611 expected = "This is a normal link: Try them out:Yahoo";
612 jQuery(document.getElementById('first')).insertBefore('#yahoo');
613 equals( jQuery('#en').text(), expected, "Insert element before" );
616 expected = "This is a normal link: Try them out:diveintomarkYahoo";
617 jQuery([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
618 equals( jQuery('#en').text(), expected, "Insert array of elements before" );
621 expected = "This is a normal link: diveintomarkTry them out:Yahoo";
622 jQuery("#mark, #first").insertBefore('#yahoo');
623 equals( jQuery('#en').text(), expected, "Insert jQuery before" );
626 var testAfter = function(val) {
628 var expected = 'This is a normal link: Yahoobuga';
629 jQuery('#yahoo').after(val( '<b>buga</b>' ));
630 equals( jQuery('#en').text(), expected, 'Insert String after' );
633 expected = "This is a normal link: YahooTry them out:";
634 jQuery('#yahoo').after(val( document.getElementById('first') ));
635 equals( jQuery('#en').text(), expected, "Insert element after" );
638 expected = "This is a normal link: YahooTry them out:diveintomark";
639 jQuery('#yahoo').after(val( [document.getElementById('first'), document.getElementById('mark')] ));
640 equals( jQuery('#en').text(), expected, "Insert array of elements after" );
643 expected = "This is a normal link: YahoodiveintomarkTry them out:";
644 jQuery('#yahoo').after(val( jQuery("#mark, #first") ));
645 equals( jQuery('#en').text(), expected, "Insert jQuery after" );
647 var set = jQuery("<div/>").after("<span>test</span>");
648 equals( set[1].nodeName.toLowerCase(), "span", "Insert the element after the disconnected node." );
649 equals( set.length, 2, "Insert the element after the disconnected node." );
652 test("after(String|Element|Array<Element>|jQuery)", function() {
656 test("after(Function)", function() {
657 testAfter(functionReturningObj);
660 test("insertAfter(String|Element|Array<Element>|jQuery)", function() {
662 var expected = 'This is a normal link: Yahoobuga';
663 jQuery('<b>buga</b>').insertAfter('#yahoo');
664 equals( jQuery('#en').text(), expected, 'Insert String after' );
667 expected = "This is a normal link: YahooTry them out:";
668 jQuery(document.getElementById('first')).insertAfter('#yahoo');
669 equals( jQuery('#en').text(), expected, "Insert element after" );
672 expected = "This is a normal link: YahooTry them out:diveintomark";
673 jQuery([document.getElementById('first'), document.getElementById('mark')]).insertAfter('#yahoo');
674 equals( jQuery('#en').text(), expected, "Insert array of elements after" );
677 expected = "This is a normal link: YahoodiveintomarkTry them out:";
678 jQuery("#mark, #first").insertAfter('#yahoo');
679 equals( jQuery('#en').text(), expected, "Insert jQuery after" );
682 var testReplaceWith = function(val) {
684 jQuery('#yahoo').replaceWith(val( '<b id="replace">buga</b>' ));
685 ok( jQuery("#replace")[0], 'Replace element with string' );
686 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
689 jQuery('#yahoo').replaceWith(val( document.getElementById('first') ));
690 ok( jQuery("#first")[0], 'Replace element with element' );
691 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
694 jQuery("#main").append('<div id="bar"><div id="baz">Foo</div></div>');
695 jQuery('#baz').replaceWith("Baz");
696 equals( jQuery("#bar").text(),"Baz", 'Replace element with text' );
697 ok( !jQuery("#baz")[0], 'Verify that original element is gone, after element' );
700 jQuery('#yahoo').replaceWith(val( [document.getElementById('first'), document.getElementById('mark')] ));
701 ok( jQuery("#first")[0], 'Replace element with array of elements' );
702 ok( jQuery("#mark")[0], 'Replace element with array of elements' );
703 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
706 jQuery('#yahoo').replaceWith(val( jQuery("#mark, #first") ));
707 ok( jQuery("#first")[0], 'Replace element with set of elements' );
708 ok( jQuery("#mark")[0], 'Replace element with set of elements' );
709 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
712 var tmp = jQuery("<div/>").appendTo("body").click(function(){ ok(true, "Newly bound click run." ); });
713 var y = jQuery('<div/>').appendTo("body").click(function(){ ok(true, "Previously bound click run." ); });
714 var child = y.append("<b>test</b>").find("b").click(function(){ ok(true, "Child bound click run." ); return false; });
716 y.replaceWith( tmp );
719 y.click(); // Shouldn't be run
720 child.click(); // Shouldn't be run
728 y = jQuery('<div/>').appendTo("body").click(function(){ ok(true, "Previously bound click run." ); });
729 var child2 = y.append("<u>test</u>").find("u").click(function(){ ok(true, "Child 2 bound click run." ); return false; });
731 y.replaceWith( child2 );
740 var set = jQuery("<div/>").replaceWith(val("<span>test</span>"));
741 equals( set[0].nodeName.toLowerCase(), "span", "Replace the disconnected node." );
742 equals( set.length, 1, "Replace the disconnected node." );
744 var $div = jQuery("<div class='replacewith'></div>").appendTo("body");
745 // TODO: Work on jQuery(...) inline script execution
746 //$div.replaceWith("<div class='replacewith'></div><script>" +
747 //"equals(jQuery('.replacewith').length, 1, 'Check number of elements in page.');" +
749 equals(jQuery('.replacewith').length, 1, 'Check number of elements in page.');
750 jQuery('.replacewith').remove();
753 test("replaceWith(String|Element|Array<Element>|jQuery)", function() {
754 testReplaceWith(bareObj);
757 test("replaceWith(Function)", function() {
758 testReplaceWith(functionReturningObj);
762 var y = jQuery("#yahoo")[0];
764 jQuery(y).replaceWith(function(){
765 equals( this, y, "Make sure the context is coming in correctly." );
771 test("replaceWith(string) for more than one element", function(){
774 equals(jQuery('#foo p').length, 3, 'ensuring that test data has not changed');
776 jQuery('#foo p').replaceWith('<span>bar</span>');
777 equals(jQuery('#foo span').length, 3, 'verify that all the three original element have been replaced');
778 equals(jQuery('#foo p').length, 0, 'verify that all the three original element have been replaced');
781 test("replaceAll(String|Element|Array<Element>|jQuery)", function() {
783 jQuery('<b id="replace">buga</b>').replaceAll("#yahoo");
784 ok( jQuery("#replace")[0], 'Replace element with string' );
785 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
788 jQuery(document.getElementById('first')).replaceAll("#yahoo");
789 ok( jQuery("#first")[0], 'Replace element with element' );
790 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
793 jQuery([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo");
794 ok( jQuery("#first")[0], 'Replace element with array of elements' );
795 ok( jQuery("#mark")[0], 'Replace element with array of elements' );
796 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
799 jQuery("#mark, #first").replaceAll("#yahoo");
800 ok( jQuery("#first")[0], 'Replace element with set of elements' );
801 ok( jQuery("#mark")[0], 'Replace element with set of elements' );
802 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
805 test("clone()", function() {
807 equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Assert text for #en' );
808 var clone = jQuery('#yahoo').clone();
809 equals( 'Try them out:Yahoo', jQuery('#first').append(clone).text(), 'Check for clone' );
810 equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Reassert text for #en' );
813 "<table/>", "<tr/>", "<td/>", "<div/>",
814 "<button/>", "<ul/>", "<ol/>", "<li/>",
815 "<input type='checkbox' />", "<select/>", "<option/>", "<textarea/>",
816 "<tbody/>", "<thead/>", "<tfoot/>", "<iframe/>"
818 for (var i = 0; i < cloneTags.length; i++) {
819 var j = jQuery(cloneTags[i]);
820 equals( j[0].tagName, j.clone()[0].tagName, 'Clone a <' + cloneTags[i].substring(1));
823 // using contents will get comments regular, text, and comment nodes
824 var cl = jQuery("#nonnodes").contents().clone();
825 ok( cl.length >= 2, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
827 var div = jQuery("<div><ul><li>test</li></ul></div>").click(function(){
828 ok( true, "Bound event still exists." );
831 div = div.clone(true).clone(true);
832 equals( div.length, 1, "One element cloned" );
833 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
834 div.trigger("click");
836 div = jQuery("<div/>").append([ document.createElement("table"), document.createElement("table") ]);
837 div.find("table").click(function(){
838 ok( true, "Bound event still exists." );
841 div = div.clone(true);
842 equals( div.length, 1, "One element cloned" );
843 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
844 div.find("table:last").trigger("click");
846 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>');
848 div = div.clone(true);
849 equals( div.length, 1, "One element cloned" );
850 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
852 div = jQuery("<div/>").data({ a: true, b: true });
853 div = div.clone(true);
854 equals( div.data("a"), true, "Data cloned." );
855 equals( div.data("b"), true, "Data cloned." );
857 var form = document.createElement("form");
858 form.action = "/test/";
859 var div = document.createElement("div");
860 div.appendChild( document.createTextNode("test") );
861 form.appendChild( div );
863 equals( jQuery(form).clone().children().length, 1, "Make sure we just get the form back." );
867 test("clone() on XML nodes", function() {
870 jQuery.get("data/dashboard.xml", function (xml) {
871 var root = jQuery(xml.documentElement).clone();
872 var origTab = jQuery("tab", xml).eq(0);
873 var cloneTab = jQuery("tab", root).eq(0);
874 origTab.text("origval");
875 cloneTab.text("cloneval");
876 equals(origTab.text(), "origval", "Check original XML node was correctly set");
877 equals(cloneTab.text(), "cloneval", "Check cloned XML node was correctly set");
883 var testHtml = function(valueObj) {
886 jQuery.scriptorder = 0;
888 var div = jQuery("#main > div");
889 div.html(valueObj("<b>test</b>"));
891 for ( var i = 0; i < div.size(); i++ ) {
892 if ( div.get(i).childNodes.length != 1 ) pass = false;
894 ok( pass, "Set HTML" );
896 div = jQuery("<div/>").html( valueObj('<div id="parent_1"><div id="child_1"/></div><div id="parent_2"/>') );
898 equals( div.children().length, 2, "Make sure two child nodes exist." );
899 equals( div.children().children().length, 1, "Make sure that a grandchild exists." );
901 var space = jQuery("<div/>").html(valueObj(" "))[0].innerHTML;
902 ok( /^\s$|^ $/.test( space ), "Make sure entities are passed through correctly." );
903 equals( jQuery("<div/>").html(valueObj("&"))[0].innerHTML, "&", "Make sure entities are passed through correctly." );
905 jQuery("#main").html(valueObj("<style>.foobar{color:green;}</style>"));
907 equals( jQuery("#main").children().length, 1, "Make sure there is a child element." );
908 equals( jQuery("#main").children()[0].nodeName.toUpperCase(), "STYLE", "And that a style element was inserted." );
911 // using contents will get comments regular, text, and comment nodes
912 var j = jQuery("#nonnodes").contents();
913 j.html(valueObj("<b>bold</b>"));
915 // this is needed, or the expando added by jQuery unique will yield a different html
916 j.find('b').removeData();
917 equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
919 jQuery("#main").html(valueObj("<select/>"));
920 jQuery("#main select").html(valueObj("<option>O1</option><option selected='selected'>O2</option><option>O3</option>"));
921 equals( jQuery("#main select").val(), "O2", "Selected option correct" );
923 var $div = jQuery('<div />');
924 equals( $div.html(valueObj( 5 )).html(), '5', 'Setting a number as html' );
925 equals( $div.html(valueObj( 0 )).html(), '0', 'Setting a zero as html' );
927 var $div2 = jQuery('<div/>'), insert = "<div>hello1</div>";
928 equals( $div2.html(insert).html().replace(/>/g, ">"), insert, "Verify escaped insertion." );
929 equals( $div2.html("x" + insert).html().replace(/>/g, ">"), "x" + insert, "Verify escaped insertion." );
930 equals( $div2.html(" " + insert).html().replace(/>/g, ">"), " " + insert, "Verify escaped insertion." );
932 var map = jQuery("<map/>").html(valueObj("<area id='map01' shape='rect' coords='50,50,150,150' href='http://www.jquery.com/' alt='jQuery'>"));
934 equals( map[0].childNodes.length, 1, "The area was inserted." );
935 equals( map[0].firstChild.nodeName.toLowerCase(), "area", "The area was inserted." );
939 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>'));
941 jQuery("#main").html(valueObj("<script>ok( true, 'Test repeated injection of script.' );</script>"));
942 jQuery("#main").html(valueObj("<script>ok( true, 'Test repeated injection of script.' );</script>"));
943 jQuery("#main").html(valueObj("<script>ok( true, 'Test repeated injection of script.' );</script>"));
945 jQuery("#main").html(valueObj('<script type="text/javascript">ok( true, "jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975 (1)" );</script>'));
947 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>'));
949 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>"));
952 test("html(String)", function() {
956 test("html(Function)", function() {
957 testHtml(functionReturningObj);
960 test("html(Function) with incoming value", function() {
963 var div = jQuery("#main > div"), old = div.map(function(){ return jQuery(this).html() });
965 div.html(function(i, val) {
966 equals( val, old[i], "Make sure the incoming value is correct." );
967 return "<b>test</b>";
972 if ( this.childNodes.length !== 1 ) {
976 ok( pass, "Set HTML" );
979 // using contents will get comments regular, text, and comment nodes
980 var j = jQuery("#nonnodes").contents();
981 old = j.map(function(){ return jQuery(this).html(); });
983 j.html(function(i, val) {
984 equals( val, old[i], "Make sure the incoming value is correct." );
985 return "<b>bold</b>";
988 // Handle the case where no comment is in the document
989 if ( j.length === 2 ) {
990 equals( null, null, "Make sure the incoming value is correct." );
993 j.find('b').removeData();
994 equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
996 var $div = jQuery('<div />');
998 equals( $div.html(function(i, val) {
999 equals( val, "", "Make sure the incoming value is correct." );
1001 }).html(), '5', 'Setting a number as html' );
1003 equals( $div.html(function(i, val) {
1004 equals( val, "5", "Make sure the incoming value is correct." );
1006 }).html(), '0', 'Setting a zero as html' );
1008 var $div2 = jQuery('<div/>'), insert = "<div>hello1</div>";
1009 equals( $div2.html(function(i, val) {
1010 equals( val, "", "Make sure the incoming value is correct." );
1012 }).html().replace(/>/g, ">"), insert, "Verify escaped insertion." );
1014 equals( $div2.html(function(i, val) {
1015 equals( val.replace(/>/g, ">"), insert, "Make sure the incoming value is correct." );
1016 return "x" + insert;
1017 }).html().replace(/>/g, ">"), "x" + insert, "Verify escaped insertion." );
1019 equals( $div2.html(function(i, val) {
1020 equals( val.replace(/>/g, ">"), "x" + insert, "Make sure the incoming value is correct." );
1021 return " " + insert;
1022 }).html().replace(/>/g, ">"), " " + insert, "Verify escaped insertion." );
1025 var testRemove = function(method) {
1028 var first = jQuery("#ap").children(":first");
1029 first.data("foo", "bar");
1031 jQuery("#ap").children()[method]();
1032 ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
1033 equals( jQuery("#ap").children().length, 0, "Check remove" );
1035 equals( first.data("foo"), method == "remove" ? null : "bar" );
1038 jQuery("#ap").children()[method]("a");
1039 ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
1040 equals( jQuery("#ap").children().length, 1, "Check filtered remove" );
1042 jQuery("#ap").children()[method]("a, code");
1043 equals( jQuery("#ap").children().length, 0, "Check multi-filtered remove" );
1045 // using contents will get comments regular, text, and comment nodes
1046 // Handle the case where no comment is in the document
1047 ok( jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment remove works" );
1048 jQuery("#nonnodes").contents()[method]();
1049 equals( jQuery("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" );
1054 var first = jQuery("#ap").children(":first");
1055 var cleanUp = first.click(function() { count++ })[method]().appendTo("body").click();
1057 equals( method == "remove" ? 0 : 1, count );
1062 test("remove()", function() {
1063 testRemove("remove");
1066 test("detach()", function() {
1067 testRemove("detach");
1070 test("empty()", function() {
1072 equals( jQuery("#ap").children().empty().text().length, 0, "Check text is removed" );
1073 equals( jQuery("#ap").children().length, 4, "Check elements are not removed" );
1075 // using contents will get comments regular, text, and comment nodes
1076 var j = jQuery("#nonnodes").contents();
1078 equals( j.html(), "", "Check node,textnode,comment empty works" );
1081 test("jQuery.cleanData", function() {
1084 var type, pos, div, child;
1088 // Should trigger 4 remove event
1089 div = getDiv().remove();
1091 // Should both do nothing
1093 div.trigger("click");
1096 div.children().trigger("click");
1100 child = div.children();
1102 // Should trigger 2 remove event
1107 div.trigger("click");
1109 // Should do nothing
1111 child.trigger("click");
1119 child = div.children();
1121 // Should trigger 2 remove event
1122 div.html("<div></div>");
1126 div.trigger("click");
1128 // Should do nothing
1130 child.trigger("click");
1136 var div = jQuery("<div class='outer'><div class='inner'></div></div>").click(function(){
1137 ok( true, type + " " + pos + " Click event fired." );
1138 }).focus(function(){
1139 ok( true, type + " " + pos + " Focus event fired." );
1140 }).find("div").click(function(){
1141 ok( false, type + " " + pos + " Click event fired." );
1142 }).focus(function(){
1143 ok( false, type + " " + pos + " Focus event fired." );
1144 }).end().appendTo("body");
1146 div[0].detachEvent = div[0].removeEventListener = function(t){
1147 ok( true, type + " Outer " + t + " event unbound" );
1150 div[0].firstChild.detachEvent = div[0].firstChild.removeEventListener = function(t){
1151 ok( true, type + " Inner " + t + " event unbound" );