1 module("manipulation");
3 var bareObj = function(value) { return value; };
4 var functionReturningObj = function(value) { return (function() { return value; }); };
6 test("text()", function() {
8 var expected = "This link has class=\"blog\": Simon Willison's Weblog";
9 equals( jQuery('#sap').text(), expected, 'Check for merged text of more then one element.' );
11 // Check serialization of text values
12 equals( jQuery(document.createTextNode("foo")).text(), "foo", "Text node was retreived from .text()." );
15 var testText = function(valueObj) {
17 var val = valueObj("<div><b>Hello</b> cruel world!</div>");
18 equals( jQuery("#foo").text(val)[0].innerHTML.replace(/>/g, ">"), "<div><b>Hello</b> cruel world!</div>", "Check escaped text" );
20 // using contents will get comments regular, text, and comment nodes
21 var j = jQuery("#nonnodes").contents();
22 j.text(valueObj("hi!"));
23 equals( jQuery(j[0]).text(), "hi!", "Check node,textnode,comment with text()" );
24 equals( j[1].nodeValue, " there ", "Check node,textnode,comment with text()" );
25 equals( j[2].nodeType, 8, "Check node,textnode,comment with text()" );
28 test("text(String)", function() {
32 test("text(Function)", function() {
33 testText(functionReturningObj);
36 test("text(Function) with incoming value", function() {
39 var old = "This link has class=\"blog\": Simon Willison's Weblog";
41 jQuery('#sap').text(function(i, val) {
42 equals( val, old, "Make sure the incoming value is correct." );
46 equals( jQuery("#sap").text(), "foobar", 'Check for merged text of more then one element.' );
51 var testWrap = function(val) {
53 var defaultText = 'Try them out:'
54 var result = jQuery('#first').wrap(val( '<div class="red"><span></span></div>' )).text();
55 equals( defaultText, result, 'Check for wrapping of on-the-fly html' );
56 ok( jQuery('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
59 var defaultText = 'Try them out:'
60 var result = jQuery('#first').wrap(val( document.getElementById('empty') )).parent();
61 ok( result.is('ol'), 'Check for element wrapping' );
62 equals( result.text(), defaultText, 'Check for element wrapping' );
65 jQuery('#check1').click(function() {
67 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
68 jQuery(checkbox).wrap(val( '<div id="c1" style="display:none;"></div>' ));
69 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
72 // using contents will get comments regular, text, and comment nodes
73 var j = jQuery("#nonnodes").contents();
74 j.wrap(val( "<i></i>" ));
75 equals( jQuery("#nonnodes > i").length, 3, "Check node,textnode,comment wraps ok" );
76 equals( jQuery("#nonnodes > i").text(), j.text(), "Check node,textnode,comment wraps doesn't hurt text" );
78 // Try wrapping a disconnected node
79 j = jQuery("<label/>").wrap(val( "<li/>" ));
80 equals( j[0].nodeName.toUpperCase(), "LABEL", "Element is a label" );
81 equals( j[0].parentNode.nodeName.toUpperCase(), "LI", "Element has been wrapped" );
83 // Wrap an element containing a text node
84 j = jQuery("<span/>").wrap("<div>test</div>");
85 equals( j[0].previousSibling.nodeType, 3, "Make sure the previous node is a text element" );
86 equals( j[0].parentNode.nodeName.toUpperCase(), "DIV", "And that we're in the div element." );
88 // Try to wrap an element with multiple elements (should fail)
89 j = jQuery("<div><span></span></div>").children().wrap("<p></p><div></div>");
90 equals( j[0].parentNode.parentNode.childNodes.length, 1, "There should only be one element wrapping." );
91 equals( j.length, 1, "There should only be one element (no cloning)." );
92 equals( j[0].parentNode.nodeName.toUpperCase(), "P", "The span should be in the paragraph." );
94 // Wrap an element with a jQuery set
95 j = jQuery("<span/>").wrap(jQuery("<div></div>"));
96 equals( j[0].parentNode.nodeName.toLowerCase(), "div", "Wrapping works." );
98 // Wrap an element with a jQuery set and event
99 result = jQuery("<div></div>").click(function(){
100 ok(true, "Event triggered.");
103 j = jQuery("<span/>").wrap(result);
104 equals( j[0].parentNode.nodeName.toLowerCase(), "div", "Wrapping works." );
106 j.parent().trigger("click");
109 test("wrap(String|Element)", function() {
113 test("wrap(Function)", function() {
114 testWrap(functionReturningObj);
117 var testWrapAll = function(val) {
119 var prev = jQuery("#firstp")[0].previousSibling;
120 var p = jQuery("#firstp,#first")[0].parentNode;
122 var result = jQuery('#firstp,#first').wrapAll(val( '<div class="red"><div class="tmp"></div></div>' ));
123 equals( result.parent().length, 1, 'Check for wrapping of on-the-fly html' );
124 ok( jQuery('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
125 ok( jQuery('#firstp').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
126 equals( jQuery("#first").parent().parent()[0].previousSibling, prev, "Correct Previous Sibling" );
127 equals( jQuery("#first").parent().parent()[0].parentNode, p, "Correct Parent" );
130 var prev = jQuery("#firstp")[0].previousSibling;
131 var p = jQuery("#first")[0].parentNode;
132 var result = jQuery('#firstp,#first').wrapAll(val( document.getElementById('empty') ));
133 equals( jQuery("#first").parent()[0], jQuery("#firstp").parent()[0], "Same Parent" );
134 equals( jQuery("#first").parent()[0].previousSibling, prev, "Correct Previous Sibling" );
135 equals( jQuery("#first").parent()[0].parentNode, p, "Correct Parent" );
138 test("wrapAll(String|Element)", function() {
139 testWrapAll(bareObj);
142 // TODO: Figure out why each(wrapAll) is not equivalent to wrapAll
143 // test("wrapAll(Function)", function() {
144 // testWrapAll(functionReturningObj);
147 var testWrapInner = function(val) {
149 var num = jQuery("#first").children().length;
150 var result = jQuery('#first').wrapInner('<div class="red"><div id="tmp"></div></div>');
151 equals( jQuery("#first").children().length, 1, "Only one child" );
152 ok( jQuery("#first").children().is(".red"), "Verify Right Element" );
153 equals( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" );
156 var num = jQuery("#first").children().length;
157 var result = jQuery('#first').wrapInner(document.getElementById('empty'));
158 equals( jQuery("#first").children().length, 1, "Only one child" );
159 ok( jQuery("#first").children().is("#empty"), "Verify Right Element" );
160 equals( jQuery("#first").children().children().length, num, "Verify Elements Intact" );
163 test("wrapInner(String|Element)", function() {
164 testWrapInner(bareObj);
167 // TODO: wrapInner uses wrapAll -- get wrapAll working with Function
168 // test("wrapInner(Function)", function() {
169 // testWrapInner(functionReturningObj)
172 test("unwrap()", function() {
175 jQuery("body").append(' <div id="unwrap" style="display: none;"> <div id="unwrap1"> <span class="unwrap">a</span> <span class="unwrap">b</span> </div> <div id="unwrap2"> <span class="unwrap">c</span> <span class="unwrap">d</span> </div> <div id="unwrap3"> <b><span class="unwrap unwrap3">e</span></b> <b><span class="unwrap unwrap3">f</span></b> </div> </div>');
177 var abcd = jQuery('#unwrap1 > span, #unwrap2 > span').get(),
178 abcdef = jQuery('#unwrap span').get();
180 equals( jQuery('#unwrap1 span').add('#unwrap2 span:first').unwrap().length, 3, 'make #unwrap1 and #unwrap2 go away' );
181 same( jQuery('#unwrap > span').get(), abcd, 'all four spans should still exist' );
183 same( jQuery('#unwrap3 span').unwrap().get(), jQuery('#unwrap3 > span').get(), 'make all b in #unwrap3 go away' );
185 same( jQuery('#unwrap3 span').unwrap().get(), jQuery('#unwrap > span.unwrap3').get(), 'make #unwrap3 go away' );
187 same( jQuery('#unwrap').children().get(), abcdef, '#unwrap only contains 6 child spans' );
189 same( jQuery('#unwrap > span').unwrap().get(), jQuery('body > span.unwrap').get(), 'make the 6 spans become children of body' );
191 same( jQuery('body > span.unwrap').unwrap().get(), jQuery('body > span.unwrap').get(), 'can\'t unwrap children of body' );
192 same( jQuery('body > span.unwrap').unwrap().get(), abcdef, 'can\'t unwrap children of body' );
194 same( jQuery('body > span.unwrap').get(), abcdef, 'body contains 6 .unwrap child spans' );
196 jQuery('body > span.unwrap').remove();
199 var testAppend = function(valueObj) {
201 var defaultText = 'Try them out:'
202 var result = jQuery('#first').append(valueObj('<b>buga</b>'));
203 equals( result.text(), defaultText + 'buga', 'Check if text appending works' );
204 equals( jQuery('#select3').append(valueObj('<option value="appendTest">Append Test</option>')).find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
207 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
208 jQuery('#sap').append(valueObj(document.getElementById('first')));
209 equals( expected, jQuery('#sap').text(), "Check for appending of element" );
212 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
213 jQuery('#sap').append(valueObj([document.getElementById('first'), document.getElementById('yahoo')]));
214 equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
217 expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
218 jQuery('#sap').append(valueObj(jQuery("#first, #yahoo")));
219 equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
222 jQuery("#sap").append(valueObj( 5 ));
223 ok( jQuery("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
226 jQuery("#sap").append(valueObj( " text with spaces " ));
227 ok( jQuery("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );
230 ok( jQuery("#sap").append(valueObj( [] )), "Check for appending an empty array." );
231 ok( jQuery("#sap").append(valueObj( "" )), "Check for appending an empty string." );
232 ok( jQuery("#sap").append(valueObj( document.getElementsByTagName("foo") )), "Check for appending an empty nodelist." );
235 jQuery("#sap").append(valueObj( document.getElementById('form') ));
236 equals( jQuery("#sap>form").size(), 1, "Check for appending a form" ); // Bug #910
241 jQuery( jQuery("#iframe")[0].contentWindow.document.body ).append(valueObj( "<div>test</div>" ));
246 ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
249 jQuery('<fieldset/>').appendTo('#form').append(valueObj( '<legend id="legend">test</legend>' ));
250 t( 'Append legend', '#legend', ['legend'] );
253 jQuery('#select1').append(valueObj( '<OPTION>Test</OPTION>' ));
254 equals( jQuery('#select1 option:last').text(), "Test", "Appending <OPTION> (all caps)" );
256 jQuery('#table').append(valueObj( '<colgroup></colgroup>' ));
257 ok( jQuery('#table colgroup').length, "Append colgroup" );
259 jQuery('#table colgroup').append(valueObj( '<col/>' ));
260 ok( jQuery('#table colgroup col').length, "Append col" );
263 jQuery('#table').append(valueObj( '<caption></caption>' ));
264 ok( jQuery('#table caption').length, "Append caption" );
268 .append(valueObj( '<select id="appendSelect1"></select>' ))
269 .append(valueObj( '<select id="appendSelect2"><option>Test</option></select>' ));
271 t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );
273 equals( "Two nodes", jQuery('<div />').append("Two", " nodes").text(), "Appending two text nodes (#4011)" );
275 // using contents will get comments regular, text, and comment nodes
276 var j = jQuery("#nonnodes").contents();
277 var d = jQuery("<div/>").appendTo("#nonnodes").append(j);
278 equals( jQuery("#nonnodes").length, 1, "Check node,textnode,comment append moved leaving just the div" );
279 ok( d.contents().length >= 2, "Check node,textnode,comment append works" );
280 d.contents().appendTo("#nonnodes");
282 ok( jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment append cleanup worked" );
285 test("append(String|Element|Array<Element>|jQuery)", function() {
289 test("append(Function)", function() {
290 testAppend(functionReturningObj);
293 test("appendTo(String|Element|Array<Element>|jQuery)", function() {
295 var defaultText = 'Try them out:'
296 jQuery('<b>buga</b>').appendTo('#first');
297 equals( jQuery("#first").text(), defaultText + 'buga', 'Check if text appending works' );
298 equals( jQuery('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
301 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
302 jQuery(document.getElementById('first')).appendTo('#sap');
303 equals( expected, jQuery('#sap').text(), "Check for appending of element" );
306 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
307 jQuery([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
308 equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
311 ok( jQuery(document.createElement("script")).appendTo("body").length, "Make sure a disconnected script can be appended." );
314 expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
315 jQuery("#first, #yahoo").appendTo('#sap');
316 equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
319 jQuery('#select1').appendTo('#foo');
320 t( 'Append select', '#foo select', ['select1'] );
323 var div = jQuery("<div/>").click(function(){
324 ok(true, "Running a cloned click.");
326 div.appendTo("#main, #moretests");
328 jQuery("#main div:last").click();
329 jQuery("#moretests div:last").click();
332 var div = jQuery("<div/>").appendTo("#main, #moretests");
334 equals( div.length, 2, "appendTo returns the inserted elements" );
336 div.addClass("test");
338 ok( jQuery("#main div:last").hasClass("test"), "appendTo element was modified after the insertion" );
339 ok( jQuery("#moretests div:last").hasClass("test"), "appendTo element was modified after the insertion" );
344 var testPrepend = function(val) {
346 var defaultText = 'Try them out:'
347 var result = jQuery('#first').prepend(val( '<b>buga</b>' ));
348 equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
349 equals( jQuery('#select3').prepend(val( '<option value="prependTest">Prepend Test</option>' )).find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
352 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
353 jQuery('#sap').prepend(val( document.getElementById('first') ));
354 equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
357 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
358 jQuery('#sap').prepend(val( [document.getElementById('first'), document.getElementById('yahoo')] ));
359 equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
362 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
363 jQuery('#sap').prepend(val( jQuery("#first, #yahoo") ));
364 equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
367 test("prepend(String|Element|Array<Element>|jQuery)", function() {
368 testPrepend(bareObj);
371 test("prepend(Function)", function() {
372 testPrepend(functionReturningObj);
375 test("prependTo(String|Element|Array<Element>|jQuery)", function() {
377 var defaultText = 'Try them out:'
378 jQuery('<b>buga</b>').prependTo('#first');
379 equals( jQuery('#first').text(), 'buga' + defaultText, 'Check if text prepending works' );
380 equals( jQuery('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
383 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
384 jQuery(document.getElementById('first')).prependTo('#sap');
385 equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
388 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
389 jQuery([document.getElementById('first'), document.getElementById('yahoo')]).prependTo('#sap');
390 equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
393 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
394 jQuery("#first, #yahoo").prependTo('#sap');
395 equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
398 jQuery('<select id="prependSelect1"></select>').prependTo('form:last');
399 jQuery('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');
401 t( "Prepend Select", "#prependSelect2, #prependSelect1", ["prependSelect2", "prependSelect1"] );
404 var testBefore = function(val) {
406 var expected = 'This is a normal link: bugaYahoo';
407 jQuery('#yahoo').before(val( '<b>buga</b>' ));
408 equals( expected, jQuery('#en').text(), 'Insert String before' );
411 expected = "This is a normal link: Try them out:Yahoo";
412 jQuery('#yahoo').before(val( document.getElementById('first') ));
413 equals( expected, jQuery('#en').text(), "Insert element before" );
416 expected = "This is a normal link: Try them out:diveintomarkYahoo";
417 jQuery('#yahoo').before(val( [document.getElementById('first'), document.getElementById('mark')] ));
418 equals( expected, jQuery('#en').text(), "Insert array of elements before" );
421 expected = "This is a normal link: diveintomarkTry them out:Yahoo";
422 jQuery('#yahoo').before(val( jQuery("#first, #mark") ));
423 equals( expected, jQuery('#en').text(), "Insert jQuery before" );
425 var set = jQuery("<div/>").before("<span>test</span>");
426 equals( set[0].nodeName.toLowerCase(), "span", "Insert the element before the disconnected node." );
427 equals( set.length, 2, "Insert the element before the disconnected node." );
430 test("before(String|Element|Array<Element>|jQuery)", function() {
434 test("before(Function)", function() {
435 testBefore(functionReturningObj);
438 test("insertBefore(String|Element|Array<Element>|jQuery)", function() {
440 var expected = 'This is a normal link: bugaYahoo';
441 jQuery('<b>buga</b>').insertBefore('#yahoo');
442 equals( expected, jQuery('#en').text(), 'Insert String before' );
445 expected = "This is a normal link: Try them out:Yahoo";
446 jQuery(document.getElementById('first')).insertBefore('#yahoo');
447 equals( expected, jQuery('#en').text(), "Insert element before" );
450 expected = "This is a normal link: Try them out:diveintomarkYahoo";
451 jQuery([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
452 equals( expected, jQuery('#en').text(), "Insert array of elements before" );
455 expected = "This is a normal link: diveintomarkTry them out:Yahoo";
456 jQuery("#first, #mark").insertBefore('#yahoo');
457 equals( expected, jQuery('#en').text(), "Insert jQuery before" );
460 var testAfter = function(val) {
462 var expected = 'This is a normal link: Yahoobuga';
463 jQuery('#yahoo').after(val( '<b>buga</b>' ));
464 equals( expected, jQuery('#en').text(), 'Insert String after' );
467 expected = "This is a normal link: YahooTry them out:";
468 jQuery('#yahoo').after(val( document.getElementById('first') ));
469 equals( expected, jQuery('#en').text(), "Insert element after" );
472 expected = "This is a normal link: YahooTry them out:diveintomark";
473 jQuery('#yahoo').after(val( [document.getElementById('first'), document.getElementById('mark')] ));
474 equals( expected, jQuery('#en').text(), "Insert array of elements after" );
477 expected = "This is a normal link: YahoodiveintomarkTry them out:";
478 jQuery('#yahoo').after(val( jQuery("#first, #mark") ));
479 equals( expected, jQuery('#en').text(), "Insert jQuery after" );
481 var set = jQuery("<div/>").after("<span>test</span>");
482 equals( set[1].nodeName.toLowerCase(), "span", "Insert the element after the disconnected node." );
483 equals( set.length, 2, "Insert the element after the disconnected node." );
486 test("after(String|Element|Array<Element>|jQuery)", function() {
490 test("after(Function)", function() {
491 testAfter(functionReturningObj);
494 test("insertAfter(String|Element|Array<Element>|jQuery)", function() {
496 var expected = 'This is a normal link: Yahoobuga';
497 jQuery('<b>buga</b>').insertAfter('#yahoo');
498 equals( expected, jQuery('#en').text(), 'Insert String after' );
501 expected = "This is a normal link: YahooTry them out:";
502 jQuery(document.getElementById('first')).insertAfter('#yahoo');
503 equals( expected, jQuery('#en').text(), "Insert element after" );
506 expected = "This is a normal link: YahooTry them out:diveintomark";
507 jQuery([document.getElementById('first'), document.getElementById('mark')]).insertAfter('#yahoo');
508 equals( expected, jQuery('#en').text(), "Insert array of elements after" );
511 expected = "This is a normal link: YahoodiveintomarkTry them out:";
512 jQuery("#first, #mark").insertAfter('#yahoo');
513 equals( expected, jQuery('#en').text(), "Insert jQuery after" );
516 var testReplaceWith = function(val) {
518 jQuery('#yahoo').replaceWith(val( '<b id="replace">buga</b>' ));
519 ok( jQuery("#replace")[0], 'Replace element with string' );
520 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
523 jQuery('#yahoo').replaceWith(val( document.getElementById('first') ));
524 ok( jQuery("#first")[0], 'Replace element with element' );
525 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
528 jQuery('#yahoo').replaceWith(val( [document.getElementById('first'), document.getElementById('mark')] ));
529 ok( jQuery("#first")[0], 'Replace element with array of elements' );
530 ok( jQuery("#mark")[0], 'Replace element with array of elements' );
531 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
534 jQuery('#yahoo').replaceWith(val( jQuery("#first, #mark") ));
535 ok( jQuery("#first")[0], 'Replace element with set of elements' );
536 ok( jQuery("#mark")[0], 'Replace element with set of elements' );
537 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
539 var set = jQuery("<div/>").replaceWith(val("<span>test</span>"));
540 equals( set[0].nodeName.toLowerCase(), "span", "Replace the disconnected node." );
541 equals( set.length, 1, "Replace the disconnected node." );
543 var $div = jQuery("<div class='replacewith'></div>").appendTo("body");
544 $div.replaceWith("<div class='replacewith'></div><script>" +
545 "equals(jQuery('.replacewith').length, 1, 'Check number of elements in page.');" +
547 equals(jQuery('.replacewith').length, 1, 'Check number of elements in page.');
548 jQuery('.replacewith').remove();
551 test("replaceWith(String|Element|Array<Element>|jQuery)", function() {
552 testReplaceWith(bareObj);
555 test("replaceWith(Function)", function() {
556 testReplaceWith(functionReturningObj);
559 test("replaceAll(String|Element|Array<Element>|jQuery)", function() {
561 jQuery('<b id="replace">buga</b>').replaceAll("#yahoo");
562 ok( jQuery("#replace")[0], 'Replace element with string' );
563 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
566 jQuery(document.getElementById('first')).replaceAll("#yahoo");
567 ok( jQuery("#first")[0], 'Replace element with element' );
568 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
571 jQuery([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo");
572 ok( jQuery("#first")[0], 'Replace element with array of elements' );
573 ok( jQuery("#mark")[0], 'Replace element with array of elements' );
574 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
577 jQuery("#first, #mark").replaceAll("#yahoo");
578 ok( jQuery("#first")[0], 'Replace element with set of elements' );
579 ok( jQuery("#mark")[0], 'Replace element with set of elements' );
580 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
583 test("clone()", function() {
585 equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Assert text for #en' );
586 var clone = jQuery('#yahoo').clone();
587 equals( 'Try them out:Yahoo', jQuery('#first').append(clone).text(), 'Check for clone' );
588 equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Reassert text for #en' );
591 "<table/>", "<tr/>", "<td/>", "<div/>",
592 "<button/>", "<ul/>", "<ol/>", "<li/>",
593 "<input type='checkbox' />", "<select/>", "<option/>", "<textarea/>",
594 "<tbody/>", "<thead/>", "<tfoot/>", "<iframe/>"
596 for (var i = 0; i < cloneTags.length; i++) {
597 var j = jQuery(cloneTags[i]);
598 equals( j[0].tagName, j.clone()[0].tagName, 'Clone a <' + cloneTags[i].substring(1));
601 // using contents will get comments regular, text, and comment nodes
602 var cl = jQuery("#nonnodes").contents().clone();
603 ok( cl.length >= 2, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
605 var div = jQuery("<div><ul><li>test</li></ul></div>").click(function(){
606 ok( true, "Bound event still exists." );
609 div = div.clone(true).clone(true);
610 equals( div.length, 1, "One element cloned" );
611 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
612 div.trigger("click");
614 div = jQuery("<div/>").append([ document.createElement("table"), document.createElement("table") ]);
615 div.find("table").click(function(){
616 ok( true, "Bound event still exists." );
619 div = div.clone(true);
620 equals( div.length, 1, "One element cloned" );
621 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
622 div.find("table:last").trigger("click");
624 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>');
626 div = div.clone(true);
627 equals( div.length, 1, "One element cloned" );
628 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
630 div = jQuery("<div/>").data({ a: true, b: true });
631 div = div.clone(true);
632 equals( div.data("a"), true, "Data cloned." );
633 equals( div.data("b"), true, "Data cloned." );
637 test("clone() on XML nodes", function() {
640 jQuery.get("data/dashboard.xml", function (xml) {
641 var root = jQuery(xml.documentElement).clone();
642 var origTab = jQuery("tab", xml).eq(0);
643 var cloneTab = jQuery("tab", root).eq(0);
644 origTab.text("origval");
645 cloneTab.text("cloneval");
646 equals(origTab.text(), "origval", "Check original XML node was correctly set");
647 equals(cloneTab.text(), "cloneval", "Check cloned XML node was correctly set");
653 var testHtml = function(valueObj) {
656 jQuery.scriptorder = 0;
658 var div = jQuery("#main > div");
659 div.html(valueObj("<b>test</b>"));
661 for ( var i = 0; i < div.size(); i++ ) {
662 if ( div.get(i).childNodes.length != 1 ) pass = false;
664 ok( pass, "Set HTML" );
667 // using contents will get comments regular, text, and comment nodes
668 var j = jQuery("#nonnodes").contents();
669 j.html(valueObj("<b>bold</b>"));
671 // this is needed, or the expando added by jQuery unique will yield a different html
672 j.find('b').removeData();
673 equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
675 jQuery("#main").html(valueObj("<select/>"));
676 jQuery("#main select").html(valueObj("<option>O1</option><option selected='selected'>O2</option><option>O3</option>"));
677 equals( jQuery("#main select").val(), "O2", "Selected option correct" );
679 var $div = jQuery('<div />');
680 equals( $div.html(valueObj( 5 )).html(), '5', 'Setting a number as html' );
681 equals( $div.html(valueObj( 0 )).html(), '0', 'Setting a zero as html' );
683 var $div2 = jQuery('<div/>'), insert = "<div>hello1</div>";
684 equals( $div2.html(insert).html(), insert, "Verify escaped insertion." );
685 equals( $div2.html("x" + insert).html(), "x" + insert, "Verify escaped insertion." );
686 equals( $div2.html(" " + insert).html(), " " + insert, "Verify escaped insertion." );
688 var map = jQuery("<map/>").html(valueObj("<area id='map01' shape='rect' coords='50,50,150,150' href='http://www.jquery.com/' alt='jQuery'>"));
690 equals( map[0].childNodes.length, 1, "The area was inserted." );
691 equals( map[0].firstChild.nodeName.toLowerCase(), "area", "The area was inserted." );
695 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>'));
699 jQuery("#main").html(valueObj('<script type="text/javascript">ok( true, "jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975 (1)" );</script>'));
701 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>'));
703 // it was decided that waiting to execute ALL scripts makes sense since nested ones have to wait anyway so this test case is changed, see #1959
704 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>"));
706 setTimeout( start, 100 );
709 test("html(String)", function() {
713 test("html(Function)", function() {
714 testHtml(functionReturningObj);
717 var testRemove = function(method) {
720 var first = jQuery("#ap").children(":first");
721 first.data("foo", "bar");
723 jQuery("#ap").children()[method]();
724 ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
725 equals( jQuery("#ap").children().length, 0, "Check remove" );
727 equals( first.data("foo"), method == "remove" ? null : "bar" );
730 jQuery("#ap").children()[method]("a");
731 ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
732 equals( jQuery("#ap").children().length, 1, "Check filtered remove" );
734 jQuery("#ap").children()[method]("a, code");
735 equals( jQuery("#ap").children().length, 0, "Check multi-filtered remove" );
737 // using contents will get comments regular, text, and comment nodes
738 equals( jQuery("#nonnodes").contents().length, 3, "Check node,textnode,comment remove works" );
739 jQuery("#nonnodes").contents()[method]();
740 equals( jQuery("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" );
745 var first = jQuery("#ap").children(":first");
746 var cleanUp = first.click(function() { count++ })[method]().appendTo("body").click();
748 equals( method == "remove" ? 0 : 1, count );
753 test("remove()", function() {
754 testRemove("remove");
757 test("detach()", function() {
758 testRemove("detach");
761 test("empty()", function() {
763 equals( jQuery("#ap").children().empty().text().length, 0, "Check text is removed" );
764 equals( jQuery("#ap").children().length, 4, "Check elements are not removed" );
766 // using contents will get comments regular, text, and comment nodes
767 var j = jQuery("#nonnodes").contents();
769 equals( j.html(), "", "Check node,textnode,comment empty works" );