1 module("manipulation", { teardown: moduleTeardown });
3 // Ensure that an extended Array prototype doesn't break jQuery
4 Array.prototype.arrayProtoFn = function(arg) { throw("arrayProtoFn should not be called"); };
6 var bareObj = function(value) { return value; };
7 var functionReturningObj = function(value) { return (function() { return value; }); };
9 test("text()", function() {
11 var expected = "This link has class=\"blog\": Simon Willison's Weblog";
12 equal( jQuery("#sap").text(), expected, "Check for merged text of more then one element." );
14 // Check serialization of text values
15 equal( jQuery(document.createTextNode("foo")).text(), "foo", "Text node was retreived from .text()." );
16 notEqual( jQuery(document).text(), "", "Retrieving text for the document retrieves all text (#10724).");
18 // Retrieve from document fragments #10864
19 var frag = document.createDocumentFragment();
20 frag.appendChild( document.createTextNode("foo") );
22 equal( jQuery( frag ).text(), "foo", "Document Fragment Text node was retreived from .text().");
24 var $newLineTest = jQuery("<div>test<br/>testy</div>").appendTo("#moretests");
25 $newLineTest.find("br").replaceWith("\n");
26 equal( $newLineTest.text(), "test\ntesty", "text() does not remove new lines (#11153)" );
29 test("text(undefined)", function() {
31 equal( jQuery("#foo").text("<div").text(undefined)[0].innerHTML, "<div", ".text(undefined) is chainable (#5571)" );
34 var testText = function(valueObj) {
36 var val = valueObj("<div><b>Hello</b> cruel world!</div>");
37 equal( jQuery("#foo").text(val)[0].innerHTML.replace(/>/g, ">"), "<div><b>Hello</b> cruel world!</div>", "Check escaped text" );
39 // using contents will get comments regular, text, and comment nodes
40 var j = jQuery("#nonnodes").contents();
41 j.text(valueObj("hi!"));
42 equal( jQuery(j[0]).text(), "hi!", "Check node,textnode,comment with text()" );
43 equal( j[1].nodeValue, " there ", "Check node,textnode,comment with text()" );
45 // Blackberry 4.6 doesn't maintain comments in the DOM
46 equal( jQuery("#nonnodes")[0].childNodes.length < 3 ? 8 : j[2].nodeType, 8, "Check node,textnode,comment with text()" );
49 test("text(String)", function() {
53 test("text(Function)", function() {
54 testText(functionReturningObj);
57 test("text(Function) with incoming value", function() {
60 var old = "This link has class=\"blog\": Simon Willison's Weblog";
62 jQuery("#sap").text(function(i, val) {
63 equal( val, old, "Make sure the incoming value is correct." );
67 equal( jQuery("#sap").text(), "foobar", "Check for merged text of more then one element." );
72 var testWrap = function(val) {
74 var defaultText = "Try them out:";
75 var result = jQuery("#first").wrap(val( "<div class='red'><span></span></div>" )).text();
76 equal( defaultText, result, "Check for wrapping of on-the-fly html" );
77 ok( jQuery("#first").parent().parent().is(".red"), "Check if wrapper has class 'red'" );
80 result = jQuery("#first").wrap(val( document.getElementById("empty") )).parent();
81 ok( result.is("ol"), "Check for element wrapping" );
82 equal( result.text(), defaultText, "Check for element wrapping" );
85 jQuery("#check1").click(function() {
87 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
88 jQuery(checkbox).wrap(val( "<div id='c1' style='display:none;'></div>" ));
89 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
92 // using contents will get comments regular, text, and comment nodes
93 var j = jQuery("#nonnodes").contents();
94 j.wrap(val( "<i></i>" ));
96 // Blackberry 4.6 doesn't maintain comments in the DOM
97 equal( jQuery("#nonnodes > i").length, jQuery("#nonnodes")[0].childNodes.length, "Check node,textnode,comment wraps ok" );
98 equal( jQuery("#nonnodes > i").text(), j.text(), "Check node,textnode,comment wraps doesn't hurt text" );
100 // Try wrapping a disconnected node
102 for (var i in jQuery.cache) {
106 j = jQuery("<label/>").wrap(val( "<li/>" ));
107 equal( j[0].nodeName.toUpperCase(), "LABEL", "Element is a label" );
108 equal( j[0].parentNode.nodeName.toUpperCase(), "LI", "Element has been wrapped" );
110 for (i in jQuery.cache) {
113 equal(cacheLength, 0, "No memory leak in jQuery.cache (bug #7165)");
115 // Wrap an element containing a text node
116 j = jQuery("<span/>").wrap("<div>test</div>");
117 equal( j[0].previousSibling.nodeType, 3, "Make sure the previous node is a text element" );
118 equal( j[0].parentNode.nodeName.toUpperCase(), "DIV", "And that we're in the div element." );
120 // Try to wrap an element with multiple elements (should fail)
121 j = jQuery("<div><span></span></div>").children().wrap("<p></p><div></div>");
122 equal( j[0].parentNode.parentNode.childNodes.length, 1, "There should only be one element wrapping." );
123 equal( j.length, 1, "There should only be one element (no cloning)." );
124 equal( j[0].parentNode.nodeName.toUpperCase(), "P", "The span should be in the paragraph." );
126 // Wrap an element with a jQuery set
127 j = jQuery("<span/>").wrap(jQuery("<div></div>"));
128 equal( j[0].parentNode.nodeName.toLowerCase(), "div", "Wrapping works." );
130 // Wrap an element with a jQuery set and event
131 result = jQuery("<div></div>").click(function(){
132 ok(true, "Event triggered.");
134 // Remove handlers on detached elements
136 jQuery(this).unbind();
139 j = jQuery("<span/>").wrap(result);
140 equal( j[0].parentNode.nodeName.toLowerCase(), "div", "Wrapping works." );
142 j.parent().trigger("click");
144 // clean up attached elements
148 test("wrap(String|Element)", function() {
152 test("wrap(Function)", function() {
153 testWrap(functionReturningObj);
156 test("wrap(Function) with index (#10177)", function() {
157 var expectedIndex = 0,
158 targets = jQuery("#qunit-fixture p");
160 expect(targets.length);
161 targets.wrap(function(i) {
162 equal( i, expectedIndex, "Check if the provided index (" + i + ") is as expected (" + expectedIndex + ")" );
165 return "<div id='wrap_index_'" + i + "'></div>";
169 test("wrap(String) consecutive elements (#10177)", function() {
170 var targets = jQuery("#qunit-fixture p");
172 expect(targets.length * 2);
173 targets.wrap("<div class='wrapper'></div>");
175 targets.each(function() {
176 var $this = jQuery(this);
178 ok( $this.parent().is('.wrapper'), "Check each elements parent is correct (.wrapper)" );
179 equal( $this.siblings().length, 0, "Each element should be wrapped individually" );
183 var testWrapAll = function(val) {
185 var prev = jQuery("#firstp")[0].previousSibling;
186 var p = jQuery("#firstp,#first")[0].parentNode;
188 var result = jQuery("#firstp,#first").wrapAll(val( "<div class='red'><div class='tmp'></div></div>" ));
189 equal( result.parent().length, 1, "Check for wrapping of on-the-fly html" );
190 ok( jQuery("#first").parent().parent().is(".red"), "Check if wrapper has class 'red'" );
191 ok( jQuery("#firstp").parent().parent().is(".red"), "Check if wrapper has class 'red'" );
192 equal( jQuery("#first").parent().parent()[0].previousSibling, prev, "Correct Previous Sibling" );
193 equal( jQuery("#first").parent().parent()[0].parentNode, p, "Correct Parent" );
196 var prev = jQuery("#firstp")[0].previousSibling;
197 var p = jQuery("#first")[0].parentNode;
198 var result = jQuery("#firstp,#first").wrapAll(val( document.getElementById("empty") ));
199 equal( jQuery("#first").parent()[0], jQuery("#firstp").parent()[0], "Same Parent" );
200 equal( jQuery("#first").parent()[0].previousSibling, prev, "Correct Previous Sibling" );
201 equal( jQuery("#first").parent()[0].parentNode, p, "Correct Parent" );
204 test("wrapAll(String|Element)", function() {
205 testWrapAll(bareObj);
208 var testWrapInner = function(val) {
210 var num = jQuery("#first").children().length;
211 var result = jQuery("#first").wrapInner(val("<div class='red'><div id='tmp'></div></div>"));
212 equal( jQuery("#first").children().length, 1, "Only one child" );
213 ok( jQuery("#first").children().is(".red"), "Verify Right Element" );
214 equal( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" );
217 var num = jQuery("#first").html("foo<div>test</div><div>test2</div>").children().length;
218 var result = jQuery("#first").wrapInner(val("<div class='red'><div id='tmp'></div></div>"));
219 equal( jQuery("#first").children().length, 1, "Only one child" );
220 ok( jQuery("#first").children().is(".red"), "Verify Right Element" );
221 equal( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" );
224 var num = jQuery("#first").children().length;
225 var result = jQuery("#first").wrapInner(val(document.getElementById("empty")));
226 equal( jQuery("#first").children().length, 1, "Only one child" );
227 ok( jQuery("#first").children().is("#empty"), "Verify Right Element" );
228 equal( jQuery("#first").children().children().length, num, "Verify Elements Intact" );
230 var div = jQuery("<div/>");
231 div.wrapInner(val("<span></span>"));
232 equal(div.children().length, 1, "The contents were wrapped.");
233 equal(div.children()[0].nodeName.toLowerCase(), "span", "A span was inserted.");
236 test("wrapInner(String|Element)", function() {
237 testWrapInner(bareObj);
240 test("wrapInner(Function)", function() {
241 testWrapInner(functionReturningObj)
244 test("unwrap()", function() {
247 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>");
249 var abcd = jQuery("#unwrap1 > span, #unwrap2 > span").get(),
250 abcdef = jQuery("#unwrap span").get();
252 equal( jQuery("#unwrap1 span").add("#unwrap2 span:first").unwrap().length, 3, "make #unwrap1 and #unwrap2 go away" );
253 deepEqual( jQuery("#unwrap > span").get(), abcd, "all four spans should still exist" );
255 deepEqual( jQuery("#unwrap3 span").unwrap().get(), jQuery("#unwrap3 > span").get(), "make all b in #unwrap3 go away" );
257 deepEqual( jQuery("#unwrap3 span").unwrap().get(), jQuery("#unwrap > span.unwrap3").get(), "make #unwrap3 go away" );
259 deepEqual( jQuery("#unwrap").children().get(), abcdef, "#unwrap only contains 6 child spans" );
261 deepEqual( jQuery("#unwrap > span").unwrap().get(), jQuery("body > span.unwrap").get(), "make the 6 spans become children of body" );
263 deepEqual( jQuery("body > span.unwrap").unwrap().get(), jQuery("body > span.unwrap").get(), "can't unwrap children of body" );
264 deepEqual( jQuery("body > span.unwrap").unwrap().get(), abcdef, "can't unwrap children of body" );
266 deepEqual( jQuery("body > span.unwrap").get(), abcdef, "body contains 6 .unwrap child spans" );
268 jQuery("body > span.unwrap").remove();
271 var testAppend = function(valueObj) {
273 var defaultText = "Try them out:"
274 var result = jQuery("#first").append(valueObj("<b>buga</b>"));
275 equal( result.text(), defaultText + "buga", "Check if text appending works" );
276 equal( jQuery("#select3").append(valueObj("<option value='appendTest'>Append Test</option>")).find("option:last-child").attr("value"), "appendTest", "Appending html options to select element");
279 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
280 jQuery("#sap").append(valueObj(document.getElementById("first")));
281 equal( jQuery("#sap").text(), expected, "Check for appending of element" );
284 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
285 jQuery("#sap").append(valueObj([document.getElementById("first"), document.getElementById("yahoo")]));
286 equal( jQuery("#sap").text(), expected, "Check for appending of array of elements" );
289 expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
290 jQuery("#sap").append(valueObj(jQuery("#yahoo, #first")));
291 equal( jQuery("#sap").text(), expected, "Check for appending of jQuery object" );
294 jQuery("#sap").append(valueObj( 5 ));
295 ok( jQuery("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
298 jQuery("#sap").append(valueObj( " text with spaces " ));
299 ok( jQuery("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );
302 ok( jQuery("#sap").append(valueObj( [] )), "Check for appending an empty array." );
303 ok( jQuery("#sap").append(valueObj( "" )), "Check for appending an empty string." );
304 ok( jQuery("#sap").append(valueObj( document.getElementsByTagName("foo") )), "Check for appending an empty nodelist." );
307 jQuery("form").append(valueObj("<input name='radiotest' type='radio' checked='checked' />"));
308 jQuery("form input[name=radiotest]").each(function(){
309 ok( jQuery(this).is(":checked"), "Append checked radio");
313 jQuery("form").append(valueObj("<input name='radiotest' type='radio' checked = 'checked' />"));
314 jQuery("form input[name=radiotest]").each(function(){
315 ok( jQuery(this).is(":checked"), "Append alternately formated checked radio");
319 jQuery("form").append(valueObj("<input name='radiotest' type='radio' checked />"));
320 jQuery("form input[name=radiotest]").each(function(){
321 ok( jQuery(this).is(":checked"), "Append HTML5-formated checked radio");
325 jQuery("form").append(valueObj("<input type='radio' checked='checked' name='radiotest' />"));
326 jQuery("form input[name=radiotest]").each(function(){
327 ok( jQuery(this).is(":checked"), "Append with name attribute after checked attribute");
331 jQuery("#sap").append(valueObj( document.getElementById("form") ));
332 equal( jQuery("#sap>form").size(), 1, "Check for appending a form" ); // Bug #910
337 var body = jQuery("#iframe")[0].contentWindow.document.body;
340 jQuery( body ).append(valueObj( "<div>test</div>" ));
344 ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
347 jQuery("<fieldset/>").appendTo("#form").append(valueObj( "<legend id='legend'>test</legend>" ));
348 t( "Append legend", "#legend", ["legend"] );
351 jQuery("#select1").append(valueObj( "<OPTION>Test</OPTION>" ));
352 equal( jQuery("#select1 option:last").text(), "Test", "Appending <OPTION> (all caps)" );
354 jQuery("#table").append(valueObj( "<colgroup></colgroup>" ));
355 ok( jQuery("#table colgroup").length, "Append colgroup" );
357 jQuery("#table colgroup").append(valueObj( "<col/>" ));
358 ok( jQuery("#table colgroup col").length, "Append col" );
361 jQuery("#table").append(valueObj( "<caption></caption>" ));
362 ok( jQuery("#table caption").length, "Append caption" );
366 .append(valueObj( "<select id='appendSelect1'></select>" ))
367 .append(valueObj( "<select id='appendSelect2'><option>Test</option></select>" ));
369 t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );
371 equal( "Two nodes", jQuery("<div />").append("Two", " nodes").text(), "Appending two text nodes (#4011)" );
373 // using contents will get comments regular, text, and comment nodes
374 var j = jQuery("#nonnodes").contents();
375 var d = jQuery("<div/>").appendTo("#nonnodes").append(j);
376 equal( jQuery("#nonnodes").length, 1, "Check node,textnode,comment append moved leaving just the div" );
377 ok( d.contents().length >= 2, "Check node,textnode,comment append works" );
378 d.contents().appendTo("#nonnodes");
380 ok( jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment append cleanup worked" );
383 var $input = jQuery("<input />").attr({ "type": "checkbox", "checked": true }).appendTo('#testForm');
384 equal( $input[0].checked, true, "A checked checkbox that is appended stays checked" );
387 var $radios = jQuery("input:radio[name='R1']"),
388 $radioNot = jQuery("<input type='radio' name='R1' checked='checked'/>").insertAfter( $radios ),
389 $radio = $radios.eq(1).click();
390 $radioNot[0].checked = false;
391 $radios.parent().wrap("<div></div>");
392 equal( $radio[0].checked, true, "Reappending radios uphold which radio is checked" );
393 equal( $radioNot[0].checked, false, "Reappending radios uphold not being checked" );
396 var prev = jQuery("#sap").children().length;
398 jQuery("#sap").append(
404 equal( jQuery("#sap").children().length, prev + 3, "Make sure that multiple arguments works." );
408 test("append(String|Element|Array<Element>|jQuery)", function() {
412 test("append(Function)", function() {
413 testAppend(functionReturningObj);
416 test("append(Function) with incoming value", function() {
419 var defaultText = "Try them out:", old = jQuery("#first").html();
421 var result = jQuery("#first").append(function(i, val){
422 equal( val, old, "Make sure the incoming value is correct." );
423 return "<b>buga</b>";
425 equal( result.text(), defaultText + "buga", "Check if text appending works" );
427 var select = jQuery("#select3");
430 equal( select.append(function(i, val){
431 equal( val, old, "Make sure the incoming value is correct." );
432 return "<option value='appendTest'>Append Test</option>";
433 }).find("option:last-child").attr("value"), "appendTest", "Appending html options to select element");
436 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
437 old = jQuery("#sap").html();
439 jQuery("#sap").append(function(i, val){
440 equal( val, old, "Make sure the incoming value is correct." );
441 return document.getElementById("first");
443 equal( jQuery("#sap").text(), expected, "Check for appending of element" );
446 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
447 old = jQuery("#sap").html();
449 jQuery("#sap").append(function(i, val){
450 equal( val, old, "Make sure the incoming value is correct." );
451 return [document.getElementById("first"), document.getElementById("yahoo")];
453 equal( jQuery("#sap").text(), expected, "Check for appending of array of elements" );
456 expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
457 old = jQuery("#sap").html();
459 jQuery("#sap").append(function(i, val){
460 equal( val, old, "Make sure the incoming value is correct." );
461 return jQuery("#yahoo, #first");
463 equal( jQuery("#sap").text(), expected, "Check for appending of jQuery object" );
466 old = jQuery("#sap").html();
468 jQuery("#sap").append(function(i, val){
469 equal( val, old, "Make sure the incoming value is correct." );
472 ok( jQuery("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
477 test("append the same fragment with events (Bug #6997, 5566)", function () {
478 var doExtra = !jQuery.support.noCloneEvent && document.fireEvent;
479 expect(2 + (doExtra ? 1 : 0));
484 // This patch modified the way that cloning occurs in IE; we need to make sure that
485 // native event handlers on the original object don't get disturbed when they are
486 // modified on the clone
488 element = jQuery("div:first").click(function () {
489 ok(true, "Event exists on original after being unbound on clone");
490 jQuery(this).unbind("click");
492 var clone = element.clone(true).unbind("click");
493 clone[0].fireEvent("onclick");
494 element[0].fireEvent("onclick");
496 // manually clean up detached elements
500 element = jQuery("<a class='test6997'></a>").click(function () {
501 ok(true, "Append second element events work");
504 jQuery("#listWithTabIndex li").append(element)
505 .find("a.test6997").eq(1).click();
507 element = jQuery("<li class='test6997'></li>").click(function () {
508 ok(true, "Before second element events work");
512 jQuery("#listWithTabIndex li").before(element);
513 jQuery("#listWithTabIndex li.test6997").eq(1).click();
516 test("append HTML5 sectioning elements (Bug #6485)", function () {
519 jQuery("#qunit-fixture").append("<article style='font-size:10px'><section><aside>HTML5 elements</aside></section></article>");
521 var article = jQuery("article"),
522 aside = jQuery("aside");
524 equal( article.css("fontSize"), "10px", "HTML5 elements are styleable");
525 equal( aside.length, 1, "HTML5 elements do not collapse their children")
528 test("HTML5 Elements inherit styles from style rules (Bug #10501)", function () {
531 jQuery("#qunit-fixture").append("<article id='article'></article>");
532 jQuery("#article").append("<section>This section should have a pink background.</section>");
534 // In IE, the missing background color will claim its value is "transparent"
535 notEqual( jQuery("section").css("background-color"), "transparent", "HTML5 elements inherit styles");
538 test("html5 clone() cannot use the fragment cache in IE (#6485)", function () {
541 jQuery("<article><section><aside>HTML5 elements</aside></section></article>").appendTo("#qunit-fixture");
543 var clone = jQuery("article").clone();
545 jQuery("#qunit-fixture").append( clone );
547 equal( jQuery("aside").length, 2, "clone()ing HTML5 elems does not collapse them" );
550 test("html(String) with HTML5 (Bug #6485)", function() {
553 jQuery("#qunit-fixture").html("<article><section><aside>HTML5 elements</aside></section></article>");
554 equal( jQuery("#qunit-fixture").children().children().length, 1, "Make sure HTML5 article elements can hold children. innerHTML shortcut path" );
555 equal( jQuery("#qunit-fixture").children().children().children().length, 1, "Make sure nested HTML5 elements can hold children." );
558 test("append(xml)", function() {
561 function createXMLDoc() {
562 // Initialize DOM based upon latest installed MSXML or Netscape
565 [ "MSXML6.DomDocument",
566 "MSXML3.DomDocument",
567 "MSXML2.DomDocument",
569 "Microsoft.XmlDom" ];
571 if ( document.implementation && "createDocument" in document.implementation ) {
572 return document.implementation.createDocument( "", "", null );
575 for ( var n = 0, len = aActiveX.length; n < len; n++ ) {
577 elem = new ActiveXObject( aActiveX[ n ] );
584 var xmlDoc = createXMLDoc(),
585 xml1 = xmlDoc.createElement("head"),
586 xml2 = xmlDoc.createElement("test");
588 ok( jQuery( xml1 ).append( xml2 ), "Append an xml element to another without raising an exception." );
592 test("appendTo(String|Element|Array<Element>|jQuery)", function() {
595 var defaultText = "Try them out:"
596 jQuery("<b>buga</b>").appendTo("#first");
597 equal( jQuery("#first").text(), defaultText + "buga", "Check if text appending works" );
598 equal( jQuery("<option value='appendTest'>Append Test</option>").appendTo("#select3").parent().find("option:last-child").attr("value"), "appendTest", "Appending html options to select element");
601 var l = jQuery("#first").children().length + 2;
602 jQuery("<strong>test</strong>");
603 jQuery("<strong>test</strong>");
604 jQuery([ jQuery("<strong>test</strong>")[0], jQuery("<strong>test</strong>")[0] ])
606 equal( jQuery("#first").children().length, l, "Make sure the elements were inserted." );
607 equal( jQuery("#first").children().last()[0].nodeName.toLowerCase(), "strong", "Verify the last element." );
610 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
611 jQuery(document.getElementById("first")).appendTo("#sap");
612 equal( jQuery("#sap").text(), expected, "Check for appending of element" );
615 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
616 jQuery([document.getElementById("first"), document.getElementById("yahoo")]).appendTo("#sap");
617 equal( jQuery("#sap").text(), expected, "Check for appending of array of elements" );
620 ok( jQuery(document.createElement("script")).appendTo("body").length, "Make sure a disconnected script can be appended." );
623 expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
624 jQuery("#yahoo, #first").appendTo("#sap");
625 equal( jQuery("#sap").text(), expected, "Check for appending of jQuery object" );
628 jQuery("#select1").appendTo("#foo");
629 t( "Append select", "#foo select", ["select1"] );
632 var div = jQuery("<div/>").click(function(){
633 ok(true, "Running a cloned click.");
635 div.appendTo("#qunit-fixture, #moretests");
637 jQuery("#qunit-fixture div:last").click();
638 jQuery("#moretests div:last").click();
641 div = jQuery("<div/>").appendTo("#qunit-fixture, #moretests");
643 equal( div.length, 2, "appendTo returns the inserted elements" );
645 div.addClass("test");
647 ok( jQuery("#qunit-fixture div:last").hasClass("test"), "appendTo element was modified after the insertion" );
648 ok( jQuery("#moretests div:last").hasClass("test"), "appendTo element was modified after the insertion" );
652 div = jQuery("<div/>");
653 jQuery("<span>a</span><b>b</b>").filter("span").appendTo( div );
655 equal( div.children().length, 1, "Make sure the right number of children were inserted." );
657 div = jQuery("#moretests div");
659 var num = jQuery("#qunit-fixture div").length;
660 div.remove().appendTo("#qunit-fixture");
662 equal( jQuery("#qunit-fixture div").length, num, "Make sure all the removed divs were inserted." );
667 jQuery.getScript('data/test.js', function() {
668 jQuery('script[src*="data\\/test\\.js"]').remove();
673 var testPrepend = function(val) {
675 var defaultText = "Try them out:"
676 var result = jQuery("#first").prepend(val( "<b>buga</b>" ));
677 equal( result.text(), "buga" + defaultText, "Check if text prepending works" );
678 equal( jQuery("#select3").prepend(val( "<option value='prependTest'>Prepend Test</option>" )).find("option:first-child").attr("value"), "prependTest", "Prepending html options to select element");
681 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
682 jQuery("#sap").prepend(val( document.getElementById("first") ));
683 equal( jQuery("#sap").text(), expected, "Check for prepending of element" );
686 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
687 jQuery("#sap").prepend(val( [document.getElementById("first"), document.getElementById("yahoo")] ));
688 equal( jQuery("#sap").text(), expected, "Check for prepending of array of elements" );
691 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
692 jQuery("#sap").prepend(val( jQuery("#yahoo, #first") ));
693 equal( jQuery("#sap").text(), expected, "Check for prepending of jQuery object" );
696 test("prepend(String|Element|Array<Element>|jQuery)", function() {
697 testPrepend(bareObj);
700 test("prepend(Function)", function() {
701 testPrepend(functionReturningObj);
704 test("prepend(Function) with incoming value", function() {
707 var defaultText = "Try them out:", old = jQuery("#first").html();
708 var result = jQuery("#first").prepend(function(i, val) {
709 equal( val, old, "Make sure the incoming value is correct." );
710 return "<b>buga</b>";
712 equal( result.text(), "buga" + defaultText, "Check if text prepending works" );
714 old = jQuery("#select3").html();
716 equal( jQuery("#select3").prepend(function(i, val) {
717 equal( val, old, "Make sure the incoming value is correct." );
718 return "<option value='prependTest'>Prepend Test</option>";
719 }).find("option:first-child").attr("value"), "prependTest", "Prepending html options to select element");
722 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
723 old = jQuery("#sap").html();
725 jQuery("#sap").prepend(function(i, val) {
726 equal( val, old, "Make sure the incoming value is correct." );
727 return document.getElementById("first");
730 equal( jQuery("#sap").text(), expected, "Check for prepending of element" );
733 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
734 old = jQuery("#sap").html();
736 jQuery("#sap").prepend(function(i, val) {
737 equal( val, old, "Make sure the incoming value is correct." );
738 return [document.getElementById("first"), document.getElementById("yahoo")];
741 equal( jQuery("#sap").text(), expected, "Check for prepending of array of elements" );
744 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
745 old = jQuery("#sap").html();
747 jQuery("#sap").prepend(function(i, val) {
748 equal( val, old, "Make sure the incoming value is correct." );
749 return jQuery("#yahoo, #first");
752 equal( jQuery("#sap").text(), expected, "Check for prepending of jQuery object" );
755 test("prependTo(String|Element|Array<Element>|jQuery)", function() {
757 var defaultText = "Try them out:"
758 jQuery("<b>buga</b>").prependTo("#first");
759 equal( jQuery("#first").text(), "buga" + defaultText, "Check if text prepending works" );
760 equal( jQuery("<option value='prependTest'>Prepend Test</option>").prependTo("#select3").parent().find("option:first-child").attr("value"), "prependTest", "Prepending html options to select element");
763 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
764 jQuery(document.getElementById("first")).prependTo("#sap");
765 equal( jQuery("#sap").text(), expected, "Check for prepending of element" );
768 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
769 jQuery([document.getElementById("first"), document.getElementById("yahoo")]).prependTo("#sap");
770 equal( jQuery("#sap").text(), expected, "Check for prepending of array of elements" );
773 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
774 jQuery("#yahoo, #first").prependTo("#sap");
775 equal( jQuery("#sap").text(), expected, "Check for prepending of jQuery object" );
778 jQuery("<select id='prependSelect1'></select>").prependTo("form:last");
779 jQuery("<select id='prependSelect2'><option>Test</option></select>").prependTo("form:last");
781 t( "Prepend Select", "#prependSelect2, #prependSelect1", ["prependSelect2", "prependSelect1"] );
784 var testBefore = function(val) {
786 var expected = "This is a normal link: bugaYahoo";
787 jQuery("#yahoo").before(val( "<b>buga</b>" ));
788 equal( jQuery("#en").text(), expected, "Insert String before" );
791 expected = "This is a normal link: Try them out:Yahoo";
792 jQuery("#yahoo").before(val( document.getElementById("first") ));
793 equal( jQuery("#en").text(), expected, "Insert element before" );
796 expected = "This is a normal link: Try them out:diveintomarkYahoo";
797 jQuery("#yahoo").before(val( [document.getElementById("first"), document.getElementById("mark")] ));
798 equal( jQuery("#en").text(), expected, "Insert array of elements before" );
801 expected = "This is a normal link: diveintomarkTry them out:Yahoo";
802 jQuery("#yahoo").before(val( jQuery("#mark, #first") ));
803 equal( jQuery("#en").text(), expected, "Insert jQuery before" );
805 var set = jQuery("<div/>").before("<span>test</span>");
806 equal( set[0].nodeName.toLowerCase(), "span", "Insert the element before the disconnected node." );
807 equal( set.length, 2, "Insert the element before the disconnected node." );
810 test("before(String|Element|Array<Element>|jQuery)", function() {
814 test("before(Function)", function() {
815 testBefore(functionReturningObj);
818 test("before and after w/ empty object (#10812)", function() {
821 var res = jQuery( "#notInTheDocument" ).before( "(" ).after( ")" );
822 equal( res.length, 2, "didn't choke on empty object" );
823 equal( res.wrapAll("<div/>").parent().text(), "()", "correctly appended text" );
826 test("insertBefore(String|Element|Array<Element>|jQuery)", function() {
828 var expected = "This is a normal link: bugaYahoo";
829 jQuery("<b>buga</b>").insertBefore("#yahoo");
830 equal( jQuery("#en").text(), expected, "Insert String before" );
833 expected = "This is a normal link: Try them out:Yahoo";
834 jQuery(document.getElementById("first")).insertBefore("#yahoo");
835 equal( jQuery("#en").text(), expected, "Insert element before" );
838 expected = "This is a normal link: Try them out:diveintomarkYahoo";
839 jQuery([document.getElementById("first"), document.getElementById("mark")]).insertBefore("#yahoo");
840 equal( jQuery("#en").text(), expected, "Insert array of elements before" );
843 expected = "This is a normal link: diveintomarkTry them out:Yahoo";
844 jQuery("#mark, #first").insertBefore("#yahoo");
845 equal( jQuery("#en").text(), expected, "Insert jQuery before" );
848 var testAfter = function(val) {
850 var expected = "This is a normal link: Yahoobuga";
851 jQuery("#yahoo").after(val( "<b>buga</b>" ));
852 equal( jQuery("#en").text(), expected, "Insert String after" );
855 expected = "This is a normal link: YahooTry them out:";
856 jQuery("#yahoo").after(val( document.getElementById("first") ));
857 equal( jQuery("#en").text(), expected, "Insert element after" );
860 expected = "This is a normal link: YahooTry them out:diveintomark";
861 jQuery("#yahoo").after(val( [document.getElementById("first"), document.getElementById("mark")] ));
862 equal( jQuery("#en").text(), expected, "Insert array of elements after" );
865 expected = "This is a normal link: YahoodiveintomarkTry them out:";
866 jQuery("#yahoo").after(val( jQuery("#mark, #first") ));
867 equal( jQuery("#en").text(), expected, "Insert jQuery after" );
869 var set = jQuery("<div/>").after("<span>test</span>");
870 equal( set[1].nodeName.toLowerCase(), "span", "Insert the element after the disconnected node." );
871 equal( set.length, 2, "Insert the element after the disconnected node." );
874 test("after(String|Element|Array<Element>|jQuery)", function() {
878 test("after(Function)", function() {
879 testAfter(functionReturningObj);
882 test("insertAfter(String|Element|Array<Element>|jQuery)", function() {
884 var expected = "This is a normal link: Yahoobuga";
885 jQuery("<b>buga</b>").insertAfter("#yahoo");
886 equal( jQuery("#en").text(), expected, "Insert String after" );
889 expected = "This is a normal link: YahooTry them out:";
890 jQuery(document.getElementById("first")).insertAfter("#yahoo");
891 equal( jQuery("#en").text(), expected, "Insert element after" );
894 expected = "This is a normal link: YahooTry them out:diveintomark";
895 jQuery([document.getElementById("first"), document.getElementById("mark")]).insertAfter("#yahoo");
896 equal( jQuery("#en").text(), expected, "Insert array of elements after" );
899 expected = "This is a normal link: YahoodiveintomarkTry them out:";
900 jQuery("#mark, #first").insertAfter("#yahoo");
901 equal( jQuery("#en").text(), expected, "Insert jQuery after" );
904 var testReplaceWith = function(val) {
906 jQuery("#yahoo").replaceWith(val( "<b id='replace'>buga</b>" ));
907 ok( jQuery("#replace")[0], "Replace element with string" );
908 ok( !jQuery("#yahoo")[0], "Verify that original element is gone, after string" );
911 jQuery("#yahoo").replaceWith(val( document.getElementById("first") ));
912 ok( jQuery("#first")[0], "Replace element with element" );
913 ok( !jQuery("#yahoo")[0], "Verify that original element is gone, after element" );
916 jQuery("#qunit-fixture").append("<div id='bar'><div id='baz'</div></div>");
917 jQuery("#baz").replaceWith("Baz");
918 equal( jQuery("#bar").text(),"Baz", "Replace element with text" );
919 ok( !jQuery("#baz")[0], "Verify that original element is gone, after element" );
922 jQuery("#yahoo").replaceWith(val( [document.getElementById("first"), document.getElementById("mark")] ));
923 ok( jQuery("#first")[0], "Replace element with array of elements" );
924 ok( jQuery("#mark")[0], "Replace element with array of elements" );
925 ok( !jQuery("#yahoo")[0], "Verify that original element is gone, after array of elements" );
928 jQuery("#yahoo").replaceWith(val( jQuery("#mark, #first") ));
929 ok( jQuery("#first")[0], "Replace element with set of elements" );
930 ok( jQuery("#mark")[0], "Replace element with set of elements" );
931 ok( !jQuery("#yahoo")[0], "Verify that original element is gone, after set of elements" );
934 var tmp = jQuery("<div/>").appendTo("body").click(function(){ ok(true, "Newly bound click run." ); });
935 var y = jQuery("<div/>").appendTo("body").click(function(){ ok(true, "Previously bound click run." ); });
936 var child = y.append("<b>test</b>").find("b").click(function(){ ok(true, "Child bound click run." ); return false; });
938 y.replaceWith( tmp );
941 y.click(); // Shouldn't be run
942 child.click(); // Shouldn't be run
950 y = jQuery("<div/>").appendTo("body").click(function(){ ok(true, "Previously bound click run." ); });
951 var child2 = y.append("<u>test</u>").find("u").click(function(){ ok(true, "Child 2 bound click run." ); return false; });
953 y.replaceWith( child2 );
962 var set = jQuery("<div/>").replaceWith(val("<span>test</span>"));
963 equal( set[0].nodeName.toLowerCase(), "span", "Replace the disconnected node." );
964 equal( set.length, 1, "Replace the disconnected node." );
966 var non_existant = jQuery("#does-not-exist").replaceWith( val("<b>should not throw an error</b>") );
967 equal( non_existant.length, 0, "Length of non existant element." );
969 var $div = jQuery("<div class='replacewith'></div>").appendTo("body");
970 // TODO: Work on jQuery(...) inline script execution
971 //$div.replaceWith("<div class='replacewith'></div><script>" +
972 //"equal(jQuery('.replacewith').length, 1, 'Check number of elements in page.');" +
974 equal(jQuery(".replacewith").length, 1, "Check number of elements in page.");
975 jQuery(".replacewith").remove();
979 jQuery("#qunit-fixture").append("<div id='replaceWith'></div>");
980 equal( jQuery("#qunit-fixture").find("div[id=replaceWith]").length, 1, "Make sure only one div exists." );
982 jQuery("#replaceWith").replaceWith( val("<div id='replaceWith'></div>") );
983 equal( jQuery("#qunit-fixture").find("div[id=replaceWith]").length, 1, "Make sure only one div exists." );
985 jQuery("#replaceWith").replaceWith( val("<div id='replaceWith'></div>") );
986 equal( jQuery("#qunit-fixture").find("div[id=replaceWith]").length, 1, "Make sure only one div exists." );
989 test("replaceWith(String|Element|Array<Element>|jQuery)", function() {
990 testReplaceWith(bareObj);
993 test("replaceWith(Function)", function() {
994 testReplaceWith(functionReturningObj);
998 var y = jQuery("#yahoo")[0];
1000 jQuery(y).replaceWith(function(){
1001 equal( this, y, "Make sure the context is coming in correctly." );
1007 test("replaceWith(string) for more than one element", function(){
1010 equal(jQuery("#foo p").length, 3, "ensuring that test data has not changed");
1012 jQuery("#foo p").replaceWith("<span>bar</span>");
1013 equal(jQuery("#foo span").length, 3, "verify that all the three original element have been replaced");
1014 equal(jQuery("#foo p").length, 0, "verify that all the three original element have been replaced");
1017 test("replaceAll(String|Element|Array<Element>|jQuery)", function() {
1019 jQuery("<b id='replace'>buga</b>").replaceAll("#yahoo");
1020 ok( jQuery("#replace")[0], "Replace element with string" );
1021 ok( !jQuery("#yahoo")[0], "Verify that original element is gone, after string" );
1024 jQuery(document.getElementById("first")).replaceAll("#yahoo");
1025 ok( jQuery("#first")[0], "Replace element with element" );
1026 ok( !jQuery("#yahoo")[0], "Verify that original element is gone, after element" );
1029 jQuery([document.getElementById("first"), document.getElementById("mark")]).replaceAll("#yahoo");
1030 ok( jQuery("#first")[0], "Replace element with array of elements" );
1031 ok( jQuery("#mark")[0], "Replace element with array of elements" );
1032 ok( !jQuery("#yahoo")[0], "Verify that original element is gone, after array of elements" );
1035 jQuery("#mark, #first").replaceAll("#yahoo");
1036 ok( jQuery("#first")[0], "Replace element with set of elements" );
1037 ok( jQuery("#mark")[0], "Replace element with set of elements" );
1038 ok( !jQuery("#yahoo")[0], "Verify that original element is gone, after set of elements" );
1041 test("jQuery.clone() (#8017)", function() {
1045 ok( jQuery.clone && jQuery.isFunction( jQuery.clone ) , "jQuery.clone() utility exists and is a function.");
1047 var main = jQuery("#qunit-fixture")[0],
1048 clone = jQuery.clone( main );
1050 equal( main.childNodes.length, clone.childNodes.length, "Simple child length to ensure a large dom tree copies correctly" );
1053 test("clone() (#8070)", function () {
1056 jQuery("<select class='test8070'></select><select class='test8070'></select>").appendTo("#qunit-fixture");
1057 var selects = jQuery(".test8070");
1058 selects.append("<OPTION>1</OPTION><OPTION>2</OPTION>");
1060 equal( selects[0].childNodes.length, 2, "First select got two nodes" );
1061 equal( selects[1].childNodes.length, 2, "Second select got two nodes" );
1066 test("clone()", function() {
1068 equal( "This is a normal link: Yahoo", jQuery("#en").text(), "Assert text for #en" );
1069 var clone = jQuery("#yahoo").clone();
1070 equal( "Try them out:Yahoo", jQuery("#first").append(clone).text(), "Check for clone" );
1071 equal( "This is a normal link: Yahoo", jQuery("#en").text(), "Reassert text for #en" );
1074 "<table/>", "<tr/>", "<td/>", "<div/>",
1075 "<button/>", "<ul/>", "<ol/>", "<li/>",
1076 "<input type='checkbox' />", "<select/>", "<option/>", "<textarea/>",
1077 "<tbody/>", "<thead/>", "<tfoot/>", "<iframe/>"
1079 for (var i = 0; i < cloneTags.length; i++) {
1080 var j = jQuery(cloneTags[i]);
1081 equal( j[0].tagName, j.clone()[0].tagName, "Clone a " + cloneTags[i]);
1084 // using contents will get comments regular, text, and comment nodes
1085 var cl = jQuery("#nonnodes").contents().clone();
1086 ok( cl.length >= 2, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
1088 var div = jQuery("<div><ul><li>test</li></ul></div>").click(function(){
1089 ok( true, "Bound event still exists." );
1092 clone = div.clone(true);
1094 // manually clean up detached elements
1097 div = clone.clone(true);
1099 // manually clean up detached elements
1102 equal( div.length, 1, "One element cloned" );
1103 equal( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
1104 div.trigger("click");
1106 // manually clean up detached elements
1109 div = jQuery("<div/>").append([ document.createElement("table"), document.createElement("table") ]);
1110 div.find("table").click(function(){
1111 ok( true, "Bound event still exists." );
1114 clone = div.clone(true);
1115 equal( clone.length, 1, "One element cloned" );
1116 equal( clone[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
1117 clone.find("table:last").trigger("click");
1119 // manually clean up detached elements
1123 var divEvt = jQuery("<div><ul><li>test</li></ul></div>").click(function(){
1124 ok( false, "Bound event still exists after .clone()." );
1126 cloneEvt = divEvt.clone();
1128 // Make sure that doing .clone() doesn't clone events
1129 cloneEvt.trigger("click");
1134 // Test both html() and clone() for <embed and <object types
1135 div = jQuery("<div/>").html('<embed height="355" width="425" src="http://www.youtube.com/v/3KANI2dpXLw&hl=en"></embed>');
1137 clone = div.clone(true);
1138 equal( clone.length, 1, "One element cloned" );
1139 equal( clone.html(), div.html(), "Element contents cloned" );
1140 equal( clone[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
1142 // this is technically an invalid object, but because of the special
1143 // classid instantiation it is the only kind that IE has trouble with,
1144 // so let's test with it too.
1145 div = jQuery("<div/>").html("<object height='355' width='425' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'> <param name='movie' value='http://www.youtube.com/v/3KANI2dpXLw&hl=en'> <param name='wmode' value='transparent'> </object>");
1147 clone = div.clone(true);
1148 equal( clone.length, 1, "One element cloned" );
1149 // equal( clone.html(), div.html(), "Element contents cloned" );
1150 equal( clone[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
1152 // and here's a valid one.
1153 div = jQuery("<div/>").html("<object height='355' width='425' type='application/x-shockwave-flash' data='http://www.youtube.com/v/3KANI2dpXLw&hl=en'> <param name='movie' value='http://www.youtube.com/v/3KANI2dpXLw&hl=en'> <param name='wmode' value='transparent'> </object>");
1155 clone = div.clone(true);
1156 equal( clone.length, 1, "One element cloned" );
1157 equal( clone.html(), div.html(), "Element contents cloned" );
1158 equal( clone[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
1160 div = jQuery("<div/>").data({ a: true });
1161 clone = div.clone(true);
1162 equal( clone.data("a"), true, "Data cloned." );
1163 clone.data("a", false);
1164 equal( clone.data("a"), false, "Ensure cloned element data object was correctly modified" );
1165 equal( div.data("a"), true, "Ensure cloned element data object is copied, not referenced" );
1167 // manually clean up detached elements
1171 var form = document.createElement("form");
1172 form.action = "/test/";
1173 var div = document.createElement("div");
1174 div.appendChild( document.createTextNode("test") );
1175 form.appendChild( div );
1177 equal( jQuery(form).clone().children().length, 1, "Make sure we just get the form back." );
1179 equal( jQuery("body").clone().children()[0].id, "qunit-header", "Make sure cloning body works" );
1182 test("clone(script type=non-javascript) (#11359)", function() {
1184 var src = jQuery("<script type='text/filler'>Lorem ipsum dolor sit amet</script><q><script type='text/filler'>consectetur adipiscing elit</script></q>");
1185 var dest = src.clone();
1186 equal( dest[0].text, "Lorem ipsum dolor sit amet", "Cloning preserves script text" );
1187 equal( dest.last().html(), src.last().html(), "Cloning preserves nested script text" );
1188 ok( /^\s*<scr.pt\s+type=['"]?text\/filler['"]?\s*>consectetur adipiscing elit<\/scr.pt>\s*$/i.test( dest.last().html() ), "Cloning preserves nested script text" );
1191 test("clone(form element) (Bug #3879, #6655)", function() {
1193 var element = jQuery("<select><option>Foo</option><option selected>Bar</option></select>");
1195 equal( element.clone().find("option:selected").val(), element.find("option:selected").val(), "Selected option cloned correctly" );
1197 element = jQuery("<input type='checkbox' value='foo'>").attr("checked", "checked");
1198 clone = element.clone();
1200 equal( clone.is(":checked"), element.is(":checked"), "Checked input cloned correctly" );
1201 equal( clone[0].defaultValue, "foo", "Checked input defaultValue cloned correctly" );
1203 // defaultChecked also gets set now due to setAttribute in attr, is this check still valid?
1204 // equal( clone[0].defaultChecked, !jQuery.support.noCloneChecked, "Checked input defaultChecked cloned correctly" );
1206 element = jQuery("<input type='text' value='foo'>");
1207 clone = element.clone();
1208 equal( clone[0].defaultValue, "foo", "Text input defaultValue cloned correctly" );
1210 element = jQuery("<textarea>foo</textarea>");
1211 clone = element.clone();
1212 equal( clone[0].defaultValue, "foo", "Textarea defaultValue cloned correctly" );
1215 test("clone(multiple selected options) (Bug #8129)", function() {
1217 var element = jQuery("<select><option>Foo</option><option selected>Bar</option><option selected>Baz</option></select>");
1219 equal( element.clone().find("option:selected").length, element.find("option:selected").length, "Multiple selected options cloned correctly" );
1224 test("clone() on XML nodes", function() {
1227 jQuery.get("data/dashboard.xml", function (xml) {
1228 var root = jQuery(xml.documentElement).clone();
1229 var origTab = jQuery("tab", xml).eq(0);
1230 var cloneTab = jQuery("tab", root).eq(0);
1231 origTab.text("origval");
1232 cloneTab.text("cloneval");
1233 equal(origTab.text(), "origval", "Check original XML node was correctly set");
1234 equal(cloneTab.text(), "cloneval", "Check cloned XML node was correctly set");
1240 test("clone() on local XML nodes with html5 nodename", function() {
1243 var $xmlDoc = jQuery( jQuery.parseXML( "<root><meter /></root>" ) ),
1244 $meter = $xmlDoc.find( "meter" ).clone();
1246 equal( $meter[0].nodeName, "meter", "Check if nodeName was not changed due to cloning" );
1247 equal( $meter[0].nodeType, 1, "Check if nodeType is not changed due to cloning" );
1250 test("html(undefined)", function() {
1252 equal( jQuery("#foo").html("<i>test</i>").html(undefined).html().toLowerCase(), "<i>test</i>", ".html(undefined) is chainable (#5571)" );
1255 var testHtml = function(valueObj) {
1258 jQuery.scriptorder = 0;
1260 var div = jQuery("#qunit-fixture > div");
1261 div.html(valueObj("<b>test</b>"));
1263 for ( var i = 0; i < div.size(); i++ ) {
1264 if ( div.get(i).childNodes.length != 1 ) pass = false;
1266 ok( pass, "Set HTML" );
1268 div = jQuery("<div/>").html( valueObj("<div id='parent_1'><div id='child_1'/></div><div id='parent_2'/>") );
1270 equal( div.children().length, 2, "Make sure two child nodes exist." );
1271 equal( div.children().children().length, 1, "Make sure that a grandchild exists." );
1273 var space = jQuery("<div/>").html(valueObj(" "))[0].innerHTML;
1274 ok( /^\xA0$|^ $/.test( space ), "Make sure entities are passed through correctly." );
1275 equal( jQuery("<div/>").html(valueObj("&"))[0].innerHTML, "&", "Make sure entities are passed through correctly." );
1277 jQuery("#qunit-fixture").html(valueObj("<style>.foobar{color:green;}</style>"));
1279 equal( jQuery("#qunit-fixture").children().length, 1, "Make sure there is a child element." );
1280 equal( jQuery("#qunit-fixture").children()[0].nodeName.toUpperCase(), "STYLE", "And that a style element was inserted." );
1283 // using contents will get comments regular, text, and comment nodes
1284 var j = jQuery("#nonnodes").contents();
1285 j.html(valueObj("<b>bold</b>"));
1287 // this is needed, or the expando added by jQuery unique will yield a different html
1288 j.find("b").removeData();
1289 equal( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
1291 jQuery("#qunit-fixture").html(valueObj("<select/>"));
1292 jQuery("#qunit-fixture select").html(valueObj("<option>O1</option><option selected='selected'>O2</option><option>O3</option>"));
1293 equal( jQuery("#qunit-fixture select").val(), "O2", "Selected option correct" );
1295 var $div = jQuery("<div />");
1296 equal( $div.html(valueObj( 5 )).html(), "5", "Setting a number as html" );
1297 equal( $div.html(valueObj( 0 )).html(), "0", "Setting a zero as html" );
1299 var $div2 = jQuery("<div/>"), insert = "<div>hello1</div>";
1300 equal( $div2.html(insert).html().replace(/>/g, ">"), insert, "Verify escaped insertion." );
1301 equal( $div2.html("x" + insert).html().replace(/>/g, ">"), "x" + insert, "Verify escaped insertion." );
1302 equal( $div2.html(" " + insert).html().replace(/>/g, ">"), " " + insert, "Verify escaped insertion." );
1304 var map = jQuery("<map/>").html(valueObj("<area id='map01' shape='rect' coords='50,50,150,150' href='http://www.jquery.com/' alt='jQuery'>"));
1306 equal( map[0].childNodes.length, 1, "The area was inserted." );
1307 equal( map[0].firstChild.nodeName.toLowerCase(), "area", "The area was inserted." );
1311 jQuery("#qunit-fixture").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><script type='type/ecmascript'>ok( true, 'type/ecmascript evaluated.' );</script></div>"));
1313 var child = jQuery("#qunit-fixture").find("script");
1315 equal( child.length, 2, "Make sure that two non-JavaScript script tags are left." );
1316 equal( child[0].type, "something/else", "Verify type of script tag." );
1317 equal( child[1].type, "something/else", "Verify type of script tag." );
1319 jQuery("#qunit-fixture").html(valueObj("<script>ok( true, 'Test repeated injection of script.' );</script>"));
1320 jQuery("#qunit-fixture").html(valueObj("<script>ok( true, 'Test repeated injection of script.' );</script>"));
1321 jQuery("#qunit-fixture").html(valueObj("<script>ok( true, 'Test repeated injection of script.' );</script>"));
1323 jQuery("#qunit-fixture").html(valueObj("<script type='text/javascript'>ok( true, 'jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975 (1)' );</script>"));
1325 jQuery("#qunit-fixture").html(valueObj("foo <form><script type='text/javascript'>ok( true, 'jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975 (2)' );</script></form>"));
1327 jQuery("#qunit-fixture").html(valueObj("<script>equal(jQuery.scriptorder++, 0, 'Script is executed in order');equal(jQuery('#scriptorder').length, 1,'Execute after html (even though appears before)')<\/script><span id='scriptorder'><script>equal(jQuery.scriptorder++, 1, 'Script (nested) is executed in order');equal(jQuery('#scriptorder').length, 1,'Execute after html')<\/script></span><script>equal(jQuery.scriptorder++, 2, 'Script (unnested) is executed in order');equal(jQuery('#scriptorder').length, 1,'Execute after html')<\/script>"));
1330 test("html(String)", function() {
1334 test("html(Function)", function() {
1335 testHtml(functionReturningObj);
1341 jQuery("#qunit-fixture").html(function(){
1342 return jQuery(this).text();
1345 ok( !/</.test( jQuery("#qunit-fixture").html() ), "Replace html with text." );
1346 ok( jQuery("#qunit-fixture").html().length > 0, "Make sure text exists." );
1349 test("html(Function) with incoming value", function() {
1352 var div = jQuery("#qunit-fixture > div"), old = div.map(function(){ return jQuery(this).html() });
1354 div.html(function(i, val) {
1355 equal( val, old[i], "Make sure the incoming value is correct." );
1356 return "<b>test</b>";
1360 div.each(function(){
1361 if ( this.childNodes.length !== 1 ) {
1365 ok( pass, "Set HTML" );
1368 // using contents will get comments regular, text, and comment nodes
1369 var j = jQuery("#nonnodes").contents();
1370 old = j.map(function(){ return jQuery(this).html(); });
1372 j.html(function(i, val) {
1373 equal( val, old[i], "Make sure the incoming value is correct." );
1374 return "<b>bold</b>";
1377 // Handle the case where no comment is in the document
1378 if ( j.length === 2 ) {
1379 equal( null, null, "Make sure the incoming value is correct." );
1382 j.find("b").removeData();
1383 equal( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
1385 var $div = jQuery("<div />");
1387 equal( $div.html(function(i, val) {
1388 equal( val, "", "Make sure the incoming value is correct." );
1390 }).html(), "5", "Setting a number as html" );
1392 equal( $div.html(function(i, val) {
1393 equal( val, "5", "Make sure the incoming value is correct." );
1395 }).html(), "0", "Setting a zero as html" );
1397 var $div2 = jQuery("<div/>"), insert = "<div>hello1</div>";
1398 equal( $div2.html(function(i, val) {
1399 equal( val, "", "Make sure the incoming value is correct." );
1401 }).html().replace(/>/g, ">"), insert, "Verify escaped insertion." );
1403 equal( $div2.html(function(i, val) {
1404 equal( val.replace(/>/g, ">"), insert, "Make sure the incoming value is correct." );
1405 return "x" + insert;
1406 }).html().replace(/>/g, ">"), "x" + insert, "Verify escaped insertion." );
1408 equal( $div2.html(function(i, val) {
1409 equal( val.replace(/>/g, ">"), "x" + insert, "Make sure the incoming value is correct." );
1410 return " " + insert;
1411 }).html().replace(/>/g, ">"), " " + insert, "Verify escaped insertion." );
1414 var testRemove = function(method) {
1417 var first = jQuery("#ap").children(":first");
1418 first.data("foo", "bar");
1420 jQuery("#ap").children()[method]();
1421 ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
1422 equal( jQuery("#ap").children().length, 0, "Check remove" );
1424 equal( first.data("foo"), method == "remove" ? null : "bar" );
1427 jQuery("#ap").children()[method]("a");
1428 ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
1429 equal( jQuery("#ap").children().length, 1, "Check filtered remove" );
1431 jQuery("#ap").children()[method]("a, code");
1432 equal( jQuery("#ap").children().length, 0, "Check multi-filtered remove" );
1434 // using contents will get comments regular, text, and comment nodes
1435 // Handle the case where no comment is in the document
1436 ok( jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment remove works" );
1437 jQuery("#nonnodes").contents()[method]();
1438 equal( jQuery("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" );
1440 // manually clean up detached elements
1441 if (method === "detach") {
1448 var first = jQuery("#ap").children(":first");
1449 var cleanUp = first.click(function() { count++ })[method]().appendTo("#qunit-fixture").click();
1451 equal( method == "remove" ? 0 : 1, count );
1453 // manually clean up detached elements
1457 test("remove()", function() {
1458 testRemove("remove");
1461 test("detach()", function() {
1462 testRemove("detach");
1465 test("empty()", function() {
1467 equal( jQuery("#ap").children().empty().text().length, 0, "Check text is removed" );
1468 equal( jQuery("#ap").children().length, 4, "Check elements are not removed" );
1470 // using contents will get comments regular, text, and comment nodes
1471 var j = jQuery("#nonnodes").contents();
1473 equal( j.html(), "", "Check node,textnode,comment empty works" );
1476 test("jQuery.cleanData", function() {
1479 var type, pos, div, child;
1483 // Should trigger 4 remove event
1484 div = getDiv().remove();
1486 // Should both do nothing
1488 div.trigger("click");
1491 div.children().trigger("click");
1495 child = div.children();
1497 // Should trigger 2 remove event
1502 div.trigger("click");
1504 // Should do nothing
1506 child.trigger("click");
1514 child = div.children();
1516 // Should trigger 2 remove event
1517 div.html("<div></div>");
1521 div.trigger("click");
1523 // Should do nothing
1525 child.trigger("click");
1531 var div = jQuery("<div class='outer'><div class='inner'></div></div>").click(function(){
1532 ok( true, type + " " + pos + " Click event fired." );
1533 }).focus(function(){
1534 ok( true, type + " " + pos + " Focus event fired." );
1535 }).find("div").click(function(){
1536 ok( false, type + " " + pos + " Click event fired." );
1537 }).focus(function(){
1538 ok( false, type + " " + pos + " Focus event fired." );
1539 }).end().appendTo("body");
1541 div[0].detachEvent = div[0].removeEventListener = function(t){
1542 ok( true, type + " Outer " + t + " event unbound" );
1545 div[0].firstChild.detachEvent = div[0].firstChild.removeEventListener = function(t){
1546 ok( true, type + " Inner " + t + " event unbound" );
1553 test("jQuery.buildFragment - no plain-text caching (Bug #6779)", function() {
1556 // DOM manipulation fails if added text matches an Object method
1557 var $f = jQuery( "<div />" ).appendTo( "#qunit-fixture" ),
1558 bad = [ "start-", "toString", "hasOwnProperty", "append", "here&there!", "-end" ];
1560 for ( var i=0; i < bad.length; i++ ) {
1562 $f.append( bad[i] );
1566 equal($f.text(), bad.join(""), "Cached strings that match Object properties");
1570 test( "jQuery.html - execute scripts escaped with html comment or CDATA (#9221)", function() {
1573 '<script type="text/javascript">',
1575 'ok( true, "<!-- handled" );',
1578 ].join ( "\n" ) ).appendTo( "#qunit-fixture" );
1580 '<script type="text/javascript">',
1582 'ok( true, "<![CDATA[ handled" );',
1585 ].join ( "\n" ) ).appendTo( "#qunit-fixture" );
1587 '<script type="text/javascript">',
1588 '<!--//--><![CDATA[//><!--',
1589 'ok( true, "<!--//--><![CDATA[//><!-- (Drupal case) handled" );',
1592 ].join ( "\n" ) ).appendTo( "#qunit-fixture" );
1595 test("jQuery.buildFragment - plain objects are not a document #8950", function() {
1599 jQuery('<input type="hidden">', {});
1600 ok( true, "Does not allow attribute object to be treated like a doc object");
1605 test("jQuery.clone - no exceptions for object elements #9587", function() {
1609 jQuery("#no-clone-exception").clone();
1610 ok( true, "cloned with no exceptions" );
1612 ok( false, e.message );
1616 test("jQuery(<tag>) & wrap[Inner/All]() handle unknown elems (#10667)", function() {
1619 var $wraptarget = jQuery( "<div id='wrap-target'>Target</div>" ).appendTo( "#qunit-fixture" ),
1620 $section = jQuery( "<section>" ).appendTo( "#qunit-fixture" );
1622 $wraptarget.wrapAll("<aside style='background-color:green'></aside>");
1624 notEqual( $wraptarget.parent("aside").css("background-color"), "transparent", "HTML5 elements created with wrapAll inherit styles" );
1625 notEqual( $section.css("background-color"), "transparent", "HTML5 elements create with jQuery( string ) inherit styles" );
1628 test("Cloned, detached HTML5 elems (#10667,10670)", function() {
1631 var $section = jQuery( "<section>" ).appendTo( "#qunit-fixture" ),
1635 $clone = $section.clone();
1637 // Infer that the test is being run in IE<=8
1638 if ( $clone[0].outerHTML && !jQuery.support.opacity ) {
1639 // This branch tests cloning nodes by reading the outerHTML, used only in IE<=8
1640 equal( $clone[0].outerHTML, "<section></section>", "detached clone outerHTML matches '<section></section>'" );
1642 // This branch tests a known behaviour in modern browsers that should never fail.
1643 // Included for expected test count symmetry (expecting 1)
1644 equal( $clone[0].nodeName, "SECTION", "detached clone nodeName matches 'SECTION' in modern browsers" );
1648 $section.bind( "click", function( event ) {
1649 ok( true, "clone fired event" );
1652 // Second clone (will have an event bound)
1653 $clone = $section.clone( true );
1655 // Trigger an event from the first clone
1656 $clone.trigger( "click" );
1657 $clone.unbind( "click" );
1659 // Add a child node with text to the original
1660 $section.append( "<p>Hello</p>" );
1662 // Third clone (will have child node and text)
1663 $clone = $section.clone( true );
1665 equal( $clone.find("p").text(), "Hello", "Assert text in child of clone" );
1667 // Trigger an event from the third clone
1668 $clone.trigger( "click" );
1669 $clone.unbind( "click" );
1671 // Add attributes to copy
1673 "class": "foo bar baz",
1674 "title": "This is a title"
1677 // Fourth clone (will have newly added attributes)
1678 $clone = $section.clone( true );
1680 equal( $clone.attr("class"), $section.attr("class"), "clone and element have same class attribute" );
1681 equal( $clone.attr("title"), $section.attr("title"), "clone and element have same title attribute" );
1683 // Remove the original
1687 $section = $clone.clone( true );
1692 // Trigger an event from the clone of the clone
1693 $section.trigger( "click" );
1695 // Unbind any remaining events
1696 $section.unbind( "click" );
1697 $clone.unbind( "click" );
1700 test("jQuery.fragments cache expectations", function() {
1704 jQuery.fragments = {};
1706 function fragmentCacheSize() {
1709 for ( c in jQuery.fragments ) {
1715 jQuery("<li></li>");
1716 jQuery("<li>?</li>");
1717 jQuery("<li>whip</li>");
1718 jQuery("<li>it</li>");
1719 jQuery("<li>good</li>");
1720 jQuery("<div></div>");
1721 jQuery("<div><div><span></span></div></div>");
1722 jQuery("<tr><td></td></tr>");
1723 jQuery("<tr><td></tr>");
1724 jQuery("<li>aaa</li>");
1725 jQuery("<ul><li>?</li></ul>");
1726 jQuery("<div><p>arf</p>nnn</div>");
1727 jQuery("<div><p>dog</p>?</div>");
1728 jQuery("<span><span>");
1730 equal( fragmentCacheSize(), 12, "12 entries exist in jQuery.fragments, 1" );
1733 "<tr><td></td></tr>",
1734 "<ul><li>?</li></ul>",
1735 "<div><p>dog</p>?</div>",
1737 ], function( i, frag ) {
1741 equal( jQuery.fragments[ frag ].nodeType, 11, "Second call with " + frag + " creates a cached DocumentFragment, has nodeType 11" );
1742 ok( jQuery.fragments[ frag ].childNodes.length, "Second call with " + frag + " creates a cached DocumentFragment, has childNodes with length" );
1745 equal( fragmentCacheSize(), 12, "12 entries exist in jQuery.fragments, 2" );
1748 test("Guard against exceptions when clearing safeChildNodes", function() {
1754 div = jQuery("<div/><hr/><code/><b/>");
1757 ok( div && div.jquery, "Created nodes safely, guarded against exceptions on safeChildNodes[ -1 ]" );