manipulation.js 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121
  1. module( "manipulation", {
  2. teardown: moduleTeardown
  3. });
  4. // Ensure that an extended Array prototype doesn't break jQuery
  5. Array.prototype.arrayProtoFn = function() {
  6. throw("arrayProtoFn should not be called");
  7. };
  8. function manipulationBareObj( value ) {
  9. return value;
  10. }
  11. function manipulationFunctionReturningObj( value ) {
  12. return function() {
  13. return value;
  14. };
  15. }
  16. /*
  17. ======== local reference =======
  18. manipulationBareObj and manipulationFunctionReturningObj can be used to test passing functions to setters
  19. See testVal below for an example
  20. bareObj( value );
  21. This function returns whatever value is passed in
  22. functionReturningObj( value );
  23. Returns a function that returns the value
  24. */
  25. test( "text()", function() {
  26. expect( 5 );
  27. var expected, frag, $newLineTest;
  28. expected = "This link has class=\"blog\": Simon Willison's Weblog";
  29. equal( jQuery("#sap").text(), expected, "Check for merged text of more then one element." );
  30. // Check serialization of text values
  31. equal( jQuery(document.createTextNode("foo")).text(), "foo", "Text node was retrieved from .text()." );
  32. notEqual( jQuery(document).text(), "", "Retrieving text for the document retrieves all text (#10724)." );
  33. // Retrieve from document fragments #10864
  34. frag = document.createDocumentFragment();
  35. frag.appendChild( document.createTextNode("foo") );
  36. equal( jQuery(frag).text(), "foo", "Document Fragment Text node was retrieved from .text()." );
  37. $newLineTest = jQuery("<div>test<br/>testy</div>").appendTo("#moretests");
  38. $newLineTest.find("br").replaceWith("\n");
  39. equal( $newLineTest.text(), "test\ntesty", "text() does not remove new lines (#11153)" );
  40. $newLineTest.remove();
  41. });
  42. test( "text(undefined)", function() {
  43. expect( 1 );
  44. equal( jQuery("#foo").text("<div").text(undefined)[ 0 ].innerHTML, "&lt;div", ".text(undefined) is chainable (#5571)" );
  45. });
  46. function testText( valueObj ) {
  47. expect( 4 );
  48. var val, j;
  49. val = valueObj("<div><b>Hello</b> cruel world!</div>");
  50. equal( jQuery("#foo").text(val)[ 0 ].innerHTML.replace(/>/g, "&gt;"), "&lt;div&gt;&lt;b&gt;Hello&lt;/b&gt; cruel world!&lt;/div&gt;", "Check escaped text" );
  51. // using contents will get comments regular, text, and comment nodes
  52. j = jQuery("#nonnodes").contents();
  53. j.text( valueObj("hi!") );
  54. equal( jQuery( j[ 0 ] ).text(), "hi!", "Check node,textnode,comment with text()" );
  55. equal( j[ 1 ].nodeValue, " there ", "Check node,textnode,comment with text()" );
  56. // Blackberry 4.6 doesn't maintain comments in the DOM
  57. equal( jQuery("#nonnodes")[ 0 ].childNodes.length < 3 ? 8 : j[ 2 ].nodeType, 8, "Check node,textnode,comment with text()" );
  58. }
  59. test( "text(String)", function() {
  60. testText( manipulationBareObj );
  61. });
  62. test( "text(Function)", function() {
  63. testText( manipulationFunctionReturningObj );
  64. });
  65. test( "text(Function) with incoming value", function() {
  66. expect( 2 );
  67. var old = "This link has class=\"blog\": Simon Willison's Weblog";
  68. jQuery("#sap").text(function( i, val ) {
  69. equal( val, old, "Make sure the incoming value is correct." );
  70. return "foobar";
  71. });
  72. equal( jQuery("#sap").text(), "foobar", "Check for merged text of more then one element." );
  73. });
  74. function testAppendForObject( valueObj, isFragment ) {
  75. var $base,
  76. type = isFragment ? " (DocumentFragment)" : " (Element)",
  77. text = "This link has class=\"blog\": Simon Willison's Weblog",
  78. el = document.getElementById("sap").cloneNode( true ),
  79. first = document.getElementById("first"),
  80. yahoo = document.getElementById("yahoo");
  81. if ( isFragment ) {
  82. $base = document.createDocumentFragment();
  83. jQuery( el ).contents().each(function() {
  84. $base.appendChild( this );
  85. });
  86. $base = jQuery( $base );
  87. } else {
  88. $base = jQuery( el );
  89. }
  90. equal( $base.clone().append( valueObj(first.cloneNode(true)) ).text(),
  91. text + "Try them out:",
  92. "Check for appending of element" + type
  93. );
  94. equal( $base.clone().append( valueObj([ first.cloneNode(true), yahoo.cloneNode(true) ]) ).text(),
  95. text + "Try them out:Yahoo",
  96. "Check for appending of array of elements" + type
  97. );
  98. equal( $base.clone().append( valueObj(jQuery("#yahoo, #first").clone()) ).text(),
  99. text + "YahooTry them out:",
  100. "Check for appending of jQuery object" + type
  101. );
  102. equal( $base.clone().append( valueObj( 5 ) ).text(),
  103. text + "5",
  104. "Check for appending a number" + type
  105. );
  106. equal( $base.clone().append( valueObj([ jQuery("#first").clone(), jQuery("#yahoo, #google").clone() ]) ).text(),
  107. text + "Try them out:GoogleYahoo",
  108. "Check for appending of array of jQuery objects"
  109. );
  110. equal( $base.clone().append( valueObj(" text with spaces ") ).text(),
  111. text + " text with spaces ",
  112. "Check for appending text with spaces" + type
  113. );
  114. equal( $base.clone().append( valueObj([]) ).text(),
  115. text,
  116. "Check for appending an empty array" + type
  117. );
  118. equal( $base.clone().append( valueObj("") ).text(),
  119. text,
  120. "Check for appending an empty string" + type
  121. );
  122. equal( $base.clone().append( valueObj(document.getElementsByTagName("foo")) ).text(),
  123. text,
  124. "Check for appending an empty nodelist" + type
  125. );
  126. equal( $base.clone().append( "<span></span>", "<span></span>", "<span></span>" ).children().length,
  127. $base.children().length + 3,
  128. "Make sure that multiple arguments works." + type
  129. );
  130. equal( $base.clone().append( valueObj(document.getElementById("form").cloneNode(true)) ).children("form").length,
  131. 1,
  132. "Check for appending a form (#910)" + type
  133. );
  134. }
  135. function testAppend( valueObj ) {
  136. expect( 78 );
  137. testAppendForObject( valueObj, false );
  138. testAppendForObject( valueObj, true );
  139. var defaultText, result, message, iframe, iframeDoc, j, d,
  140. $input, $radioChecked, $radioUnchecked, $radioParent, $map, $table;
  141. defaultText = "Try them out:";
  142. result = jQuery("#first").append( valueObj("<b>buga</b>") );
  143. equal( result.text(), defaultText + "buga", "Check if text appending works" );
  144. equal( jQuery("#select3").append( valueObj("<option value='appendTest'>Append Test</option>") ).find("option:last-child").attr("value"), "appendTest", "Appending html options to select element" );
  145. jQuery("form").append( valueObj("<input name='radiotest' type='radio' checked='checked' />") );
  146. jQuery("form input[name=radiotest]").each(function() {
  147. ok( jQuery(this).is(":checked"), "Append checked radio" );
  148. }).remove();
  149. jQuery("form").append( valueObj("<input name='radiotest2' type='radio' checked = 'checked' />") );
  150. jQuery("form input[name=radiotest2]").each(function() {
  151. ok( jQuery(this).is(":checked"), "Append alternately formated checked radio" );
  152. }).remove();
  153. jQuery("form").append( valueObj("<input name='radiotest3' type='radio' checked />") );
  154. jQuery("form input[name=radiotest3]").each(function() {
  155. ok( jQuery(this).is(":checked"), "Append HTML5-formated checked radio" );
  156. }).remove();
  157. jQuery("form").append( valueObj("<input type='radio' checked='checked' name='radiotest4' />") );
  158. jQuery("form input[name=radiotest4]").each(function() {
  159. ok( jQuery(this).is(":checked"), "Append with name attribute after checked attribute" );
  160. }).remove();
  161. message = "Test for appending a DOM node to the contents of an iframe";
  162. iframe = jQuery("#iframe")[ 0 ];
  163. iframeDoc = iframe.contentDocument || iframe.contentWindow && iframe.contentWindow.document;
  164. try {
  165. if ( iframeDoc && iframeDoc.body ) {
  166. equal( jQuery(iframeDoc.body).append( valueObj("<div id='success'>test</div>") )[ 0 ].lastChild.id, "success", message );
  167. } else {
  168. ok( true, message + " - can't test" );
  169. }
  170. } catch( e ) {
  171. strictEqual( e.message || e, undefined, message );
  172. }
  173. jQuery("<fieldset/>").appendTo("#form").append( valueObj("<legend id='legend'>test</legend>") );
  174. t( "Append legend", "#legend", [ "legend" ] );
  175. $map = jQuery("<map/>").append( valueObj("<area id='map01' shape='rect' coords='50,50,150,150' href='http://www.jquery.com/' alt='jQuery'>") );
  176. equal( $map[ 0 ].childNodes.length, 1, "The area was inserted." );
  177. equal( $map[ 0 ].firstChild.nodeName.toLowerCase(), "area", "The area was inserted." );
  178. jQuery("#select1").append( valueObj("<OPTION>Test</OPTION>") );
  179. equal( jQuery("#select1 option:last-child").text(), "Test", "Appending OPTION (all caps)" );
  180. jQuery("#select1").append( valueObj("<optgroup label='optgroup'><option>optgroup</option></optgroup>") );
  181. equal( jQuery("#select1 optgroup").attr("label"), "optgroup", "Label attribute in newly inserted optgroup is correct" );
  182. equal( jQuery("#select1 option").last().text(), "optgroup", "Appending optgroup" );
  183. $table = jQuery("#table");
  184. jQuery.each( "thead tbody tfoot colgroup caption tr th td".split(" "), function( i, name ) {
  185. $table.append( valueObj( "<" + name + "/>" ) );
  186. equal( $table.find( name ).length, 1, "Append " + name );
  187. ok( jQuery.parseHTML( "<" + name + "/>" ).length, name + " wrapped correctly" );
  188. });
  189. jQuery("#table colgroup").append( valueObj("<col/>") );
  190. equal( jQuery("#table colgroup col").length, 1, "Append col" );
  191. jQuery("#form")
  192. .append( valueObj("<select id='appendSelect1'></select>") )
  193. .append( valueObj("<select id='appendSelect2'><option>Test</option></select>") );
  194. t( "Append Select", "#appendSelect1, #appendSelect2", [ "appendSelect1", "appendSelect2" ] );
  195. equal( "Two nodes", jQuery("<div />").append( "Two", " nodes" ).text(), "Appending two text nodes (#4011)" );
  196. equal( jQuery("<div />").append( "1", "", 3 ).text(), "13", "If median is false-like value, subsequent arguments should not be ignored" );
  197. // using contents will get comments regular, text, and comment nodes
  198. j = jQuery("#nonnodes").contents();
  199. d = jQuery("<div/>").appendTo("#nonnodes").append( j );
  200. equal( jQuery("#nonnodes").length, 1, "Check node,textnode,comment append moved leaving just the div" );
  201. equal( d.contents().length, 3, "Check node,textnode,comment append works" );
  202. d.contents().appendTo("#nonnodes");
  203. d.remove();
  204. equal( jQuery("#nonnodes").contents().length, 3, "Check node,textnode,comment append cleanup worked" );
  205. $input = jQuery("<input type='checkbox'/>").prop( "checked", true ).appendTo("#testForm");
  206. equal( $input[ 0 ].checked, true, "A checked checkbox that is appended stays checked" );
  207. $radioChecked = jQuery("input[type='radio'][name='R1']").eq( 1 );
  208. $radioParent = $radioChecked.parent();
  209. $radioUnchecked = jQuery("<input type='radio' name='R1' checked='checked'/>").appendTo( $radioParent );
  210. $radioChecked.trigger("click");
  211. $radioUnchecked[ 0 ].checked = false;
  212. jQuery("<div/>").insertBefore($radioParent).append($radioParent);
  213. equal( $radioChecked[ 0 ].checked, true, "Reappending radios uphold which radio is checked" );
  214. equal( $radioUnchecked[ 0 ].checked, false, "Reappending radios uphold not being checked" );
  215. equal( jQuery("<div/>").append( valueObj("option<area/>") )[ 0 ].childNodes.length, 2, "HTML-string with leading text should be processed correctly" );
  216. }
  217. test( "append(String|Element|Array<Element>|jQuery)", function() {
  218. testAppend( manipulationBareObj );
  219. });
  220. test( "append(Function)", function() {
  221. testAppend( manipulationFunctionReturningObj );
  222. });
  223. test( "append(param) to object, see #11280", function() {
  224. expect( 5 );
  225. var object = jQuery( document.createElement("object") ).appendTo( document.body );
  226. equal( object.children().length, 0, "object does not start with children" );
  227. object.append( jQuery("<param type='wmode' name='foo'>") );
  228. equal( object.children().length, 1, "appended param" );
  229. equal( object.children().eq(0).attr("name"), "foo", "param has name=foo" );
  230. object = jQuery("<object><param type='baz' name='bar'></object>");
  231. equal( object.children().length, 1, "object created with child param" );
  232. equal( object.children().eq(0).attr("name"), "bar", "param has name=bar" );
  233. });
  234. test( "append(Function) with incoming value", function() {
  235. expect( 12 );
  236. var defaultText, result, select, old, expected;
  237. defaultText = "Try them out:";
  238. old = jQuery("#first").html();
  239. result = jQuery("#first").append(function( i, val ) {
  240. equal( val, old, "Make sure the incoming value is correct." );
  241. return "<b>buga</b>";
  242. });
  243. equal( result.text(), defaultText + "buga", "Check if text appending works" );
  244. select = jQuery("#select3");
  245. old = select.html();
  246. equal( select.append(function( i, val ) {
  247. equal( val, old, "Make sure the incoming value is correct." );
  248. return "<option value='appendTest'>Append Test</option>";
  249. }).find("option:last-child").attr("value"), "appendTest", "Appending html options to select element" );
  250. QUnit.reset();
  251. expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
  252. old = jQuery("#sap").html();
  253. jQuery("#sap").append(function( i, val ) {
  254. equal( val, old, "Make sure the incoming value is correct." );
  255. return document.getElementById("first");
  256. });
  257. equal( jQuery("#sap").text(), expected, "Check for appending of element" );
  258. QUnit.reset();
  259. expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
  260. old = jQuery("#sap").html();
  261. jQuery("#sap").append(function( i, val ) {
  262. equal( val, old, "Make sure the incoming value is correct." );
  263. return [ document.getElementById("first"), document.getElementById("yahoo") ];
  264. });
  265. equal( jQuery("#sap").text(), expected, "Check for appending of array of elements" );
  266. QUnit.reset();
  267. expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
  268. old = jQuery("#sap").html();
  269. jQuery("#sap").append(function( i, val ) {
  270. equal( val, old, "Make sure the incoming value is correct." );
  271. return jQuery("#yahoo, #first");
  272. });
  273. equal( jQuery("#sap").text(), expected, "Check for appending of jQuery object" );
  274. QUnit.reset();
  275. old = jQuery("#sap").html();
  276. jQuery("#sap").append(function( i, val ) {
  277. equal( val, old, "Make sure the incoming value is correct." );
  278. return 5;
  279. });
  280. ok( jQuery("#sap")[ 0 ].innerHTML.match( /5$/ ), "Check for appending a number" );
  281. QUnit.reset();
  282. });
  283. test( "XML DOM manipulation (#9960)", function() {
  284. expect( 5 );
  285. var
  286. 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>"),
  287. xmlDoc2 = jQuery.parseXML("<scxml xmlns='http://www.w3.org/2005/07/scxml' version='1.0'><state id='provisioning3'></state></scxml>"),
  288. xml1 = jQuery( xmlDoc1 ),
  289. xml2 = jQuery( xmlDoc2 ),
  290. scxml1 = jQuery( "scxml", xml1 ),
  291. scxml2 = jQuery( "scxml", xml2 ),
  292. state = scxml2.find("state");
  293. scxml1.append( state );
  294. strictEqual( scxml1[0].lastChild, state[0], "append" );
  295. scxml1.prepend( state );
  296. strictEqual( scxml1[0].firstChild, state[0], "prepend" );
  297. scxml1.find("#finished").after( state );
  298. strictEqual( scxml1[0].lastChild, state[0], "after" );
  299. scxml1.find("#provisioning").before( state );
  300. strictEqual( scxml1[0].firstChild, state[0], "before" );
  301. scxml2.replaceWith( scxml1 );
  302. deepEqual( jQuery( "state", xml2 ).get(), scxml1.find("state").get(), "replaceWith" );
  303. });
  304. test( "append HTML5 sectioning elements (Bug #6485)", function() {
  305. expect( 2 );
  306. var article, aside;
  307. jQuery("#qunit-fixture").append("<article style='font-size:10px'><section><aside>HTML5 elements</aside></section></article>");
  308. article = jQuery("article");
  309. aside = jQuery("aside");
  310. equal( article.get( 0 ).style.fontSize, "10px", "HTML5 elements are styleable" );
  311. equal( aside.length, 1, "HTML5 elements do not collapse their children" );
  312. });
  313. if ( jQuery.css ) {
  314. test( "HTML5 Elements inherit styles from style rules (Bug #10501)", function() {
  315. expect( 1 );
  316. jQuery("#qunit-fixture").append("<article id='article'></article>");
  317. jQuery("#article").append("<section>This section should have a pink background.</section>");
  318. // In IE, the missing background color will claim its value is "transparent"
  319. notEqual( jQuery("section").css("background-color"), "transparent", "HTML5 elements inherit styles" );
  320. });
  321. }
  322. test( "html(String) with HTML5 (Bug #6485)", function() {
  323. expect( 2 );
  324. jQuery("#qunit-fixture").html("<article><section><aside>HTML5 elements</aside></section></article>");
  325. equal( jQuery("#qunit-fixture").children().children().length, 1, "Make sure HTML5 article elements can hold children. innerHTML shortcut path" );
  326. equal( jQuery("#qunit-fixture").children().children().children().length, 1, "Make sure nested HTML5 elements can hold children." );
  327. });
  328. test( "IE8 serialization bug", function() {
  329. expect( 2 );
  330. var wrapper = jQuery("<div></div>");
  331. wrapper.html("<div></div><article></article>");
  332. equal( wrapper.children("article").length, 1, "HTML5 elements are insertable with .html()" );
  333. wrapper.html("<div></div><link></link>");
  334. equal( wrapper.children("link").length, 1, "Link elements are insertable with .html()" );
  335. });
  336. test( "html() object element #10324", function() {
  337. expect( 1 );
  338. var object = jQuery("<object id='object2'><param name='object2test' value='test'></param></object>?").appendTo("#qunit-fixture"),
  339. clone = object.clone();
  340. equal( clone.html(), object.html(), "html() returns correct innerhtml of cloned object elements" );
  341. });
  342. test( "append(xml)", function() {
  343. expect( 1 );
  344. var xmlDoc, xml1, xml2;
  345. function createXMLDoc() {
  346. // Initialize DOM based upon latest installed MSXML or Netscape
  347. var elem, n, len,
  348. aActiveX =
  349. [ "MSXML6.DomDocument",
  350. "MSXML3.DomDocument",
  351. "MSXML2.DomDocument",
  352. "MSXML.DomDocument",
  353. "Microsoft.XmlDom" ];
  354. if ( document.implementation && "createDocument" in document.implementation ) {
  355. return document.implementation.createDocument( "", "", null );
  356. } else {
  357. // IE
  358. for ( n = 0, len = aActiveX.length; n < len; n++ ) {
  359. try {
  360. elem = new ActiveXObject( aActiveX[ n ] );
  361. return elem;
  362. } catch(_) {}
  363. }
  364. }
  365. }
  366. xmlDoc = createXMLDoc();
  367. xml1 = xmlDoc.createElement("head");
  368. xml2 = xmlDoc.createElement("test");
  369. ok( jQuery(xml1).append(xml2), "Append an xml element to another without raising an exception." );
  370. });
  371. test( "appendTo(String|Element|Array<Element>|jQuery)", function() {
  372. expect( 16 );
  373. var defaultText, l, expected, num, div;
  374. defaultText = "Try them out:";
  375. jQuery("<b>buga</b>").appendTo("#first");
  376. equal( jQuery("#first").text(), defaultText + "buga", "Check if text appending works" );
  377. equal( jQuery("<option value='appendTest'>Append Test</option>").appendTo("#select3").parent().find("option:last-child").attr("value"), "appendTest", "Appending html options to select element" );
  378. l = jQuery("#first").children().length + 2;
  379. jQuery("<strong>test</strong>");
  380. jQuery("<strong>test</strong>");
  381. jQuery([ jQuery("<strong>test</strong>")[ 0 ], jQuery("<strong>test</strong>")[ 0 ] ])
  382. .appendTo("#first");
  383. equal( jQuery("#first").children().length, l, "Make sure the elements were inserted." );
  384. equal( jQuery("#first").children().last()[ 0 ].nodeName.toLowerCase(), "strong", "Verify the last element." );
  385. QUnit.reset();
  386. expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
  387. jQuery( document.getElementById("first") ).appendTo("#sap");
  388. equal( jQuery("#sap").text(), expected, "Check for appending of element" );
  389. expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
  390. jQuery([ document.getElementById("first"), document.getElementById("yahoo") ]).appendTo("#sap");
  391. equal( jQuery("#sap").text(), expected, "Check for appending of array of elements" );
  392. QUnit.reset();
  393. ok( jQuery(document.createElement("script")).appendTo("body").length, "Make sure a disconnected script can be appended." );
  394. expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
  395. jQuery("#yahoo, #first").appendTo("#sap");
  396. equal( jQuery("#sap").text(), expected, "Check for appending of jQuery object" );
  397. jQuery("#select1").appendTo("#foo");
  398. t( "Append select", "#foo select", [ "select1" ] );
  399. div = jQuery("<div/>").on( "click", function() {
  400. ok( true, "Running a cloned click." );
  401. });
  402. div.appendTo("#qunit-fixture, #moretests");
  403. jQuery("#qunit-fixture div").last().trigger("click");
  404. jQuery("#moretests div").last().trigger("click");
  405. div = jQuery("<div/>").appendTo("#qunit-fixture, #moretests");
  406. equal( div.length, 2, "appendTo returns the inserted elements" );
  407. div.addClass("test");
  408. ok( jQuery("#qunit-fixture div").last().hasClass("test"), "appendTo element was modified after the insertion" );
  409. ok( jQuery("#moretests div").last().hasClass("test"), "appendTo element was modified after the insertion" );
  410. div = jQuery("<div/>");
  411. jQuery("<span>a</span><b>b</b>").filter("span").appendTo( div );
  412. equal( div.children().length, 1, "Make sure the right number of children were inserted." );
  413. div = jQuery("#moretests div");
  414. num = jQuery("#qunit-fixture div").length;
  415. div.remove().appendTo("#qunit-fixture");
  416. equal( jQuery("#qunit-fixture div").length, num, "Make sure all the removed divs were inserted." );
  417. });
  418. function testPrepend( val ) {
  419. expect( 6 );
  420. var defaultText, result, expected;
  421. defaultText = "Try them out:";
  422. result = jQuery("#first").prepend( val("<b>buga</b>") );
  423. equal( result.text(), "buga" + defaultText, "Check if text prepending works" );
  424. equal( jQuery("#select3").prepend( val("<option value='prependTest'>Prepend Test</option>" ) ).find("option:first-child").attr("value"), "prependTest", "Prepending html options to select element" );
  425. QUnit.reset();
  426. expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
  427. jQuery("#sap").prepend( val(document.getElementById("first")) );
  428. equal( jQuery("#sap").text(), expected, "Check for prepending of element" );
  429. QUnit.reset();
  430. expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
  431. jQuery("#sap").prepend( val([ document.getElementById("first"), document.getElementById("yahoo") ]) );
  432. equal( jQuery("#sap").text(), expected, "Check for prepending of array of elements" );
  433. QUnit.reset();
  434. expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
  435. jQuery("#sap").prepend( val(jQuery("#yahoo, #first")) );
  436. equal( jQuery("#sap").text(), expected, "Check for prepending of jQuery object" );
  437. QUnit.reset();
  438. expected = "Try them out:GoogleYahooThis link has class=\"blog\": Simon Willison's Weblog";
  439. jQuery("#sap").prepend( val([ jQuery("#first"), jQuery("#yahoo, #google") ]) );
  440. equal( jQuery("#sap").text(), expected, "Check for prepending of array of jQuery objects" );
  441. }
  442. test( "prepend(String|Element|Array<Element>|jQuery)", function() {
  443. testPrepend( manipulationBareObj );
  444. });
  445. test( "prepend(Function)", function() {
  446. testPrepend( manipulationFunctionReturningObj );
  447. });
  448. test( "prepend(Function) with incoming value", function() {
  449. expect( 10 );
  450. var defaultText, old, result, expected;
  451. defaultText = "Try them out:";
  452. old = jQuery("#first").html();
  453. result = jQuery("#first").prepend(function( i, val ) {
  454. equal( val, old, "Make sure the incoming value is correct." );
  455. return "<b>buga</b>";
  456. });
  457. equal( result.text(), "buga" + defaultText, "Check if text prepending works" );
  458. old = jQuery("#select3").html();
  459. equal( jQuery("#select3").prepend(function( i, val ) {
  460. equal( val, old, "Make sure the incoming value is correct." );
  461. return "<option value='prependTest'>Prepend Test</option>";
  462. }).find("option:first-child").attr("value"), "prependTest", "Prepending html options to select element" );
  463. QUnit.reset();
  464. expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
  465. old = jQuery("#sap").html();
  466. jQuery("#sap").prepend(function( i, val ) {
  467. equal( val, old, "Make sure the incoming value is correct." );
  468. return document.getElementById("first");
  469. });
  470. equal( jQuery("#sap").text(), expected, "Check for prepending of element" );
  471. QUnit.reset();
  472. expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
  473. old = jQuery("#sap").html();
  474. jQuery("#sap").prepend(function( i, val ) {
  475. equal( val, old, "Make sure the incoming value is correct." );
  476. return [ document.getElementById("first"), document.getElementById("yahoo") ];
  477. });
  478. equal( jQuery("#sap").text(), expected, "Check for prepending of array of elements" );
  479. QUnit.reset();
  480. expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
  481. old = jQuery("#sap").html();
  482. jQuery("#sap").prepend(function( i, val ) {
  483. equal( val, old, "Make sure the incoming value is correct." );
  484. return jQuery("#yahoo, #first");
  485. });
  486. equal( jQuery("#sap").text(), expected, "Check for prepending of jQuery object" );
  487. });
  488. test( "prependTo(String|Element|Array<Element>|jQuery)", function() {
  489. expect( 6 );
  490. var defaultText, expected;
  491. defaultText = "Try them out:";
  492. jQuery("<b>buga</b>").prependTo("#first");
  493. equal( jQuery("#first").text(), "buga" + defaultText, "Check if text prepending works" );
  494. equal( jQuery("<option value='prependTest'>Prepend Test</option>").prependTo("#select3").parent().find("option:first-child").attr("value"), "prependTest", "Prepending html options to select element" );
  495. QUnit.reset();
  496. expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
  497. jQuery( document.getElementById("first") ).prependTo("#sap");
  498. equal( jQuery("#sap").text(), expected, "Check for prepending of element" );
  499. QUnit.reset();
  500. expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
  501. jQuery( [ document.getElementById("first"), document.getElementById("yahoo") ] ).prependTo("#sap");
  502. equal( jQuery("#sap").text(), expected, "Check for prepending of array of elements" );
  503. QUnit.reset();
  504. expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
  505. jQuery("#yahoo, #first").prependTo("#sap");
  506. equal( jQuery("#sap").text(), expected, "Check for prepending of jQuery object" );
  507. QUnit.reset();
  508. jQuery("<select id='prependSelect1'></select>").prependTo("#form");
  509. jQuery("<select id='prependSelect2'><option>Test</option></select>").prependTo("#form");
  510. t( "Prepend Select", "#prependSelect2, #prependSelect1", [ "prependSelect2", "prependSelect1" ] );
  511. });
  512. function testBefore( val ) {
  513. expect( 7 );
  514. var expected, set;
  515. expected = "This is a normal link: bugaYahoo";
  516. jQuery("#yahoo").before( val("<b>buga</b>") );
  517. equal( jQuery("#en").text(), expected, "Insert String before" );
  518. QUnit.reset();
  519. expected = "This is a normal link: Try them out:Yahoo";
  520. jQuery("#yahoo").before( val(document.getElementById("first")) );
  521. equal( jQuery("#en").text(), expected, "Insert element before" );
  522. QUnit.reset();
  523. expected = "This is a normal link: Try them out:diveintomarkYahoo";
  524. jQuery("#yahoo").before( val([ document.getElementById("first"), document.getElementById("mark") ]) );
  525. equal( jQuery("#en").text(), expected, "Insert array of elements before" );
  526. QUnit.reset();
  527. expected = "This is a normal link: diveintomarkTry them out:Yahoo";
  528. jQuery("#yahoo").before( val(jQuery("#mark, #first")) );
  529. equal( jQuery("#en").text(), expected, "Insert jQuery before" );
  530. QUnit.reset();
  531. expected = "This is a normal link: Try them out:GooglediveintomarkYahoo";
  532. jQuery("#yahoo").before( val([ jQuery("#first"), jQuery("#mark, #google") ]) );
  533. equal( jQuery("#en").text(), expected, "Insert array of jQuery objects before" );
  534. set = jQuery("<div/>").before("<span>test</span>");
  535. equal( set[ 0 ].nodeName.toLowerCase(), "div", "Insert before a disconnected node should be a no-op" );
  536. equal( set.length, 1, "Insert the element before the disconnected node. should be a no-op" );
  537. }
  538. test( "before(String|Element|Array<Element>|jQuery)", function() {
  539. testBefore( manipulationBareObj );
  540. });
  541. test( "before(Function)", function() {
  542. testBefore( manipulationFunctionReturningObj );
  543. });
  544. test( "before and after w/ empty object (#10812)", function() {
  545. expect( 1 );
  546. var res;
  547. res = jQuery( "#notInTheDocument" ).before( "(" ).after( ")" );
  548. equal( res.length, 0, "didn't choke on empty object" );
  549. });
  550. test( "before and after on disconnected node (#10517)", function() {
  551. expect( 6 );
  552. var expectedBefore = "This is a normal link: bugaYahoo",
  553. expectedAfter = "This is a normal link: Yahoobuga";
  554. equal( jQuery("<input type='checkbox'/>").before("<div/>").length, 1, "before() on disconnected node is no-op" );
  555. equal( jQuery("<input type='checkbox'/>").after("<div/>").length, 1, "after() on disconnected node is no-op" );
  556. QUnit.reset();
  557. jQuery("#yahoo").add("<span/>").before("<b>buga</b>");
  558. equal( jQuery("#en").text(), expectedBefore, "Insert String before with disconnected node last" );
  559. QUnit.reset();
  560. jQuery("<span/>").add("#yahoo").before("<b>buga</b>");
  561. equal( jQuery("#en").text(), expectedBefore, "Insert String before with disconnected node first" );
  562. QUnit.reset();
  563. jQuery("#yahoo").add("<span/>").after("<b>buga</b>");
  564. equal( jQuery("#en").text(), expectedAfter, "Insert String after with disconnected node last" );
  565. QUnit.reset();
  566. jQuery("<span/>").add("#yahoo").after("<b>buga</b>");
  567. equal( jQuery("#en").text(), expectedAfter, "Insert String after with disconnected node first" );
  568. });
  569. test( "insertBefore(String|Element|Array<Element>|jQuery)", function() {
  570. expect( 4 );
  571. var expected;
  572. expected = "This is a normal link: bugaYahoo";
  573. jQuery("<b>buga</b>").insertBefore("#yahoo");
  574. equal( jQuery("#en").text(), expected, "Insert String before" );
  575. QUnit.reset();
  576. expected = "This is a normal link: Try them out:Yahoo";
  577. jQuery( document.getElementById("first") ).insertBefore("#yahoo");
  578. equal( jQuery("#en").text(), expected, "Insert element before" );
  579. QUnit.reset();
  580. expected = "This is a normal link: Try them out:diveintomarkYahoo";
  581. jQuery( [ document.getElementById("first"), document.getElementById("mark") ] ).insertBefore("#yahoo");
  582. equal( jQuery("#en").text(), expected, "Insert array of elements before" );
  583. QUnit.reset();
  584. expected = "This is a normal link: diveintomarkTry them out:Yahoo";
  585. jQuery("#mark, #first").insertBefore("#yahoo");
  586. equal( jQuery("#en").text(), expected, "Insert jQuery before" );
  587. });
  588. function testAfter( val ) {
  589. expect( 7 );
  590. var set, expected;
  591. expected = "This is a normal link: Yahoobuga";
  592. jQuery("#yahoo").after( val("<b>buga</b>") );
  593. equal( jQuery("#en").text(), expected, "Insert String after" );
  594. QUnit.reset();
  595. expected = "This is a normal link: YahooTry them out:";
  596. jQuery("#yahoo").after( val(document.getElementById("first")) );
  597. equal( jQuery("#en").text(), expected, "Insert element after" );
  598. QUnit.reset();
  599. expected = "This is a normal link: YahooTry them out:diveintomark";
  600. jQuery("#yahoo").after( val([ document.getElementById("first"), document.getElementById("mark") ]) );
  601. equal( jQuery("#en").text(), expected, "Insert array of elements after" );
  602. QUnit.reset();
  603. expected = "This is a normal link: YahoodiveintomarkTry them out:";
  604. jQuery("#yahoo").after(val( jQuery("#mark, #first") ));
  605. equal( jQuery("#en").text(), expected, "Insert jQuery after" );
  606. QUnit.reset();
  607. expected = "This is a normal link: YahooTry them out:Googlediveintomark";
  608. jQuery("#yahoo").after( val([ jQuery("#first"), jQuery("#mark, #google") ]) );
  609. equal( jQuery("#en").text(), expected, "Insert array of jQuery objects after" );
  610. set = jQuery("<div/>").before("<span>test</span>");
  611. equal( set[ 0 ].nodeName.toLowerCase(), "div", "Insert after a disconnected node should be a no-op" );
  612. equal( set.length, 1, "Insert the element after the disconnected node should be a no-op" );
  613. }
  614. test( "after(String|Element|Array<Element>|jQuery)", function() {
  615. testAfter( manipulationBareObj );
  616. });
  617. test( "after(Function)", function() {
  618. testAfter( manipulationFunctionReturningObj );
  619. });
  620. test( "insertAfter(String|Element|Array<Element>|jQuery)", function() {
  621. expect( 4 ) ;
  622. var expected;
  623. expected = "This is a normal link: Yahoobuga";
  624. jQuery("<b>buga</b>").insertAfter("#yahoo");
  625. equal( jQuery("#en").text(), expected, "Insert String after" );
  626. QUnit.reset();
  627. expected = "This is a normal link: YahooTry them out:";
  628. jQuery( document.getElementById("first") ).insertAfter("#yahoo");
  629. equal( jQuery("#en").text(), expected, "Insert element after" );
  630. QUnit.reset();
  631. expected = "This is a normal link: YahooTry them out:diveintomark";
  632. jQuery( [ document.getElementById("first"), document.getElementById("mark") ] ).insertAfter("#yahoo");
  633. equal( jQuery("#en").text(), expected, "Insert array of elements after" );
  634. QUnit.reset();
  635. expected = "This is a normal link: YahoodiveintomarkTry them out:";
  636. jQuery("#mark, #first").insertAfter("#yahoo");
  637. equal( jQuery("#en").text(), expected, "Insert jQuery after" );
  638. });
  639. function testReplaceWith( val ) {
  640. var tmp, y, child, child2, set, non_existent, $div,
  641. expected = 29;
  642. expect( expected );
  643. jQuery("#yahoo").replaceWith( val("<b id='replace'>buga</b>") );
  644. ok( jQuery("#replace")[ 0 ], "Replace element with element from string" );
  645. ok( !jQuery("#yahoo")[ 0 ], "Verify that original element is gone, after string" );
  646. jQuery("#anchor2").replaceWith( val(document.getElementById("first")) );
  647. ok( jQuery("#first")[ 0 ], "Replace element with element" );
  648. ok( !jQuery("#anchor2")[ 0 ], "Verify that original element is gone, after element" );
  649. jQuery("#qunit-fixture").append("<div id='bar'><div id='baz'></div></div>");
  650. jQuery("#baz").replaceWith( val("Baz") );
  651. equal( jQuery("#bar").text(),"Baz", "Replace element with text" );
  652. ok( !jQuery("#baz")[ 0 ], "Verify that original element is gone, after element" );
  653. jQuery("#bar").replaceWith( "<div id='yahoo'></div>", "...", "<div id='baz'></div>" );
  654. deepEqual( jQuery("#yahoo, #baz").get(), q( "yahoo", "baz" ), "Replace element with multiple arguments (#13722)" );
  655. strictEqual( jQuery("#yahoo")[0].nextSibling, jQuery("#baz")[0].previousSibling, "Argument order preserved" );
  656. deepEqual( jQuery("#bar").get(), [], "Verify that original element is gone, after multiple arguments" );
  657. jQuery("#google").replaceWith( val([ document.getElementById("first"), document.getElementById("mark") ]) );
  658. deepEqual( jQuery("#mark, #first").get(), q( "first", "mark" ), "Replace element with array of elements" );
  659. ok( !jQuery("#google")[ 0 ], "Verify that original element is gone, after array of elements" );
  660. jQuery("#groups").replaceWith( val(jQuery("#mark, #first")) );
  661. deepEqual( jQuery("#mark, #first").get(), q( "first", "mark" ), "Replace element with jQuery collection" );
  662. ok( !jQuery("#groups")[ 0 ], "Verify that original element is gone, after jQuery collection" );
  663. jQuery("#mark, #first").replaceWith( val("<span class='replacement'></span><span class='replacement'></span>") );
  664. equal( jQuery("#qunit-fixture .replacement").length, 4, "Replace multiple elements (#12449)" );
  665. deepEqual( jQuery("#mark, #first").get(), [], "Verify that original elements are gone, after replace multiple" );
  666. tmp = jQuery("<b>content</b>")[0];
  667. jQuery("#anchor1").contents().replaceWith( val(tmp) );
  668. deepEqual( jQuery("#anchor1").contents().get(), [ tmp ], "Replace text node with element" );
  669. tmp = jQuery("<div/>").appendTo("#qunit-fixture").on( "click", function() {
  670. ok( true, "Newly bound click run." );
  671. });
  672. y = jQuery("<div/>").appendTo("#qunit-fixture").on( "click", function() {
  673. ok( false, "Previously bound click run." );
  674. });
  675. child = y.append("<b>test</b>").find("b").on( "click", function() {
  676. ok( true, "Child bound click run." );
  677. return false;
  678. });
  679. y.replaceWith( val(tmp) );
  680. tmp.trigger("click");
  681. y.trigger("click"); // Shouldn't be run
  682. child.trigger("click"); // Shouldn't be run
  683. y = jQuery("<div/>").appendTo("#qunit-fixture").on( "click", function() {
  684. ok( false, "Previously bound click run." );
  685. });
  686. child2 = y.append("<u>test</u>").find("u").on( "click", function() {
  687. ok( true, "Child 2 bound click run." );
  688. return false;
  689. });
  690. y.replaceWith( val(child2) );
  691. child2.trigger("click");
  692. set = jQuery("<div/>").replaceWith( val("<span>test</span>") );
  693. equal( set[0].nodeName.toLowerCase(), "div", "No effect on a disconnected node." );
  694. equal( set.length, 1, "No effect on a disconnected node." );
  695. equal( set[0].childNodes.length, 0, "No effect on a disconnected node." );
  696. child = jQuery("#qunit-fixture").children().first();
  697. $div = jQuery("<div class='pathological'/>").insertBefore( child );
  698. $div.replaceWith( $div );
  699. deepEqual( jQuery( ".pathological", "#qunit-fixture" ).get(), $div.get(),
  700. "Self-replacement" );
  701. $div.replaceWith( child );
  702. deepEqual( jQuery("#qunit-fixture").children().first().get(), child.get(),
  703. "Replacement with following sibling (#13810)" );
  704. deepEqual( jQuery( ".pathological", "#qunit-fixture" ).get(), [],
  705. "Replacement with following sibling (context removed)" );
  706. non_existent = jQuery("#does-not-exist").replaceWith( val("<b>should not throw an error</b>") );
  707. equal( non_existent.length, 0, "Length of non existent element." );
  708. $div = jQuery("<div class='replacewith'></div>").appendTo("#qunit-fixture");
  709. $div.replaceWith( val("<div class='replacewith'></div><script>" +
  710. "equal( jQuery('.replacewith').length, 1, 'Check number of elements in page.' );" +
  711. "</script>") );
  712. jQuery("#qunit-fixture").append("<div id='replaceWith'></div>");
  713. equal( jQuery("#qunit-fixture").find("div[id=replaceWith]").length, 1, "Make sure only one div exists." );
  714. jQuery("#replaceWith").replaceWith( val("<div id='replaceWith'></div>") );
  715. equal( jQuery("#qunit-fixture").find("div[id=replaceWith]").length, 1, "Make sure only one div exists after replacement." );
  716. jQuery("#replaceWith").replaceWith( val("<div id='replaceWith'></div>") );
  717. equal( jQuery("#qunit-fixture").find("div[id=replaceWith]").length, 1, "Make sure only one div exists after subsequent replacement." );
  718. return expected;
  719. }
  720. test( "replaceWith(String|Element|Array<Element>|jQuery)", function() {
  721. testReplaceWith( manipulationBareObj );
  722. });
  723. test( "replaceWith(Function)", function() {
  724. expect( testReplaceWith(manipulationFunctionReturningObj) + 1 );
  725. var y = jQuery("#foo")[ 0 ];
  726. jQuery( y ).replaceWith(function() {
  727. equal( this, y, "Make sure the context is coming in correctly." );
  728. });
  729. });
  730. test( "replaceWith(string) for more than one element", function() {
  731. expect( 3 );
  732. equal( jQuery("#foo p").length, 3, "ensuring that test data has not changed" );
  733. jQuery("#foo p").replaceWith("<span>bar</span>");
  734. equal(jQuery("#foo span").length, 3, "verify that all the three original element have been replaced");
  735. equal(jQuery("#foo p").length, 0, "verify that all the three original element have been replaced");
  736. });
  737. test( "empty replaceWith (#13401; #13596)", 4, function() {
  738. expect( 6 );
  739. var $el = jQuery("<div/>"),
  740. tests = {
  741. "empty string": "",
  742. "empty array": [],
  743. "empty collection": jQuery("#nonexistent")
  744. };
  745. jQuery.each( tests, function( label, input ) {
  746. $el.html("<a/>").children().replaceWith( input );
  747. strictEqual( $el.html(), "", "replaceWith(" + label + ")" );
  748. $el.html("<b/>").children().replaceWith(function() { return input; });
  749. strictEqual( $el.html(), "", "replaceWith(function returning " + label + ")" );
  750. });
  751. });
  752. test( "replaceAll(String|Element|Array<Element>|jQuery)", function() {
  753. expect( 10 );
  754. jQuery("<b id='replace'>buga</b>").replaceAll("#yahoo");
  755. ok( jQuery("#replace")[ 0 ], "Replace element with string" );
  756. ok( !jQuery("#yahoo")[ 0 ], "Verify that original element is gone, after string" );
  757. QUnit.reset();
  758. jQuery( document.getElementById("first") ).replaceAll("#yahoo");
  759. ok( jQuery("#first")[ 0 ], "Replace element with element" );
  760. ok( !jQuery("#yahoo")[ 0 ], "Verify that original element is gone, after element" );
  761. QUnit.reset();
  762. jQuery( [ document.getElementById("first"), document.getElementById("mark") ] ).replaceAll("#yahoo");
  763. ok( jQuery("#first")[ 0 ], "Replace element with array of elements" );
  764. ok( jQuery("#mark")[ 0 ], "Replace element with array of elements" );
  765. ok( !jQuery("#yahoo")[ 0 ], "Verify that original element is gone, after array of elements" );
  766. QUnit.reset();
  767. jQuery("#mark, #first").replaceAll("#yahoo");
  768. ok( jQuery("#first")[ 0 ], "Replace element with set of elements" );
  769. ok( jQuery("#mark")[ 0 ], "Replace element with set of elements" );
  770. ok( !jQuery("#yahoo")[ 0 ], "Verify that original element is gone, after set of elements" );
  771. });
  772. test( "jQuery.clone() (#8017)", function() {
  773. expect( 2 );
  774. ok( jQuery.clone && jQuery.isFunction( jQuery.clone ) , "jQuery.clone() utility exists and is a function.");
  775. var main = jQuery("#qunit-fixture")[ 0 ],
  776. clone = jQuery.clone( main );
  777. equal( main.childNodes.length, clone.childNodes.length, "Simple child length to ensure a large dom tree copies correctly" );
  778. });
  779. test( "append to multiple elements (#8070)", function() {
  780. expect( 2 );
  781. var selects = jQuery("<select class='test8070'></select><select class='test8070'></select>").appendTo("#qunit-fixture");
  782. selects.append("<OPTION>1</OPTION><OPTION>2</OPTION>");
  783. equal( selects[ 0 ].childNodes.length, 2, "First select got two nodes" );
  784. equal( selects[ 1 ].childNodes.length, 2, "Second select got two nodes" );
  785. });
  786. test( "table manipulation", function() {
  787. expect( 2 );
  788. var table = jQuery("<table style='font-size:16px'></table>").appendTo("#qunit-fixture").empty(),
  789. height = table[0].offsetHeight;
  790. table.append("<tr><td>DATA</td></tr>");
  791. ok( table[0].offsetHeight - height >= 15, "appended rows are visible" );
  792. table.empty();
  793. height = table[0].offsetHeight;
  794. table.prepend("<tr><td>DATA</td></tr>");
  795. ok( table[0].offsetHeight - height >= 15, "prepended rows are visible" );
  796. });
  797. test( "clone()", function() {
  798. expect( 45 );
  799. var div, clone, form, body;
  800. equal( jQuery("#en").text(), "This is a normal link: Yahoo", "Assert text for #en" );
  801. equal( jQuery("#first").append( jQuery("#yahoo").clone() ).text(), "Try them out:Yahoo", "Check for clone" );
  802. equal( jQuery("#en").text(), "This is a normal link: Yahoo", "Reassert text for #en" );
  803. jQuery.each( "table thead tbody tfoot tr td div button ul ol li select option textarea iframe".split(" "), function( i, nodeName ) {
  804. equal( jQuery( "<" + nodeName + "/>" ).clone()[ 0 ].nodeName.toLowerCase(), nodeName, "Clone a " + nodeName );
  805. });
  806. equal( jQuery("<input type='checkbox' />").clone()[ 0 ].nodeName.toLowerCase(), "input", "Clone a <input type='checkbox' />" );
  807. // Check cloning non-elements
  808. equal( jQuery("#nonnodes").contents().clone().length, 3, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
  809. // Verify that clones of clones can keep event listeners
  810. div = jQuery("<div><ul><li>test</li></ul></div>").on( "click", function() {
  811. ok( true, "Bound event still exists." );
  812. });
  813. clone = div.clone( true ); div.remove();
  814. div = clone.clone( true ); clone.remove();
  815. equal( div.length, 1, "One element cloned" );
  816. equal( div[ 0 ].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
  817. div.trigger("click");
  818. // Manually clean up detached elements
  819. div.remove();
  820. // Verify that cloned children can keep event listeners
  821. div = jQuery("<div/>").append([ document.createElement("table"), document.createElement("table") ]);
  822. div.find("table").on( "click", function() {
  823. ok( true, "Bound event still exists." );
  824. });
  825. clone = div.clone( true );
  826. equal( clone.length, 1, "One element cloned" );
  827. equal( clone[ 0 ].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
  828. clone.find("table").trigger("click");
  829. // Manually clean up detached elements
  830. div.remove();
  831. clone.remove();
  832. // Make sure that doing .clone() doesn't clone event listeners
  833. div = jQuery("<div><ul><li>test</li></ul></div>").on( "click", function() {
  834. ok( false, "Bound event still exists after .clone()." );
  835. });
  836. clone = div.clone();
  837. clone.trigger("click");
  838. // Manually clean up detached elements
  839. clone.remove();
  840. div.remove();
  841. // Test both html() and clone() for <embed> and <object> types
  842. div = jQuery("<div/>").html("<embed height='355' width='425' src='http://www.youtube.com/v/3KANI2dpXLw&amp;hl=en'></embed>");
  843. clone = div.clone( true );
  844. equal( clone.length, 1, "One element cloned" );
  845. equal( clone.html(), div.html(), "Element contents cloned" );
  846. equal( clone[ 0 ].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
  847. // this is technically an invalid object, but because of the special
  848. // classid instantiation it is the only kind that IE has trouble with,
  849. // so let's test with it too.
  850. 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&amp;hl=en'> <param name='wmode' value='transparent'> </object>");
  851. clone = div.clone( true );
  852. equal( clone.length, 1, "One element cloned" );
  853. equal( clone[ 0 ].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
  854. div = div.find("object");
  855. clone = clone.find("object");
  856. // oldIE adds extra attributes and <param> elements, so just test for existence of the defined set
  857. jQuery.each( [ "height", "width", "classid" ], function( i, attr ) {
  858. equal( clone.attr( attr ), div.attr( attr ), "<object> attribute cloned: " + attr );
  859. } );
  860. (function() {
  861. var params = {};
  862. clone.find("param").each(function( index, param ) {
  863. params[ param.attributes.name.nodeValue.toLowerCase() ] =
  864. param.attributes.value.nodeValue.toLowerCase();
  865. });
  866. div.find("param").each(function( index, param ) {
  867. var key = param.attributes.name.nodeValue.toLowerCase();
  868. equal( params[ key ], param.attributes.value.nodeValue.toLowerCase(), "<param> cloned: " + key );
  869. });
  870. })();
  871. // and here's a valid one.
  872. div = jQuery("<div/>").html("<object height='355' width='425' type='application/x-shockwave-flash' data='http://www.youtube.com/v/3KANI2dpXLw&amp;hl=en'> <param name='movie' value='http://www.youtube.com/v/3KANI2dpXLw&amp;hl=en'> <param name='wmode' value='transparent'> </object>");
  873. clone = div.clone(true);
  874. equal( clone.length, 1, "One element cloned" );
  875. equal( clone.html(), div.html(), "Element contents cloned" );
  876. equal( clone[ 0 ].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
  877. div = jQuery("<div/>").data({ "a": true });
  878. clone = div.clone( true );
  879. equal( clone.data("a"), true, "Data cloned." );
  880. clone.data( "a", false );
  881. equal( clone.data("a"), false, "Ensure cloned element data object was correctly modified" );
  882. equal( div.data("a"), true, "Ensure cloned element data object is copied, not referenced" );
  883. // manually clean up detached elements
  884. div.remove();
  885. clone.remove();
  886. form = document.createElement("form");
  887. form.action = "/test/";
  888. div = document.createElement("div");
  889. div.appendChild( document.createTextNode("test") );
  890. form.appendChild( div );
  891. equal( jQuery(form).clone().children().length, 1, "Make sure we just get the form back." );
  892. body = jQuery("body").clone();
  893. equal( body.children()[ 0 ].id, "qunit", "Make sure cloning body works" );
  894. body.remove();
  895. });
  896. test( "clone(script type=non-javascript) (#11359)", function() {
  897. expect( 3 );
  898. var src = jQuery("<script type='text/filler'>Lorem ipsum dolor sit amet</script><q><script type='text/filler'>consectetur adipiscing elit</script></q>"),
  899. dest = src.clone();
  900. equal( dest[ 0 ].text, "Lorem ipsum dolor sit amet", "Cloning preserves script text" );
  901. equal( dest.last().html(), src.last().html(), "Cloning preserves nested script text" );
  902. 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" );
  903. dest.remove();
  904. });
  905. test( "clone(form element) (Bug #3879, #6655)", function() {
  906. expect( 5 );
  907. var clone, element;
  908. element = jQuery("<select><option>Foo</option><option value='selected' selected>Bar</option></select>");
  909. equal( element.clone().find("option").filter(function() { return this.selected; }).val(), "selected", "Selected option cloned correctly" );
  910. element = jQuery("<input type='checkbox' value='foo'>").attr( "checked", "checked" );
  911. clone = element.clone();
  912. equal( clone.is(":checked"), element.is(":checked"), "Checked input cloned correctly" );
  913. equal( clone[ 0 ].defaultValue, "foo", "Checked input defaultValue cloned correctly" );
  914. element = jQuery("<input type='text' value='foo'>");
  915. clone = element.clone();
  916. equal( clone[ 0 ].defaultValue, "foo", "Text input defaultValue cloned correctly" );
  917. element = jQuery("<textarea>foo</textarea>");
  918. clone = element.clone();
  919. equal( clone[ 0 ].defaultValue, "foo", "Textarea defaultValue cloned correctly" );
  920. });
  921. test( "clone(multiple selected options) (Bug #8129)", function() {
  922. expect( 1 );
  923. var element = jQuery("<select><option>Foo</option><option selected>Bar</option><option selected>Baz</option></select>");
  924. equal( element.clone().find("option:selected").length, element.find("option:selected").length, "Multiple selected options cloned correctly" );
  925. });
  926. test( "clone() on XML nodes", function() {
  927. expect( 2 );
  928. var xml = createDashboardXML(),
  929. root = jQuery(xml.documentElement).clone(),
  930. origTab = jQuery("tab", xml).eq( 0 ),
  931. cloneTab = jQuery("tab", root).eq( 0 );
  932. origTab.text("origval");
  933. cloneTab.text("cloneval");
  934. equal( origTab.text(), "origval", "Check original XML node was correctly set" );
  935. equal( cloneTab.text(), "cloneval", "Check cloned XML node was correctly set" );
  936. });
  937. test( "clone() on local XML nodes with html5 nodename", function() {
  938. expect( 2 );
  939. var $xmlDoc = jQuery( jQuery.parseXML( "<root><meter /></root>" ) ),
  940. $meter = $xmlDoc.find( "meter" ).clone();
  941. equal( $meter[ 0 ].nodeName, "meter", "Check if nodeName was not changed due to cloning" );
  942. equal( $meter[ 0 ].nodeType, 1, "Check if nodeType is not changed due to cloning" );
  943. });
  944. test( "html(undefined)", function() {
  945. expect( 1 );
  946. equal( jQuery("#foo").html("<i>test</i>").html(undefined).html().toLowerCase(), "<i>test</i>", ".html(undefined) is chainable (#5571)" );
  947. });
  948. test( "html() on empty set", function() {
  949. expect( 1 );
  950. strictEqual( jQuery().html(), undefined, ".html() returns undefined for empty sets (#11962)" );
  951. });
  952. function childNodeNames( node ) {
  953. return jQuery.map( node.childNodes, function( child ) {
  954. return child.nodeName.toUpperCase();
  955. }).join(" ");
  956. }
  957. function testHtml( valueObj ) {
  958. expect( 37 );
  959. var actual, expected, tmp,
  960. div = jQuery("<div></div>"),
  961. fixture = jQuery("#qunit-fixture");
  962. div.html( valueObj("<div id='parent_1'><div id='child_1'/></div><div id='parent_2'/>") );
  963. equal( div.children().length, 2, "Found children" );
  964. equal( div.children().children().length, 1, "Found grandchild" );
  965. actual = []; expected = [];
  966. tmp = jQuery("<map/>").html( valueObj("<area alt='area'/>") ).each(function() {
  967. expected.push("AREA");
  968. actual.push( childNodeNames( this ) );
  969. });
  970. equal( expected.length, 1, "Expecting one parent" );
  971. deepEqual( actual, expected, "Found the inserted area element" );
  972. equal( div.html(valueObj(5)).html(), "5", "Setting a number as html" );
  973. equal( div.html(valueObj(0)).html(), "0", "Setting a zero as html" );
  974. div.html( valueObj("&#160;&amp;") );
  975. equal(
  976. div[ 0 ].innerHTML.replace( /\xA0/, "&nbsp;" ),
  977. "&nbsp;&amp;",
  978. "Entities are passed through correctly"
  979. );
  980. tmp = "&lt;div&gt;hello1&lt;/div&gt;";
  981. equal( div.html(valueObj(tmp) ).html().replace( />/g, "&gt;" ), tmp, "Escaped html" );
  982. tmp = "x" + tmp;
  983. equal( div.html(valueObj( tmp )).html().replace( />/g, "&gt;" ), tmp, "Escaped html, leading x" );
  984. tmp = " " + tmp.slice( 1 );
  985. equal( div.html(valueObj( tmp )).html().replace( />/g, "&gt;" ), tmp, "Escaped html, leading space" );
  986. actual = []; expected = []; tmp = {};
  987. jQuery("#nonnodes").contents().html( valueObj("<b>bold</b>") ).each(function() {
  988. var html = jQuery( this ).html();
  989. tmp[ this.nodeType ] = true;
  990. expected.push( this.nodeType === 1 ? "<b>bold</b>" : undefined );
  991. actual.push( html ? html.toLowerCase() : html );
  992. });
  993. deepEqual( actual, expected, "Set containing element, text node, comment" );
  994. ok( tmp[ 1 ], "element" );
  995. ok( tmp[ 3 ], "text node" );
  996. ok( tmp[ 8 ], "comment" );
  997. actual = []; expected = [];
  998. fixture.children("div").html( valueObj("<b>test</b>") ).each(function() {
  999. expected.push("B");
  1000. actual.push( childNodeNames( this ) );
  1001. });
  1002. equal( expected.length, 7, "Expecting many parents" );
  1003. deepEqual( actual, expected, "Correct childNodes after setting HTML" );
  1004. actual = []; expected = [];
  1005. fixture.html( valueObj("<style>.foobar{color:green;}</style>") ).each(function() {
  1006. expected.push("STYLE");
  1007. actual.push( childNodeNames( this ) );
  1008. });
  1009. equal( expected.length, 1, "Expecting one parent" );
  1010. deepEqual( actual, expected, "Found the inserted style element" );
  1011. fixture.html( valueObj("<select/>") );
  1012. jQuery("#qunit-fixture select").html( valueObj("<option>O1</option><option selected='selected'>O2</option><option>O3</option>") );
  1013. equal( jQuery("#qunit-fixture select").val(), "O2", "Selected option correct" );
  1014. tmp = fixture.html(
  1015. valueObj([
  1016. "<script type='something/else'>ok( false, 'evaluated: non-script' );</script>",
  1017. "<script type='text/javascript'>ok( true, 'evaluated: text/javascript' );</script>",
  1018. "<script type='text/ecmascript'>ok( true, 'evaluated: text/ecmascript' );</script>",
  1019. "<script>ok( true, 'evaluated: no type' );</script>",
  1020. "<div>",
  1021. "<script type='something/else'>ok( false, 'evaluated: inner non-script' );</script>",
  1022. "<script type='text/javascript'>ok( true, 'evaluated: inner text/javascript' );</script>",
  1023. "<script type='text/ecmascript'>ok( true, 'evaluated: inner text/ecmascript' );</script>",
  1024. "<script>ok( true, 'evaluated: inner no type' );</script>",
  1025. "</div>"
  1026. ].join(""))
  1027. ).find("script");
  1028. equal( tmp.length, 8, "All script tags remain." );
  1029. equal( tmp[ 0 ].type, "something/else", "Non-evaluated type." );
  1030. equal( tmp[ 1 ].type, "text/javascript", "Evaluated type." );
  1031. fixture.html( valueObj("<script type='text/javascript'>ok( true, 'Injection of identical script' );</script>") );
  1032. fixture.html( valueObj("<script type='text/javascript'>ok( true, 'Injection of identical script' );</script>") );
  1033. fixture.html( valueObj("<script type='text/javascript'>ok( true, 'Injection of identical script' );</script>") );
  1034. fixture.html( valueObj("foo <form><script type='text/javascript'>ok( true, 'Injection of identical script (#975)' );</script></form>") );
  1035. jQuery.scriptorder = 0;
  1036. fixture.html( valueObj([
  1037. "<script>",
  1038. "equal( jQuery('#scriptorder').length, 1,'Execute after html' );",
  1039. "equal( jQuery.scriptorder++, 0, 'Script is executed in order' );",
  1040. "</script>",
  1041. "<span id='scriptorder'><script>equal( jQuery.scriptorder++, 1, 'Script (nested) is executed in order');</script></span>",
  1042. "<script>equal( jQuery.scriptorder++, 2, 'Script (unnested) is executed in order' );</script>"
  1043. ].join("")) );
  1044. QUnit.reset();
  1045. fixture.html( valueObj( fixture.text() ) );
  1046. ok( /^[^<]*[^<\s][^<]*$/.test( fixture.html() ), "Replace html with text" );
  1047. }
  1048. test( "html(String)", function() {
  1049. testHtml( manipulationBareObj );
  1050. });
  1051. test( "html(Function)", function() {
  1052. testHtml( manipulationFunctionReturningObj );
  1053. });
  1054. test( "html(Function) with incoming value", function() {
  1055. expect( 18 );
  1056. var els, actualhtml, pass, j, $div, $div2, insert;
  1057. els = jQuery("#foo > p");
  1058. actualhtml = els.map(function() {
  1059. return jQuery( this ).html();
  1060. });
  1061. els.html(function( i, val ) {
  1062. equal( val, actualhtml[ i ], "Make sure the incoming value is correct." );
  1063. return "<b>test</b>";
  1064. });
  1065. pass = true;
  1066. els.each(function() {
  1067. if ( this.childNodes.length !== 1 ) {
  1068. pass = false;
  1069. }
  1070. });
  1071. ok( pass, "Set HTML" );
  1072. QUnit.reset();
  1073. // using contents will get comments regular, text, and comment nodes
  1074. j = jQuery("#nonnodes").contents();
  1075. actualhtml = j.map(function() {
  1076. return jQuery( this ).html();
  1077. });
  1078. j.html(function( i, val ) {
  1079. equal( val, actualhtml[ i ], "Make sure the incoming value is correct." );
  1080. return "<b>bold</b>";
  1081. });
  1082. // Handle the case where no comment is in the document
  1083. if ( j.length === 2 ) {
  1084. equal( null, null, "Make sure the incoming value is correct." );
  1085. }
  1086. equal( j.html().replace( / xmlns="[^"]+"/g, "" ).toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
  1087. $div = jQuery("<div />");
  1088. equal( $div.html(function( i, val ) {
  1089. equal( val, "", "Make sure the incoming value is correct." );
  1090. return 5;
  1091. }).html(), "5", "Setting a number as html" );
  1092. equal( $div.html(function( i, val ) {
  1093. equal( val, "5", "Make sure the incoming value is correct." );
  1094. return 0;
  1095. }).html(), "0", "Setting a zero as html" );
  1096. $div2 = jQuery("<div/>");
  1097. insert = "&lt;div&gt;hello1&lt;/div&gt;";
  1098. equal( $div2.html(function( i, val ) {
  1099. equal( val, "", "Make sure the incoming value is correct." );
  1100. return insert;
  1101. }).html().replace(/>/g, "&gt;"), insert, "Verify escaped insertion." );
  1102. equal( $div2.html(function( i, val ) {
  1103. equal( val.replace(/>/g, "&gt;"), insert, "Make sure the incoming value is correct." );
  1104. return "x" + insert;
  1105. }).html().replace( />/g, "&gt;" ), "x" + insert, "Verify escaped insertion." );
  1106. equal( $div2.html(function( i, val ) {
  1107. equal( val.replace( />/g, "&gt;" ), "x" + insert, "Make sure the incoming value is correct." );
  1108. return " " + insert;
  1109. }).html().replace( />/g, "&gt;" ), " " + insert, "Verify escaped insertion." );
  1110. });
  1111. test( "clone()/html() don't expose jQuery/Sizzle expandos (#12858)", function() {
  1112. expect( 2 );
  1113. var $content = jQuery("<div><b><i>text</i></b></div>").appendTo("#qunit-fixture"),
  1114. expected = /^<b><i>text<\/i><\/b>$/i;
  1115. // Attach jQuery and Sizzle data (the latter with a non-qSA nth-child)
  1116. try {
  1117. $content.find(":nth-child(1):lt(4)").data( "test", true );
  1118. // But don't break on a non-Sizzle build
  1119. } catch( e ) {
  1120. $content.find("*").data( "test", true );
  1121. }
  1122. ok( expected.test( $content.clone( false )[ 0 ].innerHTML ), "clone()" );
  1123. ok( expected.test( $content.html() ), "html()" );
  1124. });
  1125. function testRemove( method ) {
  1126. var markup, div,
  1127. first = jQuery("#ap").children().first();
  1128. first.data("foo", "bar");
  1129. jQuery("#ap").children()[ method ]();
  1130. ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
  1131. equal( jQuery("#ap").children().length, 0, "Check remove" );
  1132. equal( first.data("foo"), method === "remove" ? null : "bar", "first data" );
  1133. QUnit.reset();
  1134. jQuery("#ap").children()[ method ]("a");
  1135. ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
  1136. equal( jQuery("#ap").children().length, 1, "Check filtered remove" );
  1137. jQuery("#ap").children()[ method ]("a, code");
  1138. equal( jQuery("#ap").children().length, 0, "Check multi-filtered remove" );
  1139. // Positional and relative selectors
  1140. markup = "<div><span>1</span><span>2</span><span>3</span><span>4</span></div>";
  1141. div = jQuery( markup );
  1142. div.children().remove("span:nth-child(2n)");
  1143. equal( div.text(), "13", "relative selector in " + method );
  1144. div = jQuery( markup );
  1145. div.children().remove("span:first");
  1146. equal( div.text(), "234", "positional selector in " + method );
  1147. div = jQuery( markup );
  1148. div.children().remove("span:last");
  1149. equal( div.text(), "123", "positional selector in " + method );
  1150. // using contents will get comments regular, text, and comment nodes
  1151. // Handle the case where no comment is in the document
  1152. ok( jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment remove works" );
  1153. jQuery("#nonnodes").contents()[ method ]();
  1154. equal( jQuery("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" );
  1155. // manually clean up detached elements
  1156. if (method === "detach") {
  1157. first.remove();
  1158. }
  1159. }
  1160. test( "remove()", 11, function() {
  1161. testRemove("remove");
  1162. });
  1163. test( "remove() event cleaning ", 1, function() {
  1164. var count, first, cleanUp;
  1165. count = 0;
  1166. first = jQuery("#ap").children().first();
  1167. cleanUp = first.on( "click", function() {
  1168. count++;
  1169. }).remove().appendTo("#qunit-fixture").trigger("click");
  1170. strictEqual( 0, count, "Event handler has been removed" );
  1171. // Clean up detached data
  1172. cleanUp.remove();
  1173. });
  1174. test( "remove() in document order #13779", 1, function() {
  1175. var last,
  1176. cleanData = jQuery.cleanData;
  1177. jQuery.cleanData = function( nodes ) {
  1178. last = jQuery.text( nodes[0] );
  1179. cleanData.call( this, nodes );
  1180. };
  1181. jQuery("#qunit-fixture").append(
  1182. jQuery.parseHTML(
  1183. "<div class='removal-fixture'>1</div>" +
  1184. "<div class='removal-fixture'>2</div>" +
  1185. "<div class='removal-fixture'>3</div>"
  1186. )
  1187. );
  1188. jQuery(".removal-fixture").remove();
  1189. equal( last, 3, "The removal fixtures were removed in document order" );
  1190. jQuery.cleanData = cleanData;
  1191. });
  1192. test( "detach()", 11, function() {
  1193. testRemove("detach");
  1194. });
  1195. test( "detach() event cleaning ", 1, function() {
  1196. var count, first, cleanUp;
  1197. count = 0;
  1198. first = jQuery("#ap").children().first();
  1199. cleanUp = first.on( "click", function() {
  1200. count++;
  1201. }).detach().appendTo("#qunit-fixture").trigger("click");
  1202. strictEqual( 1, count, "Event handler has not been removed" );
  1203. // Clean up detached data
  1204. cleanUp.remove();
  1205. });
  1206. test("empty()", function() {
  1207. expect( 3 );
  1208. equal( jQuery("#ap").children().empty().text().length, 0, "Check text is removed" );
  1209. equal( jQuery("#ap").children().length, 4, "Check elements are not removed" );
  1210. // using contents will get comments regular, text, and comment nodes
  1211. var j = jQuery("#nonnodes").contents();
  1212. j.empty();
  1213. equal( j.html(), "", "Check node,textnode,comment empty works" );
  1214. });
  1215. test( "jQuery.cleanData", function() {
  1216. expect( 14 );
  1217. var type, pos, div, child;
  1218. type = "remove";
  1219. // Should trigger 4 remove event
  1220. div = getDiv().remove();
  1221. // Should both do nothing
  1222. pos = "Outer";
  1223. div.trigger("click");
  1224. pos = "Inner";
  1225. div.children().trigger("click");
  1226. type = "empty";
  1227. div = getDiv();
  1228. child = div.children();
  1229. // Should trigger 2 remove event
  1230. div.empty();
  1231. // Should trigger 1
  1232. pos = "Outer";
  1233. div.trigger("click");
  1234. // Should do nothing
  1235. pos = "Inner";
  1236. child.trigger("click");
  1237. // Should trigger 2
  1238. div.remove();
  1239. type = "html";
  1240. div = getDiv();
  1241. child = div.children();
  1242. // Should trigger 2 remove event
  1243. div.html("<div></div>");
  1244. // Should trigger 1
  1245. pos = "Outer";
  1246. div.trigger("click");
  1247. // Should do nothing
  1248. pos = "Inner";
  1249. child.trigger("click");
  1250. // Should trigger 2
  1251. div.remove();
  1252. function getDiv() {
  1253. var div = jQuery("<div class='outer'><div class='inner'></div></div>").on( "click", function() {
  1254. ok( true, type + " " + pos + " Click event fired." );
  1255. }).on( "focus", function() {
  1256. ok( true, type + " " + pos + " Focus event fired." );
  1257. }).find("div").on( "click", function() {
  1258. ok( false, type + " " + pos + " Click event fired." );
  1259. }).on( "focus", function() {
  1260. ok( false, type + " " + pos + " Focus event fired." );
  1261. }).end().appendTo("body");
  1262. div[ 0 ].detachEvent = div[ 0 ].removeEventListener = function( t ) {
  1263. ok( true, type + " Outer " + t + " event unbound" );
  1264. };
  1265. div[ 0 ].firstChild.detachEvent = div[ 0 ].firstChild.removeEventListener = function( t ) {
  1266. ok( true, type + " Inner " + t + " event unbound" );
  1267. };
  1268. return div;
  1269. }
  1270. });
  1271. test( "jQuery.buildFragment - no plain-text caching (Bug #6779)", function() {
  1272. expect( 1 );
  1273. // DOM manipulation fails if added text matches an Object method
  1274. var i,
  1275. $f = jQuery( "<div />" ).appendTo( "#qunit-fixture" ),
  1276. bad = [ "start-", "toString", "hasOwnProperty", "append", "here&there!", "-end" ];
  1277. for ( i = 0; i < bad.length; i++ ) {
  1278. try {
  1279. $f.append( bad[ i ] );
  1280. }
  1281. catch( e ) {}
  1282. }
  1283. equal( $f.text(), bad.join(""), "Cached strings that match Object properties" );
  1284. $f.remove();
  1285. });
  1286. test( "jQuery.html - execute scripts escaped with html comment or CDATA (#9221)", function() {
  1287. expect( 3 );
  1288. jQuery([
  1289. "<script type='text/javascript'>",
  1290. "<!--",
  1291. "ok( true, '<!-- handled' );",
  1292. "//-->",
  1293. "</script>"
  1294. ].join("\n")).appendTo("#qunit-fixture");
  1295. jQuery([
  1296. "<script type='text/javascript'>",
  1297. "<![CDATA[",
  1298. "ok( true, '<![CDATA[ handled' );",
  1299. "//]]>",
  1300. "</script>"
  1301. ].join("\n")).appendTo("#qunit-fixture");
  1302. jQuery([
  1303. "<script type='text/javascript'>",
  1304. "<!--//--><![CDATA[//><!--",
  1305. "ok( true, '<!--//--><![CDATA[//><!-- (Drupal case) handled' );",
  1306. "//--><!]]>",
  1307. "</script>"
  1308. ].join("\n")).appendTo("#qunit-fixture");
  1309. });
  1310. test( "jQuery.buildFragment - plain objects are not a document #8950", function() {
  1311. expect( 1 );
  1312. try {
  1313. jQuery( "<input type='hidden'>", {} );
  1314. ok( true, "Does not allow attribute object to be treated like a doc object" );
  1315. } catch ( e ) {}
  1316. });
  1317. test( "jQuery.clone - no exceptions for object elements #9587", function() {
  1318. expect( 1 );
  1319. try {
  1320. jQuery("#no-clone-exception").clone();
  1321. ok( true, "cloned with no exceptions" );
  1322. } catch( e ) {
  1323. ok( false, e.message );
  1324. }
  1325. });
  1326. test( "Cloned, detached HTML5 elems (#10667,10670)", function() {
  1327. expect( 7 );
  1328. var $clone,
  1329. $section = jQuery( "<section>" ).appendTo( "#qunit-fixture" );
  1330. // First clone
  1331. $clone = $section.clone();
  1332. // Infer that the test is being run in IE<=8
  1333. if ( $clone[ 0 ].outerHTML && !jQuery.support.opacity ) {
  1334. // This branch tests cloning nodes by reading the outerHTML, used only in IE<=8
  1335. equal( $clone[ 0 ].outerHTML, "<section></section>", "detached clone outerHTML matches '<section></section>'" );
  1336. } else {
  1337. // This branch tests a known behaviour in modern browsers that should never fail.
  1338. // Included for expected test count symmetry (expecting 1)
  1339. equal( $clone[ 0 ].nodeName, "SECTION", "detached clone nodeName matches 'SECTION' in modern browsers" );
  1340. }
  1341. // Bind an event
  1342. $section.on( "click", function() {
  1343. ok( true, "clone fired event" );
  1344. });
  1345. // Second clone (will have an event bound)
  1346. $clone = $section.clone( true );
  1347. // Trigger an event from the first clone
  1348. $clone.trigger("click");
  1349. $clone.off("click");
  1350. // Add a child node with text to the original
  1351. $section.append("<p>Hello</p>");
  1352. // Third clone (will have child node and text)
  1353. $clone = $section.clone( true );
  1354. equal( $clone.find("p").text(), "Hello", "Assert text in child of clone" );
  1355. // Trigger an event from the third clone
  1356. $clone.trigger("click");
  1357. $clone.off("click");
  1358. // Add attributes to copy
  1359. $section.attr({
  1360. "class": "foo bar baz",
  1361. "title": "This is a title"
  1362. });
  1363. // Fourth clone (will have newly added attributes)
  1364. $clone = $section.clone( true );
  1365. equal( $clone.attr("class"), $section.attr("class"), "clone and element have same class attribute" );
  1366. equal( $clone.attr("title"), $section.attr("title"), "clone and element have same title attribute" );
  1367. // Remove the original
  1368. $section.remove();
  1369. // Clone the clone
  1370. $section = $clone.clone( true );
  1371. // Remove the clone
  1372. $clone.remove();
  1373. // Trigger an event from the clone of the clone
  1374. $section.trigger("click");
  1375. // Unbind any remaining events
  1376. $section.off("click");
  1377. $clone.off("click");
  1378. });
  1379. test( "Guard against exceptions when clearing safeChildNodes", function() {
  1380. expect( 1 );
  1381. var div;
  1382. try {
  1383. div = jQuery("<div/><hr/><code/><b/>");
  1384. } catch(e) {}
  1385. ok( div && div.jquery, "Created nodes safely, guarded against exceptions on safeChildNodes[ -1 ]" );
  1386. });
  1387. test( "Ensure oldIE creates a new set on appendTo (#8894)", function() {
  1388. expect( 5 );
  1389. strictEqual( jQuery("<div/>").clone().addClass("test").appendTo("<div/>").end().end().hasClass("test"), false, "Check jQuery.fn.appendTo after jQuery.clone" );
  1390. strictEqual( jQuery("<div/>").find("p").end().addClass("test").appendTo("<div/>").end().end().hasClass("test"), false, "Check jQuery.fn.appendTo after jQuery.fn.find" );
  1391. strictEqual( jQuery("<div/>").text("test").addClass("test").appendTo("<div/>").end().end().hasClass("test"), false, "Check jQuery.fn.appendTo after jQuery.fn.text" );
  1392. strictEqual( jQuery("<bdi/>").clone().addClass("test").appendTo("<div/>").end().end().hasClass("test"), false, "Check jQuery.fn.appendTo after clone html5 element" );
  1393. 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" );
  1394. });
  1395. test( "html() - script exceptions bubble (#11743)", function() {
  1396. expect( 3 );
  1397. raises(function() {
  1398. jQuery("#qunit-fixture").html("<script>undefined(); ok( false, 'Exception not thrown' );</script>");
  1399. ok( false, "Exception ignored" );
  1400. }, "Exception bubbled from inline script" );
  1401. if ( jQuery.ajax ) {
  1402. var onerror = window.onerror;
  1403. window.onerror = function() {
  1404. ok( true, "Exception thrown in remote script" );
  1405. };
  1406. jQuery("#qunit-fixture").html("<script src='data/badcall.js'></script>");
  1407. ok( true, "Exception ignored" );
  1408. window.onerror = onerror;
  1409. } else {
  1410. ok( true, "No jQuery.ajax" );
  1411. ok( true, "No jQuery.ajax" );
  1412. }
  1413. });
  1414. test( "checked state is cloned with clone()", function() {
  1415. expect( 2 );
  1416. var elem = jQuery.parseHTML("<input type='checkbox' checked='checked'/>")[ 0 ];
  1417. elem.checked = false;
  1418. equal( jQuery(elem).clone().attr("id","clone")[ 0 ].checked, false, "Checked false state correctly cloned" );
  1419. elem = jQuery.parseHTML("<input type='checkbox'/>")[ 0 ];
  1420. elem.checked = true;
  1421. equal( jQuery(elem).clone().attr("id","clone")[ 0 ].checked, true, "Checked true state correctly cloned" );
  1422. });
  1423. test( "manipulate mixed jQuery and text (#12384, #12346)", function() {
  1424. expect( 2 );
  1425. var div = jQuery("<div>a</div>").append( "&nbsp;", jQuery("<span>b</span>"), "&nbsp;", jQuery("<span>c</span>") ),
  1426. nbsp = String.fromCharCode( 160 );
  1427. equal( div.text(), "a" + nbsp + "b" + nbsp+ "c", "Appending mixed jQuery with text nodes" );
  1428. div = jQuery("<div><div></div></div>")
  1429. .find("div")
  1430. .after( "<p>a</p>", "<p>b</p>" )
  1431. .parent();
  1432. equal( div.find("*").length, 3, "added 2 paragraphs after inner div" );
  1433. });
  1434. testIframeWithCallback( "buildFragment works even if document[0] is iframe's window object in IE9/10 (#12266)", "manipulation/iframe-denied.html", function( test ) {
  1435. expect( 1 );
  1436. ok( test.status, test.description );
  1437. });
  1438. test( "script evaluation (#11795)", function() {
  1439. expect( 13 );
  1440. var scriptsIn, scriptsOut,
  1441. fixture = jQuery("#qunit-fixture").empty(),
  1442. objGlobal = (function() {
  1443. return this;
  1444. })(),
  1445. isOk = objGlobal.ok,
  1446. notOk = function() {
  1447. var args = arguments;
  1448. args[ 0 ] = !args[ 0 ];
  1449. return isOk.apply( this, args );
  1450. };
  1451. objGlobal.ok = notOk;
  1452. scriptsIn = jQuery([
  1453. "<script type='something/else'>ok( false, 'evaluated: non-script' );</script>",
  1454. "<script type='text/javascript'>ok( true, 'evaluated: text/javascript' );</script>",
  1455. "<script type='text/ecmascript'>ok( true, 'evaluated: text/ecmascript' );</script>",
  1456. "<script>ok( true, 'evaluated: no type' );</script>",
  1457. "<div>",
  1458. "<script type='something/else'>ok( false, 'evaluated: inner non-script' );</script>",
  1459. "<script type='text/javascript'>ok( true, 'evaluated: inner text/javascript' );</script>",
  1460. "<script type='text/ecmascript'>ok( true, 'evaluated: inner text/ecmascript' );</script>",
  1461. "<script>ok( true, 'evaluated: inner no type' );</script>",
  1462. "</div>"
  1463. ].join(""));
  1464. scriptsIn.appendTo( jQuery("<div class='detached'/>") );
  1465. objGlobal.ok = isOk;
  1466. scriptsOut = fixture.append( scriptsIn ).find("script");
  1467. equal( scriptsOut[ 0 ].type, "something/else", "Non-evaluated type." );
  1468. equal( scriptsOut[ 1 ].type, "text/javascript", "Evaluated type." );
  1469. deepEqual( scriptsOut.get(), fixture.find("script").get(), "All script tags remain." );
  1470. objGlobal.ok = notOk;
  1471. scriptsOut = scriptsOut.add( scriptsOut.clone() ).appendTo( fixture.find("div") );
  1472. deepEqual( fixture.find("div script").get(), scriptsOut.get(), "Scripts cloned without reevaluation" );
  1473. fixture.append( scriptsOut.detach() );
  1474. deepEqual( fixture.children("script").get(), scriptsOut.get(), "Scripts detached without reevaluation" );
  1475. objGlobal.ok = isOk;
  1476. if ( jQuery.ajax ) {
  1477. Globals.register("testBar");
  1478. jQuery("#qunit-fixture").append( "<script src='" + url("data/test.js") + "'/>" );
  1479. strictEqual( window["testBar"], "bar", "Global script evaluation" );
  1480. } else {
  1481. ok( true, "No jQuery.ajax" );
  1482. ok( true, "No jQuery.ajax" );
  1483. }
  1484. });
  1485. test( "jQuery._evalUrl (#12838)", function() {
  1486. expect( 5 );
  1487. var message, expectedArgument,
  1488. ajax = jQuery.ajax,
  1489. evalUrl = jQuery._evalUrl;
  1490. message = "jQuery.ajax implementation";
  1491. expectedArgument = 1;
  1492. jQuery.ajax = function( input ) {
  1493. equal( ( input.url || input ).slice( -1 ), expectedArgument, message );
  1494. expectedArgument++;
  1495. };
  1496. jQuery("#qunit-fixture").append("<script src='1'/><script src='2'/>");
  1497. equal( expectedArgument, 3, "synchronous execution" );
  1498. message = "custom implementation";
  1499. expectedArgument = 3;
  1500. jQuery._evalUrl = jQuery.ajax;
  1501. jQuery.ajax = function( options ) {
  1502. strictEqual( options, {}, "Unexpected call to jQuery.ajax" );
  1503. };
  1504. jQuery("#qunit-fixture").append("<script src='3'/><script src='4'/>");
  1505. jQuery.ajax = ajax;
  1506. jQuery._evalUrl = evalUrl;
  1507. });
  1508. test( "insertAfter, insertBefore, etc do not work when destination is original element. Element is removed (#4087)", function() {
  1509. expect( 10 );
  1510. var elems;
  1511. jQuery.each([
  1512. "appendTo",
  1513. "prependTo",
  1514. "insertBefore",
  1515. "insertAfter",
  1516. "replaceAll"
  1517. ], function( index, name ) {
  1518. elems = jQuery( [
  1519. "<ul id='test4087-complex'><li class='test4087'><div>c1</div>h1</li><li><div>c2</div>h2</li></ul>",
  1520. "<div id='test4087-simple'><div class='test4087-1'>1<div class='test4087-2'>2</div><div class='test4087-3'>3</div></div></div>",
  1521. "<div id='test4087-multiple'><div class='test4087-multiple'>1</div><div class='test4087-multiple'>2</div></div>"
  1522. ].join("") ).appendTo( "#qunit-fixture" );
  1523. // complex case based on http://jsfiddle.net/pbramos/gZ7vB/
  1524. jQuery("#test4087-complex div")[ name ]("#test4087-complex li:last-child div:last-child");
  1525. equal( jQuery("#test4087-complex li:last-child div").length, name === "replaceAll" ? 1 : 2, name +" a node to itself, complex case." );
  1526. // simple case
  1527. jQuery( ".test4087-1" )[ name ](".test4087-1");
  1528. equal( jQuery(".test4087-1").length, 1, name +" a node to itself, simple case." );
  1529. // clean for next test
  1530. jQuery("#test4087-complex").remove();
  1531. jQuery("#test4087-simple").remove();
  1532. jQuery("#test4087-multiple").remove();
  1533. });
  1534. });
  1535. test( "Index for function argument should be received (#13094)", 2, function() {
  1536. var i = 0;
  1537. jQuery("<div/><div/>").before(function( index ) {
  1538. equal( index, i++, "Index should be correct" );
  1539. });
  1540. });
  1541. test( "Make sure jQuery.fn.remove can work on elements in documentFragment", 1, function() {
  1542. var fragment = document.createDocumentFragment(),
  1543. div = fragment.appendChild( document.createElement("div") );
  1544. jQuery( div ).remove();
  1545. equal( fragment.childNodes.length, 0, "div element was removed from documentFragment" );
  1546. });
  1547. test( "Make sure specific elements with content created correctly (#13232)", 20, function() {
  1548. var results = [],
  1549. args = [],
  1550. elems = {
  1551. thead: "<tr><td>thead</td></tr>",
  1552. tbody: "<tr><td>tbody</td></tr>",
  1553. tfoot: "<tr><td>tfoot</td></tr>",
  1554. colgroup: "<col span='5' />",
  1555. caption: "caption",
  1556. tr: "<td>tr</td>",
  1557. th: "th",
  1558. td: "<div>td</div>",
  1559. optgroup: "<option>optgroup</option>",
  1560. option: "option"
  1561. };
  1562. jQuery.each( elems, function( name, value ) {
  1563. var html = "<" + name + ">" + value + "</" + name + ">";
  1564. ok( jQuery.nodeName( jQuery.parseHTML( "<" + name + ">" + value + "</" + name + ">" )[ 0 ], name ), name + " is created correctly" );
  1565. results.push( name );
  1566. args.push( html );
  1567. });
  1568. jQuery.fn.append.apply( jQuery("<div/>"), args ).children().each(function( i ) {
  1569. ok( jQuery.nodeName( this, results[ i ] ) );
  1570. });
  1571. });
  1572. test( "Validate creation of multiple quantities of certain elements (#13818)", 44, function() {
  1573. var tags = [ "thead", "tbody", "tfoot", "colgroup", "col", "caption", "tr", "th", "td", "optgroup", "option" ];
  1574. jQuery.each( tags, function( index, tag ) {
  1575. jQuery( "<" + tag + "/><" + tag + "/>" ).each(function() {
  1576. ok( jQuery.nodeName( this, tag ), tag + " empty elements created correctly" );
  1577. });
  1578. jQuery( "<" + this + "></" + tag + "><" + tag + "></" + tag + ">" ).each(function() {
  1579. ok( jQuery.nodeName( this, tag ), tag + " elements with closing tag created correctly" );
  1580. });
  1581. });
  1582. });