1 module( "manipulation", {
2 teardown: moduleTeardown
5 // Ensure that an extended Array prototype doesn't break jQuery
6 Array.prototype.arrayProtoFn = function() {
7 throw("arrayProtoFn should not be called");
10 function manipulationBareObj( value ) {
14 function manipulationFunctionReturningObj( value ) {
21 ======== local reference =======
22 manipulationBareObj and manipulationFunctionReturningObj can be used to test passing functions to setters
23 See testVal below for an example
26 This function returns whatever value is passed in
28 functionReturningObj( value );
29 Returns a function that returns the value
32 test( "text()", function() {
36 var expected, frag, $newLineTest;
38 expected = "This link has class=\"blog\": Simon Willison's Weblog";
39 equal( jQuery("#sap").text(), expected, "Check for merged text of more then one element." );
41 // Check serialization of text values
42 equal( jQuery(document.createTextNode("foo")).text(), "foo", "Text node was retrieved from .text()." );
43 notEqual( jQuery(document).text(), "", "Retrieving text for the document retrieves all text (#10724)." );
45 // Retrieve from document fragments #10864
46 frag = document.createDocumentFragment();
47 frag.appendChild( document.createTextNode("foo") );
49 equal( jQuery(frag).text(), "foo", "Document Fragment Text node was retrieved from .text()." );
51 $newLineTest = jQuery("<div>test<br/>testy</div>").appendTo("#moretests");
52 $newLineTest.find("br").replaceWith("\n");
53 equal( $newLineTest.text(), "test\ntesty", "text() does not remove new lines (#11153)" );
55 $newLineTest.remove();
58 test( "text(undefined)", function() {
62 equal( jQuery("#foo").text("<div").text(undefined)[ 0 ].innerHTML, "<div", ".text(undefined) is chainable (#5571)" );
65 function testText( valueObj ) {
69 var val, j, expected, $multipleElements, $parentDiv, $childDiv;
71 val = valueObj("<div><b>Hello</b> cruel world!</div>");
72 equal( jQuery("#foo").text(val)[ 0 ].innerHTML.replace(/>/g, ">"), "<div><b>Hello</b> cruel world!</div>", "Check escaped text" );
74 // using contents will get comments regular, text, and comment nodes
75 j = jQuery("#nonnodes").contents();
76 j.text( valueObj("hi!") );
77 equal( jQuery( j[ 0 ] ).text(), "hi!", "Check node,textnode,comment with text()" );
78 equal( j[ 1 ].nodeValue, " there ", "Check node,textnode,comment with text()" );
80 equal( j[ 2 ].nodeType, 8, "Check node,textnode,comment with text()" );
82 // Update multiple elements #11809
85 $multipleElements = jQuery( "<div>Hello</div>" ).add( "<div>World</div>" );
86 $multipleElements.text( expected );
88 equal( $multipleElements.eq(0).text(), expected, "text() updates multiple elements (#11809)" );
89 equal( $multipleElements.eq(1).text(), expected, "text() updates multiple elements (#11809)" );
91 // Prevent memory leaks #11809
92 $childDiv = jQuery( "<div/>" );
93 $childDiv.data("leak", true);
94 $parentDiv = jQuery( "<div/>" );
95 $parentDiv.append( $childDiv );
96 $parentDiv.text("Dry off");
99 test( "text(String)", function() {
100 testText( manipulationBareObj );
103 test( "text(Function)", function() {
104 testText( manipulationFunctionReturningObj );
107 test( "text(Function) with incoming value", function() {
111 var old = "This link has class=\"blog\": Simon Willison's Weblog";
113 jQuery("#sap").text(function( i, val ) {
114 equal( val, old, "Make sure the incoming value is correct." );
118 equal( jQuery("#sap").text(), "foobar", "Check for merged text of more then one element." );
121 function testAppendForObject( valueObj, isFragment ) {
123 type = isFragment ? " (DocumentFragment)" : " (Element)",
124 text = "This link has class=\"blog\": Simon Willison's Weblog",
125 el = document.getElementById("sap").cloneNode( true ),
126 first = document.getElementById("first"),
127 yahoo = document.getElementById("yahoo");
130 $base = document.createDocumentFragment();
131 jQuery( el ).contents().each(function() {
132 $base.appendChild( this );
134 $base = jQuery( $base );
136 $base = jQuery( el );
139 equal( $base.clone().append( valueObj(first.cloneNode(true)) ).text(),
140 text + "Try them out:",
141 "Check for appending of element" + type
144 equal( $base.clone().append( valueObj([ first.cloneNode(true), yahoo.cloneNode(true) ]) ).text(),
145 text + "Try them out:Yahoo",
146 "Check for appending of array of elements" + type
149 equal( $base.clone().append( valueObj(jQuery("#yahoo, #first").clone()) ).text(),
150 text + "YahooTry them out:",
151 "Check for appending of jQuery object" + type
154 equal( $base.clone().append( valueObj( 5 ) ).text(),
156 "Check for appending a number" + type
159 equal( $base.clone().append( valueObj([ jQuery("#first").clone(), jQuery("#yahoo, #google").clone() ]) ).text(),
160 text + "Try them out:GoogleYahoo",
161 "Check for appending of array of jQuery objects"
164 equal( $base.clone().append( valueObj(" text with spaces ") ).text(),
165 text + " text with spaces ",
166 "Check for appending text with spaces" + type
169 equal( $base.clone().append( valueObj([]) ).text(),
171 "Check for appending an empty array" + type
174 equal( $base.clone().append( valueObj("") ).text(),
176 "Check for appending an empty string" + type
179 equal( $base.clone().append( valueObj(document.getElementsByTagName("foo")) ).text(),
181 "Check for appending an empty nodelist" + type
184 equal( $base.clone().append( "<span></span>", "<span></span>", "<span></span>" ).children().length,
185 $base.children().length + 3,
186 "Make sure that multiple arguments works." + type
189 equal( $base.clone().append( valueObj(document.getElementById("form").cloneNode(true)) ).children("form").length,
191 "Check for appending a form (#910)" + type
195 function testAppend( valueObj ) {
199 testAppendForObject( valueObj, false );
200 testAppendForObject( valueObj, true );
202 var defaultText, result, message, iframe, iframeDoc, j, d,
203 $input, $radioChecked, $radioUnchecked, $radioParent, $map, $table;
205 defaultText = "Try them out:";
206 result = jQuery("#first").append( valueObj("<b>buga</b>") );
208 equal( result.text(), defaultText + "buga", "Check if text appending works" );
209 equal( jQuery("#select3").append( valueObj("<option value='appendTest'>Append Test</option>") ).find("option:last-child").attr("value"), "appendTest", "Appending html options to select element" );
211 jQuery("#qunit-fixture form").append( valueObj("<input name='radiotest' type='radio' checked='checked' />") );
212 jQuery("#qunit-fixture form input[name=radiotest]").each(function() {
213 ok( jQuery(this).is(":checked"), "Append checked radio" );
216 jQuery("#qunit-fixture form").append( valueObj("<input name='radiotest2' type='radio' checked = 'checked' />") );
217 jQuery("#qunit-fixture form input[name=radiotest2]").each(function() {
218 ok( jQuery(this).is(":checked"), "Append alternately formated checked radio" );
221 jQuery("#qunit-fixture form").append( valueObj("<input name='radiotest3' type='radio' checked />") );
222 jQuery("#qunit-fixture form input[name=radiotest3]").each(function() {
223 ok( jQuery(this).is(":checked"), "Append HTML5-formated checked radio" );
226 jQuery("#qunit-fixture form").append( valueObj("<input type='radio' checked='checked' name='radiotest4' />") );
227 jQuery("#qunit-fixture form input[name=radiotest4]").each(function() {
228 ok( jQuery(this).is(":checked"), "Append with name attribute after checked attribute" );
231 message = "Test for appending a DOM node to the contents of an iframe";
232 iframe = jQuery("#iframe")[ 0 ];
233 iframeDoc = iframe.contentDocument || iframe.contentWindow && iframe.contentWindow.document;
236 if ( iframeDoc && iframeDoc.body ) {
237 equal( jQuery(iframeDoc.body).append( valueObj("<div id='success'>test</div>") )[ 0 ].lastChild.id, "success", message );
239 ok( true, message + " - can't test" );
242 strictEqual( e.message || e, undefined, message );
245 jQuery("<fieldset/>").appendTo("#form").append( valueObj("<legend id='legend'>test</legend>") );
246 t( "Append legend", "#legend", [ "legend" ] );
248 $map = jQuery("<map/>").append( valueObj("<area id='map01' shape='rect' coords='50,50,150,150' href='http://www.jquery.com/' alt='jQuery'>") );
250 equal( $map[ 0 ].childNodes.length, 1, "The area was inserted." );
251 equal( $map[ 0 ].firstChild.nodeName.toLowerCase(), "area", "The area was inserted." );
253 jQuery("#select1").append( valueObj("<OPTION>Test</OPTION>") );
254 equal( jQuery("#select1 option:last-child").text(), "Test", "Appending OPTION (all caps)" );
256 jQuery("#select1").append( valueObj("<optgroup label='optgroup'><option>optgroup</option></optgroup>") );
257 equal( jQuery("#select1 optgroup").attr("label"), "optgroup", "Label attribute in newly inserted optgroup is correct" );
258 equal( jQuery("#select1 option").last().text(), "optgroup", "Appending optgroup" );
260 $table = jQuery("#table");
262 jQuery.each( "thead tbody tfoot colgroup caption tr th td".split(" "), function( i, name ) {
263 $table.append( valueObj( "<" + name + "/>" ) );
264 equal( $table.find( name ).length, 1, "Append " + name );
265 ok( jQuery.parseHTML( "<" + name + "/>" ).length, name + " wrapped correctly" );
268 jQuery("#table colgroup").append( valueObj("<col/>") );
269 equal( jQuery("#table colgroup col").length, 1, "Append col" );
272 .append( valueObj("<select id='appendSelect1'></select>") )
273 .append( valueObj("<select id='appendSelect2'><option>Test</option></select>") );
274 t( "Append Select", "#appendSelect1, #appendSelect2", [ "appendSelect1", "appendSelect2" ] );
276 equal( "Two nodes", jQuery("<div />").append( "Two", " nodes" ).text(), "Appending two text nodes (#4011)" );
277 equal( jQuery("<div />").append( "1", "", 3 ).text(), "13", "If median is false-like value, subsequent arguments should not be ignored" );
279 // using contents will get comments regular, text, and comment nodes
280 j = jQuery("#nonnodes").contents();
281 d = jQuery("<div/>").appendTo("#nonnodes").append( j );
283 equal( jQuery("#nonnodes").length, 1, "Check node,textnode,comment append moved leaving just the div" );
284 equal( d.contents().length, 3, "Check node,textnode,comment append works" );
285 d.contents().appendTo("#nonnodes");
287 equal( jQuery("#nonnodes").contents().length, 3, "Check node,textnode,comment append cleanup worked" );
289 $input = jQuery("<input type='checkbox'/>").prop( "checked", true ).appendTo("#testForm");
290 equal( $input[ 0 ].checked, true, "A checked checkbox that is appended stays checked" );
292 $radioChecked = jQuery("input[type='radio'][name='R1']").eq( 1 );
293 $radioParent = $radioChecked.parent();
294 $radioUnchecked = jQuery("<input type='radio' name='R1' checked='checked'/>").appendTo( $radioParent );
295 $radioChecked.trigger("click");
296 $radioUnchecked[ 0 ].checked = false;
298 jQuery("<div/>").insertBefore($radioParent).append($radioParent);
300 equal( $radioChecked[ 0 ].checked, true, "Reappending radios uphold which radio is checked" );
301 equal( $radioUnchecked[ 0 ].checked, false, "Reappending radios uphold not being checked" );
303 equal( jQuery("<div/>").append( valueObj("option<area/>") )[ 0 ].childNodes.length, 2, "HTML-string with leading text should be processed correctly" );
306 test( "append(String|Element|Array<Element>|jQuery)", function() {
307 testAppend( manipulationBareObj );
310 test( "append(Function)", function() {
311 testAppend( manipulationFunctionReturningObj );
314 test( "append(param) to object, see #11280", function() {
318 var object = jQuery( document.createElement("object") ).appendTo( document.body );
320 equal( object.children().length, 0, "object does not start with children" );
322 object.append( jQuery("<param type='wmode' name='foo'>") );
323 equal( object.children().length, 1, "appended param" );
324 equal( object.children().eq(0).attr("name"), "foo", "param has name=foo" );
326 object = jQuery("<object><param type='baz' name='bar'></object>");
327 equal( object.children().length, 1, "object created with child param" );
328 equal( object.children().eq(0).attr("name"), "bar", "param has name=bar" );
331 test( "append(Function) returns String", function() {
335 var defaultText, result, select, old;
337 defaultText = "Try them out:";
338 old = jQuery("#first").html();
340 result = jQuery("#first").append(function( i, val ) {
341 equal( val, old, "Make sure the incoming value is correct." );
342 return "<b>buga</b>";
344 equal( result.text(), defaultText + "buga", "Check if text appending works" );
346 select = jQuery("#select3");
349 equal( select.append(function( i, val ) {
350 equal( val, old, "Make sure the incoming value is correct." );
351 return "<option value='appendTest'>Append Test</option>";
352 }).find("option:last-child").attr("value"), "appendTest", "Appending html options to select element" );
355 test( "append(Function) returns Element", function() {
358 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:",
359 old = jQuery("#sap").html();
361 jQuery("#sap").append(function( i, val ) {
362 equal( val, old, "Make sure the incoming value is correct." );
363 return document.getElementById("first");
365 equal( jQuery("#sap").text(), expected, "Check for appending of element" );
368 test( "append(Function) returns Array<Element>", function() {
371 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo",
372 old = jQuery("#sap").html();
374 jQuery("#sap").append(function( i, val ) {
375 equal( val, old, "Make sure the incoming value is correct." );
376 return [ document.getElementById("first"), document.getElementById("yahoo") ];
378 equal( jQuery("#sap").text(), expected, "Check for appending of array of elements" );
381 test( "append(Function) returns jQuery", function() {
384 var expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:",
385 old = jQuery("#sap").html();
387 jQuery("#sap").append(function( i, val ) {
388 equal( val, old, "Make sure the incoming value is correct." );
389 return jQuery("#yahoo, #first");
391 equal( jQuery("#sap").text(), expected, "Check for appending of jQuery object" );
394 test( "append(Function) returns Number", function() {
397 var old = jQuery("#sap").html();
399 jQuery("#sap").append(function( i, val ) {
400 equal( val, old, "Make sure the incoming value is correct." );
403 ok( jQuery("#sap")[ 0 ].innerHTML.match( /5$/ ), "Check for appending a number" );
406 test( "XML DOM manipulation (#9960)", function() {
411 xmlDoc1 = jQuery.parseXML("<scxml xmlns='http://www.w3.org/2005/07/scxml' version='1.0'><state x='100' y='100' initial='actions' id='provisioning'></state><state x='100' y='100' id='error'></state><state x='100' y='100' id='finished' final='true'></state></scxml>"),
412 xmlDoc2 = jQuery.parseXML("<scxml xmlns='http://www.w3.org/2005/07/scxml' version='1.0'><state id='provisioning3'></state></scxml>"),
413 xml1 = jQuery( xmlDoc1 ),
414 xml2 = jQuery( xmlDoc2 ),
415 scxml1 = jQuery( "scxml", xml1 ),
416 scxml2 = jQuery( "scxml", xml2 ),
417 state = scxml2.find("state");
419 // Android 2.3 doesn't automatically adopt nodes from foreign documents.
420 // Although technically this is compliant behavior, no other browser
421 // (including newer Android Browsers) behave in this way so do the adopting
422 // just for Android 2.3.
423 // Support: Android 2.3
424 if ( /android 2\.3/i.test( navigator.userAgent ) ) {
425 state = jQuery( xmlDoc1.adoptNode( state[ 0 ] ) );
428 scxml1.append( state );
429 strictEqual( scxml1[0].lastChild, state[0], "append" );
431 scxml1.prepend( state );
432 strictEqual( scxml1[0].firstChild, state[0], "prepend" );
434 scxml1.find("#finished").after( state );
435 strictEqual( scxml1[0].lastChild, state[0], "after" );
437 scxml1.find("#provisioning").before( state );
438 strictEqual( scxml1[0].firstChild, state[0], "before" );
440 // Support: Android 2.3
441 if ( /android 2\.3/i.test( navigator.userAgent ) ) {
442 scxml1Adopted = jQuery( xmlDoc2.adoptNode( scxml1[ 0 ] ) );
443 scxml2.replaceWith( scxml1Adopted );
445 scxml2.replaceWith( scxml1 );
447 deepEqual( jQuery( "state", xml2 ).get(), scxml1.find("state").get(), "replaceWith" );
450 test( "append HTML5 sectioning elements (Bug #6485)", function() {
456 jQuery("#qunit-fixture").append("<article style='font-size:10px'><section><aside>HTML5 elements</aside></section></article>");
458 article = jQuery("article");
459 aside = jQuery("aside");
461 equal( article.get( 0 ).style.fontSize, "10px", "HTML5 elements are styleable" );
462 equal( aside.length, 1, "HTML5 elements do not collapse their children" );
466 test( "HTML5 Elements inherit styles from style rules (Bug #10501)", function() {
470 jQuery("#qunit-fixture").append("<article id='article'></article>");
471 jQuery("#article").append("<section>This section should have a pink background.</section>");
473 // In IE, the missing background color will claim its value is "transparent"
474 notEqual( jQuery("section").css("background-color"), "transparent", "HTML5 elements inherit styles" );
478 test( "html(String) with HTML5 (Bug #6485)", function() {
482 jQuery("#qunit-fixture").html("<article><section><aside>HTML5 elements</aside></section></article>");
483 equal( jQuery("#qunit-fixture").children().children().length, 1, "Make sure HTML5 article elements can hold children. innerHTML shortcut path" );
484 equal( jQuery("#qunit-fixture").children().children().children().length, 1, "Make sure nested HTML5 elements can hold children." );
487 test( "html(String) tag-hyphenated elements (Bug #1987)", function() {
491 jQuery.each( "thead tbody tfoot colgroup caption tr th td".split(" "), function( i, name ) {
492 var j = jQuery("<" + name + "-d></" + name + "-d><" + name + "-d></" + name + "-d>");
493 ok( j[0], "Create a tag-hyphenated element" );
494 ok( jQuery.nodeName(j[0], name.toUpperCase() + "-D"), "Hyphenated node name" );
495 ok( jQuery.nodeName(j[1], name.toUpperCase() + "-D"), "Hyphenated node name" );
498 var j = jQuery("<tr-multiple-hyphens><td-with-hyphen>text</td-with-hyphen></tr-multiple-hyphens>");
499 ok( jQuery.nodeName(j[0], "TR-MULTIPLE-HYPHENS"), "Tags with multiple hypens" );
500 ok( jQuery.nodeName(j.children()[0], "TD-WITH-HYPHEN"), "Tags with multiple hypens" );
501 equal( j.children().text(), "text", "Tags with multple hypens behave normally" );
504 test( "IE8 serialization bug", function() {
507 var wrapper = jQuery("<div></div>");
509 wrapper.html("<div></div><article></article>");
510 equal( wrapper.children("article").length, 1, "HTML5 elements are insertable with .html()" );
512 wrapper.html("<div></div><link></link>");
513 equal( wrapper.children("link").length, 1, "Link elements are insertable with .html()" );
516 test( "html() object element #10324", function() {
520 var object = jQuery("<object id='object2'><param name='object2test' value='test'></param></object>?").appendTo("#qunit-fixture"),
521 clone = object.clone();
523 equal( clone.html(), object.html(), "html() returns correct innerhtml of cloned object elements" );
526 test( "append(xml)", function() {
530 var xmlDoc, xml1, xml2;
532 function createXMLDoc() {
533 // Initialize DOM based upon latest installed MSXML or Netscape
536 [ "MSXML6.DomDocument",
537 "MSXML3.DomDocument",
538 "MSXML2.DomDocument",
540 "Microsoft.XmlDom" ];
542 if ( document.implementation && "createDocument" in document.implementation ) {
543 return document.implementation.createDocument( "", "", null );
546 for ( n = 0, len = aActiveX.length; n < len; n++ ) {
548 elem = new ActiveXObject( aActiveX[ n ] );
555 xmlDoc = createXMLDoc();
556 xml1 = xmlDoc.createElement("head");
557 xml2 = xmlDoc.createElement("test");
559 ok( jQuery(xml1).append(xml2), "Append an xml element to another without raising an exception." );
563 test( "appendTo(String)", function() {
569 defaultText = "Try them out:";
570 jQuery("<b>buga</b>").appendTo("#first");
571 equal( jQuery("#first").text(), defaultText + "buga", "Check if text appending works" );
572 equal( jQuery("<option value='appendTest'>Append Test</option>").appendTo("#select3").parent().find("option:last-child").attr("value"), "appendTest", "Appending html options to select element" );
574 l = jQuery("#first").children().length + 2;
575 jQuery("<strong>test</strong>");
576 jQuery("<strong>test</strong>");
577 jQuery([ jQuery("<strong>test</strong>")[ 0 ], jQuery("<strong>test</strong>")[ 0 ] ])
579 equal( jQuery("#first").children().length, l, "Make sure the elements were inserted." );
580 equal( jQuery("#first").children().last()[ 0 ].nodeName.toLowerCase(), "strong", "Verify the last element." );
583 test( "appendTo(Element|Array<Element>)", function() {
587 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
588 jQuery( document.getElementById("first") ).appendTo("#sap");
589 equal( jQuery("#sap").text(), expected, "Check for appending of element" );
591 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
592 jQuery([ document.getElementById("first"), document.getElementById("yahoo") ]).appendTo("#sap");
593 equal( jQuery("#sap").text(), expected, "Check for appending of array of elements" );
597 test( "appendTo(jQuery)", function() {
601 var expected, num, div;
602 ok( jQuery(document.createElement("script")).appendTo("body").length, "Make sure a disconnected script can be appended." );
604 expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
605 jQuery("#yahoo, #first").appendTo("#sap");
606 equal( jQuery("#sap").text(), expected, "Check for appending of jQuery object" );
608 jQuery("#select1").appendTo("#foo");
609 t( "Append select", "#foo select", [ "select1" ] );
611 div = jQuery("<div/>").on( "click", function() {
612 ok( true, "Running a cloned click." );
614 div.appendTo("#qunit-fixture, #moretests");
616 jQuery("#qunit-fixture div").last().trigger("click");
617 jQuery("#moretests div").last().trigger("click");
619 div = jQuery("<div/>").appendTo("#qunit-fixture, #moretests");
621 equal( div.length, 2, "appendTo returns the inserted elements" );
623 div.addClass("test");
625 ok( jQuery("#qunit-fixture div").last().hasClass("test"), "appendTo element was modified after the insertion" );
626 ok( jQuery("#moretests div").last().hasClass("test"), "appendTo element was modified after the insertion" );
628 div = jQuery("<div/>");
629 jQuery("<span>a</span><b>b</b>").filter("span").appendTo( div );
631 equal( div.children().length, 1, "Make sure the right number of children were inserted." );
633 div = jQuery("#moretests div");
635 num = jQuery("#qunit-fixture div").length;
636 div.remove().appendTo("#qunit-fixture");
638 equal( jQuery("#qunit-fixture div").length, num, "Make sure all the removed divs were inserted." );
641 test( "prepend(String)", function() {
645 var result, expected;
646 expected = "Try them out:";
647 result = jQuery("#first").prepend( "<b>buga</b>" );
648 equal( result.text(), "buga" + expected, "Check if text prepending works" );
649 equal( jQuery("#select3").prepend( "<option value='prependTest'>Prepend Test</option>" ).find("option:first-child").attr("value"), "prependTest", "Prepending html options to select element" );
652 test( "prepend(Element)", function() {
657 expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
658 jQuery("#sap").prepend( document.getElementById("first") );
659 equal( jQuery("#sap").text(), expected, "Check for prepending of element" );
662 test( "prepend(Array<Element>)", function() {
667 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
668 jQuery("#sap").prepend( [ document.getElementById("first"), document.getElementById("yahoo") ] );
669 equal( jQuery("#sap").text(), expected, "Check for prepending of array of elements" );
672 test( "prepend(jQuery)", function() {
677 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
678 jQuery("#sap").prepend( jQuery("#yahoo, #first") );
679 equal( jQuery("#sap").text(), expected, "Check for prepending of jQuery object" );
682 test( "prepend(Array<jQuery>)", function() {
687 expected = "Try them out:GoogleYahooThis link has class=\"blog\": Simon Willison's Weblog";
688 jQuery("#sap").prepend( [ jQuery("#first"), jQuery("#yahoo, #google") ] );
689 equal( jQuery("#sap").text(), expected, "Check for prepending of array of jQuery objects" );
692 test( "prepend(Function) with incoming value -- String", function() {
696 var defaultText, old, result;
698 defaultText = "Try them out:";
699 old = jQuery("#first").html();
700 result = jQuery("#first").prepend(function( i, val ) {
701 equal( val, old, "Make sure the incoming value is correct." );
702 return "<b>buga</b>";
705 equal( result.text(), "buga" + defaultText, "Check if text prepending works" );
707 old = jQuery("#select3").html();
709 equal( jQuery("#select3").prepend(function( i, val ) {
710 equal( val, old, "Make sure the incoming value is correct." );
711 return "<option value='prependTest'>Prepend Test</option>";
712 }).find("option:first-child").attr("value"), "prependTest", "Prepending html options to select element" );
715 test( "prepend(Function) with incoming value -- Element", function() {
720 expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
721 old = jQuery("#sap").html();
723 jQuery("#sap").prepend(function( i, val ) {
724 equal( val, old, "Make sure the incoming value is correct." );
725 return document.getElementById("first");
728 equal( jQuery("#sap").text(), expected, "Check for prepending of element" );
731 test( "prepend(Function) with incoming value -- Array<Element>", function() {
736 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
737 old = jQuery("#sap").html();
739 jQuery("#sap").prepend(function( i, val ) {
740 equal( val, old, "Make sure the incoming value is correct." );
741 return [ document.getElementById("first"), document.getElementById("yahoo") ];
744 equal( jQuery("#sap").text(), expected, "Check for prepending of array of elements" );
747 test( "prepend(Function) with incoming value -- jQuery", function() {
752 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
753 old = jQuery("#sap").html();
755 jQuery("#sap").prepend(function( i, val ) {
756 equal( val, old, "Make sure the incoming value is correct." );
757 return jQuery("#yahoo, #first");
760 equal( jQuery("#sap").text(), expected, "Check for prepending of jQuery object" );
763 test( "prependTo(String)", function() {
769 defaultText = "Try them out:";
770 jQuery("<b>buga</b>").prependTo("#first");
771 equal( jQuery("#first").text(), "buga" + defaultText, "Check if text prepending works" );
772 equal( jQuery("<option value='prependTest'>Prepend Test</option>").prependTo("#select3").parent().find("option:first-child").attr("value"), "prependTest", "Prepending html options to select element" );
776 test( "prependTo(Element)", function() {
782 expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
783 jQuery( document.getElementById("first") ).prependTo("#sap");
784 equal( jQuery("#sap").text(), expected, "Check for prepending of element" );
787 test( "prependTo(Array<Element>)", function() {
793 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
794 jQuery( [ document.getElementById("first"), document.getElementById("yahoo") ] ).prependTo("#sap");
795 equal( jQuery("#sap").text(), expected, "Check for prepending of array of elements" );
798 test( "prependTo(jQuery)", function() {
804 expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
805 jQuery("#yahoo, #first").prependTo("#sap");
806 equal( jQuery("#sap").text(), expected, "Check for prepending of jQuery object" );
809 test( "prependTo(Array<jQuery>)", function() {
813 jQuery("<select id='prependSelect1'></select>").prependTo("#form");
814 jQuery("<select id='prependSelect2'><option>Test</option></select>").prependTo("#form");
816 t( "Prepend Select", "#prependSelect2, #prependSelect1", [ "prependSelect2", "prependSelect1" ] );
819 test( "before(String)", function() {
825 expected = "This is a normal link: bugaYahoo";
826 jQuery("#yahoo").before( manipulationBareObj("<b>buga</b>") );
827 equal( jQuery("#en").text(), expected, "Insert String before" );
830 test( "before(Element)", function() {
836 expected = "This is a normal link: Try them out:Yahoo";
837 jQuery("#yahoo").before( manipulationBareObj(document.getElementById("first")) );
838 equal( jQuery("#en").text(), expected, "Insert element before" );
841 test( "before(Array<Element>)", function() {
846 expected = "This is a normal link: Try them out:diveintomarkYahoo";
847 jQuery("#yahoo").before( manipulationBareObj([ document.getElementById("first"), document.getElementById("mark") ]) );
848 equal( jQuery("#en").text(), expected, "Insert array of elements before" );
851 test( "before(jQuery)", function() {
856 expected = "This is a normal link: diveintomarkTry them out:Yahoo";
857 jQuery("#yahoo").before( manipulationBareObj(jQuery("#mark, #first")) );
858 equal( jQuery("#en").text(), expected, "Insert jQuery before" );
861 test( "before(Array<jQuery>)", function() {
866 expected = "This is a normal link: Try them out:GooglediveintomarkYahoo";
867 jQuery("#yahoo").before( manipulationBareObj([ jQuery("#first"), jQuery("#mark, #google") ]) );
868 equal( jQuery("#en").text(), expected, "Insert array of jQuery objects before" );
871 test( "before(Function) -- Returns String", function() {
877 expected = "This is a normal link: bugaYahoo";
878 jQuery("#yahoo").before( manipulationFunctionReturningObj("<b>buga</b>") );
879 equal( jQuery("#en").text(), expected, "Insert String before" );
882 test( "before(Function) -- Returns Element", function() {
888 expected = "This is a normal link: Try them out:Yahoo";
889 jQuery("#yahoo").before( manipulationFunctionReturningObj(document.getElementById("first")) );
890 equal( jQuery("#en").text(), expected, "Insert element before" );
893 test( "before(Function) -- Returns Array<Element>", function() {
898 expected = "This is a normal link: Try them out:diveintomarkYahoo";
899 jQuery("#yahoo").before( manipulationFunctionReturningObj([ document.getElementById("first"), document.getElementById("mark") ]) );
900 equal( jQuery("#en").text(), expected, "Insert array of elements before" );
903 test( "before(Function) -- Returns jQuery", function() {
908 expected = "This is a normal link: diveintomarkTry them out:Yahoo";
909 jQuery("#yahoo").before( manipulationFunctionReturningObj(jQuery("#mark, #first")) );
910 equal( jQuery("#en").text(), expected, "Insert jQuery before" );
913 test( "before(Function) -- Returns Array<jQuery>", function() {
918 expected = "This is a normal link: Try them out:GooglediveintomarkYahoo";
919 jQuery("#yahoo").before( manipulationFunctionReturningObj([ jQuery("#first"), jQuery("#mark, #google") ]) );
920 equal( jQuery("#en").text(), expected, "Insert array of jQuery objects before" );
923 test( "before(no-op)", function() {
928 set = jQuery("<div/>").before("<span>test</span>");
929 equal( set[ 0 ].nodeName.toLowerCase(), "div", "Insert before a disconnected node should be a no-op" );
930 equal( set.length, 1, "Insert the element before the disconnected node. should be a no-op" );
933 test( "before and after w/ empty object (#10812)", function() {
939 res = jQuery( "#notInTheDocument" ).before( "(" ).after( ")" );
940 equal( res.length, 0, "didn't choke on empty object" );
943 test( ".before() and .after() disconnected node", function() {
947 equal( jQuery("<input type='checkbox'/>").before("<div/>").length, 1, "before() on disconnected node is no-op" );
948 equal( jQuery("<input type='checkbox'/>").after("<div/>").length, 1, "after() on disconnected node is no-op" );
951 test( "insert with .before() on disconnected node last", function() {
955 var expectedBefore = "This is a normal link: bugaYahoo";
957 jQuery("#yahoo").add("<span/>").before("<b>buga</b>");
958 equal( jQuery("#en").text(), expectedBefore, "Insert String before with disconnected node last" );
961 test( "insert with .before() on disconnected node first", function() {
965 var expectedBefore = "This is a normal link: bugaYahoo";
967 jQuery("<span/>").add("#yahoo").before("<b>buga</b>");
968 equal( jQuery("#en").text(), expectedBefore, "Insert String before with disconnected node first" );
971 test( "insert with .before() on disconnected node last", function() {
975 var expectedAfter = "This is a normal link: Yahoobuga";
977 jQuery("#yahoo").add("<span/>").after("<b>buga</b>");
978 equal( jQuery("#en").text(), expectedAfter, "Insert String after with disconnected node last" );
981 test( "insert with .before() on disconnected node last", function() {
985 var expectedAfter = "This is a normal link: Yahoobuga";
987 jQuery("<span/>").add("#yahoo").after("<b>buga</b>");
988 equal( jQuery("#en").text(), expectedAfter, "Insert String after with disconnected node first" );
991 test( "insertBefore(String)", function() {
995 var expected = "This is a normal link: bugaYahoo";
996 jQuery("<b>buga</b>").insertBefore("#yahoo");
997 equal( jQuery("#en").text(), expected, "Insert String before" );
1000 test( "insertBefore(Element)", function() {
1004 var expected = "This is a normal link: Try them out:Yahoo";
1005 jQuery( document.getElementById("first") ).insertBefore("#yahoo");
1006 equal( jQuery("#en").text(), expected, "Insert element before" );
1009 test( "insertBefore(Array<Element>)", function() {
1013 var expected = "This is a normal link: Try them out:diveintomarkYahoo";
1014 jQuery( [ document.getElementById("first"), document.getElementById("mark") ] ).insertBefore("#yahoo");
1015 equal( jQuery("#en").text(), expected, "Insert array of elements before" );
1018 test( "insertBefore(jQuery)", function() {
1022 var expected = "This is a normal link: diveintomarkTry them out:Yahoo";
1023 jQuery("#mark, #first").insertBefore("#yahoo");
1024 equal( jQuery("#en").text(), expected, "Insert jQuery before" );
1027 test( ".after(String)", function() {
1031 var expected = "This is a normal link: Yahoobuga";
1032 jQuery("#yahoo").after( "<b>buga</b>" );
1033 equal( jQuery("#en").text(), expected, "Insert String after" );
1036 test( ".after(Element)", function() {
1040 var expected = "This is a normal link: YahooTry them out:";
1041 jQuery("#yahoo").after( document.getElementById("first") );
1042 equal( jQuery("#en").text(), expected, "Insert element after" );
1045 test( ".after(Array<Element>)", function() {
1049 var expected = "This is a normal link: YahooTry them out:diveintomark";
1050 jQuery("#yahoo").after( [ document.getElementById("first"), document.getElementById("mark") ] );
1051 equal( jQuery("#en").text(), expected, "Insert array of elements after" );
1054 test( ".after(jQuery)", function() {
1058 var expected = "This is a normal link: YahooTry them out:Googlediveintomark";
1059 jQuery("#yahoo").after( [ jQuery("#first"), jQuery("#mark, #google") ] );
1060 equal( jQuery("#en").text(), expected, "Insert array of jQuery objects after" );
1063 test( ".after(Function) returns String", function() {
1067 var expected = "This is a normal link: Yahoobuga",
1068 val = manipulationFunctionReturningObj;
1069 jQuery("#yahoo").after( val("<b>buga</b>") );
1070 equal( jQuery("#en").text(), expected, "Insert String after" );
1073 test( ".after(Function) returns Element", function() {
1077 var expected = "This is a normal link: YahooTry them out:",
1078 val = manipulationFunctionReturningObj;
1079 jQuery("#yahoo").after( val(document.getElementById("first")) );
1080 equal( jQuery("#en").text(), expected, "Insert element after" );
1083 test( ".after(Function) returns Array<Element>", function() {
1087 var expected = "This is a normal link: YahooTry them out:diveintomark",
1088 val = manipulationFunctionReturningObj;
1089 jQuery("#yahoo").after( val([ document.getElementById("first"), document.getElementById("mark") ]) );
1090 equal( jQuery("#en").text(), expected, "Insert array of elements after" );
1093 test( ".after(Function) returns jQuery", function() {
1097 var expected = "This is a normal link: YahooTry them out:Googlediveintomark",
1098 val = manipulationFunctionReturningObj;
1099 jQuery("#yahoo").after( val([ jQuery("#first"), jQuery("#mark, #google") ]) );
1100 equal( jQuery("#en").text(), expected, "Insert array of jQuery objects after" );
1103 test( ".after(disconnected node)", function() {
1107 var set = jQuery("<div/>").before("<span>test</span>");
1108 equal( set[ 0 ].nodeName.toLowerCase(), "div", "Insert after a disconnected node should be a no-op" );
1109 equal( set.length, 1, "Insert the element after the disconnected node should be a no-op" );
1112 test( "insertAfter(String)", function() {
1116 var expected = "This is a normal link: Yahoobuga";
1117 jQuery("<b>buga</b>").insertAfter("#yahoo");
1118 equal( jQuery("#en").text(), expected, "Insert String after" );
1121 test( "insertAfter(Element)", function() {
1125 var expected = "This is a normal link: YahooTry them out:";
1126 jQuery( document.getElementById("first") ).insertAfter("#yahoo");
1127 equal( jQuery("#en").text(), expected, "Insert element after" );
1130 test( "insertAfter(Array<Element>)", function() {
1134 var expected = "This is a normal link: YahooTry them out:diveintomark";
1135 jQuery( [ document.getElementById("first"), document.getElementById("mark") ] ).insertAfter("#yahoo");
1136 equal( jQuery("#en").text(), expected, "Insert array of elements after" );
1139 test( "insertAfter(jQuery)", function() {
1143 var expected = "This is a normal link: YahoodiveintomarkTry them out:";
1144 jQuery("#mark, #first").insertAfter("#yahoo");
1145 equal( jQuery("#en").text(), expected, "Insert jQuery after" );
1148 function testReplaceWith( val ) {
1150 var tmp, y, child, child2, set, non_existent, $div,
1155 jQuery("#yahoo").replaceWith( val("<b id='replace'>buga</b>") );
1156 ok( jQuery("#replace")[ 0 ], "Replace element with element from string" );
1157 ok( !jQuery("#yahoo")[ 0 ], "Verify that original element is gone, after string" );
1159 jQuery("#anchor2").replaceWith( val(document.getElementById("first")) );
1160 ok( jQuery("#first")[ 0 ], "Replace element with element" );
1161 ok( !jQuery("#anchor2")[ 0 ], "Verify that original element is gone, after element" );
1163 jQuery("#qunit-fixture").append("<div id='bar'><div id='baz'></div></div>");
1164 jQuery("#baz").replaceWith( val("Baz") );
1165 equal( jQuery("#bar").text(),"Baz", "Replace element with text" );
1166 ok( !jQuery("#baz")[ 0 ], "Verify that original element is gone, after element" );
1168 jQuery("#bar").replaceWith( "<div id='yahoo'></div>", "...", "<div id='baz'></div>" );
1169 deepEqual( jQuery("#yahoo, #baz").get(), q( "yahoo", "baz" ), "Replace element with multiple arguments (#13722)" );
1170 strictEqual( jQuery("#yahoo")[0].nextSibling, jQuery("#baz")[0].previousSibling, "Argument order preserved" );
1171 deepEqual( jQuery("#bar").get(), [], "Verify that original element is gone, after multiple arguments" );
1173 jQuery("#google").replaceWith( val([ document.getElementById("first"), document.getElementById("mark") ]) );
1174 deepEqual( jQuery("#mark, #first").get(), q( "first", "mark" ), "Replace element with array of elements" );
1175 ok( !jQuery("#google")[ 0 ], "Verify that original element is gone, after array of elements" );
1177 jQuery("#groups").replaceWith( val(jQuery("#mark, #first")) );
1178 deepEqual( jQuery("#mark, #first").get(), q( "first", "mark" ), "Replace element with jQuery collection" );
1179 ok( !jQuery("#groups")[ 0 ], "Verify that original element is gone, after jQuery collection" );
1181 jQuery("#mark, #first").replaceWith( val("<span class='replacement'></span><span class='replacement'></span>") );
1182 equal( jQuery("#qunit-fixture .replacement").length, 4, "Replace multiple elements (#12449)" );
1183 deepEqual( jQuery("#mark, #first").get(), [], "Verify that original elements are gone, after replace multiple" );
1185 tmp = jQuery("<b>content</b>")[0];
1186 jQuery("#anchor1").contents().replaceWith( val(tmp) );
1187 deepEqual( jQuery("#anchor1").contents().get(), [ tmp ], "Replace text node with element" );
1190 tmp = jQuery("<div/>").appendTo("#qunit-fixture").on( "click", function() {
1191 ok( true, "Newly bound click run." );
1193 y = jQuery("<div/>").appendTo("#qunit-fixture").on( "click", function() {
1194 ok( false, "Previously bound click run." );
1196 child = y.append("<b>test</b>").find("b").on( "click", function() {
1197 ok( true, "Child bound click run." );
1201 y.replaceWith( val(tmp) );
1203 tmp.trigger("click");
1204 y.trigger("click"); // Shouldn't be run
1205 child.trigger("click"); // Shouldn't be run
1208 y = jQuery("<div/>").appendTo("#qunit-fixture").on( "click", function() {
1209 ok( false, "Previously bound click run." );
1211 child2 = y.append("<u>test</u>").find("u").on( "click", function() {
1212 ok( true, "Child 2 bound click run." );
1216 y.replaceWith( val(child2) );
1218 child2.trigger("click");
1221 set = jQuery("<div/>").replaceWith( val("<span>test</span>") );
1222 equal( set[0].nodeName.toLowerCase(), "div", "No effect on a disconnected node." );
1223 equal( set.length, 1, "No effect on a disconnected node." );
1224 equal( set[0].childNodes.length, 0, "No effect on a disconnected node." );
1227 child = jQuery("#qunit-fixture").children().first();
1228 $div = jQuery("<div class='pathological'/>").insertBefore( child );
1229 $div.replaceWith( $div );
1230 deepEqual( jQuery( ".pathological", "#qunit-fixture" ).get(), $div.get(),
1231 "Self-replacement" );
1232 $div.replaceWith( child );
1233 deepEqual( jQuery("#qunit-fixture").children().first().get(), child.get(),
1234 "Replacement with following sibling (#13810)" );
1235 deepEqual( jQuery( ".pathological", "#qunit-fixture" ).get(), [],
1236 "Replacement with following sibling (context removed)" );
1239 non_existent = jQuery("#does-not-exist").replaceWith( val("<b>should not throw an error</b>") );
1240 equal( non_existent.length, 0, "Length of non existent element." );
1242 $div = jQuery("<div class='replacewith'></div>").appendTo("#qunit-fixture");
1243 $div.replaceWith( val("<div class='replacewith'></div><script>" +
1244 "equal( jQuery('.replacewith').length, 1, 'Check number of elements in page.' );" +
1247 jQuery("#qunit-fixture").append("<div id='replaceWith'></div>");
1248 equal( jQuery("#qunit-fixture").find("div[id=replaceWith]").length, 1, "Make sure only one div exists." );
1249 jQuery("#replaceWith").replaceWith( val("<div id='replaceWith'></div>") );
1250 equal( jQuery("#qunit-fixture").find("div[id=replaceWith]").length, 1, "Make sure only one div exists after replacement." );
1251 jQuery("#replaceWith").replaceWith( val("<div id='replaceWith'></div>") );
1252 equal( jQuery("#qunit-fixture").find("div[id=replaceWith]").length, 1, "Make sure only one div exists after subsequent replacement." );
1257 test( "replaceWith(String|Element|Array<Element>|jQuery)", function() {
1258 testReplaceWith( manipulationBareObj );
1261 test( "replaceWith(Function)", function() {
1262 expect( testReplaceWith(manipulationFunctionReturningObj) + 1 );
1264 var y = jQuery("#foo")[ 0 ];
1266 jQuery( y ).replaceWith(function() {
1267 equal( this, y, "Make sure the context is coming in correctly." );
1271 test( "replaceWith(string) for more than one element", function() {
1275 equal( jQuery("#foo p").length, 3, "ensuring that test data has not changed" );
1277 jQuery("#foo p").replaceWith("<span>bar</span>");
1278 equal(jQuery("#foo span").length, 3, "verify that all the three original element have been replaced");
1279 equal(jQuery("#foo p").length, 0, "verify that all the three original element have been replaced");
1282 test( "Empty replaceWith (trac-13401; trac-13596; gh-2204)", function() {
1286 var $el = jQuery( "<div/><div/>" ).html( "<p>0</p>" ),
1287 expectedHTML = $el.html(),
1291 "array of empty string": [ "" ],
1292 "empty collection": jQuery( "#nonexistent" ),
1294 // in case of jQuery(...).replaceWith();
1295 "undefined": undefined
1298 jQuery.each( tests, function( label, input ) {
1299 $el.html( "<a/>" ).children().replaceWith( input );
1300 strictEqual( $el.html(), "", "replaceWith(" + label + ")" );
1301 $el.html( "<b/>" ).children().replaceWith(function() { return input; });
1302 strictEqual( $el.html(), "", "replaceWith(function returning " + label + ")" );
1303 $el.html( "<i/>" ).children().replaceWith(function( i ) { i; return input; });
1304 strictEqual( $el.html(), "", "replaceWith(other function returning " + label + ")" );
1305 $el.html( "<p/>" ).children().replaceWith(function( i ) {
1308 jQuery( this ).html( i + "" );
1310 strictEqual( $el.eq( 0 ).html(), expectedHTML,
1311 "replaceWith(function conditionally returning context)" );
1312 strictEqual( $el.eq( 1 ).html(), "",
1313 "replaceWith(function conditionally returning " + label + ")" );
1317 test( "replaceAll(String)", function() {
1321 jQuery("<b id='replace'>buga</b>").replaceAll("#yahoo");
1322 ok( jQuery("#replace")[ 0 ], "Replace element with string" );
1323 ok( !jQuery("#yahoo")[ 0 ], "Verify that original element is gone, after string" );
1326 test( "replaceAll(Element)", function() {
1330 jQuery( document.getElementById("first") ).replaceAll("#yahoo");
1331 ok( jQuery("#first")[ 0 ], "Replace element with element" );
1332 ok( !jQuery("#yahoo")[ 0 ], "Verify that original element is gone, after element" );
1335 test( "replaceAll(Array<Element>)", function() {
1339 jQuery( [ document.getElementById("first"), document.getElementById("mark") ] ).replaceAll("#yahoo");
1340 ok( jQuery("#first")[ 0 ], "Replace element with array of elements" );
1341 ok( jQuery("#mark")[ 0 ], "Replace element with array of elements" );
1342 ok( !jQuery("#yahoo")[ 0 ], "Verify that original element is gone, after array of elements" );
1345 test( "replaceAll(jQuery)", function() {
1349 jQuery("#mark, #first").replaceAll("#yahoo");
1350 ok( jQuery("#first")[ 0 ], "Replace element with set of elements" );
1351 ok( jQuery("#mark")[ 0 ], "Replace element with set of elements" );
1352 ok( !jQuery("#yahoo")[ 0 ], "Verify that original element is gone, after set of elements" );
1355 test( "jQuery.clone() (#8017)", function() {
1359 ok( jQuery.clone && jQuery.isFunction( jQuery.clone ) , "jQuery.clone() utility exists and is a function.");
1361 var main = jQuery("#qunit-fixture")[ 0 ],
1362 clone = jQuery.clone( main );
1364 equal( main.childNodes.length, clone.childNodes.length, "Simple child length to ensure a large dom tree copies correctly" );
1367 test( "append to multiple elements (#8070)", function() {
1371 var selects = jQuery("<select class='test8070'></select><select class='test8070'></select>").appendTo("#qunit-fixture");
1372 selects.append("<OPTION>1</OPTION><OPTION>2</OPTION>");
1374 equal( selects[ 0 ].childNodes.length, 2, "First select got two nodes" );
1375 equal( selects[ 1 ].childNodes.length, 2, "Second select got two nodes" );
1378 test( "table manipulation", function() {
1381 var table = jQuery("<table style='font-size:16px'></table>").appendTo("#qunit-fixture").empty(),
1382 height = table[0].offsetHeight;
1384 table.append("<tr><td>DATA</td></tr>");
1385 ok( table[0].offsetHeight - height >= 15, "appended rows are visible" );
1388 height = table[0].offsetHeight;
1389 table.prepend("<tr><td>DATA</td></tr>");
1390 ok( table[0].offsetHeight - height >= 15, "prepended rows are visible" );
1393 test( "clone()", function() {
1397 var div, clone, form, body;
1399 equal( jQuery("#en").text(), "This is a normal link: Yahoo", "Assert text for #en" );
1400 equal( jQuery("#first").append( jQuery("#yahoo").clone() ).text(), "Try them out:Yahoo", "Check for clone" );
1401 equal( jQuery("#en").text(), "This is a normal link: Yahoo", "Reassert text for #en" );
1403 jQuery.each( "table thead tbody tfoot tr td div button ul ol li select option textarea iframe".split(" "), function( i, nodeName ) {
1404 equal( jQuery( "<" + nodeName + "/>" ).clone()[ 0 ].nodeName.toLowerCase(), nodeName, "Clone a " + nodeName );
1406 equal( jQuery("<input type='checkbox' />").clone()[ 0 ].nodeName.toLowerCase(), "input", "Clone a <input type='checkbox' />" );
1408 // Check cloning non-elements
1409 equal( jQuery("#nonnodes").contents().clone().length, 3, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
1411 // Verify that clones of clones can keep event listeners
1412 div = jQuery("<div><ul><li>test</li></ul></div>").on( "click", function() {
1413 ok( true, "Bound event still exists." );
1415 clone = div.clone( true ); div.remove();
1416 div = clone.clone( true ); clone.remove();
1418 equal( div.length, 1, "One element cloned" );
1419 equal( div[ 0 ].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
1420 div.trigger("click");
1422 // Manually clean up detached elements
1425 // Verify that cloned children can keep event listeners
1426 div = jQuery("<div/>").append([ document.createElement("table"), document.createElement("table") ]);
1427 div.find("table").on( "click", function() {
1428 ok( true, "Bound event still exists." );
1431 clone = div.clone( true );
1432 equal( clone.length, 1, "One element cloned" );
1433 equal( clone[ 0 ].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
1434 clone.find("table").trigger("click");
1436 // Manually clean up detached elements
1440 // Make sure that doing .clone() doesn't clone event listeners
1441 div = jQuery("<div><ul><li>test</li></ul></div>").on( "click", function() {
1442 ok( false, "Bound event still exists after .clone()." );
1444 clone = div.clone();
1446 clone.trigger("click");
1448 // Manually clean up detached elements
1452 // Test both html() and clone() for <embed> and <object> types
1453 div = jQuery("<div/>").html("<embed height='355' width='425' src='http://www.youtube.com/v/3KANI2dpXLw&hl=en'></embed>");
1455 clone = div.clone( true );
1456 equal( clone.length, 1, "One element cloned" );
1457 equal( clone.html(), div.html(), "Element contents cloned" );
1458 equal( clone[ 0 ].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
1460 // this is technically an invalid object, but because of the special
1461 // classid instantiation it is the only kind that IE has trouble with,
1462 // so let's test with it too.
1463 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>");
1465 clone = div.clone( true );
1466 equal( clone.length, 1, "One element cloned" );
1467 equal( clone[ 0 ].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
1468 div = div.find("object");
1469 clone = clone.find("object");
1470 // oldIE adds extra attributes and <param> elements, so just test for existence of the defined set
1471 jQuery.each( [ "height", "width", "classid" ], function( i, attr ) {
1472 equal( clone.attr( attr ), div.attr( attr ), "<object> attribute cloned: " + attr );
1477 clone.find("param").each(function( index, param ) {
1478 params[ param.attributes.name.nodeValue.toLowerCase() ] =
1479 param.attributes.value.nodeValue.toLowerCase();
1482 div.find("param").each(function( index, param ) {
1483 var key = param.attributes.name.nodeValue.toLowerCase();
1484 equal( params[ key ], param.attributes.value.nodeValue.toLowerCase(), "<param> cloned: " + key );
1488 // and here's a valid one.
1489 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>");
1491 clone = div.clone(true);
1492 equal( clone.length, 1, "One element cloned" );
1493 equal( clone.html(), div.html(), "Element contents cloned" );
1494 equal( clone[ 0 ].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
1496 div = jQuery("<div/>").data({ "a": true });
1497 clone = div.clone( true );
1498 equal( clone.data("a"), true, "Data cloned." );
1499 clone.data( "a", false );
1500 equal( clone.data("a"), false, "Ensure cloned element data object was correctly modified" );
1501 equal( div.data("a"), true, "Ensure cloned element data object is copied, not referenced" );
1503 // manually clean up detached elements
1507 form = document.createElement("form");
1508 form.action = "/test/";
1510 div = document.createElement("div");
1511 div.appendChild( document.createTextNode("test") );
1512 form.appendChild( div );
1514 equal( jQuery(form).clone().children().length, 1, "Make sure we just get the form back." );
1516 body = jQuery("body").clone();
1517 equal( body.children()[ 0 ].id, "qunit", "Make sure cloning body works" );
1521 test( "clone(script type=non-javascript) (#11359)", function() {
1525 var src = jQuery("<script type='text/filler'>Lorem ipsum dolor sit amet</script><q><script type='text/filler'>consectetur adipiscing elit</script></q>"),
1528 equal( dest[ 0 ].text, "Lorem ipsum dolor sit amet", "Cloning preserves script text" );
1529 equal( dest.last().html(), src.last().html(), "Cloning preserves nested script text" );
1530 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" );
1534 test( "clone(form element) (Bug #3879, #6655)", function() {
1540 element = jQuery("<select><option>Foo</option><option value='selected' selected>Bar</option></select>");
1542 equal( element.clone().find("option").filter(function() { return this.selected; }).val(), "selected", "Selected option cloned correctly" );
1544 element = jQuery("<input type='checkbox' value='foo'>").attr( "checked", "checked" );
1545 clone = element.clone();
1547 equal( clone.is(":checked"), element.is(":checked"), "Checked input cloned correctly" );
1548 equal( clone[ 0 ].defaultValue, "foo", "Checked input defaultValue cloned correctly" );
1550 element = jQuery("<input type='text' value='foo'>");
1551 clone = element.clone();
1552 equal( clone[ 0 ].defaultValue, "foo", "Text input defaultValue cloned correctly" );
1554 element = jQuery("<textarea>foo</textarea>");
1555 clone = element.clone();
1556 equal( clone[ 0 ].defaultValue, "foo", "Textarea defaultValue cloned correctly" );
1559 test( "clone(multiple selected options) (Bug #8129)", function() {
1563 var element = jQuery("<select><option>Foo</option><option selected>Bar</option><option selected>Baz</option></select>");
1565 equal( element.clone().find("option:selected").length, element.find("option:selected").length, "Multiple selected options cloned correctly" );
1569 test( "clone() on XML nodes", function() {
1573 var xml = createDashboardXML(),
1574 root = jQuery(xml.documentElement).clone(),
1575 origTab = jQuery("tab", xml).eq( 0 ),
1576 cloneTab = jQuery("tab", root).eq( 0 );
1578 origTab.text("origval");
1579 cloneTab.text("cloneval");
1580 equal( origTab.text(), "origval", "Check original XML node was correctly set" );
1581 equal( cloneTab.text(), "cloneval", "Check cloned XML node was correctly set" );
1584 test( "clone() on local XML nodes with html5 nodename", function() {
1588 var $xmlDoc = jQuery( jQuery.parseXML( "<root><meter /></root>" ) ),
1589 $meter = $xmlDoc.find( "meter" ).clone();
1591 equal( $meter[ 0 ].nodeName, "meter", "Check if nodeName was not changed due to cloning" );
1592 equal( $meter[ 0 ].nodeType, 1, "Check if nodeType is not changed due to cloning" );
1595 test( "html(undefined)", function() {
1599 equal( jQuery("#foo").html("<i>test</i>").html(undefined).html().toLowerCase(), "<i>test</i>", ".html(undefined) is chainable (#5571)" );
1602 test( "html() on empty set", function() {
1606 strictEqual( jQuery().html(), undefined, ".html() returns undefined for empty sets (#11962)" );
1609 function childNodeNames( node ) {
1610 return jQuery.map( node.childNodes, function( child ) {
1611 return child.nodeName.toUpperCase();
1615 function testHtml( valueObj ) {
1618 var actual, expected, tmp,
1619 div = jQuery("<div></div>"),
1620 fixture = jQuery("#qunit-fixture");
1622 div.html( valueObj("<div id='parent_1'><div id='child_1'/></div><div id='parent_2'/>") );
1623 equal( div.children().length, 2, "Found children" );
1624 equal( div.children().children().length, 1, "Found grandchild" );
1626 actual = []; expected = [];
1627 tmp = jQuery("<map/>").html( valueObj("<area alt='area'/>") ).each(function() {
1628 expected.push("AREA");
1629 actual.push( childNodeNames( this ) );
1631 equal( expected.length, 1, "Expecting one parent" );
1632 deepEqual( actual, expected, "Found the inserted area element" );
1634 equal( div.html(valueObj(5)).html(), "5", "Setting a number as html" );
1635 equal( div.html(valueObj(0)).html(), "0", "Setting a zero as html" );
1636 equal( div.html(valueObj(Infinity)).html(), "Infinity", "Setting Infinity as html" );
1637 equal( div.html(valueObj(NaN)).html(), "", "Setting NaN as html" );
1638 equal( div.html(valueObj(1e2)).html(), "100", "Setting exponential number notation as html" );
1640 div.html( valueObj(" &") );
1642 div[ 0 ].innerHTML.replace( /\xA0/, " " ),
1644 "Entities are passed through correctly"
1647 tmp = "<div>hello1</div>";
1648 equal( div.html(valueObj(tmp) ).html().replace( />/g, ">" ), tmp, "Escaped html" );
1650 equal( div.html(valueObj( tmp )).html().replace( />/g, ">" ), tmp, "Escaped html, leading x" );
1651 tmp = " " + tmp.slice( 1 );
1652 equal( div.html(valueObj( tmp )).html().replace( />/g, ">" ), tmp, "Escaped html, leading space" );
1654 actual = []; expected = []; tmp = {};
1655 jQuery("#nonnodes").contents().html( valueObj("<b>bold</b>") ).each(function() {
1656 var html = jQuery( this ).html();
1657 tmp[ this.nodeType ] = true;
1658 expected.push( this.nodeType === 1 ? "<b>bold</b>" : undefined );
1659 actual.push( html ? html.toLowerCase() : html );
1661 deepEqual( actual, expected, "Set containing element, text node, comment" );
1662 ok( tmp[ 1 ], "element" );
1663 ok( tmp[ 3 ], "text node" );
1664 ok( tmp[ 8 ], "comment" );
1666 actual = []; expected = [];
1667 fixture.children("div").html( valueObj("<b>test</b>") ).each(function() {
1669 actual.push( childNodeNames( this ) );
1671 equal( expected.length, 7, "Expecting many parents" );
1672 deepEqual( actual, expected, "Correct childNodes after setting HTML" );
1674 actual = []; expected = [];
1675 fixture.html( valueObj("<style>.foobar{color:green;}</style>") ).each(function() {
1676 expected.push("STYLE");
1677 actual.push( childNodeNames( this ) );
1679 equal( expected.length, 1, "Expecting one parent" );
1680 deepEqual( actual, expected, "Found the inserted style element" );
1682 fixture.html( valueObj("<select/>") );
1683 jQuery("#qunit-fixture select").html( valueObj("<option>O1</option><option selected='selected'>O2</option><option>O3</option>") );
1684 equal( jQuery("#qunit-fixture select").val(), "O2", "Selected option correct" );
1688 "<script type='something/else'>ok( false, 'evaluated: non-script' );</script>",
1689 "<script type='text/javascript'>ok( true, 'evaluated: text/javascript' );</script>",
1690 "<script type='text/ecmascript'>ok( true, 'evaluated: text/ecmascript' );</script>",
1691 "<script>ok( true, 'evaluated: no type' );</script>",
1693 "<script type='something/else'>ok( false, 'evaluated: inner non-script' );</script>",
1694 "<script type='text/javascript'>ok( true, 'evaluated: inner text/javascript' );</script>",
1695 "<script type='text/ecmascript'>ok( true, 'evaluated: inner text/ecmascript' );</script>",
1696 "<script>ok( true, 'evaluated: inner no type' );</script>",
1700 equal( tmp.length, 8, "All script tags remain." );
1701 equal( tmp[ 0 ].type, "something/else", "Non-evaluated type." );
1702 equal( tmp[ 1 ].type, "text/javascript", "Evaluated type." );
1704 fixture.html( valueObj("<script type='text/javascript'>ok( true, 'Injection of identical script' );</script>") );
1705 fixture.html( valueObj("<script type='text/javascript'>ok( true, 'Injection of identical script' );</script>") );
1706 fixture.html( valueObj("<script type='text/javascript'>ok( true, 'Injection of identical script' );</script>") );
1707 fixture.html( valueObj("foo <form><script type='text/javascript'>ok( true, 'Injection of identical script (#975)' );</script></form>") );
1709 jQuery.scriptorder = 0;
1710 fixture.html( valueObj([
1712 "equal( jQuery('#scriptorder').length, 1,'Execute after html' );",
1713 "equal( jQuery.scriptorder++, 0, 'Script is executed in order' );",
1715 "<span id='scriptorder'><script>equal( jQuery.scriptorder++, 1, 'Script (nested) is executed in order');</script></span>",
1716 "<script>equal( jQuery.scriptorder++, 2, 'Script (unnested) is executed in order' );</script>"
1719 fixture.html( valueObj( fixture.text() ) );
1720 ok( /^[^<]*[^<\s][^<]*$/.test( fixture.html() ), "Replace html with text" );
1723 test( "html(String|Number)", function() {
1724 testHtml( manipulationBareObj );
1727 test( "html(Function)", function() {
1728 testHtml( manipulationFunctionReturningObj );
1731 test( "html(Function) with incoming value -- direct selection", function() {
1735 var els, actualhtml, pass;
1737 els = jQuery("#foo > p");
1738 actualhtml = els.map(function() {
1739 return jQuery( this ).html();
1742 els.html(function( i, val ) {
1743 equal( val, actualhtml[ i ], "Make sure the incoming value is correct." );
1744 return "<b>test</b>";
1748 els.each(function() {
1749 if ( this.childNodes.length !== 1 ) {
1753 ok( pass, "Set HTML" );
1756 test( "html(Function) with incoming value -- jQuery.contents()", function() {
1760 var actualhtml, j, $div, $div2, insert;
1762 j = jQuery("#nonnodes").contents();
1763 actualhtml = j.map(function() {
1764 return jQuery( this ).html();
1767 j.html(function( i, val ) {
1768 equal( val, actualhtml[ i ], "Make sure the incoming value is correct." );
1769 return "<b>bold</b>";
1772 // Handle the case where no comment is in the document
1773 if ( j.length === 2 ) {
1774 equal( null, null, "Make sure the incoming value is correct." );
1777 equal( j.html().replace( / xmlns="[^"]+"/g, "" ).toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
1779 $div = jQuery("<div />");
1781 equal( $div.html(function( i, val ) {
1782 equal( val, "", "Make sure the incoming value is correct." );
1784 }).html(), "5", "Setting a number as html" );
1786 equal( $div.html(function( i, val ) {
1787 equal( val, "5", "Make sure the incoming value is correct." );
1789 }).html(), "0", "Setting a zero as html" );
1791 $div2 = jQuery("<div/>");
1792 insert = "<div>hello1</div>";
1793 equal( $div2.html(function( i, val ) {
1794 equal( val, "", "Make sure the incoming value is correct." );
1796 }).html().replace(/>/g, ">"), insert, "Verify escaped insertion." );
1798 equal( $div2.html(function( i, val ) {
1799 equal( val.replace(/>/g, ">"), insert, "Make sure the incoming value is correct." );
1800 return "x" + insert;
1801 }).html().replace( />/g, ">" ), "x" + insert, "Verify escaped insertion." );
1803 equal( $div2.html(function( i, val ) {
1804 equal( val.replace( />/g, ">" ), "x" + insert, "Make sure the incoming value is correct." );
1805 return " " + insert;
1806 }).html().replace( />/g, ">" ), " " + insert, "Verify escaped insertion." );
1809 test( "clone()/html() don't expose jQuery/Sizzle expandos (#12858)", function() {
1813 var $content = jQuery("<div><b><i>text</i></b></div>").appendTo("#qunit-fixture"),
1814 expected = /^<b><i>text<\/i><\/b>$/i;
1816 // Attach jQuery and Sizzle data (the latter with a non-qSA nth-child)
1818 $content.find(":nth-child(1):lt(4)").data( "test", true );
1820 // But don't break on a non-Sizzle build
1822 $content.find("*").data( "test", true );
1825 ok( expected.test( $content.clone( false )[ 0 ].innerHTML ), "clone()" );
1826 ok( expected.test( $content.html() ), "html()" );
1829 test( "remove() no filters", function() {
1833 var first = jQuery("#ap").children().first();
1835 first.data("foo", "bar");
1837 jQuery("#ap").children().remove();
1838 ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
1839 equal( jQuery("#ap").children().length, 0, "Check remove" );
1842 test( "remove() with filters", function() {
1847 jQuery("#ap").children().remove("a");
1848 ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
1849 equal( jQuery("#ap").children().length, 1, "Check filtered remove" );
1851 jQuery("#ap").children().remove("a, code");
1852 equal( jQuery("#ap").children().length, 0, "Check multi-filtered remove" );
1854 // Positional and relative selectors
1855 markup = "<div><span>1</span><span>2</span><span>3</span><span>4</span></div>";
1856 div = jQuery( markup );
1857 div.children().remove("span:nth-child(2n)");
1858 equal( div.text(), "13", "relative selector in remove" );
1859 div = jQuery( markup );
1860 div.children().remove("span:first");
1861 equal( div.text(), "234", "positional selector in remove" );
1862 div = jQuery( markup );
1863 div.children().remove("span:last");
1864 equal( div.text(), "123", "positional selector in remove" );
1866 // using contents will get comments regular, text, and comment nodes
1867 // Handle the case where no comment is in the document
1868 ok( jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment remove works" );
1869 jQuery("#nonnodes").contents().remove();
1870 equal( jQuery("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" );
1873 test( "remove() event cleaning ", 1, function() {
1874 var count, first, cleanUp;
1877 first = jQuery("#ap").children().first();
1878 cleanUp = first.on( "click", function() {
1880 }).remove().appendTo("#qunit-fixture").trigger("click");
1882 strictEqual( 0, count, "Event handler has been removed" );
1884 // Clean up detached data
1888 test( "remove() in document order #13779", 1, function() {
1890 cleanData = jQuery.cleanData;
1892 jQuery.cleanData = function( nodes ) {
1893 last = jQuery.text( nodes[0] );
1894 cleanData.call( this, nodes );
1897 jQuery("#qunit-fixture").append(
1899 "<div class='removal-fixture'>1</div>" +
1900 "<div class='removal-fixture'>2</div>" +
1901 "<div class='removal-fixture'>3</div>"
1905 jQuery(".removal-fixture").remove();
1907 equal( last, 3, "The removal fixtures were removed in document order" );
1909 jQuery.cleanData = cleanData;
1912 test("detach() no filters", function () {
1916 var first = jQuery("#ap").children().first();
1918 first.data("foo", "bar");
1920 jQuery("#ap").children().detach();
1921 ok(jQuery("#ap").text().length > 10, "Check text is not removed");
1922 equal(jQuery("#ap").children().length, 0, "Check remove");
1924 equal(first.data("foo"), "bar");
1929 test("detach() with filters", function () {
1934 jQuery("#ap").children().detach("a");
1935 ok(jQuery("#ap").text().length > 10, "Check text is not removed");
1936 equal(jQuery("#ap").children().length, 1, "Check filtered remove");
1938 jQuery("#ap").children().detach("a, code");
1939 equal(jQuery("#ap").children().length, 0, "Check multi-filtered remove");
1941 // Positional and relative selectors
1942 markup = "<div><span>1</span><span>2</span><span>3</span><span>4</span></div>";
1943 div = jQuery(markup);
1944 div.children().detach("span:nth-child(2n)");
1945 equal(div.text(), "13", "relative selector in detach");
1946 div = jQuery(markup);
1947 div.children().detach("span:first");
1948 equal(div.text(), "234", "positional selector in detach");
1949 div = jQuery(markup);
1950 div.children().detach("span:last");
1951 equal(div.text(), "123", "positional selector in detach");
1953 // using contents will get comments regular, text, and comment nodes
1954 // Handle the case where no comment is in the document
1955 ok(jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment remove works");
1956 jQuery("#nonnodes").contents().detach();
1957 equal(jQuery("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works");
1960 test( "detach() event cleaning ", 1, function() {
1961 var count, first, cleanUp;
1964 first = jQuery("#ap").children().first();
1965 cleanUp = first.on( "click", function() {
1967 }).detach().appendTo("#qunit-fixture").trigger("click");
1969 strictEqual( 1, count, "Event handler has not been removed" );
1971 // Clean up detached data
1975 test("empty()", function() {
1979 equal( jQuery("#ap").children().empty().text().length, 0, "Check text is removed" );
1980 equal( jQuery("#ap").children().length, 4, "Check elements are not removed" );
1982 // using contents will get comments regular, text, and comment nodes
1983 var j = jQuery("#nonnodes").contents();
1985 equal( j.html(), "", "Check node,textnode,comment empty works" );
1988 test( "jQuery.cleanData", function() {
1992 var type, pos, div, child;
1996 // Should trigger 4 remove event
1997 div = getDiv().remove();
1999 // Should both do nothing
2001 div.trigger("click");
2004 div.children().trigger("click");
2008 child = div.children();
2010 // Should trigger 2 remove event
2015 div.trigger("click");
2017 // Should do nothing
2019 child.trigger("click");
2027 child = div.children();
2029 // Should trigger 2 remove event
2030 div.html("<div></div>");
2034 div.trigger("click");
2036 // Should do nothing
2038 child.trigger("click");
2044 var div = jQuery("<div class='outer'><div class='inner'></div></div>").on( "click", function() {
2045 ok( true, type + " " + pos + " Click event fired." );
2046 }).on( "focus", function() {
2047 ok( true, type + " " + pos + " Focus event fired." );
2048 }).find("div").on( "click", function() {
2049 ok( false, type + " " + pos + " Click event fired." );
2050 }).on( "focus", function() {
2051 ok( false, type + " " + pos + " Focus event fired." );
2052 }).end().appendTo("body");
2054 div[ 0 ].detachEvent = div[ 0 ].removeEventListener = function( t ) {
2055 ok( true, type + " Outer " + t + " event unbound" );
2058 div[ 0 ].firstChild.detachEvent = div[ 0 ].firstChild.removeEventListener = function( t ) {
2059 ok( true, type + " Inner " + t + " event unbound" );
2066 test( "jQuery.cleanData eliminates all private data (gh-2127)", function() {
2069 var div = jQuery( "<div/>" ).appendTo( "#qunit-fixture" );
2071 jQuery._data( div[ 0 ], "gh-2127", "testing" );
2073 ok( !jQuery.isEmptyObject( jQuery._data( div[ 0 ] ) ), "Ensure some private data exists" );
2077 ok( jQuery.isEmptyObject( jQuery._data( div[ 0 ] ) ),
2078 "Private data is empty after node is removed" );
2083 test( "domManip plain-text caching (trac-6779)", function() {
2087 // DOM manipulation fails if added text matches an Object method
2089 $f = jQuery( "<div />" ).appendTo( "#qunit-fixture" ),
2090 bad = [ "start-", "toString", "hasOwnProperty", "append", "here&there!", "-end" ];
2092 for ( i = 0; i < bad.length; i++ ) {
2094 $f.append( bad[ i ] );
2098 equal( $f.text(), bad.join(""), "Cached strings that match Object properties" );
2102 test( "domManip executes scripts containing html comments or CDATA (trac-9221)", function() {
2107 "<script type='text/javascript'>",
2109 "ok( true, '<!-- handled' );",
2112 ].join( "\n" ) ).appendTo( "#qunit-fixture" );
2115 "<script type='text/javascript'>",
2117 "ok( true, '<![CDATA[ handled' );",
2120 ].join( "\n" ) ).appendTo( "#qunit-fixture" );
2123 "<script type='text/javascript'>",
2124 "<!--//--><![CDATA[//><!--",
2125 "ok( true, '<!--//--><![CDATA[//><!-- (Drupal case) handled' );",
2128 ].join( "\n" ) ).appendTo( "#qunit-fixture" );
2131 testIframeWithCallback(
2132 "domManip tolerates window-valued document[0] in IE9/10 (trac-12266)",
2133 "manipulation/iframe-denied.html",
2136 ok( test.status, test.description );
2140 test( "jQuery.clone - no exceptions for object elements #9587", function() {
2145 jQuery("#no-clone-exception").clone();
2146 ok( true, "cloned with no exceptions" );
2148 ok( false, e.message );
2152 test( "Cloned, detached HTML5 elems (#10667,10670)", function() {
2157 $section = jQuery( "<section>" ).appendTo( "#qunit-fixture" );
2160 $clone = $section.clone();
2162 // This branch tests a known behaviour in modern browsers that should never fail.
2163 // Included for expected test count symmetry (expecting 1)
2164 equal( $clone[ 0 ].nodeName, "SECTION", "detached clone nodeName matches 'SECTION'" );
2167 $section.on( "click", function() {
2168 ok( true, "clone fired event" );
2171 // Second clone (will have an event bound)
2172 $clone = $section.clone( true );
2174 // Trigger an event from the first clone
2175 $clone.trigger("click");
2176 $clone.off("click");
2178 // Add a child node with text to the original
2179 $section.append("<p>Hello</p>");
2181 // Third clone (will have child node and text)
2182 $clone = $section.clone( true );
2184 equal( $clone.find("p").text(), "Hello", "Assert text in child of clone" );
2186 // Trigger an event from the third clone
2187 $clone.trigger("click");
2188 $clone.off("click");
2190 // Add attributes to copy
2192 "class": "foo bar baz",
2193 "title": "This is a title"
2196 // Fourth clone (will have newly added attributes)
2197 $clone = $section.clone( true );
2199 equal( $clone.attr("class"), $section.attr("class"), "clone and element have same class attribute" );
2200 equal( $clone.attr("title"), $section.attr("title"), "clone and element have same title attribute" );
2202 // Remove the original
2206 $section = $clone.clone( true );
2211 // Trigger an event from the clone of the clone
2212 $section.trigger("click");
2214 // Unbind any remaining events
2215 $section.off("click");
2216 $clone.off("click");
2219 test( "Guard against exceptions when clearing safeChildNodes", function() {
2226 div = jQuery("<div/><hr/><code/><b/>");
2229 ok( div && div.jquery, "Created nodes safely, guarded against exceptions on safeChildNodes[ -1 ]" );
2232 test( "Ensure oldIE creates a new set on appendTo (#8894)", function() {
2236 strictEqual( jQuery("<div/>").clone().addClass("test").appendTo("<div/>").end().end().hasClass("test"), false, "Check jQuery.fn.appendTo after jQuery.clone" );
2237 strictEqual( jQuery("<div/>").find("p").end().addClass("test").appendTo("<div/>").end().end().hasClass("test"), false, "Check jQuery.fn.appendTo after jQuery.fn.find" );
2238 strictEqual( jQuery("<div/>").text("test").addClass("test").appendTo("<div/>").end().end().hasClass("test"), false, "Check jQuery.fn.appendTo after jQuery.fn.text" );
2239 strictEqual( jQuery("<bdi/>").clone().addClass("test").appendTo("<div/>").end().end().hasClass("test"), false, "Check jQuery.fn.appendTo after clone html5 element" );
2240 strictEqual( jQuery("<p/>").appendTo("<div/>").end().length, jQuery("<p>test</p>").appendTo("<div/>").end().length, "Elements created with createElement and with createDocumentFragment should be treated alike" );
2243 asyncTest( "html() - script exceptions bubble (#11743)", 2, function() {
2244 var onerror = window.onerror;
2246 setTimeout(function() {
2247 window.onerror = onerror;
2252 window.onerror = function() {
2253 ok( true, "Exception thrown" );
2255 if ( jQuery.ajax ) {
2256 window.onerror = function() {
2257 ok( true, "Exception thrown in remote script" );
2260 jQuery( "#qunit-fixture" ).html( "<script src='data/badcall.js'></script>" );
2261 ok( true, "Exception ignored" );
2263 ok( true, "No jQuery.ajax" );
2264 ok( true, "No jQuery.ajax" );
2268 jQuery( "#qunit-fixture" ).html( "<script>undefined();</script>" );
2271 test( "checked state is cloned with clone()", function() {
2275 var elem = jQuery.parseHTML("<input type='checkbox' checked='checked'/>")[ 0 ];
2276 elem.checked = false;
2277 equal( jQuery(elem).clone().attr("id","clone")[ 0 ].checked, false, "Checked false state correctly cloned" );
2279 elem = jQuery.parseHTML("<input type='checkbox'/>")[ 0 ];
2280 elem.checked = true;
2281 equal( jQuery(elem).clone().attr("id","clone")[ 0 ].checked, true, "Checked true state correctly cloned" );
2284 test( "manipulate mixed jQuery and text (#12384, #12346)", function() {
2288 var div = jQuery("<div>a</div>").append( " ", jQuery("<span>b</span>"), " ", jQuery("<span>c</span>") ),
2289 nbsp = String.fromCharCode( 160 );
2291 equal( div.text(), "a" + nbsp + "b" + nbsp+ "c", "Appending mixed jQuery with text nodes" );
2293 div = jQuery("<div><div></div></div>")
2295 .after( "<p>a</p>", "<p>b</p>" )
2297 equal( div.find("*").length, 3, "added 2 paragraphs after inner div" );
2300 test( "script evaluation (#11795)", function() {
2304 var scriptsIn, scriptsOut,
2305 fixture = jQuery("#qunit-fixture").empty(),
2306 objGlobal = (function() {
2309 isOk = objGlobal.ok,
2310 notOk = function() {
2311 var args = arguments;
2312 args[ 0 ] = !args[ 0 ];
2313 return isOk.apply( this, args );
2316 objGlobal.ok = notOk;
2317 scriptsIn = jQuery([
2318 "<script type='something/else'>ok( false, 'evaluated: non-script' );</script>",
2319 "<script type='text/javascript'>ok( true, 'evaluated: text/javascript' );</script>",
2320 "<script type='text/ecmascript'>ok( true, 'evaluated: text/ecmascript' );</script>",
2321 "<script>ok( true, 'evaluated: no type' );</script>",
2323 "<script type='something/else'>ok( false, 'evaluated: inner non-script' );</script>",
2324 "<script type='text/javascript'>ok( true, 'evaluated: inner text/javascript' );</script>",
2325 "<script type='text/ecmascript'>ok( true, 'evaluated: inner text/ecmascript' );</script>",
2326 "<script>ok( true, 'evaluated: inner no type' );</script>",
2329 scriptsIn.appendTo( jQuery("<div class='detached'/>") );
2330 objGlobal.ok = isOk;
2332 scriptsOut = fixture.append( scriptsIn ).find("script");
2333 equal( scriptsOut[ 0 ].type, "something/else", "Non-evaluated type." );
2334 equal( scriptsOut[ 1 ].type, "text/javascript", "Evaluated type." );
2335 deepEqual( scriptsOut.get(), fixture.find("script").get(), "All script tags remain." );
2337 objGlobal.ok = notOk;
2338 scriptsOut = scriptsOut.add( scriptsOut.clone() ).appendTo( fixture.find("div") );
2339 deepEqual( fixture.find("div script").get(), scriptsOut.get(), "Scripts cloned without reevaluation" );
2340 fixture.append( scriptsOut.detach() );
2341 deepEqual( fixture.children("script").get(), scriptsOut.get(), "Scripts detached without reevaluation" );
2342 objGlobal.ok = isOk;
2344 if ( jQuery.ajax ) {
2345 Globals.register("testBar");
2346 jQuery("#qunit-fixture").append( "<script src='" + url("data/testbar.php") + "'/>" );
2347 strictEqual( window["testBar"], "bar", "Global script evaluation" );
2349 ok( true, "No jQuery.ajax" );
2350 ok( true, "No jQuery.ajax" );
2354 test( "jQuery._evalUrl (#12838)", function() {
2358 var message, expectedArgument,
2360 evalUrl = jQuery._evalUrl;
2362 message = "jQuery.ajax implementation";
2363 expectedArgument = 1;
2364 jQuery.ajax = function( input ) {
2365 equal( ( input.url || input ).slice( -1 ), expectedArgument, message );
2368 jQuery("#qunit-fixture").append("<script src='1'/><script src='2'/>");
2369 equal( expectedArgument, 3, "synchronous execution" );
2371 message = "custom implementation";
2372 expectedArgument = 3;
2373 jQuery._evalUrl = jQuery.ajax;
2374 jQuery.ajax = function( options ) {
2375 strictEqual( options, {}, "Unexpected call to jQuery.ajax" );
2377 jQuery("#qunit-fixture").append("<script src='3'/><script src='4'/>");
2380 jQuery._evalUrl = evalUrl;
2383 test( "jQuery.htmlPrefilter (gh-1747)", function( assert ) {
2387 var expectedArgument,
2389 htmlPrefilter = jQuery.htmlPrefilter,
2390 fixture = jQuery( "<div/>" ).appendTo( "#qunit-fixture" ),
2391 poison = "<script>jQuery.htmlPrefilter.assert.ok( false, 'script not executed' );</script>",
2392 done = assert.async();
2394 jQuery.htmlPrefilter = function( html ) {
2396 assert.equal( html, expectedArgument, "Expected input" );
2398 // Remove <script> and <del> elements
2399 return htmlPrefilter.apply( this, arguments )
2400 .replace( /<(script|del)(?=[\s>])[\w\W]*?<\/\1\s*>/ig, "" );
2402 jQuery.htmlPrefilter.assert = assert;
2404 expectedArgument = "A-" + poison + "B-" + poison + poison + "C-";
2405 fixture.html( expectedArgument );
2407 expectedArgument = "D-" + poison + "E-" + "<del/><div>" + poison + poison + "</div>" + "F-";
2408 fixture.append( expectedArgument );
2410 expectedArgument = poison;
2411 fixture.find( "div" ).replaceWith( expectedArgument );
2413 assert.equal( invocations, 3, "htmlPrefilter invoked for all DOM manipulations" );
2414 assert.equal( fixture.html(), "A-B-C-D-E-F-", "htmlPrefilter modified HTML" );
2416 // Allow asynchronous script execution to generate assertions
2417 setTimeout( function() {
2418 jQuery.htmlPrefilter = htmlPrefilter;
2423 test( "insertAfter, insertBefore, etc do not work when destination is original element. Element is removed (#4087)", function() {
2435 ], function( index, name ) {
2437 "<ul id='test4087-complex'><li class='test4087'><div>c1</div>h1</li><li><div>c2</div>h2</li></ul>",
2438 "<div id='test4087-simple'><div class='test4087-1'>1<div class='test4087-2'>2</div><div class='test4087-3'>3</div></div></div>",
2439 "<div id='test4087-multiple'><div class='test4087-multiple'>1</div><div class='test4087-multiple'>2</div></div>"
2440 ].join("") ).appendTo( "#qunit-fixture" );
2442 // complex case based on http://jsfiddle.net/pbramos/gZ7vB/
2443 jQuery("#test4087-complex div")[ name ]("#test4087-complex li:last-child div:last-child");
2444 equal( jQuery("#test4087-complex li:last-child div").length, name === "replaceAll" ? 1 : 2, name +" a node to itself, complex case." );
2447 jQuery( ".test4087-1" )[ name ](".test4087-1");
2448 equal( jQuery(".test4087-1").length, 1, name +" a node to itself, simple case." );
2450 // clean for next test
2451 jQuery("#test4087-complex").remove();
2452 jQuery("#test4087-simple").remove();
2453 jQuery("#test4087-multiple").remove();
2457 test( "Index for function argument should be received (#13094)", 2, function() {
2460 jQuery("<div/><div/>").before(function( index ) {
2461 equal( index, i++, "Index should be correct" );
2466 test( "Make sure jQuery.fn.remove can work on elements in documentFragment", 1, function() {
2467 var fragment = document.createDocumentFragment(),
2468 div = fragment.appendChild( document.createElement("div") );
2470 jQuery( div ).remove();
2472 equal( fragment.childNodes.length, 0, "div element was removed from documentFragment" );
2475 test( "Make sure specific elements with content created correctly (#13232)", 20, function() {
2479 thead: "<tr><td>thead</td></tr>",
2480 tbody: "<tr><td>tbody</td></tr>",
2481 tfoot: "<tr><td>tfoot</td></tr>",
2482 colgroup: "<col span='5' />",
2486 td: "<div>td</div>",
2487 optgroup: "<option>optgroup</option>",
2491 jQuery.each( elems, function( name, value ) {
2492 var html = "<" + name + ">" + value + "</" + name + ">";
2493 ok( jQuery.nodeName( jQuery.parseHTML( "<" + name + ">" + value + "</" + name + ">" )[ 0 ], name ), name + " is created correctly" );
2495 results.push( name );
2499 jQuery.fn.append.apply( jQuery("<div/>"), args ).children().each(function( i ) {
2500 ok( jQuery.nodeName( this, results[ i ] ) );
2504 test( "Validate creation of multiple quantities of certain elements (#13818)", 44, function() {
2505 var tags = [ "thead", "tbody", "tfoot", "colgroup", "col", "caption", "tr", "th", "td", "optgroup", "option" ];
2507 jQuery.each( tags, function( index, tag ) {
2508 jQuery( "<" + tag + "/><" + tag + "/>" ).each(function() {
2509 ok( jQuery.nodeName( this, tag ), tag + " empty elements created correctly" );
2512 jQuery( "<" + this + "></" + tag + "><" + tag + "></" + tag + ">" ).each(function() {
2513 ok( jQuery.nodeName( this, tag ), tag + " elements with closing tag created correctly" );
2518 test( "Make sure tr element will be appended to tbody element of table when present", function() {
2522 table = document.createElement( "table" );
2524 table.appendChild( document.createElement( "tbody" ) );
2525 document.getElementById( "qunit-fixture" ).appendChild( table );
2527 jQuery( table ).append( "<tr><td>test</td></tr>" );
2529 // Lowercase and replace spaces to remove possible browser inconsistencies
2530 html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
2532 strictEqual( html, "<tbody><tr><td>test</td></tr></tbody>" );
2535 test( "Make sure tr elements will be appended to tbody element of table when present", function() {
2539 table = document.createElement( "table" );
2541 table.appendChild( document.createElement( "tbody" ) );
2542 document.getElementById( "qunit-fixture" ).appendChild( table );
2544 jQuery( table ).append( "<tr><td>1</td></tr><tr><td>2</td></tr>" );
2546 // Lowercase and replace spaces to remove possible browser inconsistencies
2547 html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
2549 strictEqual( html, "<tbody><tr><td>1</td></tr><tr><td>2</td></tr></tbody>" );
2552 test( "Make sure tfoot element will not be appended to tbody element of table when present", function() {
2556 table = document.createElement( "table" );
2558 table.appendChild( document.createElement( "tbody" ) );
2559 document.getElementById( "qunit-fixture" ).appendChild( table );
2561 jQuery( table ).append( "<tfoot/>" );
2563 // Lowercase and replace spaces to remove possible browser inconsistencies
2564 html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
2566 strictEqual( html, "<tbody></tbody><tfoot></tfoot>" );
2569 test( "Make sure document fragment will be appended to tbody element of table when present", function() {
2573 fragment = document.createDocumentFragment(),
2574 table = document.createElement( "table" ),
2575 tr = document.createElement( "tr" ),
2576 td = document.createElement( "td" );
2578 table.appendChild( document.createElement( "tbody" ) );
2579 document.getElementById( "qunit-fixture" ).appendChild( table );
2581 fragment.appendChild( tr );
2582 tr.appendChild( td );
2583 td.innerHTML = "test";
2585 jQuery( table ).append( fragment );
2587 // Lowercase and replace spaces to remove possible browser inconsistencies
2588 html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
2590 strictEqual( html, "<tbody><tr><td>test</td></tr></tbody>" );
2593 test( "Make sure col element is appended correctly", function() {
2596 var table = jQuery( "<table cellpadding='0'><tr><td>test</td></tr></table>" );
2598 jQuery( table ).appendTo( "#qunit-fixture" );
2600 jQuery( "<col width='150'/>" ).prependTo( table );
2602 strictEqual( table.find( "td" ).width(), 150 );
2605 asyncTest( "Insert script with data-URI (gh-1887)", 1, function() {
2606 Globals.register( "testFoo" );
2607 Globals.register( "testSrcFoo" );
2609 var script = document.createElement( "script" ),
2610 fixture = document.getElementById( "qunit-fixture" );
2612 script.src = "data:text/javascript,testSrcFoo = 'foo';";
2614 fixture.appendChild( script );
2616 jQuery( fixture ).append( "<script src=\"data:text/javascript,testFoo = 'foo';\"></script>" );
2618 setTimeout(function() {
2619 if ( window[ "testSrcFoo" ] === "foo" ) {
2620 strictEqual( window[ "testFoo" ], window[ "testSrcFoo" ], "data-URI script executed" );
2623 ok( true, "data-URI script is not supported by this environment" );