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 testWrap = function(val) {
17 var defaultText = 'Try them out:'
18 var result = jQuery('#first').wrap(val( '<div class="red"><span></span></div>' )).text();
19 equals( defaultText, result, 'Check for wrapping of on-the-fly html' );
20 ok( jQuery('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
23 var defaultText = 'Try them out:'
24 var result = jQuery('#first').wrap(val( document.getElementById('empty') )).parent();
25 ok( result.is('ol'), 'Check for element wrapping' );
26 equals( result.text(), defaultText, 'Check for element wrapping' );
29 jQuery('#check1').click(function() {
31 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
32 jQuery(checkbox).wrap(val( '<div id="c1" style="display:none;"></div>' ));
33 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
36 // using contents will get comments regular, text, and comment nodes
37 var j = jQuery("#nonnodes").contents();
38 j.wrap(val( "<i></i>" ));
39 equals( jQuery("#nonnodes > i").length, 3, "Check node,textnode,comment wraps ok" );
40 equals( jQuery("#nonnodes > i").text(), j.text(), "Check node,textnode,comment wraps doesn't hurt text" );
42 // Try wrapping a disconnected node
43 j = jQuery("<label/>").wrap(val( "<li/>" ));
44 equals( j[0].nodeName.toUpperCase(), "LABEL", "Element is a label" );
45 equals( j[0].parentNode.nodeName.toUpperCase(), "LI", "Element has been wrapped" );
47 // Wrap an element containing a text node
48 j = jQuery("<span/>").wrap("<div>test</div>");
49 equals( j[0].previousSibling.nodeType, 3, "Make sure the previous node is a text element" );
50 equals( j[0].parentNode.nodeName.toUpperCase(), "DIV", "And that we're in the div element." );
52 // Try to wrap an element with multiple elements (should fail)
53 j = jQuery("<div><span></span></div>").children().wrap("<p></p><div></div>");
54 equals( j[0].parentNode.parentNode.childNodes.length, 1, "There should only be one element wrapping." );
55 equals( j.length, 1, "There should only be one element (no cloning)." );
56 equals( j[0].parentNode.nodeName.toUpperCase(), "P", "The span should be in the paragraph." );
59 test("wrap(String|Element)", function() {
63 test("wrap(Function)", function() {
64 testWrap(functionReturningObj);
67 var testWrapAll = function(val) {
69 var prev = jQuery("#firstp")[0].previousSibling;
70 var p = jQuery("#firstp,#first")[0].parentNode;
72 var result = jQuery('#firstp,#first').wrapAll(val( '<div class="red"><div class="tmp"></div></div>' ));
73 equals( result.parent().length, 1, 'Check for wrapping of on-the-fly html' );
74 ok( jQuery('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
75 ok( jQuery('#firstp').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
76 equals( jQuery("#first").parent().parent()[0].previousSibling, prev, "Correct Previous Sibling" );
77 equals( jQuery("#first").parent().parent()[0].parentNode, p, "Correct Parent" );
80 var prev = jQuery("#firstp")[0].previousSibling;
81 var p = jQuery("#first")[0].parentNode;
82 var result = jQuery('#firstp,#first').wrapAll(val( document.getElementById('empty') ));
83 equals( jQuery("#first").parent()[0], jQuery("#firstp").parent()[0], "Same Parent" );
84 equals( jQuery("#first").parent()[0].previousSibling, prev, "Correct Previous Sibling" );
85 equals( jQuery("#first").parent()[0].parentNode, p, "Correct Parent" );
88 test("wrapAll(String|Element)", function() {
92 // TODO: Figure out why each(wrapAll) is not equivalent to wrapAll
93 // test("wrapAll(Function)", function() {
94 // testWrapAll(functionReturningObj);
97 var testWrapInner = function(val) {
99 var num = jQuery("#first").children().length;
100 var result = jQuery('#first').wrapInner('<div class="red"><div id="tmp"></div></div>');
101 equals( jQuery("#first").children().length, 1, "Only one child" );
102 ok( jQuery("#first").children().is(".red"), "Verify Right Element" );
103 equals( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" );
106 var num = jQuery("#first").children().length;
107 var result = jQuery('#first').wrapInner(document.getElementById('empty'));
108 equals( jQuery("#first").children().length, 1, "Only one child" );
109 ok( jQuery("#first").children().is("#empty"), "Verify Right Element" );
110 equals( jQuery("#first").children().children().length, num, "Verify Elements Intact" );
113 test("wrapInner(String|Element)", function() {
114 testWrapInner(bareObj);
117 // TODO: wrapInner uses wrapAll -- get wrapAll working with Function
118 // test("wrapInner(Function)", function() {
119 // testWrapInner(functionReturningObj)
122 var testUnwrap = function() {
125 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>');
127 var abcd = jQuery('#unwrap1 > span, #unwrap2 > span').get(),
128 abcdef = jQuery('#unwrap span').get();
130 equals( jQuery('#unwrap1 span').add('#unwrap2 span:first').unwrap().length, 3, 'make #unwrap1 and #unwrap2 go away' );
131 same( jQuery('#unwrap > span').get(), abcd, 'all four spans should still exist' );
133 same( jQuery('#unwrap3 span').unwrap().get(), jQuery('#unwrap3 > span').get(), 'make all b in #unwrap3 go away' );
135 same( jQuery('#unwrap3 span').unwrap().get(), jQuery('#unwrap > span.unwrap3').get(), 'make #unwrap3 go away' );
137 same( jQuery('#unwrap').children().get(), abcdef, '#unwrap only contains 6 child spans' );
139 same( jQuery('#unwrap > span').unwrap().get(), jQuery('body > span.unwrap').get(), 'make the 6 spans become children of body' );
141 same( jQuery('body > span.unwrap').unwrap().get(), jQuery('body > span.unwrap').get(), 'can\'t unwrap children of body' );
142 same( jQuery('body > span.unwrap').unwrap().get(), abcdef, 'can\'t unwrap children of body' );
144 same( jQuery('body > span.unwrap').get(), abcdef, 'body contains 6 .unwrap child spans' );
146 jQuery('body > span.unwrap').remove();
149 test("unwrap()", function() {
153 var testAppend = function(valueObj) {
155 var defaultText = 'Try them out:'
156 var result = jQuery('#first').append(valueObj('<b>buga</b>'));
157 equals( result.text(), defaultText + 'buga', 'Check if text appending works' );
158 equals( jQuery('#select3').append(valueObj('<option value="appendTest">Append Test</option>')).find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
161 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
162 jQuery('#sap').append(valueObj(document.getElementById('first')));
163 equals( expected, jQuery('#sap').text(), "Check for appending of element" );
166 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
167 jQuery('#sap').append(valueObj([document.getElementById('first'), document.getElementById('yahoo')]));
168 equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
171 expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
172 jQuery('#sap').append(valueObj(jQuery("#first, #yahoo")));
173 equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
176 jQuery("#sap").append(valueObj( 5 ));
177 ok( jQuery("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
180 jQuery("#sap").append(valueObj( " text with spaces " ));
181 ok( jQuery("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );
184 ok( jQuery("#sap").append(valueObj( [] )), "Check for appending an empty array." );
185 ok( jQuery("#sap").append(valueObj( "" )), "Check for appending an empty string." );
186 ok( jQuery("#sap").append(valueObj( document.getElementsByTagName("foo") )), "Check for appending an empty nodelist." );
189 jQuery("#sap").append(valueObj( document.getElementById('form') ));
190 equals( jQuery("#sap>form").size(), 1, "Check for appending a form" ); // Bug #910
195 jQuery( jQuery("#iframe")[0].contentWindow.document.body ).append(valueObj( "<div>test</div>" ));
200 ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
203 jQuery('<fieldset/>').appendTo('#form').append(valueObj( '<legend id="legend">test</legend>' ));
204 t( 'Append legend', '#legend', ['legend'] );
207 jQuery('#select1').append(valueObj( '<OPTION>Test</OPTION>' ));
208 equals( jQuery('#select1 option:last').text(), "Test", "Appending <OPTION> (all caps)" );
210 jQuery('#table').append(valueObj( '<colgroup></colgroup>' ));
211 ok( jQuery('#table colgroup').length, "Append colgroup" );
213 jQuery('#table colgroup').append(valueObj( '<col/>' ));
214 ok( jQuery('#table colgroup col').length, "Append col" );
217 jQuery('#table').append(valueObj( '<caption></caption>' ));
218 ok( jQuery('#table caption').length, "Append caption" );
222 .append(valueObj( '<select id="appendSelect1"></select>' ))
223 .append(valueObj( '<select id="appendSelect2"><option>Test</option></select>' ));
225 t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );
227 // using contents will get comments regular, text, and comment nodes
228 var j = jQuery("#nonnodes").contents();
229 var d = jQuery("<div/>").appendTo("#nonnodes").append(j);
230 equals( jQuery("#nonnodes").length, 1, "Check node,textnode,comment append moved leaving just the div" );
231 ok( d.contents().length >= 2, "Check node,textnode,comment append works" );
232 d.contents().appendTo("#nonnodes");
234 ok( jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment append cleanup worked" );
237 test("append(String|Element|Array<Element>|jQuery)", function() {
241 test("append(Function)", function() {
242 testAppend(functionReturningObj);
245 test("appendTo(String|Element|Array<Element>|jQuery)", function() {
247 var defaultText = 'Try them out:'
248 jQuery('<b>buga</b>').appendTo('#first');
249 equals( jQuery("#first").text(), defaultText + 'buga', 'Check if text appending works' );
250 equals( jQuery('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
253 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
254 jQuery(document.getElementById('first')).appendTo('#sap');
255 equals( expected, jQuery('#sap').text(), "Check for appending of element" );
258 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
259 jQuery([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
260 equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
263 ok( jQuery(document.createElement("script")).appendTo("body").length, "Make sure a disconnected script can be appended." );
266 expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
267 jQuery("#first, #yahoo").appendTo('#sap');
268 equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
271 jQuery('#select1').appendTo('#foo');
272 t( 'Append select', '#foo select', ['select1'] );
275 var div = jQuery("<div/>").click(function(){
276 ok(true, "Running a cloned click.");
278 div.appendTo("#main, #moretests");
280 jQuery("#main div:last").click();
281 jQuery("#moretests div:last").click();
284 var div = jQuery("<div/>").appendTo("#main, #moretests");
286 equals( div.length, 2, "appendTo returns the inserted elements" );
288 div.addClass("test");
290 ok( jQuery("#main div:last").hasClass("test"), "appendTo element was modified after the insertion" );
291 ok( jQuery("#moretests div:last").hasClass("test"), "appendTo element was modified after the insertion" );
296 var testPrepend = function(val) {
298 var defaultText = 'Try them out:'
299 var result = jQuery('#first').prepend(val( '<b>buga</b>' ));
300 equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
301 equals( jQuery('#select3').prepend(val( '<option value="prependTest">Prepend Test</option>' )).find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
304 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
305 jQuery('#sap').prepend(val( document.getElementById('first') ));
306 equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
309 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
310 jQuery('#sap').prepend(val( [document.getElementById('first'), document.getElementById('yahoo')] ));
311 equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
314 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
315 jQuery('#sap').prepend(val( jQuery("#first, #yahoo") ));
316 equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
319 test("prepend(String|Element|Array<Element>|jQuery)", function() {
320 testPrepend(bareObj);
323 test("prepend(Function)", function() {
324 testPrepend(functionReturningObj);
327 test("prependTo(String|Element|Array<Element>|jQuery)", function() {
329 var defaultText = 'Try them out:'
330 jQuery('<b>buga</b>').prependTo('#first');
331 equals( jQuery('#first').text(), 'buga' + defaultText, 'Check if text prepending works' );
332 equals( jQuery('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
335 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
336 jQuery(document.getElementById('first')).prependTo('#sap');
337 equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
340 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
341 jQuery([document.getElementById('first'), document.getElementById('yahoo')]).prependTo('#sap');
342 equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
345 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
346 jQuery("#first, #yahoo").prependTo('#sap');
347 equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
350 jQuery('<select id="prependSelect1"></select>').prependTo('form:last');
351 jQuery('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');
353 t( "Prepend Select", "#prependSelect2, #prependSelect1", ["prependSelect2", "prependSelect1"] );
356 var testBefore = function(val) {
358 var expected = 'This is a normal link: bugaYahoo';
359 jQuery('#yahoo').before(val( '<b>buga</b>' ));
360 equals( expected, jQuery('#en').text(), 'Insert String before' );
363 expected = "This is a normal link: Try them out:Yahoo";
364 jQuery('#yahoo').before(val( document.getElementById('first') ));
365 equals( expected, jQuery('#en').text(), "Insert element before" );
368 expected = "This is a normal link: Try them out:diveintomarkYahoo";
369 jQuery('#yahoo').before(val( [document.getElementById('first'), document.getElementById('mark')] ));
370 equals( expected, jQuery('#en').text(), "Insert array of elements before" );
373 expected = "This is a normal link: diveintomarkTry them out:Yahoo";
374 jQuery('#yahoo').before(val( jQuery("#first, #mark") ));
375 equals( expected, jQuery('#en').text(), "Insert jQuery before" );
377 var set = jQuery("<div/>").before("<span>test</span>");
378 equals( set[0].nodeName.toLowerCase(), "span", "Insert the element before the disconnected node." );
379 equals( set.length, 2, "Insert the element before the disconnected node." );
382 test("before(String|Element|Array<Element>|jQuery)", function() {
386 test("before(Function)", function() {
387 testBefore(functionReturningObj);
390 test("insertBefore(String|Element|Array<Element>|jQuery)", function() {
392 var expected = 'This is a normal link: bugaYahoo';
393 jQuery('<b>buga</b>').insertBefore('#yahoo');
394 equals( expected, jQuery('#en').text(), 'Insert String before' );
397 expected = "This is a normal link: Try them out:Yahoo";
398 jQuery(document.getElementById('first')).insertBefore('#yahoo');
399 equals( expected, jQuery('#en').text(), "Insert element before" );
402 expected = "This is a normal link: Try them out:diveintomarkYahoo";
403 jQuery([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
404 equals( expected, jQuery('#en').text(), "Insert array of elements before" );
407 expected = "This is a normal link: diveintomarkTry them out:Yahoo";
408 jQuery("#first, #mark").insertBefore('#yahoo');
409 equals( expected, jQuery('#en').text(), "Insert jQuery before" );
412 var testAfter = function(val) {
414 var expected = 'This is a normal link: Yahoobuga';
415 jQuery('#yahoo').after(val( '<b>buga</b>' ));
416 equals( expected, jQuery('#en').text(), 'Insert String after' );
419 expected = "This is a normal link: YahooTry them out:";
420 jQuery('#yahoo').after(val( document.getElementById('first') ));
421 equals( expected, jQuery('#en').text(), "Insert element after" );
424 expected = "This is a normal link: YahooTry them out:diveintomark";
425 jQuery('#yahoo').after(val( [document.getElementById('first'), document.getElementById('mark')] ));
426 equals( expected, jQuery('#en').text(), "Insert array of elements after" );
429 expected = "This is a normal link: YahoodiveintomarkTry them out:";
430 jQuery('#yahoo').after(val( jQuery("#first, #mark") ));
431 equals( expected, jQuery('#en').text(), "Insert jQuery after" );
433 var set = jQuery("<div/>").after("<span>test</span>");
434 equals( set[1].nodeName.toLowerCase(), "span", "Insert the element after the disconnected node." );
435 equals( set.length, 2, "Insert the element after the disconnected node." );
438 test("after(String|Element|Array<Element>|jQuery)", function() {
442 test("after(Function)", function() {
443 testAfter(functionReturningObj);
446 test("insertAfter(String|Element|Array<Element>|jQuery)", function() {
448 var expected = 'This is a normal link: Yahoobuga';
449 jQuery('<b>buga</b>').insertAfter('#yahoo');
450 equals( expected, jQuery('#en').text(), 'Insert String after' );
453 expected = "This is a normal link: YahooTry them out:";
454 jQuery(document.getElementById('first')).insertAfter('#yahoo');
455 equals( expected, jQuery('#en').text(), "Insert element after" );
458 expected = "This is a normal link: YahooTry them out:diveintomark";
459 jQuery([document.getElementById('first'), document.getElementById('mark')]).insertAfter('#yahoo');
460 equals( expected, jQuery('#en').text(), "Insert array of elements after" );
463 expected = "This is a normal link: YahoodiveintomarkTry them out:";
464 jQuery("#first, #mark").insertAfter('#yahoo');
465 equals( expected, jQuery('#en').text(), "Insert jQuery after" );
468 var testReplaceWith = function(val) {
470 jQuery('#yahoo').replaceWith(val( '<b id="replace">buga</b>' ));
471 ok( jQuery("#replace")[0], 'Replace element with string' );
472 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
475 jQuery('#yahoo').replaceWith(val( document.getElementById('first') ));
476 ok( jQuery("#first")[0], 'Replace element with element' );
477 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
480 jQuery('#yahoo').replaceWith(val( [document.getElementById('first'), document.getElementById('mark')] ));
481 ok( jQuery("#first")[0], 'Replace element with array of elements' );
482 ok( jQuery("#mark")[0], 'Replace element with array of elements' );
483 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
486 jQuery('#yahoo').replaceWith(val( jQuery("#first, #mark") ));
487 ok( jQuery("#first")[0], 'Replace element with set of elements' );
488 ok( jQuery("#mark")[0], 'Replace element with set of elements' );
489 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
491 var set = jQuery("<div/>").replaceWith(val("<span>test</span>"));
492 equals( set[0].nodeName.toLowerCase(), "span", "Replace the disconnected node." );
493 equals( set.length, 1, "Replace the disconnected node." );
495 var $div = jQuery("<div class='replacewith'></div>").appendTo("body");
496 $div.replaceWith("<div class='replacewith'></div><script>" +
497 "equals(jQuery('.replacewith').length, 1, 'Check number of elements in page.');" +
499 equals(jQuery('.replacewith').length, 1, 'Check number of elements in page.');
500 jQuery('.replacewith').remove();
503 test("replaceWith(String|Element|Array<Element>|jQuery)", function() {
504 testReplaceWith(bareObj);
507 test("replaceWith(Function)", function() {
508 testReplaceWith(functionReturningObj);
511 test("replaceAll(String|Element|Array<Element>|jQuery)", function() {
513 jQuery('<b id="replace">buga</b>').replaceAll("#yahoo");
514 ok( jQuery("#replace")[0], 'Replace element with string' );
515 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
518 jQuery(document.getElementById('first')).replaceAll("#yahoo");
519 ok( jQuery("#first")[0], 'Replace element with element' );
520 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
523 jQuery([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo");
524 ok( jQuery("#first")[0], 'Replace element with array of elements' );
525 ok( jQuery("#mark")[0], 'Replace element with array of elements' );
526 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
529 jQuery("#first, #mark").replaceAll("#yahoo");
530 ok( jQuery("#first")[0], 'Replace element with set of elements' );
531 ok( jQuery("#mark")[0], 'Replace element with set of elements' );
532 ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
535 test("clone()", function() {
537 equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Assert text for #en' );
538 var clone = jQuery('#yahoo').clone();
539 equals( 'Try them out:Yahoo', jQuery('#first').append(clone).text(), 'Check for clone' );
540 equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Reassert text for #en' );
543 "<table/>", "<tr/>", "<td/>", "<div/>",
544 "<button/>", "<ul/>", "<ol/>", "<li/>",
545 "<input type='checkbox' />", "<select/>", "<option/>", "<textarea/>",
546 "<tbody/>", "<thead/>", "<tfoot/>", "<iframe/>"
548 for (var i = 0; i < cloneTags.length; i++) {
549 var j = jQuery(cloneTags[i]);
550 equals( j[0].tagName, j.clone()[0].tagName, 'Clone a <' + cloneTags[i].substring(1));
553 // using contents will get comments regular, text, and comment nodes
554 var cl = jQuery("#nonnodes").contents().clone();
555 ok( cl.length >= 2, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
557 var div = jQuery("<div><ul><li>test</li></ul></div>").click(function(){
558 ok( true, "Bound event still exists." );
561 div = div.clone(true).clone(true);
562 equals( div.length, 1, "One element cloned" );
563 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
564 div.trigger("click");
566 div = jQuery("<div/>").append([ document.createElement("table"), document.createElement("table") ]);
567 div.find("table").click(function(){
568 ok( true, "Bound event still exists." );
571 div = div.clone(true);
572 equals( div.length, 1, "One element cloned" );
573 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
574 div.find("table:last").trigger("click");
576 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>');
578 div = div.clone(true);
579 equals( div.length, 1, "One element cloned" );
580 equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
584 test("clone() on XML nodes", function() {
587 jQuery.get("data/dashboard.xml", function (xml) {
588 var root = jQuery(xml.documentElement).clone();
589 var origTab = jQuery("tab", xml).eq(0);
590 var cloneTab = jQuery("tab", root).eq(0);
591 origTab.text("origval");
592 cloneTab.text("cloneval");
593 equals(origTab.text(), "origval", "Check original XML node was correctly set");
594 equals(cloneTab.text(), "cloneval", "Check cloned XML node was correctly set");
600 test("val()", function() {
603 document.getElementById('text1').value = "bla";
604 equals( jQuery("#text1").val(), "bla", "Check for modified value of input element" );
608 equals( jQuery("#text1").val(), "Test", "Check for value of input element" );
609 // ticket #1714 this caused a JS error in IE
610 equals( jQuery("#first").val(), "", "Check a paragraph element to see if it has a value" );
611 ok( jQuery([]).val() === undefined, "Check an empty jQuery object will return undefined from val" );
613 equals( jQuery('#select2').val(), '3', 'Call val() on a single="single" select' );
615 same( jQuery('#select3').val(), ['1', '2'], 'Call val() on a multiple="multiple" select' );
617 equals( jQuery('#option3c').val(), '2', 'Call val() on a option element with value' );
619 equals( jQuery('#option3a').val(), '', 'Call val() on a option element with empty value' );
621 equals( jQuery('#option3e').val(), 'no value', 'Call val() on a option element with no value attribute' );
625 var testVal = function(valueObj) {
628 jQuery("#text1").val(valueObj( 'test' ));
629 equals( document.getElementById('text1').value, "test", "Check for modified (via val(String)) value of input element" );
631 jQuery("#text1").val(valueObj( 67 ));
632 equals( document.getElementById('text1').value, "67", "Check for modified (via val(Number)) value of input element" );
634 jQuery("#select1").val(valueObj( "3" ));
635 equals( jQuery("#select1").val(), "3", "Check for modified (via val(String)) value of select element" );
637 jQuery("#select1").val(valueObj( 2 ));
638 equals( jQuery("#select1").val(), "2", "Check for modified (via val(Number)) value of select element" );
640 jQuery("#select1").append("<option value='4'>four</option>");
641 jQuery("#select1").val(valueObj( 4 ));
642 equals( jQuery("#select1").val(), "4", "Should be possible to set the val() to a newly created option" );
644 // using contents will get comments regular, text, and comment nodes
645 var j = jQuery("#nonnodes").contents();
646 j.val(valueObj( "asdf" ));
647 equals( j.val(), "asdf", "Check node,textnode,comment with val()" );
648 j.removeAttr("value");
651 test("val(String/Number)", function() {
655 test("val(Function)", function() {
656 testVal(functionReturningObj);
659 var testHtml = function(valueObj) {
662 jQuery.scriptorder = 0;
664 var div = jQuery("#main > div");
665 div.html(valueObj("<b>test</b>"));
667 for ( var i = 0; i < div.size(); i++ ) {
668 if ( div.get(i).childNodes.length != 1 ) pass = false;
670 ok( pass, "Set HTML" );
673 // using contents will get comments regular, text, and comment nodes
674 var j = jQuery("#nonnodes").contents();
675 j.html(valueObj("<b>bold</b>"));
677 // this is needed, or the expando added by jQuery unique will yield a different html
678 j.find('b').removeData();
679 equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
681 jQuery("#main").html(valueObj("<select/>"));
682 jQuery("#main select").html(valueObj("<option>O1</option><option selected='selected'>O2</option><option>O3</option>"));
683 equals( jQuery("#main select").val(), "O2", "Selected option correct" );
685 var $div = jQuery('<div />');
686 equals( $div.html(valueObj( 5 )).html(), '5', 'Setting a number as html' );
687 equals( $div.html(valueObj( 0 )).html(), '0', 'Setting a zero as html' );
689 var $div2 = jQuery('<div/>'), insert = "<div>hello1</div>";
690 equals( $div2.html(insert).html(), insert, "Verify escaped insertion." );
691 equals( $div2.html("x" + insert).html(), "x" + insert, "Verify escaped insertion." );
692 equals( $div2.html(" " + insert).html(), " " + insert, "Verify escaped insertion." );
697 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>'));
701 jQuery("#main").html(valueObj('<script type="text/javascript">ok( true, "jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975 (1)" );</script>'));
703 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>'));
705 // 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
706 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>"));
708 setTimeout( start, 100 );
711 test("html(String)", function() {
715 test("html(Function)", function() {
716 testHtml(functionReturningObj);
719 var testText = function(valueObj) {
721 equals( jQuery("#foo").text("<div><b>Hello</b> cruel world!</div>")[0].innerHTML.replace(/>/g, ">"), "<div><b>Hello</b> cruel world!</div>", "Check escaped text" );
723 // using contents will get comments regular, text, and comment nodes
724 var j = jQuery("#nonnodes").contents();
726 equals( jQuery(j[0]).text(), "hi!", "Check node,textnode,comment with text()" );
727 equals( j[1].nodeValue, " there ", "Check node,textnode,comment with text()" );
728 equals( j[2].nodeType, 8, "Check node,textnode,comment with text()" );
731 test("text(String)", function() {
735 test("text(Function)", function() {
736 testText(functionReturningObj);
739 var testRemove = function(method) {
742 var first = jQuery("#ap").children(":first");
743 first.data("foo", "bar");
745 jQuery("#ap").children()[method]();
746 ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
747 equals( jQuery("#ap").children().length, 0, "Check remove" );
749 equals( first.data("foo"), method == "remove" ? null : "bar" );
752 jQuery("#ap").children()[method]("a");
753 ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
754 equals( jQuery("#ap").children().length, 1, "Check filtered remove" );
756 jQuery("#ap").children()[method]("a, code");
757 equals( jQuery("#ap").children().length, 0, "Check multi-filtered remove" );
759 // using contents will get comments regular, text, and comment nodes
760 equals( jQuery("#nonnodes").contents().length, 3, "Check node,textnode,comment remove works" );
761 jQuery("#nonnodes").contents()[method]();
762 equals( jQuery("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" );
767 var first = jQuery("#ap").children(":first");
768 var cleanUp = first.click(function() { count++ })[method]().appendTo("body").click();
770 equals( method == "remove" ? 0 : 1, count );
775 test("remove()", function() {
776 testRemove("remove");
779 test("detach()", function() {
780 testRemove("detach");
783 test("empty()", function() {
785 equals( jQuery("#ap").children().empty().text().length, 0, "Check text is removed" );
786 equals( jQuery("#ap").children().length, 4, "Check elements are not removed" );
788 // using contents will get comments regular, text, and comment nodes
789 var j = jQuery("#nonnodes").contents();
791 equals( j.html(), "", "Check node,textnode,comment empty works" );