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("append the same fragment with events (Bug #6997, 5566)", function () {
388 var element = jQuery("<a class='test6997'></a>").click(function () {
389 ok(true, "Append second element events work");
392 jQuery("#listWithTabIndex li").append(element)
393 .find('a.test6997').eq(1).click();
395 element = jQuery("<li class='test6997'></li>").click(function () {
396 ok(true, "Before second element events work");
400 jQuery("#listWithTabIndex li").before(element);
401 jQuery("#listWithTabIndex li.test6997").eq(1).click();
404 test("appendTo(String|Element|Array<Element>|jQuery)", function() {
407 var defaultText = 'Try them out:'
408 jQuery('<b>buga</b>').appendTo('#first');
409 equals( jQuery("#first").text(), defaultText + 'buga', 'Check if text appending works' );
410 equals( jQuery('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
413 var l = jQuery("#first").children().length + 2;
414 jQuery("<strong>test</strong>");
415 jQuery("<strong>test</strong>");
416 jQuery([ jQuery("<strong>test</strong>")[0], jQuery("<strong>test</strong>")[0] ])
418 equals( jQuery("#first").children().length, l, "Make sure the elements were inserted." );
419 equals( jQuery("#first").children().last()[0].nodeName.toLowerCase(), "strong", "Verify the last element." );
422 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
423 jQuery(document.getElementById('first')).appendTo('#sap');
424 equals( jQuery('#sap').text(), expected, "Check for appending of element" );
427 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
428 jQuery([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
429 equals( jQuery('#sap').text(), expected, "Check for appending of array of elements" );
432 ok( jQuery(document.createElement("script")).appendTo("body").length, "Make sure a disconnected script can be appended." );
435 expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
436 jQuery("#yahoo, #first").appendTo('#sap');
437 equals( jQuery('#sap').text(), expected, "Check for appending of jQuery object" );
440 jQuery('#select1').appendTo('#foo');
441 t( 'Append select', '#foo select', ['select1'] );
444 var div = jQuery("<div/>").click(function(){
445 ok(true, "Running a cloned click.");
447 div.appendTo("#main, #moretests");
449 jQuery("#main div:last").click();
450 jQuery("#moretests div:last").click();
453 var div = jQuery("<div/>").appendTo("#main, #moretests");
455 equals( div.length, 2, "appendTo returns the inserted elements" );
457 div.addClass("test");
459 ok( jQuery("#main div:last").hasClass("test"), "appendTo element was modified after the insertion" );
460 ok( jQuery("#moretests div:last").hasClass("test"), "appendTo element was modified after the insertion" );
464 div = jQuery("<div/>");
465 jQuery("<span>a</span><b>b</b>").filter("span").appendTo( div );
467 equals( div.children().length, 1, "Make sure the right number of children were inserted." );
469 div = jQuery("#moretests div");
471 var num = jQuery("#main div").length;
472 div.remove().appendTo("#main");
474 equals( jQuery("#main div").length, num, "Make sure all the removed divs were inserted." );
479 var testPrepend = function(val) {
481 var defaultText = 'Try them out:'
482 var result = jQuery('#first').prepend(val( '<b>buga</b>' ));
483 equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
484 equals( jQuery('#select3').prepend(val( '<option value="prependTest">Prepend Test</option>' )).find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
487 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
488 jQuery('#sap').prepend(val( document.getElementById('first') ));
489 equals( jQuery('#sap').text(), expected, "Check for prepending of element" );
492 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
493 jQuery('#sap').prepend(val( [document.getElementById('first'), document.getElementById('yahoo')] ));
494 equals( jQuery('#sap').text(), expected, "Check for prepending of array of elements" );
497 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
498 jQuery('#sap').prepend(val( jQuery("#yahoo, #first") ));
499 equals( jQuery('#sap').text(), expected, "Check for prepending of jQuery object" );
502 test("prepend(String|Element|Array<Element>|jQuery)", function() {
503 testPrepend(bareObj);
506 test("prepend(Function)", function() {
507 testPrepend(functionReturningObj);
510 test("prepend(Function) with incoming value", function() {
513 var defaultText = 'Try them out:', old = jQuery('#first').html();
514 var result = jQuery('#first').prepend(function(i, val) {
515 equals( val, old, "Make sure the incoming value is correct." );
516 return '<b>buga</b>';
518 equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
520 old = jQuery("#select3").html();
522 equals( jQuery('#select3').prepend(function(i, val) {
523 equals( val, old, "Make sure the incoming value is correct." );
524 return '<option value="prependTest">Prepend Test</option>';
525 }).find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
528 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
529 old = jQuery('#sap').html();
531 jQuery('#sap').prepend(function(i, val) {
532 equals( val, old, "Make sure the incoming value is correct." );
533 return document.getElementById('first');
536 equals( jQuery('#sap').text(), expected, "Check for prepending of element" );
539 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
540 old = jQuery('#sap').html();
542 jQuery('#sap').prepend(function(i, val) {
543 equals( val, old, "Make sure the incoming value is correct." );
544 return [document.getElementById('first'), document.getElementById('yahoo')];
547 equals( jQuery('#sap').text(), expected, "Check for prepending of array of elements" );
550 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
551 old = jQuery('#sap').html();
553 jQuery('#sap').prepend(function(i, val) {
554 equals( val, old, "Make sure the incoming value is correct." );
555 return jQuery("#yahoo, #first");
558 equals( jQuery('#sap').text(), expected, "Check for prepending of jQuery object" );
561 test("prependTo(String|Element|Array<Element>|jQuery)", function() {
563 var defaultText = 'Try them out:'
564 jQuery('<b>buga</b>').prependTo('#first');
565 equals( jQuery('#first').text(), 'buga' + defaultText, 'Check if text prepending works' );
566 equals( jQuery('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
569 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
570 jQuery(document.getElementById('first')).prependTo('#sap');
571 equals( jQuery('#sap').text(), expected, "Check for prepending of element" );
574 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
575 jQuery([document.getElementById('first'), document.getElementById('yahoo')]).prependTo('#sap');
576 equals( jQuery('#sap').text(), expected, "Check for prepending of array of elements" );
579 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
580 jQuery("#yahoo, #first").prependTo('#sap');
581 equals( jQuery('#sap').text(), expected, "Check for prepending of jQuery object" );
584 jQuery('<select id="prependSelect1"></select>').prependTo('form:last');
585 jQuery('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');
587 t( "Prepend Select", "#prependSelect2, #prependSelect1", ["prependSelect2", "prependSelect1"] );
590 var testBefore = function(val) {
592 var expected = 'This is a normal link: bugaYahoo';
593 jQuery('#yahoo').before(val( '<b>buga</b>' ));
594 equals( jQuery('#en').text(), expected, 'Insert String before' );
597 expected = "This is a normal link: Try them out:Yahoo";
598 jQuery('#yahoo').before(val( document.getElementById('first') ));
599 equals( jQuery('#en').text(), expected, "Insert element before" );
602 expected = "This is a normal link: Try them out:diveintomarkYahoo";
603 jQuery('#yahoo').before(val( [document.getElementById('first'), document.getElementById('mark')] ));
604 equals( jQuery('#en').text(), expected, "Insert array of elements before" );
607 expected = "This is a normal link: diveintomarkTry them out:Yahoo";
608 jQuery('#yahoo').before(val( jQuery("#mark, #first") ));
609 equals( jQuery('#en').text(), expected, "Insert jQuery before" );
611 var set = jQuery("<div/>").before("<span>test</span>");
612 equals( set[0].nodeName.toLowerCase(), "span", "Insert the element before the disconnected node." );
613 equals( set.length, 2, "Insert the element before the disconnected node." );
616 test("before(String|Element|Array<Element>|jQuery)", function() {
620 test("before(Function)", function() {
621 testBefore(functionReturningObj);
624 test("insertBefore(String|Element|Array<Element>|jQuery)", function() {
626 var expected = 'This is a normal link: bugaYahoo';
627 jQuery('<b>buga</b>').insertBefore('#yahoo');
628 equals( jQuery('#en').text(), expected, 'Insert String before' );
631 expected = "This is a normal link: Try them out:Yahoo";
632 jQuery(document.getElementById('first')).insertBefore('#yahoo');
633 equals( jQuery('#en').text(), expected, "Insert element before" );
636 expected = "This is a normal link: Try them out:diveintomarkYahoo";
637 jQuery([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
638 equals( jQuery('#en').text(), expected, "Insert array of elements before" );
641 expected = "This is a normal link: diveintomarkTry them out:Yahoo";
642 jQuery("#mark, #first").insertBefore('#yahoo');
643 equals( jQuery('#en').text(), expected, "Insert jQuery before" );
646 var testAfter = function(val) {
648 var expected = 'This is a normal link: Yahoobuga';
649 jQuery('#yahoo').after(val( '<b>buga</b>' ));
650 equals( jQuery('#en').text(), expected, 'Insert String after' );
653 expected = "This is a normal link: YahooTry them out:";
654 jQuery('#yahoo').after(val( document.getElementById('first') ));
655 equals( jQuery('#en').text(), expected, "Insert element after" );
658 expected = "This is a normal link: YahooTry them out:diveintomark";
659 jQuery('#yahoo').after(val( [document.getElementById('first'), document.getElementById('mark')] ));
660 equals( jQuery('#en').text(), expected, "Insert array of elements after" );
663 expected = "This is a normal link: YahoodiveintomarkTry them out:";
664 jQuery('#yahoo').after(val( jQuery("#mark, #first") ));
665 equals( jQuery('#en').text(), expected, "Insert jQuery after" );
667 var set = jQuery("<div/>").after("<span>test</span>");
668 equals( set[1].nodeName.toLowerCase(), "span", "Insert the element after the disconnected node." );
669 equals( set.length, 2, "Insert the element after the disconnected node." );
672 test("after(String|Element|Array<Element>|jQuery)", function() {
676 test("after(Function)", function() {
677 testAfter(functionReturningObj);
680 test("insertAfter(String|Element|Array<Element>|jQuery)", function() {
682 var expected = 'This is a normal link: Yahoobuga';
683 jQuery('<b>buga</b>').insertAfter('#yahoo');
684 equals( jQuery('#en').text(), expected, 'Insert String after' );
687 expected = "This is a normal link: YahooTry them out:";
688 jQuery(document.getElementById('first')).insertAfter('#yahoo');
689 equals( jQuery('#en').text(), expected, "Insert element after" );
692 expected = "This is a normal link: YahooTry them out:diveintomark";
693 jQuery([document.getElementById('first'), document.getElementById('mark')]).insertAfter('#yahoo');
694 equals( jQuery('#en').text(), expected, "Insert array of elements after" );
697 expected = "This is a normal link: YahoodiveintomarkTry them out:";
698 jQuery("#mark, #first").insertAfter('#yahoo');
699 equals( jQuery('#en').text(), expected, "Insert jQuery after" );
702 var testReplaceWith = function(val) {
704 jQuery('#yahoo').replaceWith(val( '<b id="replace">buga</b>' ));
705 ok( jQuery("#replace")[0], 'Replace element with string' );
706 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
709 jQuery('#yahoo').replaceWith(val( document.getElementById('first') ));
710 ok( jQuery("#first")[0], 'Replace element with element' );
711 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
714 jQuery("#main").append('<div id="bar"><div id="baz">Foo</div></div>');
715 jQuery('#baz').replaceWith("Baz");
716 equals( jQuery("#bar").text(),"Baz", 'Replace element with text' );
717 ok( !jQuery("#baz")[0], 'Verify that original element is gone, after element' );
720 jQuery('#yahoo').replaceWith(val( [document.getElementById('first'), document.getElementById('mark')] ));
721 ok( jQuery("#first")[0], 'Replace element with array of elements' );
722 ok( jQuery("#mark")[0], 'Replace element with array of elements' );
723 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
726 jQuery('#yahoo').replaceWith(val( jQuery("#mark, #first") ));
727 ok( jQuery("#first")[0], 'Replace element with set of elements' );
728 ok( jQuery("#mark")[0], 'Replace element with set of elements' );
729 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
732 var tmp = jQuery("<div/>").appendTo("body").click(function(){ ok(true, "Newly bound click run." ); });
733 var y = jQuery('<div/>').appendTo("body").click(function(){ ok(true, "Previously bound click run." ); });
734 var child = y.append("<b>test</b>").find("b").click(function(){ ok(true, "Child bound click run." ); return false; });
736 y.replaceWith( tmp );
739 y.click(); // Shouldn't be run
740 child.click(); // Shouldn't be run
748 y = jQuery('<div/>').appendTo("body").click(function(){ ok(true, "Previously bound click run." ); });
749 var child2 = y.append("<u>test</u>").find("u").click(function(){ ok(true, "Child 2 bound click run." ); return false; });
751 y.replaceWith( child2 );
760 var set = jQuery("<div/>").replaceWith(val("<span>test</span>"));
761 equals( set[0].nodeName.toLowerCase(), "span", "Replace the disconnected node." );
762 equals( set.length, 1, "Replace the disconnected node." );
764 var $div = jQuery("<div class='replacewith'></div>").appendTo("body");
765 // TODO: Work on jQuery(...) inline script execution
766 //$div.replaceWith("<div class='replacewith'></div><script>" +
767 //"equals(jQuery('.replacewith').length, 1, 'Check number of elements in page.');" +
769 equals(jQuery('.replacewith').length, 1, 'Check number of elements in page.');
770 jQuery('.replacewith').remove();
774 jQuery("#main").append("<div id='replaceWith'></div>");
775 equals( jQuery("#main").find("div[id=replaceWith]").length, 1, "Make sure only one div exists." );
777 jQuery("#replaceWith").replaceWith( val("<div id='replaceWith'></div>") );
778 equals( jQuery("#main").find("div[id=replaceWith]").length, 1, "Make sure only one div exists." );
780 jQuery("#replaceWith").replaceWith( val("<div id='replaceWith'></div>") );
781 equals( jQuery("#main").find("div[id=replaceWith]").length, 1, "Make sure only one div exists." );
784 test("replaceWith(String|Element|Array<Element>|jQuery)", function() {
785 testReplaceWith(bareObj);
788 test("replaceWith(Function)", function() {
789 testReplaceWith(functionReturningObj);
793 var y = jQuery("#yahoo")[0];
795 jQuery(y).replaceWith(function(){
796 equals( this, y, "Make sure the context is coming in correctly." );
802 test("replaceWith(string) for more than one element", function(){
805 equals(jQuery('#foo p').length, 3, 'ensuring that test data has not changed');
807 jQuery('#foo p').replaceWith('<span>bar</span>');
808 equals(jQuery('#foo span').length, 3, 'verify that all the three original element have been replaced');
809 equals(jQuery('#foo p').length, 0, 'verify that all the three original element have been replaced');
812 test("replaceAll(String|Element|Array<Element>|jQuery)", function() {
814 jQuery('<b id="replace">buga</b>').replaceAll("#yahoo");
815 ok( jQuery("#replace")[0], 'Replace element with string' );
816 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
819 jQuery(document.getElementById('first')).replaceAll("#yahoo");
820 ok( jQuery("#first")[0], 'Replace element with element' );
821 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
824 jQuery([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo");
825 ok( jQuery("#first")[0], 'Replace element with array of elements' );
826 ok( jQuery("#mark")[0], 'Replace element with array of elements' );
827 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
830 jQuery("#mark, #first").replaceAll("#yahoo");
831 ok( jQuery("#first")[0], 'Replace element with set of elements' );
832 ok( jQuery("#mark")[0], 'Replace element with set of elements' );
833 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
836 test("clone()", function() {
838 equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Assert text for #en' );
839 var clone = jQuery('#yahoo').clone();
840 equals( 'Try them out:Yahoo', jQuery('#first').append(clone).text(), 'Check for clone' );
841 equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Reassert text for #en' );
844 "<table/>", "<tr/>", "<td/>", "<div/>",
845 "<button/>", "<ul/>", "<ol/>", "<li/>",
846 "<input type='checkbox' />", "<select/>", "<option/>", "<textarea/>",
847 "<tbody/>", "<thead/>", "<tfoot/>", "<iframe/>"
849 for (var i = 0; i < cloneTags.length; i++) {
850 var j = jQuery(cloneTags[i]);
851 equals( j[0].tagName, j.clone()[0].tagName, 'Clone a <' + cloneTags[i].substring(1));
854 // using contents will get comments regular, text, and comment nodes
855 var cl = jQuery("#nonnodes").contents().clone();
856 ok( cl.length >= 2, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
858 var div = jQuery("<div><ul><li>test</li></ul></div>").click(function(){
859 ok( true, "Bound event still exists." );
862 div = div.clone(true).clone(true);
863 equals( div.length, 1, "One element cloned" );
864 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
865 div.trigger("click");
867 div = jQuery("<div/>").append([ document.createElement("table"), document.createElement("table") ]);
868 div.find("table").click(function(){
869 ok( true, "Bound event still exists." );
872 div = div.clone(true);
873 equals( div.length, 1, "One element cloned" );
874 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
875 div.find("table:last").trigger("click");
877 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>');
879 div = div.clone(true);
880 equals( div.length, 1, "One element cloned" );
881 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
883 div = jQuery("<div/>").data({ a: true, b: true });
884 div = div.clone(true);
885 equals( div.data("a"), true, "Data cloned." );
886 equals( div.data("b"), true, "Data cloned." );
888 var form = document.createElement("form");
889 form.action = "/test/";
890 var div = document.createElement("div");
891 div.appendChild( document.createTextNode("test") );
892 form.appendChild( div );
894 equals( jQuery(form).clone().children().length, 1, "Make sure we just get the form back." );
896 equal( jQuery("body").clone().children()[0].id, "qunit-header", "Make sure cloning body works" );
900 test("clone() on XML nodes", function() {
903 jQuery.get("data/dashboard.xml", function (xml) {
904 var root = jQuery(xml.documentElement).clone();
905 var origTab = jQuery("tab", xml).eq(0);
906 var cloneTab = jQuery("tab", root).eq(0);
907 origTab.text("origval");
908 cloneTab.text("cloneval");
909 equals(origTab.text(), "origval", "Check original XML node was correctly set");
910 equals(cloneTab.text(), "cloneval", "Check cloned XML node was correctly set");
916 var testHtml = function(valueObj) {
919 jQuery.scriptorder = 0;
921 var div = jQuery("#main > div");
922 div.html(valueObj("<b>test</b>"));
924 for ( var i = 0; i < div.size(); i++ ) {
925 if ( div.get(i).childNodes.length != 1 ) pass = false;
927 ok( pass, "Set HTML" );
929 div = jQuery("<div/>").html( valueObj('<div id="parent_1"><div id="child_1"/></div><div id="parent_2"/>') );
931 equals( div.children().length, 2, "Make sure two child nodes exist." );
932 equals( div.children().children().length, 1, "Make sure that a grandchild exists." );
934 var space = jQuery("<div/>").html(valueObj(" "))[0].innerHTML;
935 ok( /^\xA0$|^ $/.test( space ), "Make sure entities are passed through correctly." );
936 equals( jQuery("<div/>").html(valueObj("&"))[0].innerHTML, "&", "Make sure entities are passed through correctly." );
938 jQuery("#main").html(valueObj("<style>.foobar{color:green;}</style>"));
940 equals( jQuery("#main").children().length, 1, "Make sure there is a child element." );
941 equals( jQuery("#main").children()[0].nodeName.toUpperCase(), "STYLE", "And that a style element was inserted." );
944 // using contents will get comments regular, text, and comment nodes
945 var j = jQuery("#nonnodes").contents();
946 j.html(valueObj("<b>bold</b>"));
948 // this is needed, or the expando added by jQuery unique will yield a different html
949 j.find('b').removeData();
950 equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
952 jQuery("#main").html(valueObj("<select/>"));
953 jQuery("#main select").html(valueObj("<option>O1</option><option selected='selected'>O2</option><option>O3</option>"));
954 equals( jQuery("#main select").val(), "O2", "Selected option correct" );
956 var $div = jQuery('<div />');
957 equals( $div.html(valueObj( 5 )).html(), '5', 'Setting a number as html' );
958 equals( $div.html(valueObj( 0 )).html(), '0', 'Setting a zero as html' );
960 var $div2 = jQuery('<div/>'), insert = "<div>hello1</div>";
961 equals( $div2.html(insert).html().replace(/>/g, ">"), insert, "Verify escaped insertion." );
962 equals( $div2.html("x" + insert).html().replace(/>/g, ">"), "x" + insert, "Verify escaped insertion." );
963 equals( $div2.html(" " + insert).html().replace(/>/g, ">"), " " + insert, "Verify escaped insertion." );
965 var map = jQuery("<map/>").html(valueObj("<area id='map01' shape='rect' coords='50,50,150,150' href='http://www.jquery.com/' alt='jQuery'>"));
967 equals( map[0].childNodes.length, 1, "The area was inserted." );
968 equals( map[0].firstChild.nodeName.toLowerCase(), "area", "The area was inserted." );
972 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>'));
974 jQuery("#main").html(valueObj("<script>ok( true, 'Test repeated injection of script.' );</script>"));
975 jQuery("#main").html(valueObj("<script>ok( true, 'Test repeated injection of script.' );</script>"));
976 jQuery("#main").html(valueObj("<script>ok( true, 'Test repeated injection of script.' );</script>"));
978 jQuery("#main").html(valueObj('<script type="text/javascript">ok( true, "jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975 (1)" );</script>'));
980 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>'));
982 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>"));
985 test("html(String)", function() {
989 test("html(Function)", function() {
990 testHtml(functionReturningObj);
996 jQuery("#main").html(function(){
997 return jQuery(this).text();
1000 ok( !/</.test( jQuery("#main").html() ), "Replace html with text." );
1001 ok( jQuery("#main").html().length > 0, "Make sure text exists." );
1004 test("html(Function) with incoming value", function() {
1007 var div = jQuery("#main > div"), old = div.map(function(){ return jQuery(this).html() });
1009 div.html(function(i, val) {
1010 equals( val, old[i], "Make sure the incoming value is correct." );
1011 return "<b>test</b>";
1015 div.each(function(){
1016 if ( this.childNodes.length !== 1 ) {
1020 ok( pass, "Set HTML" );
1023 // using contents will get comments regular, text, and comment nodes
1024 var j = jQuery("#nonnodes").contents();
1025 old = j.map(function(){ return jQuery(this).html(); });
1027 j.html(function(i, val) {
1028 equals( val, old[i], "Make sure the incoming value is correct." );
1029 return "<b>bold</b>";
1032 // Handle the case where no comment is in the document
1033 if ( j.length === 2 ) {
1034 equals( null, null, "Make sure the incoming value is correct." );
1037 j.find('b').removeData();
1038 equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
1040 var $div = jQuery('<div />');
1042 equals( $div.html(function(i, val) {
1043 equals( val, "", "Make sure the incoming value is correct." );
1045 }).html(), '5', 'Setting a number as html' );
1047 equals( $div.html(function(i, val) {
1048 equals( val, "5", "Make sure the incoming value is correct." );
1050 }).html(), '0', 'Setting a zero as html' );
1052 var $div2 = jQuery('<div/>'), insert = "<div>hello1</div>";
1053 equals( $div2.html(function(i, val) {
1054 equals( val, "", "Make sure the incoming value is correct." );
1056 }).html().replace(/>/g, ">"), insert, "Verify escaped insertion." );
1058 equals( $div2.html(function(i, val) {
1059 equals( val.replace(/>/g, ">"), insert, "Make sure the incoming value is correct." );
1060 return "x" + insert;
1061 }).html().replace(/>/g, ">"), "x" + insert, "Verify escaped insertion." );
1063 equals( $div2.html(function(i, val) {
1064 equals( val.replace(/>/g, ">"), "x" + insert, "Make sure the incoming value is correct." );
1065 return " " + insert;
1066 }).html().replace(/>/g, ">"), " " + insert, "Verify escaped insertion." );
1069 var testRemove = function(method) {
1072 var first = jQuery("#ap").children(":first");
1073 first.data("foo", "bar");
1075 jQuery("#ap").children()[method]();
1076 ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
1077 equals( jQuery("#ap").children().length, 0, "Check remove" );
1079 equals( first.data("foo"), method == "remove" ? null : "bar" );
1082 jQuery("#ap").children()[method]("a");
1083 ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
1084 equals( jQuery("#ap").children().length, 1, "Check filtered remove" );
1086 jQuery("#ap").children()[method]("a, code");
1087 equals( jQuery("#ap").children().length, 0, "Check multi-filtered remove" );
1089 // using contents will get comments regular, text, and comment nodes
1090 // Handle the case where no comment is in the document
1091 ok( jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment remove works" );
1092 jQuery("#nonnodes").contents()[method]();
1093 equals( jQuery("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" );
1098 var first = jQuery("#ap").children(":first");
1099 var cleanUp = first.click(function() { count++ })[method]().appendTo("body").click();
1101 equals( method == "remove" ? 0 : 1, count );
1106 test("remove()", function() {
1107 testRemove("remove");
1110 test("detach()", function() {
1111 testRemove("detach");
1114 test("empty()", function() {
1116 equals( jQuery("#ap").children().empty().text().length, 0, "Check text is removed" );
1117 equals( jQuery("#ap").children().length, 4, "Check elements are not removed" );
1119 // using contents will get comments regular, text, and comment nodes
1120 var j = jQuery("#nonnodes").contents();
1122 equals( j.html(), "", "Check node,textnode,comment empty works" );
1125 test("jQuery.cleanData", function() {
1128 var type, pos, div, child;
1132 // Should trigger 4 remove event
1133 div = getDiv().remove();
1135 // Should both do nothing
1137 div.trigger("click");
1140 div.children().trigger("click");
1144 child = div.children();
1146 // Should trigger 2 remove event
1151 div.trigger("click");
1153 // Should do nothing
1155 child.trigger("click");
1163 child = div.children();
1165 // Should trigger 2 remove event
1166 div.html("<div></div>");
1170 div.trigger("click");
1172 // Should do nothing
1174 child.trigger("click");
1180 var div = jQuery("<div class='outer'><div class='inner'></div></div>").click(function(){
1181 ok( true, type + " " + pos + " Click event fired." );
1182 }).focus(function(){
1183 ok( true, type + " " + pos + " Focus event fired." );
1184 }).find("div").click(function(){
1185 ok( false, type + " " + pos + " Click event fired." );
1186 }).focus(function(){
1187 ok( false, type + " " + pos + " Focus event fired." );
1188 }).end().appendTo("body");
1190 div[0].detachEvent = div[0].removeEventListener = function(t){
1191 ok( true, type + " Outer " + t + " event unbound" );
1194 div[0].firstChild.detachEvent = div[0].firstChild.removeEventListener = function(t){
1195 ok( true, type + " Inner " + t + " event unbound" );