manipulation.js 80 KB

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