Geen omschrijving

manipulation.js 109KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017
  1. QUnit.module( "manipulation", {
  2. afterEach: moduleTeardown
  3. } );
  4. // Ensure that an extended Array prototype doesn't break jQuery
  5. Array.prototype.arrayProtoFn = function() {
  6. };
  7. function manipulationBareObj( value ) {
  8. return value;
  9. }
  10. function manipulationFunctionReturningObj( value ) {
  11. return function() {
  12. return value;
  13. };
  14. }
  15. /*
  16. ======== local reference =======
  17. manipulationBareObj and manipulationFunctionReturningObj can be used to test passing functions to setters
  18. See testVal below for an example
  19. bareObj( value );
  20. This function returns whatever value is passed in
  21. functionReturningObj( value );
  22. Returns a function that returns the value
  23. */
  24. QUnit.test( "text()", function( assert ) {
  25. assert.expect( 5 );
  26. var expected, frag, $newLineTest;
  27. expected = "This link has class=\"blog\": Simon Willison's Weblog";
  28. assert.equal( jQuery( "#sap" ).text(), expected, "Check for merged text of more then one element." );
  29. // Check serialization of text values
  30. assert.equal( jQuery( document.createTextNode( "foo" ) ).text(), "foo", "Text node was retrieved from .text()." );
  31. assert.notEqual( jQuery( document ).text(), "", "Retrieving text for the document retrieves all text (#10724)." );
  32. // Retrieve from document fragments #10864
  33. frag = document.createDocumentFragment();
  34. frag.appendChild( document.createTextNode( "foo" ) );
  35. assert.equal( jQuery( frag ).text(), "foo", "Document Fragment Text node was retrieved from .text()." );
  36. $newLineTest = jQuery( "<div>test<br/>testy</div>" ).appendTo( "#moretests" );
  37. $newLineTest.find( "br" ).replaceWith( "\n" );
  38. assert.equal( $newLineTest.text(), "test\ntesty", "text() does not remove new lines (#11153)" );
  39. $newLineTest.remove();
  40. } );
  41. QUnit.test( "text(undefined)", function( assert ) {
  42. assert.expect( 1 );
  43. assert.equal( jQuery( "#foo" ).text( "<div" ).text( undefined )[ 0 ].innerHTML, "&lt;div", ".text(undefined) is chainable (#5571)" );
  44. } );
  45. function testText( valueObj, assert ) {
  46. assert.expect( 6 );
  47. var val, j, expected, $multipleElements, $parentDiv, $childDiv;
  48. val = valueObj( "<div><b>Hello</b> cruel world!</div>" );
  49. assert.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" );
  50. // using contents will get comments regular, text, and comment nodes
  51. j = jQuery( "#nonnodes" ).contents();
  52. j.text( valueObj( "hi!" ) );
  53. assert.equal( jQuery( j[ 0 ] ).text(), "hi!", "Check node,textnode,comment with text()" );
  54. assert.equal( j[ 1 ].nodeValue, " there ", "Check node,textnode,comment with text()" );
  55. assert.equal( j[ 2 ].nodeType, 8, "Check node,textnode,comment with text()" );
  56. // Update multiple elements #11809
  57. expected = "New";
  58. $multipleElements = jQuery( "<div>Hello</div>" ).add( "<div>World</div>" );
  59. $multipleElements.text( expected );
  60. assert.equal( $multipleElements.eq( 0 ).text(), expected, "text() updates multiple elements (#11809)" );
  61. assert.equal( $multipleElements.eq( 1 ).text(), expected, "text() updates multiple elements (#11809)" );
  62. // Prevent memory leaks #11809
  63. $childDiv = jQuery( "<div></div>" );
  64. $childDiv.data( "leak", true );
  65. $parentDiv = jQuery( "<div></div>" );
  66. $parentDiv.append( $childDiv );
  67. $parentDiv.text( "Dry off" );
  68. }
  69. QUnit.test( "text(String)", function( assert ) {
  70. testText( manipulationBareObj, assert );
  71. } );
  72. QUnit.test( "text(Function)", function( assert ) {
  73. testText( manipulationFunctionReturningObj, assert );
  74. } );
  75. QUnit.test( "text(Function) with incoming value", function( assert ) {
  76. assert.expect( 2 );
  77. var old = "This link has class=\"blog\": Simon Willison's Weblog";
  78. jQuery( "#sap" ).text( function( i, val ) {
  79. assert.equal( val, old, "Make sure the incoming value is correct." );
  80. return "foobar";
  81. } );
  82. assert.equal( jQuery( "#sap" ).text(), "foobar", "Check for merged text of more then one element." );
  83. } );
  84. function testAppendForObject( valueObj, isFragment, assert ) {
  85. var $base,
  86. type = isFragment ? " (DocumentFragment)" : " (Element)",
  87. text = "This link has class=\"blog\": Simon Willison's Weblog",
  88. el = document.getElementById( "sap" ).cloneNode( true ),
  89. first = document.getElementById( "first" ),
  90. yahoo = document.getElementById( "yahoo" );
  91. if ( isFragment ) {
  92. $base = document.createDocumentFragment();
  93. jQuery( el ).contents().each( function() {
  94. $base.appendChild( this );
  95. } );
  96. $base = jQuery( $base );
  97. } else {
  98. $base = jQuery( el );
  99. }
  100. assert.equal( $base.clone().append( valueObj( first.cloneNode( true ) ) ).text(),
  101. text + "Try them out:",
  102. "Check for appending of element" + type
  103. );
  104. assert.equal( $base.clone().append( valueObj( [ first.cloneNode( true ), yahoo.cloneNode( true ) ] ) ).text(),
  105. text + "Try them out:Yahoo",
  106. "Check for appending of array of elements" + type
  107. );
  108. assert.equal( $base.clone().append( valueObj( jQuery( "#yahoo, #first" ).clone() ) ).text(),
  109. text + "YahooTry them out:",
  110. "Check for appending of jQuery object" + type
  111. );
  112. assert.equal( $base.clone().append( valueObj( 5 ) ).text(),
  113. text + "5",
  114. "Check for appending a number" + type
  115. );
  116. assert.equal( $base.clone().append( valueObj( [ jQuery( "#first" ).clone(), jQuery( "#yahoo, #google" ).clone() ] ) ).text(),
  117. text + "Try them out:GoogleYahoo",
  118. "Check for appending of array of jQuery objects"
  119. );
  120. assert.equal( $base.clone().append( valueObj( " text with spaces " ) ).text(),
  121. text + " text with spaces ",
  122. "Check for appending text with spaces" + type
  123. );
  124. assert.equal( $base.clone().append( valueObj( [] ) ).text(),
  125. text,
  126. "Check for appending an empty array" + type
  127. );
  128. assert.equal( $base.clone().append( valueObj( "" ) ).text(),
  129. text,
  130. "Check for appending an empty string" + type
  131. );
  132. assert.equal( $base.clone().append( valueObj( document.getElementsByTagName( "foo" ) ) ).text(),
  133. text,
  134. "Check for appending an empty nodelist" + type
  135. );
  136. assert.equal( $base.clone().append( "<span></span>", "<span></span>", "<span></span>" ).children().length,
  137. $base.children().length + 3,
  138. "Make sure that multiple arguments works." + type
  139. );
  140. assert.equal( $base.clone().append( valueObj( document.getElementById( "form" ).cloneNode( true ) ) ).children( "form" ).length,
  141. 1,
  142. "Check for appending a form (#910)" + type
  143. );
  144. }
  145. function testAppend( valueObj, assert ) {
  146. assert.expect( 78 );
  147. testAppendForObject( valueObj, false, assert );
  148. testAppendForObject( valueObj, true, assert );
  149. var defaultText, result, message, iframe, iframeDoc, j, d,
  150. $input, $radioChecked, $radioUnchecked, $radioParent, $map, $table;
  151. defaultText = "Try them out:";
  152. result = jQuery( "#first" ).append( valueObj( "<b>buga</b>" ) );
  153. assert.equal( result.text(), defaultText + "buga", "Check if text appending works" );
  154. assert.equal( jQuery( "#select3" ).append( valueObj( "<option value='appendTest'>Append Test</option>" ) ).find( "option:last-child" ).attr( "value" ), "appendTest", "Appending html options to select element" );
  155. jQuery( "#qunit-fixture form" ).append( valueObj( "<input name='radiotest' type='radio' checked='checked' />" ) );
  156. jQuery( "#qunit-fixture form input[name=radiotest]" ).each( function() {
  157. assert.ok( jQuery( this ).is( ":checked" ), "Append checked radio" );
  158. } ).remove();
  159. jQuery( "#qunit-fixture form" ).append( valueObj( "<input name='radiotest2' type='radio' checked = 'checked' />" ) );
  160. jQuery( "#qunit-fixture form input[name=radiotest2]" ).each( function() {
  161. assert.ok( jQuery( this ).is( ":checked" ), "Append alternately formatted checked radio" );
  162. } ).remove();
  163. jQuery( "#qunit-fixture form" ).append( valueObj( "<input name='radiotest3' type='radio' checked />" ) );
  164. jQuery( "#qunit-fixture form input[name=radiotest3]" ).each( function() {
  165. assert.ok( jQuery( this ).is( ":checked" ), "Append HTML5-formatted checked radio" );
  166. } ).remove();
  167. jQuery( "#qunit-fixture form" ).append( valueObj( "<input type='radio' checked='checked' name='radiotest4' />" ) );
  168. jQuery( "#qunit-fixture form input[name=radiotest4]" ).each( function() {
  169. assert.ok( jQuery( this ).is( ":checked" ), "Append with name attribute after checked attribute" );
  170. } ).remove();
  171. message = "Test for appending a DOM node to the contents of an iframe";
  172. iframe = jQuery( "#iframe" )[ 0 ];
  173. iframeDoc = iframe.contentDocument || iframe.contentWindow && iframe.contentWindow.document;
  174. try {
  175. if ( iframeDoc && iframeDoc.body ) {
  176. assert.equal( jQuery( iframeDoc.body ).append( valueObj( "<div id='success'>test</div>" ) )[ 0 ].lastChild.id, "success", message );
  177. } else {
  178. assert.ok( true, message + " - can't test" );
  179. }
  180. } catch ( e ) {
  181. assert.strictEqual( e.message || e, undefined, message );
  182. }
  183. jQuery( "<fieldset></fieldset>" ).appendTo( "#form" ).append( valueObj( "<legend id='legend'>test</legend>" ) );
  184. assert.t( "Append legend", "#legend", [ "legend" ] );
  185. $map = jQuery( "<map></map>" ).append( valueObj( "<area id='map01' shape='rect' coords='50,50,150,150' href='http://www.jquery.com/' alt='jQuery'>" ) );
  186. assert.equal( $map[ 0 ].childNodes.length, 1, "The area was inserted." );
  187. assert.equal( $map[ 0 ].firstChild.nodeName.toLowerCase(), "area", "The area was inserted." );
  188. jQuery( "#select1" ).append( valueObj( "<OPTION>Test</OPTION>" ) );
  189. assert.equal( jQuery( "#select1 option:last-child" ).text(), "Test", "Appending OPTION (all caps)" );
  190. jQuery( "#select1" ).append( valueObj( "<optgroup label='optgroup'><option>optgroup</option></optgroup>" ) );
  191. assert.equal( jQuery( "#select1 optgroup" ).attr( "label" ), "optgroup", "Label attribute in newly inserted optgroup is correct" );
  192. assert.equal( jQuery( "#select1 option" ).last().text(), "optgroup", "Appending optgroup" );
  193. $table = jQuery( "#table" );
  194. jQuery.each( "thead tbody tfoot colgroup caption tr th td".split( " " ), function( i, name ) {
  195. $table.append( valueObj( "<" + name + "/>" ) );
  196. assert.equal( $table.find( name ).length, 1, "Append " + name );
  197. assert.ok( jQuery.parseHTML( "<" + name + "/>" ).length, name + " wrapped correctly" );
  198. } );
  199. jQuery( "#table colgroup" ).append( valueObj( "<col></col>" ) );
  200. assert.equal( jQuery( "#table colgroup col" ).length, 1, "Append col" );
  201. jQuery( "#form" )
  202. .append( valueObj( "<select id='appendSelect1'></select>" ) )
  203. .append( valueObj( "<select id='appendSelect2'><option>Test</option></select>" ) );
  204. assert.t( "Append Select", "#appendSelect1, #appendSelect2", [ "appendSelect1", "appendSelect2" ] );
  205. assert.equal( "Two nodes", jQuery( "<div></div>" ).append( "Two", " nodes" ).text(), "Appending two text nodes (#4011)" );
  206. assert.equal( jQuery( "<div></div>" ).append( "1", "", 3 ).text(), "13", "If median is false-like value, subsequent arguments should not be ignored" );
  207. // using contents will get comments regular, text, and comment nodes
  208. j = jQuery( "#nonnodes" ).contents();
  209. d = jQuery( "<div></div>" ).appendTo( "#nonnodes" ).append( j );
  210. assert.equal( jQuery( "#nonnodes" ).length, 1, "Check node,textnode,comment append moved leaving just the div" );
  211. assert.equal( d.contents().length, 3, "Check node,textnode,comment append works" );
  212. d.contents().appendTo( "#nonnodes" );
  213. d.remove();
  214. assert.equal( jQuery( "#nonnodes" ).contents().length, 3, "Check node,textnode,comment append cleanup worked" );
  215. $input = jQuery( "<input type='checkbox'/>" ).prop( "checked", true ).appendTo( "#testForm" );
  216. assert.equal( $input[ 0 ].checked, true, "A checked checkbox that is appended stays checked" );
  217. $radioChecked = jQuery( "input[type='radio'][name='R1']" ).eq( 1 );
  218. $radioParent = $radioChecked.parent();
  219. $radioUnchecked = jQuery( "<input type='radio' name='R1' checked='checked'/>" ).appendTo( $radioParent );
  220. $radioChecked.trigger( "click" );
  221. $radioUnchecked[ 0 ].checked = false;
  222. jQuery( "<div></div>" ).insertBefore( $radioParent ).append( $radioParent );
  223. assert.equal( $radioChecked[ 0 ].checked, true, "Reappending radios uphold which radio is checked" );
  224. assert.equal( $radioUnchecked[ 0 ].checked, false, "Reappending radios uphold not being checked" );
  225. assert.equal( jQuery( "<div></div>" ).append( valueObj( "option<area></area>" ) )[ 0 ].childNodes.length, 2, "HTML-string with leading text should be processed correctly" );
  226. }
  227. QUnit.test( "append(String|Element|Array<Element>|jQuery)", function( assert ) {
  228. testAppend( manipulationBareObj, assert );
  229. } );
  230. QUnit.test( "append(Function)", function( assert ) {
  231. testAppend( manipulationFunctionReturningObj, assert );
  232. } );
  233. QUnit.test( "append(param) to object, see #11280", function( assert ) {
  234. assert.expect( 5 );
  235. var object = jQuery( document.createElement( "object" ) ).appendTo( document.body );
  236. assert.equal( object.children().length, 0, "object does not start with children" );
  237. object.append( jQuery( "<param type='wmode' name='foo'>" ) );
  238. assert.equal( object.children().length, 1, "appended param" );
  239. assert.equal( object.children().eq( 0 ).attr( "name" ), "foo", "param has name=foo" );
  240. object = jQuery( "<object><param type='baz' name='bar'></object>" );
  241. assert.equal( object.children().length, 1, "object created with child param" );
  242. assert.equal( object.children().eq( 0 ).attr( "name" ), "bar", "param has name=bar" );
  243. } );
  244. QUnit.test( "append(Function) returns String", function( assert ) {
  245. assert.expect( 4 );
  246. var defaultText, result, select, old;
  247. defaultText = "Try them out:";
  248. old = jQuery( "#first" ).html();
  249. result = jQuery( "#first" ).append( function( i, val ) {
  250. assert.equal( val, old, "Make sure the incoming value is correct." );
  251. return "<b>buga</b>";
  252. } );
  253. assert.equal( result.text(), defaultText + "buga", "Check if text appending works" );
  254. select = jQuery( "#select3" );
  255. old = select.html();
  256. assert.equal( select.append( function( i, val ) {
  257. assert.equal( val, old, "Make sure the incoming value is correct." );
  258. return "<option value='appendTest'>Append Test</option>";
  259. } ).find( "option:last-child" ).attr( "value" ), "appendTest", "Appending html options to select element" );
  260. } );
  261. QUnit.test( "append(Function) returns Element", function( assert ) {
  262. assert.expect( 2 );
  263. var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:",
  264. old = jQuery( "#sap" ).html();
  265. jQuery( "#sap" ).append( function( i, val ) {
  266. assert.equal( val, old, "Make sure the incoming value is correct." );
  267. return document.getElementById( "first" );
  268. } );
  269. assert.equal( jQuery( "#sap" ).text(), expected, "Check for appending of element" );
  270. } );
  271. QUnit.test( "append(Function) returns Array<Element>", function( assert ) {
  272. assert.expect( 2 );
  273. var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo",
  274. old = jQuery( "#sap" ).html();
  275. jQuery( "#sap" ).append( function( i, val ) {
  276. assert.equal( val, old, "Make sure the incoming value is correct." );
  277. return [ document.getElementById( "first" ), document.getElementById( "yahoo" ) ];
  278. } );
  279. assert.equal( jQuery( "#sap" ).text(), expected, "Check for appending of array of elements" );
  280. } );
  281. QUnit.test( "append(Function) returns jQuery", function( assert ) {
  282. assert.expect( 2 );
  283. var expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:",
  284. old = jQuery( "#sap" ).html();
  285. jQuery( "#sap" ).append( function( i, val ) {
  286. assert.equal( val, old, "Make sure the incoming value is correct." );
  287. return jQuery( "#yahoo, #first" );
  288. } );
  289. assert.equal( jQuery( "#sap" ).text(), expected, "Check for appending of jQuery object" );
  290. } );
  291. QUnit.test( "append(Function) returns Number", function( assert ) {
  292. assert.expect( 2 );
  293. var old = jQuery( "#sap" ).html();
  294. jQuery( "#sap" ).append( function( i, val ) {
  295. assert.equal( val, old, "Make sure the incoming value is correct." );
  296. return 5;
  297. } );
  298. assert.ok( jQuery( "#sap" )[ 0 ].innerHTML.match( /5$/ ), "Check for appending a number" );
  299. } );
  300. QUnit.test( "XML DOM manipulation (#9960)", function( assert ) {
  301. assert.expect( 5 );
  302. var 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>" ),
  303. xmlDoc2 = jQuery.parseXML( "<scxml xmlns='http://www.w3.org/2005/07/scxml' version='1.0'><state id='provisioning3'></state></scxml>" ),
  304. xml1 = jQuery( xmlDoc1 ),
  305. xml2 = jQuery( xmlDoc2 ),
  306. scxml1 = jQuery( "scxml", xml1 ),
  307. scxml2 = jQuery( "scxml", xml2 ),
  308. state = scxml2.find( "state" );
  309. scxml1.append( state );
  310. assert.strictEqual( scxml1[ 0 ].lastChild, state[ 0 ], "append" );
  311. scxml1.prepend( state );
  312. assert.strictEqual( scxml1[ 0 ].firstChild, state[ 0 ], "prepend" );
  313. scxml1.find( "#finished" ).after( state );
  314. assert.strictEqual( scxml1[ 0 ].lastChild, state[ 0 ], "after" );
  315. scxml1.find( "#provisioning" ).before( state );
  316. assert.strictEqual( scxml1[ 0 ].firstChild, state[ 0 ], "before" );
  317. scxml2.replaceWith( scxml1 );
  318. assert.deepEqual( jQuery( "state", xml2 ).get(), scxml1.find( "state" ).get(), "replaceWith" );
  319. } );
  320. QUnit.test( "append HTML5 sectioning elements (Bug #6485)", function( assert ) {
  321. assert.expect( 2 );
  322. var article, aside;
  323. jQuery( "#qunit-fixture" ).append( "<article style='font-size:10px'><section><aside>HTML5 elements</aside></section></article>" );
  324. article = jQuery( "article" );
  325. aside = jQuery( "aside" );
  326. assert.equal( article.get( 0 ).style.fontSize, "10px", "HTML5 elements are styleable" );
  327. assert.equal( aside.length, 1, "HTML5 elements do not collapse their children" );
  328. } );
  329. if ( jQuery.css ) {
  330. QUnit.test( "HTML5 Elements inherit styles from style rules (Bug #10501)", function( assert ) {
  331. assert.expect( 1 );
  332. jQuery( "#qunit-fixture" ).append( "<article id='article'></article>" );
  333. jQuery( "#article" ).append( "<section>This section should have a pink background.</section>" );
  334. // In IE, the missing background color will claim its value is "transparent"
  335. assert.notEqual( jQuery( "section" ).css( "background-color" ), "transparent", "HTML5 elements inherit styles" );
  336. } );
  337. }
  338. QUnit.test( "html(String) with HTML5 (Bug #6485)", function( assert ) {
  339. assert.expect( 2 );
  340. jQuery( "#qunit-fixture" ).html( "<article><section><aside>HTML5 elements</aside></section></article>" );
  341. assert.equal( jQuery( "#qunit-fixture" ).children().children().length, 1, "Make sure HTML5 article elements can hold children. innerHTML shortcut path" );
  342. assert.equal( jQuery( "#qunit-fixture" ).children().children().children().length, 1, "Make sure nested HTML5 elements can hold children." );
  343. } );
  344. QUnit.test( "html(String) tag-hyphenated elements (Bug #1987)", function( assert ) {
  345. assert.expect( 27 );
  346. jQuery.each( "thead tbody tfoot colgroup caption tr th td".split( " " ), function( i, name ) {
  347. var j = jQuery( "<" + name + "-d></" + name + "-d><" + name + "-d></" + name + "-d>" );
  348. assert.ok( j[ 0 ], "Create a tag-hyphenated element" );
  349. assert.ok( j[ 0 ].nodeName === name.toUpperCase() + "-D", "Hyphenated node name" );
  350. assert.ok( j[ 1 ].nodeName === name.toUpperCase() + "-D", "Hyphenated node name" );
  351. } );
  352. var j = jQuery( "<tr-multiple-hyphens><td-with-hyphen>text</td-with-hyphen></tr-multiple-hyphens>" );
  353. assert.ok( j[ 0 ].nodeName === "TR-MULTIPLE-HYPHENS", "Tags with multiple hyphens" );
  354. assert.ok( j.children()[ 0 ].nodeName === "TD-WITH-HYPHEN", "Tags with multiple hyphens" );
  355. assert.equal( j.children().text(), "text", "Tags with multiple hyphens behave normally" );
  356. } );
  357. QUnit.test( "Tag name processing respects the HTML Standard (gh-2005)", function( assert ) {
  358. assert.expect( 240 );
  359. var wrapper = jQuery( "<div></div>" ),
  360. nameTerminatingChars = "\x20\t\r\n\f".split( "" ),
  361. specialChars = "[ ] { } _ - = + \\ ( ) * & ^ % $ # @ ! ~ ` ' ; ? ¥ « µ λ ⊕ ≈ ξ ℜ ♣ €"
  362. .split( " " );
  363. specialChars.push( specialChars.join( "" ) );
  364. jQuery.each( specialChars, function( i, characters ) {
  365. assertSpecialCharsSupport( "html", characters );
  366. assertSpecialCharsSupport( "append", characters );
  367. } );
  368. jQuery.each( nameTerminatingChars, function( i, character ) {
  369. assertNameTerminatingCharsHandling( "html", character );
  370. assertNameTerminatingCharsHandling( "append", character );
  371. } );
  372. function buildChild( method, html ) {
  373. wrapper[ method ]( html );
  374. return wrapper.children()[ 0 ];
  375. }
  376. function assertSpecialCharsSupport( method, characters ) {
  377. // Support: Android 4.4 only
  378. // Chromium < 35 incorrectly upper-cases µ; Android 4.4 uses such a version by default
  379. // (and its WebView, being un-updatable, will use it for eternity) so we need to blacklist
  380. // that one for the tests to pass.
  381. if ( characters === "µ" && /chrome/i.test( navigator.userAgent ) &&
  382. navigator.userAgent.match( /chrome\/(\d+)/i )[ 1 ] < 35 ) {
  383. assert.ok( true, "This Chromium version upper-cases µ incorrectly; skip test" );
  384. assert.ok( true, "This Chromium version upper-cases µ incorrectly; skip test" );
  385. assert.ok( true, "This Chromium version upper-cases µ incorrectly; skip test" );
  386. return;
  387. }
  388. var child,
  389. codepoint = characters.charCodeAt( 0 ).toString( 16 ).toUpperCase(),
  390. description = characters.length === 1 ?
  391. "U+" + ( "000" + codepoint ).slice( -4 ) + " " + characters :
  392. "all special characters",
  393. nodeName = "valid" + characters + "tagname";
  394. child = buildChild( method, "<" + nodeName + "></" + nodeName + ">" );
  395. assert.equal( child.nodeName.toUpperCase(), nodeName.toUpperCase(),
  396. method + "(): Paired tag name includes " + description );
  397. child = buildChild( method, "<" + nodeName + ">" );
  398. assert.equal( child.nodeName.toUpperCase(), nodeName.toUpperCase(),
  399. method + "(): Unpaired tag name includes " + description );
  400. child = buildChild( method, "<" + nodeName + "/>" );
  401. assert.equal( child.nodeName.toUpperCase(), nodeName.toUpperCase(),
  402. method + "(): Self-closing tag name includes " + description );
  403. }
  404. function assertNameTerminatingCharsHandling( method, character ) {
  405. var child,
  406. codepoint = character.charCodeAt( 0 ).toString( 16 ).toUpperCase(),
  407. description = "U+" + ( "000" + codepoint ).slice( -4 ) + " " + character,
  408. nodeName = "div" + character + "this-will-be-discarded";
  409. child = buildChild( method, "<" + nodeName + "></" + nodeName + ">" );
  410. assert.equal( child.nodeName.toUpperCase(), "DIV",
  411. method + "(): Paired tag name terminated by " + description );
  412. child = buildChild( method, "<" + nodeName + ">" );
  413. assert.equal( child.nodeName.toUpperCase(), "DIV",
  414. method + "(): Unpaired open tag name terminated by " + description );
  415. child = buildChild( method, "<" + nodeName + "/>" );
  416. assert.equal( child.nodeName.toUpperCase(), "DIV",
  417. method + "(): Self-closing tag name terminated by " + description );
  418. }
  419. } );
  420. QUnit.test( "IE8 serialization bug", function( assert ) {
  421. assert.expect( 2 );
  422. var wrapper = jQuery( "<div></div>" );
  423. wrapper.html( "<div></div><article></article>" );
  424. assert.equal( wrapper.children( "article" ).length, 1, "HTML5 elements are insertable with .html()" );
  425. wrapper.html( "<div></div><link></link>" );
  426. assert.equal( wrapper.children( "link" ).length, 1, "Link elements are insertable with .html()" );
  427. } );
  428. QUnit.test( "html() object element #10324", function( assert ) {
  429. assert.expect( 1 );
  430. var object = jQuery( "<object id='object2'><param name='object2test' value='test'></param></object>?" ).appendTo( "#qunit-fixture" ),
  431. clone = object.clone();
  432. assert.equal( clone.html(), object.html(), "html() returns correct innerhtml of cloned object elements" );
  433. } );
  434. QUnit.test( "append(xml)", function( assert ) {
  435. assert.expect( 1 );
  436. var xmlDoc, xml1, xml2;
  437. function createXMLDoc() {
  438. // Initialize DOM based upon latest installed MSXML or Netscape
  439. var elem, n, len,
  440. aActiveX =
  441. [ "MSXML6.DomDocument",
  442. "MSXML3.DomDocument",
  443. "MSXML2.DomDocument",
  444. "MSXML.DomDocument",
  445. "Microsoft.XmlDom" ];
  446. if ( document.implementation && "createDocument" in document.implementation ) {
  447. return document.implementation.createDocument( "", "", null );
  448. } else {
  449. // IE
  450. for ( n = 0, len = aActiveX.length; n < len; n++ ) {
  451. try {
  452. elem = new window.ActiveXObject( aActiveX[ n ] );
  453. return elem;
  454. } catch ( _ ) {}
  455. }
  456. }
  457. }
  458. xmlDoc = createXMLDoc();
  459. xml1 = xmlDoc.createElement( "head" );
  460. xml2 = xmlDoc.createElement( "test" );
  461. assert.ok( jQuery( xml1 ).append( xml2 ), "Append an xml element to another without raising an exception." );
  462. } );
  463. QUnit.test( "appendTo(String)", function( assert ) {
  464. assert.expect( 4 );
  465. var l, defaultText;
  466. defaultText = "Try them out:";
  467. jQuery( "<b>buga</b>" ).appendTo( "#first" );
  468. assert.equal( jQuery( "#first" ).text(), defaultText + "buga", "Check if text appending works" );
  469. assert.equal( jQuery( "<option value='appendTest'>Append Test</option>" ).appendTo( "#select3" ).parent().find( "option:last-child" ).attr( "value" ), "appendTest", "Appending html options to select element" );
  470. l = jQuery( "#first" ).children().length + 2;
  471. jQuery( "<strong>test</strong>" );
  472. jQuery( "<strong>test</strong>" );
  473. jQuery( [ jQuery( "<strong>test</strong>" )[ 0 ], jQuery( "<strong>test</strong>" )[ 0 ] ] )
  474. .appendTo( "#first" );
  475. assert.equal( jQuery( "#first" ).children().length, l, "Make sure the elements were inserted." );
  476. assert.equal( jQuery( "#first" ).children().last()[ 0 ].nodeName.toLowerCase(), "strong", "Verify the last element." );
  477. } );
  478. QUnit.test( "appendTo(Element|Array<Element>)", function( assert ) {
  479. assert.expect( 2 );
  480. var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
  481. jQuery( document.getElementById( "first" ) ).appendTo( "#sap" );
  482. assert.equal( jQuery( "#sap" ).text(), expected, "Check for appending of element" );
  483. expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
  484. jQuery( [ document.getElementById( "first" ), document.getElementById( "yahoo" ) ] ).appendTo( "#sap" );
  485. assert.equal( jQuery( "#sap" ).text(), expected, "Check for appending of array of elements" );
  486. } );
  487. QUnit.test( "appendTo(jQuery)", function( assert ) {
  488. assert.expect( 10 );
  489. var expected, num, div;
  490. assert.ok( jQuery( document.createElement( "script" ) ).appendTo( "body" ).length, "Make sure a disconnected script can be appended." );
  491. expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
  492. jQuery( "#yahoo, #first" ).appendTo( "#sap" );
  493. assert.equal( jQuery( "#sap" ).text(), expected, "Check for appending of jQuery object" );
  494. jQuery( "#select1" ).appendTo( "#foo" );
  495. assert.t( "Append select", "#foo select", [ "select1" ] );
  496. div = jQuery( "<div></div>" ).on( "click", function() {
  497. assert.ok( true, "Running a cloned click." );
  498. } );
  499. div.appendTo( "#qunit-fixture, #moretests" );
  500. jQuery( "#qunit-fixture div" ).last().trigger( "click" );
  501. jQuery( "#moretests div" ).last().trigger( "click" );
  502. div = jQuery( "<div></div>" ).appendTo( "#qunit-fixture, #moretests" );
  503. assert.equal( div.length, 2, "appendTo returns the inserted elements" );
  504. div.addClass( "test" );
  505. assert.ok( jQuery( "#qunit-fixture div" ).last().hasClass( "test" ), "appendTo element was modified after the insertion" );
  506. assert.ok( jQuery( "#moretests div" ).last().hasClass( "test" ), "appendTo element was modified after the insertion" );
  507. div = jQuery( "<div></div>" );
  508. jQuery( "<span>a</span><b>b</b>" ).filter( "span" ).appendTo( div );
  509. assert.equal( div.children().length, 1, "Make sure the right number of children were inserted." );
  510. div = jQuery( "#moretests div" );
  511. num = jQuery( "#qunit-fixture div" ).length;
  512. div.remove().appendTo( "#qunit-fixture" );
  513. assert.equal( jQuery( "#qunit-fixture div" ).length, num, "Make sure all the removed divs were inserted." );
  514. } );
  515. QUnit.test( "prepend(String)", function( assert ) {
  516. assert.expect( 2 );
  517. var result, expected;
  518. expected = "Try them out:";
  519. result = jQuery( "#first" ).prepend( "<b>buga</b>" );
  520. assert.equal( result.text(), "buga" + expected, "Check if text prepending works" );
  521. assert.equal( jQuery( "#select3" ).prepend( "<option value='prependTest'>Prepend Test</option>" ).find( "option:first-child" ).attr( "value" ), "prependTest", "Prepending html options to select element" );
  522. } );
  523. QUnit.test( "prepend(Element)", function( assert ) {
  524. assert.expect( 1 );
  525. var expected;
  526. expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
  527. jQuery( "#sap" ).prepend( document.getElementById( "first" ) );
  528. assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of element" );
  529. } );
  530. QUnit.test( "prepend(Array<Element>)", function( assert ) {
  531. assert.expect( 1 );
  532. var expected;
  533. expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
  534. jQuery( "#sap" ).prepend( [ document.getElementById( "first" ), document.getElementById( "yahoo" ) ] );
  535. assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of array of elements" );
  536. } );
  537. QUnit.test( "prepend(jQuery)", function( assert ) {
  538. assert.expect( 1 );
  539. var expected;
  540. expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
  541. jQuery( "#sap" ).prepend( jQuery( "#yahoo, #first" ) );
  542. assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of jQuery object" );
  543. } );
  544. QUnit.test( "prepend(Array<jQuery>)", function( assert ) {
  545. assert.expect( 1 );
  546. var expected;
  547. expected = "Try them out:GoogleYahooThis link has class=\"blog\": Simon Willison's Weblog";
  548. jQuery( "#sap" ).prepend( [ jQuery( "#first" ), jQuery( "#yahoo, #google" ) ] );
  549. assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of array of jQuery objects" );
  550. } );
  551. QUnit.test( "prepend(Function) with incoming value -- String", function( assert ) {
  552. assert.expect( 4 );
  553. var defaultText, old, result;
  554. defaultText = "Try them out:";
  555. old = jQuery( "#first" ).html();
  556. result = jQuery( "#first" ).prepend( function( i, val ) {
  557. assert.equal( val, old, "Make sure the incoming value is correct." );
  558. return "<b>buga</b>";
  559. } );
  560. assert.equal( result.text(), "buga" + defaultText, "Check if text prepending works" );
  561. old = jQuery( "#select3" ).html();
  562. assert.equal( jQuery( "#select3" ).prepend( function( i, val ) {
  563. assert.equal( val, old, "Make sure the incoming value is correct." );
  564. return "<option value='prependTest'>Prepend Test</option>";
  565. } ).find( "option:first-child" ).attr( "value" ), "prependTest", "Prepending html options to select element" );
  566. } );
  567. QUnit.test( "prepend(Function) with incoming value -- Element", function( assert ) {
  568. assert.expect( 2 );
  569. var old, expected;
  570. expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
  571. old = jQuery( "#sap" ).html();
  572. jQuery( "#sap" ).prepend( function( i, val ) {
  573. assert.equal( val, old, "Make sure the incoming value is correct." );
  574. return document.getElementById( "first" );
  575. } );
  576. assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of element" );
  577. } );
  578. QUnit.test( "prepend(Function) with incoming value -- Array<Element>", function( assert ) {
  579. assert.expect( 2 );
  580. var old, expected;
  581. expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
  582. old = jQuery( "#sap" ).html();
  583. jQuery( "#sap" ).prepend( function( i, val ) {
  584. assert.equal( val, old, "Make sure the incoming value is correct." );
  585. return [ document.getElementById( "first" ), document.getElementById( "yahoo" ) ];
  586. } );
  587. assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of array of elements" );
  588. } );
  589. QUnit.test( "prepend(Function) with incoming value -- jQuery", function( assert ) {
  590. assert.expect( 2 );
  591. var old, expected;
  592. expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
  593. old = jQuery( "#sap" ).html();
  594. jQuery( "#sap" ).prepend( function( i, val ) {
  595. assert.equal( val, old, "Make sure the incoming value is correct." );
  596. return jQuery( "#yahoo, #first" );
  597. } );
  598. assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of jQuery object" );
  599. } );
  600. QUnit.test( "prependTo(String)", function( assert ) {
  601. assert.expect( 2 );
  602. var defaultText;
  603. defaultText = "Try them out:";
  604. jQuery( "<b>buga</b>" ).prependTo( "#first" );
  605. assert.equal( jQuery( "#first" ).text(), "buga" + defaultText, "Check if text prepending works" );
  606. assert.equal( jQuery( "<option value='prependTest'>Prepend Test</option>" ).prependTo( "#select3" ).parent().find( "option:first-child" ).attr( "value" ), "prependTest", "Prepending html options to select element" );
  607. } );
  608. QUnit.test( "prependTo(Element)", function( assert ) {
  609. assert.expect( 1 );
  610. var expected;
  611. expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
  612. jQuery( document.getElementById( "first" ) ).prependTo( "#sap" );
  613. assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of element" );
  614. } );
  615. QUnit.test( "prependTo(Array<Element>)", function( assert ) {
  616. assert.expect( 1 );
  617. var expected;
  618. expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
  619. jQuery( [ document.getElementById( "first" ), document.getElementById( "yahoo" ) ] ).prependTo( "#sap" );
  620. assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of array of elements" );
  621. } );
  622. QUnit.test( "prependTo(jQuery)", function( assert ) {
  623. assert.expect( 1 );
  624. var expected;
  625. expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
  626. jQuery( "#yahoo, #first" ).prependTo( "#sap" );
  627. assert.equal( jQuery( "#sap" ).text(), expected, "Check for prepending of jQuery object" );
  628. } );
  629. QUnit.test( "prependTo(Array<jQuery>)", function( assert ) {
  630. assert.expect( 1 );
  631. jQuery( "<select id='prependSelect1'></select>" ).prependTo( "#form" );
  632. jQuery( "<select id='prependSelect2'><option>Test</option></select>" ).prependTo( "#form" );
  633. assert.t( "Prepend Select", "#prependSelect2, #prependSelect1", [ "prependSelect2", "prependSelect1" ] );
  634. } );
  635. QUnit.test( "before(String)", function( assert ) {
  636. assert.expect( 1 );
  637. var expected;
  638. expected = "This is a normal link: bugaYahoo";
  639. jQuery( "#yahoo" ).before( manipulationBareObj( "<b>buga</b>" ) );
  640. assert.equal( jQuery( "#en" ).text(), expected, "Insert String before" );
  641. } );
  642. QUnit.test( "before(Element)", function( assert ) {
  643. assert.expect( 1 );
  644. var expected;
  645. expected = "This is a normal link: Try them out:Yahoo";
  646. jQuery( "#yahoo" ).before( manipulationBareObj( document.getElementById( "first" ) ) );
  647. assert.equal( jQuery( "#en" ).text(), expected, "Insert element before" );
  648. } );
  649. QUnit.test( "before(Array<Element>)", function( assert ) {
  650. assert.expect( 1 );
  651. var expected;
  652. expected = "This is a normal link: Try them out:diveintomarkYahoo";
  653. jQuery( "#yahoo" ).before( manipulationBareObj( [ document.getElementById( "first" ), document.getElementById( "mark" ) ] ) );
  654. assert.equal( jQuery( "#en" ).text(), expected, "Insert array of elements before" );
  655. } );
  656. QUnit.test( "before(jQuery)", function( assert ) {
  657. assert.expect( 1 );
  658. var expected;
  659. expected = "This is a normal link: diveintomarkTry them out:Yahoo";
  660. jQuery( "#yahoo" ).before( manipulationBareObj( jQuery( "#mark, #first" ) ) );
  661. assert.equal( jQuery( "#en" ).text(), expected, "Insert jQuery before" );
  662. } );
  663. QUnit.test( "before(Array<jQuery>)", function( assert ) {
  664. assert.expect( 1 );
  665. var expected;
  666. expected = "This is a normal link: Try them out:GooglediveintomarkYahoo";
  667. jQuery( "#yahoo" ).before( manipulationBareObj( [ jQuery( "#first" ), jQuery( "#mark, #google" ) ] ) );
  668. assert.equal( jQuery( "#en" ).text(), expected, "Insert array of jQuery objects before" );
  669. } );
  670. QUnit.test( "before(Function) -- Returns String", function( assert ) {
  671. assert.expect( 1 );
  672. var expected;
  673. expected = "This is a normal link: bugaYahoo";
  674. jQuery( "#yahoo" ).before( manipulationFunctionReturningObj( "<b>buga</b>" ) );
  675. assert.equal( jQuery( "#en" ).text(), expected, "Insert String before" );
  676. } );
  677. QUnit.test( "before(Function) -- Returns Element", function( assert ) {
  678. assert.expect( 1 );
  679. var expected;
  680. expected = "This is a normal link: Try them out:Yahoo";
  681. jQuery( "#yahoo" ).before( manipulationFunctionReturningObj( document.getElementById( "first" ) ) );
  682. assert.equal( jQuery( "#en" ).text(), expected, "Insert element before" );
  683. } );
  684. QUnit.test( "before(Function) -- Returns Array<Element>", function( assert ) {
  685. assert.expect( 1 );
  686. var expected;
  687. expected = "This is a normal link: Try them out:diveintomarkYahoo";
  688. jQuery( "#yahoo" ).before( manipulationFunctionReturningObj( [ document.getElementById( "first" ), document.getElementById( "mark" ) ] ) );
  689. assert.equal( jQuery( "#en" ).text(), expected, "Insert array of elements before" );
  690. } );
  691. QUnit.test( "before(Function) -- Returns jQuery", function( assert ) {
  692. assert.expect( 1 );
  693. var expected;
  694. expected = "This is a normal link: diveintomarkTry them out:Yahoo";
  695. jQuery( "#yahoo" ).before( manipulationFunctionReturningObj( jQuery( "#mark, #first" ) ) );
  696. assert.equal( jQuery( "#en" ).text(), expected, "Insert jQuery before" );
  697. } );
  698. QUnit.test( "before(Function) -- Returns Array<jQuery>", function( assert ) {
  699. assert.expect( 1 );
  700. var expected;
  701. expected = "This is a normal link: Try them out:GooglediveintomarkYahoo";
  702. jQuery( "#yahoo" ).before( manipulationFunctionReturningObj( [ jQuery( "#first" ), jQuery( "#mark, #google" ) ] ) );
  703. assert.equal( jQuery( "#en" ).text(), expected, "Insert array of jQuery objects before" );
  704. } );
  705. QUnit.test( "before(no-op)", function( assert ) {
  706. assert.expect( 2 );
  707. var set;
  708. set = jQuery( "<div></div>" ).before( "<span>test</span>" );
  709. assert.equal( set[ 0 ].nodeName.toLowerCase(), "div", "Insert before a disconnected node should be a no-op" );
  710. assert.equal( set.length, 1, "Insert the element before the disconnected node. should be a no-op" );
  711. } );
  712. QUnit.test( "before and after w/ empty object (#10812)", function( assert ) {
  713. assert.expect( 1 );
  714. var res;
  715. res = jQuery( "#notInTheDocument" ).before( "(" ).after( ")" );
  716. assert.equal( res.length, 0, "didn't choke on empty object" );
  717. } );
  718. QUnit.test( ".before() and .after() disconnected node", function( assert ) {
  719. assert.expect( 2 );
  720. assert.equal( jQuery( "<input type='checkbox'/>" ).before( "<div></div>" ).length, 1, "before() on disconnected node is no-op" );
  721. assert.equal( jQuery( "<input type='checkbox'/>" ).after( "<div></div>" ).length, 1, "after() on disconnected node is no-op" );
  722. } );
  723. QUnit.test( "insert with .before() on disconnected node last", function( assert ) {
  724. assert.expect( 1 );
  725. var expectedBefore = "This is a normal link: bugaYahoo";
  726. jQuery( "#yahoo" ).add( "<span></span>" ).before( "<b>buga</b>" );
  727. assert.equal( jQuery( "#en" ).text(), expectedBefore, "Insert String before with disconnected node last" );
  728. } );
  729. QUnit.test( "insert with .before() on disconnected node first", function( assert ) {
  730. assert.expect( 1 );
  731. var expectedBefore = "This is a normal link: bugaYahoo";
  732. jQuery( "<span></span>" ).add( "#yahoo" ).before( "<b>buga</b>" );
  733. assert.equal( jQuery( "#en" ).text(), expectedBefore, "Insert String before with disconnected node first" );
  734. } );
  735. QUnit.test( "insert with .before() on disconnected node last", function( assert ) {
  736. assert.expect( 1 );
  737. var expectedAfter = "This is a normal link: Yahoobuga";
  738. jQuery( "#yahoo" ).add( "<span></span>" ).after( "<b>buga</b>" );
  739. assert.equal( jQuery( "#en" ).text(), expectedAfter, "Insert String after with disconnected node last" );
  740. } );
  741. QUnit.test( "insert with .before() on disconnected node last", function( assert ) {
  742. assert.expect( 1 );
  743. var expectedAfter = "This is a normal link: Yahoobuga";
  744. jQuery( "<span></span>" ).add( "#yahoo" ).after( "<b>buga</b>" );
  745. assert.equal( jQuery( "#en" ).text(), expectedAfter, "Insert String after with disconnected node first" );
  746. } );
  747. QUnit.test( "insertBefore(String)", function( assert ) {
  748. assert.expect( 1 );
  749. var expected = "This is a normal link: bugaYahoo";
  750. jQuery( "<b>buga</b>" ).insertBefore( "#yahoo" );
  751. assert.equal( jQuery( "#en" ).text(), expected, "Insert String before" );
  752. } );
  753. QUnit.test( "insertBefore(Element)", function( assert ) {
  754. assert.expect( 1 );
  755. var expected = "This is a normal link: Try them out:Yahoo";
  756. jQuery( document.getElementById( "first" ) ).insertBefore( "#yahoo" );
  757. assert.equal( jQuery( "#en" ).text(), expected, "Insert element before" );
  758. } );
  759. QUnit.test( "insertBefore(Array<Element>)", function( assert ) {
  760. assert.expect( 1 );
  761. var expected = "This is a normal link: Try them out:diveintomarkYahoo";
  762. jQuery( [ document.getElementById( "first" ), document.getElementById( "mark" ) ] ).insertBefore( "#yahoo" );
  763. assert.equal( jQuery( "#en" ).text(), expected, "Insert array of elements before" );
  764. } );
  765. QUnit.test( "insertBefore(jQuery)", function( assert ) {
  766. assert.expect( 1 );
  767. var expected = "This is a normal link: diveintomarkTry them out:Yahoo";
  768. jQuery( "#mark, #first" ).insertBefore( "#yahoo" );
  769. assert.equal( jQuery( "#en" ).text(), expected, "Insert jQuery before" );
  770. } );
  771. QUnit.test( ".after(String)", function( assert ) {
  772. assert.expect( 1 );
  773. var expected = "This is a normal link: Yahoobuga";
  774. jQuery( "#yahoo" ).after( "<b>buga</b>" );
  775. assert.equal( jQuery( "#en" ).text(), expected, "Insert String after" );
  776. } );
  777. QUnit.test( ".after(Element)", function( assert ) {
  778. assert.expect( 1 );
  779. var expected = "This is a normal link: YahooTry them out:";
  780. jQuery( "#yahoo" ).after( document.getElementById( "first" ) );
  781. assert.equal( jQuery( "#en" ).text(), expected, "Insert element after" );
  782. } );
  783. QUnit.test( ".after(Array<Element>)", function( assert ) {
  784. assert.expect( 1 );
  785. var expected = "This is a normal link: YahooTry them out:diveintomark";
  786. jQuery( "#yahoo" ).after( [ document.getElementById( "first" ), document.getElementById( "mark" ) ] );
  787. assert.equal( jQuery( "#en" ).text(), expected, "Insert array of elements after" );
  788. } );
  789. QUnit.test( ".after(jQuery)", function( assert ) {
  790. assert.expect( 1 );
  791. var expected = "This is a normal link: YahooTry them out:Googlediveintomark";
  792. jQuery( "#yahoo" ).after( [ jQuery( "#first" ), jQuery( "#mark, #google" ) ] );
  793. assert.equal( jQuery( "#en" ).text(), expected, "Insert array of jQuery objects after" );
  794. } );
  795. QUnit.test( ".after(Function) returns String", function( assert ) {
  796. assert.expect( 1 );
  797. var expected = "This is a normal link: Yahoobuga",
  798. val = manipulationFunctionReturningObj;
  799. jQuery( "#yahoo" ).after( val( "<b>buga</b>" ) );
  800. assert.equal( jQuery( "#en" ).text(), expected, "Insert String after" );
  801. } );
  802. QUnit.test( ".after(Function) returns Element", function( assert ) {
  803. assert.expect( 1 );
  804. var expected = "This is a normal link: YahooTry them out:",
  805. val = manipulationFunctionReturningObj;
  806. jQuery( "#yahoo" ).after( val( document.getElementById( "first" ) ) );
  807. assert.equal( jQuery( "#en" ).text(), expected, "Insert element after" );
  808. } );
  809. QUnit.test( ".after(Function) returns Array<Element>", function( assert ) {
  810. assert.expect( 1 );
  811. var expected = "This is a normal link: YahooTry them out:diveintomark",
  812. val = manipulationFunctionReturningObj;
  813. jQuery( "#yahoo" ).after( val( [ document.getElementById( "first" ), document.getElementById( "mark" ) ] ) );
  814. assert.equal( jQuery( "#en" ).text(), expected, "Insert array of elements after" );
  815. } );
  816. QUnit.test( ".after(Function) returns jQuery", function( assert ) {
  817. assert.expect( 1 );
  818. var expected = "This is a normal link: YahooTry them out:Googlediveintomark",
  819. val = manipulationFunctionReturningObj;
  820. jQuery( "#yahoo" ).after( val( [ jQuery( "#first" ), jQuery( "#mark, #google" ) ] ) );
  821. assert.equal( jQuery( "#en" ).text(), expected, "Insert array of jQuery objects after" );
  822. } );
  823. QUnit.test( ".after(disconnected node)", function( assert ) {
  824. assert.expect( 2 );
  825. var set = jQuery( "<div></div>" ).before( "<span>test</span>" );
  826. assert.equal( set[ 0 ].nodeName.toLowerCase(), "div", "Insert after a disconnected node should be a no-op" );
  827. assert.equal( set.length, 1, "Insert the element after the disconnected node should be a no-op" );
  828. } );
  829. QUnit.test( "insertAfter(String)", function( assert ) {
  830. assert.expect( 1 );
  831. var expected = "This is a normal link: Yahoobuga";
  832. jQuery( "<b>buga</b>" ).insertAfter( "#yahoo" );
  833. assert.equal( jQuery( "#en" ).text(), expected, "Insert String after" );
  834. } );
  835. QUnit.test( "insertAfter(Element)", function( assert ) {
  836. assert.expect( 1 );
  837. var expected = "This is a normal link: YahooTry them out:";
  838. jQuery( document.getElementById( "first" ) ).insertAfter( "#yahoo" );
  839. assert.equal( jQuery( "#en" ).text(), expected, "Insert element after" );
  840. } );
  841. QUnit.test( "insertAfter(Array<Element>)", function( assert ) {
  842. assert.expect( 1 );
  843. var expected = "This is a normal link: YahooTry them out:diveintomark";
  844. jQuery( [ document.getElementById( "first" ), document.getElementById( "mark" ) ] ).insertAfter( "#yahoo" );
  845. assert.equal( jQuery( "#en" ).text(), expected, "Insert array of elements after" );
  846. } );
  847. QUnit.test( "insertAfter(jQuery)", function( assert ) {
  848. assert.expect( 1 );
  849. var expected = "This is a normal link: YahoodiveintomarkTry them out:";
  850. jQuery( "#mark, #first" ).insertAfter( "#yahoo" );
  851. assert.equal( jQuery( "#en" ).text(), expected, "Insert jQuery after" );
  852. } );
  853. function testReplaceWith( val, assert ) {
  854. var tmp, y, child, child2, set, nonExistent, $div,
  855. expected = 29;
  856. assert.expect( expected );
  857. jQuery( "#yahoo" ).replaceWith( val( "<b id='replace'>buga</b>" ) );
  858. assert.ok( jQuery( "#replace" )[ 0 ], "Replace element with element from string" );
  859. assert.ok( !jQuery( "#yahoo" )[ 0 ], "Verify that original element is gone, after string" );
  860. jQuery( "#anchor2" ).replaceWith( val( document.getElementById( "first" ) ) );
  861. assert.ok( jQuery( "#first" )[ 0 ], "Replace element with element" );
  862. assert.ok( !jQuery( "#anchor2" )[ 0 ], "Verify that original element is gone, after element" );
  863. jQuery( "#qunit-fixture" ).append( "<div id='bar'><div id='baz'></div></div>" );
  864. jQuery( "#baz" ).replaceWith( val( "Baz" ) );
  865. assert.equal( jQuery( "#bar" ).text(), "Baz", "Replace element with text" );
  866. assert.ok( !jQuery( "#baz" )[ 0 ], "Verify that original element is gone, after element" );
  867. jQuery( "#bar" ).replaceWith( "<div id='yahoo'></div>", "...", "<div id='baz'></div>" );
  868. assert.deepEqual( jQuery( "#yahoo, #baz" ).get(), q( "yahoo", "baz" ), "Replace element with multiple arguments (#13722)" );
  869. assert.strictEqual( jQuery( "#yahoo" )[ 0 ].nextSibling, jQuery( "#baz" )[ 0 ].previousSibling, "Argument order preserved" );
  870. assert.deepEqual( jQuery( "#bar" ).get(), [], "Verify that original element is gone, after multiple arguments" );
  871. jQuery( "#google" ).replaceWith( val( [ document.getElementById( "first" ), document.getElementById( "mark" ) ] ) );
  872. assert.deepEqual( jQuery( "#mark, #first" ).get(), q( "first", "mark" ), "Replace element with array of elements" );
  873. assert.ok( !jQuery( "#google" )[ 0 ], "Verify that original element is gone, after array of elements" );
  874. jQuery( "#groups" ).replaceWith( val( jQuery( "#mark, #first" ) ) );
  875. assert.deepEqual( jQuery( "#mark, #first" ).get(), q( "first", "mark" ), "Replace element with jQuery collection" );
  876. assert.ok( !jQuery( "#groups" )[ 0 ], "Verify that original element is gone, after jQuery collection" );
  877. jQuery( "#mark, #first" ).replaceWith( val( "<span class='replacement'></span><span class='replacement'></span>" ) );
  878. assert.equal( jQuery( "#qunit-fixture .replacement" ).length, 4, "Replace multiple elements (#12449)" );
  879. assert.deepEqual( jQuery( "#mark, #first" ).get(), [], "Verify that original elements are gone, after replace multiple" );
  880. tmp = jQuery( "<b>content</b>" )[ 0 ];
  881. jQuery( "#anchor1" ).contents().replaceWith( val( tmp ) );
  882. assert.deepEqual( jQuery( "#anchor1" ).contents().get(), [ tmp ], "Replace text node with element" );
  883. tmp = jQuery( "<div></div>" ).appendTo( "#qunit-fixture" ).on( "click", function() {
  884. assert.ok( true, "Newly bound click run." );
  885. } );
  886. y = jQuery( "<div></div>" ).appendTo( "#qunit-fixture" ).on( "click", function() {
  887. assert.ok( false, "Previously bound click run." );
  888. } );
  889. child = y.append( "<b>test</b>" ).find( "b" ).on( "click", function() {
  890. assert.ok( true, "Child bound click run." );
  891. return false;
  892. } );
  893. y.replaceWith( val( tmp ) );
  894. tmp.trigger( "click" );
  895. y.trigger( "click" ); // Shouldn't be run
  896. child.trigger( "click" ); // Shouldn't be run
  897. y = jQuery( "<div></div>" ).appendTo( "#qunit-fixture" ).on( "click", function() {
  898. assert.ok( false, "Previously bound click run." );
  899. } );
  900. child2 = y.append( "<u>test</u>" ).find( "u" ).on( "click", function() {
  901. assert.ok( true, "Child 2 bound click run." );
  902. return false;
  903. } );
  904. y.replaceWith( val( child2 ) );
  905. child2.trigger( "click" );
  906. set = jQuery( "<div></div>" ).replaceWith( val( "<span>test</span>" ) );
  907. assert.equal( set[ 0 ].nodeName.toLowerCase(), "div", "No effect on a disconnected node." );
  908. assert.equal( set.length, 1, "No effect on a disconnected node." );
  909. assert.equal( set[ 0 ].childNodes.length, 0, "No effect on a disconnected node." );
  910. child = jQuery( "#qunit-fixture" ).children().first();
  911. $div = jQuery( "<div class='pathological'></div>" ).insertBefore( child );
  912. $div.replaceWith( $div );
  913. assert.deepEqual( jQuery( ".pathological", "#qunit-fixture" ).get(), $div.get(),
  914. "Self-replacement" );
  915. $div.replaceWith( child );
  916. assert.deepEqual( jQuery( "#qunit-fixture" ).children().first().get(), child.get(),
  917. "Replacement with following sibling (#13810)" );
  918. assert.deepEqual( jQuery( ".pathological", "#qunit-fixture" ).get(), [],
  919. "Replacement with following sibling (context removed)" );
  920. nonExistent = jQuery( "#does-not-exist" ).replaceWith( val( "<b>should not throw an error</b>" ) );
  921. assert.equal( nonExistent.length, 0, "Length of non existent element." );
  922. $div = jQuery( "<div class='replacewith'></div>" ).appendTo( "#qunit-fixture" );
  923. $div.replaceWith( val( "<div class='replacewith'></div><script>" +
  924. "QUnit.assert.equal( jQuery('.replacewith').length, 1, 'Check number of elements in page.' );" +
  925. "</script>" ) );
  926. jQuery( "#qunit-fixture" ).append( "<div id='replaceWith'></div>" );
  927. assert.equal( jQuery( "#qunit-fixture" ).find( "div[id=replaceWith]" ).length, 1, "Make sure only one div exists." );
  928. jQuery( "#replaceWith" ).replaceWith( val( "<div id='replaceWith'></div>" ) );
  929. assert.equal( jQuery( "#qunit-fixture" ).find( "div[id=replaceWith]" ).length, 1, "Make sure only one div exists after replacement." );
  930. jQuery( "#replaceWith" ).replaceWith( val( "<div id='replaceWith'></div>" ) );
  931. assert.equal( jQuery( "#qunit-fixture" ).find( "div[id=replaceWith]" ).length, 1, "Make sure only one div exists after subsequent replacement." );
  932. return expected;
  933. }
  934. QUnit.test( "replaceWith(String|Element|Array<Element>|jQuery)", function( assert ) {
  935. testReplaceWith( manipulationBareObj, assert );
  936. } );
  937. QUnit.test( "replaceWith(Function)", function( assert ) {
  938. assert.expect( testReplaceWith( manipulationFunctionReturningObj, assert ) + 1 );
  939. var y = jQuery( "#foo" )[ 0 ];
  940. jQuery( y ).replaceWith( function() {
  941. assert.equal( this, y, "Make sure the context is coming in correctly." );
  942. } );
  943. } );
  944. QUnit.test( "replaceWith(string) for more than one element", function( assert ) {
  945. assert.expect( 3 );
  946. assert.equal( jQuery( "#foo p" ).length, 3, "ensuring that test data has not changed" );
  947. jQuery( "#foo p" ).replaceWith( "<span>bar</span>" );
  948. assert.equal( jQuery( "#foo span" ).length, 3, "verify that all the three original element have been replaced" );
  949. assert.equal( jQuery( "#foo p" ).length, 0, "verify that all the three original element have been replaced" );
  950. } );
  951. QUnit.test( "Empty replaceWith (trac-13401; trac-13596; gh-2204)", function( assert ) {
  952. assert.expect( 25 );
  953. var $el = jQuery( "<div></div><div></div>" ).html( "<p>0</p>" ),
  954. expectedHTML = $el.html(),
  955. tests = {
  956. "empty string": "",
  957. "empty array": [],
  958. "array of empty string": [ "" ],
  959. "empty collection": jQuery( "#nonexistent" ),
  960. // in case of jQuery(...).replaceWith();
  961. "undefined": undefined
  962. };
  963. jQuery.each( tests, function( label, input ) {
  964. $el.html( "<a></a>" ).children().replaceWith( input );
  965. assert.strictEqual( $el.html(), "", "replaceWith(" + label + ")" );
  966. $el.html( "<b></b>" ).children().replaceWith( function() { return input; } );
  967. assert.strictEqual( $el.html(), "", "replaceWith(function returning " + label + ")" );
  968. $el.html( "<i></i>" ).children().replaceWith( function( i ) { return input; } );
  969. assert.strictEqual( $el.html(), "", "replaceWith(other function returning " + label + ")" );
  970. $el.html( "<p></p>" ).children().replaceWith( function( i ) {
  971. return i ?
  972. input :
  973. jQuery( this ).html( i + "" );
  974. } );
  975. assert.strictEqual( $el.eq( 0 ).html(), expectedHTML,
  976. "replaceWith(function conditionally returning context)" );
  977. assert.strictEqual( $el.eq( 1 ).html(), "",
  978. "replaceWith(function conditionally returning " + label + ")" );
  979. } );
  980. } );
  981. QUnit.test( "replaceAll(String)", function( assert ) {
  982. assert.expect( 2 );
  983. jQuery( "<b id='replace'>buga</b>" ).replaceAll( "#yahoo" );
  984. assert.ok( jQuery( "#replace" )[ 0 ], "Replace element with string" );
  985. assert.ok( !jQuery( "#yahoo" )[ 0 ], "Verify that original element is gone, after string" );
  986. } );
  987. QUnit.test( "replaceAll(Element)", function( assert ) {
  988. assert.expect( 2 );
  989. jQuery( document.getElementById( "first" ) ).replaceAll( "#yahoo" );
  990. assert.ok( jQuery( "#first" )[ 0 ], "Replace element with element" );
  991. assert.ok( !jQuery( "#yahoo" )[ 0 ], "Verify that original element is gone, after element" );
  992. } );
  993. QUnit.test( "replaceAll(Array<Element>)", function( assert ) {
  994. assert.expect( 3 );
  995. jQuery( [ document.getElementById( "first" ), document.getElementById( "mark" ) ] ).replaceAll( "#yahoo" );
  996. assert.ok( jQuery( "#first" )[ 0 ], "Replace element with array of elements" );
  997. assert.ok( jQuery( "#mark" )[ 0 ], "Replace element with array of elements" );
  998. assert.ok( !jQuery( "#yahoo" )[ 0 ], "Verify that original element is gone, after array of elements" );
  999. } );
  1000. QUnit.test( "replaceAll(jQuery)", function( assert ) {
  1001. assert.expect( 3 );
  1002. jQuery( "#mark, #first" ).replaceAll( "#yahoo" );
  1003. assert.ok( jQuery( "#first" )[ 0 ], "Replace element with set of elements" );
  1004. assert.ok( jQuery( "#mark" )[ 0 ], "Replace element with set of elements" );
  1005. assert.ok( !jQuery( "#yahoo" )[ 0 ], "Verify that original element is gone, after set of elements" );
  1006. } );
  1007. QUnit.test( "jQuery.clone() (#8017)", function( assert ) {
  1008. assert.expect( 2 );
  1009. assert.ok( jQuery.clone && typeof jQuery.clone === "function", "jQuery.clone() utility exists and is a function." );
  1010. var main = jQuery( "#qunit-fixture" )[ 0 ],
  1011. clone = jQuery.clone( main );
  1012. assert.equal( main.childNodes.length, clone.childNodes.length, "Simple child length to ensure a large dom tree copies correctly" );
  1013. } );
  1014. QUnit.test( "append to multiple elements (#8070)", function( assert ) {
  1015. assert.expect( 2 );
  1016. var selects = jQuery( "<select class='test8070'></select><select class='test8070'></select>" ).appendTo( "#qunit-fixture" );
  1017. selects.append( "<OPTION>1</OPTION><OPTION>2</OPTION>" );
  1018. assert.equal( selects[ 0 ].childNodes.length, 2, "First select got two nodes" );
  1019. assert.equal( selects[ 1 ].childNodes.length, 2, "Second select got two nodes" );
  1020. } );
  1021. QUnit.test( "table manipulation", function( assert ) {
  1022. assert.expect( 2 );
  1023. var table = jQuery( "<table style='font-size:16px'></table>" ).appendTo( "#qunit-fixture" ).empty(),
  1024. height = table[ 0 ].offsetHeight;
  1025. table.append( "<tr><td>DATA</td></tr>" );
  1026. assert.ok( table[ 0 ].offsetHeight - height >= 15, "appended rows are visible" );
  1027. table.empty();
  1028. height = table[ 0 ].offsetHeight;
  1029. table.prepend( "<tr><td>DATA</td></tr>" );
  1030. assert.ok( table[ 0 ].offsetHeight - height >= 15, "prepended rows are visible" );
  1031. } );
  1032. QUnit.test( "clone()", function( assert ) {
  1033. assert.expect( 45 );
  1034. var div, clone, form, body;
  1035. assert.equal( jQuery( "#en" ).text(), "This is a normal link: Yahoo", "Assert text for #en" );
  1036. assert.equal( jQuery( "#first" ).append( jQuery( "#yahoo" ).clone() ).text(), "Try them out:Yahoo", "Check for clone" );
  1037. assert.equal( jQuery( "#en" ).text(), "This is a normal link: Yahoo", "Reassert text for #en" );
  1038. jQuery.each( "table thead tbody tfoot tr td div button ul ol li select option textarea iframe".split( " " ), function( i, nodeName ) {
  1039. assert.equal( jQuery( "<" + nodeName + "/>" ).clone()[ 0 ].nodeName.toLowerCase(), nodeName, "Clone a " + nodeName );
  1040. } );
  1041. assert.equal( jQuery( "<input type='checkbox' />" ).clone()[ 0 ].nodeName.toLowerCase(), "input", "Clone a <input type='checkbox' />" );
  1042. // Check cloning non-elements
  1043. assert.equal( jQuery( "#nonnodes" ).contents().clone().length, 3, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
  1044. // Verify that clones of clones can keep event listeners
  1045. div = jQuery( "<div><ul><li>test</li></ul></div>" ).on( "click", function() {
  1046. assert.ok( true, "Bound event still exists." );
  1047. } );
  1048. clone = div.clone( true ); div.remove();
  1049. div = clone.clone( true ); clone.remove();
  1050. assert.equal( div.length, 1, "One element cloned" );
  1051. assert.equal( div[ 0 ].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
  1052. div.trigger( "click" );
  1053. // Manually clean up detached elements
  1054. div.remove();
  1055. // Verify that cloned children can keep event listeners
  1056. div = jQuery( "<div></div>" ).append( [ document.createElement( "table" ), document.createElement( "table" ) ] );
  1057. div.find( "table" ).on( "click", function() {
  1058. assert.ok( true, "Bound event still exists." );
  1059. } );
  1060. clone = div.clone( true );
  1061. assert.equal( clone.length, 1, "One element cloned" );
  1062. assert.equal( clone[ 0 ].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
  1063. clone.find( "table" ).trigger( "click" );
  1064. // Manually clean up detached elements
  1065. div.remove();
  1066. clone.remove();
  1067. // Make sure that doing .clone() doesn't clone event listeners
  1068. div = jQuery( "<div><ul><li>test</li></ul></div>" ).on( "click", function() {
  1069. assert.ok( false, "Bound event still exists after .clone()." );
  1070. } );
  1071. clone = div.clone();
  1072. clone.trigger( "click" );
  1073. // Manually clean up detached elements
  1074. clone.remove();
  1075. div.remove();
  1076. // Test both html() and clone() for <embed> and <object> types
  1077. div = jQuery( "<div></div>" ).html( "<embed height='355' width='425' src='http://www.youtube.com/v/3KANI2dpXLw&amp;hl=en'></embed>" );
  1078. clone = div.clone( true );
  1079. assert.equal( clone.length, 1, "One element cloned" );
  1080. assert.equal( clone.html(), div.html(), "Element contents cloned" );
  1081. assert.equal( clone[ 0 ].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
  1082. // this is technically an invalid object, but because of the special
  1083. // classid instantiation it is the only kind that IE has trouble with,
  1084. // so let's test with it too.
  1085. div = jQuery( "<div></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>" );
  1086. clone = div.clone( true );
  1087. assert.equal( clone.length, 1, "One element cloned" );
  1088. assert.equal( clone[ 0 ].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
  1089. div = div.find( "object" );
  1090. clone = clone.find( "object" );
  1091. // oldIE adds extra attributes and <param> elements, so just test for existence of the defined set
  1092. jQuery.each( [ "height", "width", "classid" ], function( i, attr ) {
  1093. assert.equal( clone.attr( attr ), div.attr( attr ), "<object> attribute cloned: " + attr );
  1094. } );
  1095. ( function() {
  1096. var params = {};
  1097. clone.find( "param" ).each( function( index, param ) {
  1098. params[ param.attributes.name.nodeValue.toLowerCase() ] =
  1099. param.attributes.value.nodeValue.toLowerCase();
  1100. } );
  1101. div.find( "param" ).each( function( index, param ) {
  1102. var key = param.attributes.name.nodeValue.toLowerCase();
  1103. assert.equal( params[ key ], param.attributes.value.nodeValue.toLowerCase(), "<param> cloned: " + key );
  1104. } );
  1105. } )();
  1106. // and here's a valid one.
  1107. div = jQuery( "<div></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>" );
  1108. clone = div.clone( true );
  1109. assert.equal( clone.length, 1, "One element cloned" );
  1110. assert.equal( clone.html(), div.html(), "Element contents cloned" );
  1111. assert.equal( clone[ 0 ].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
  1112. div = jQuery( "<div></div>" ).data( { "a": true } );
  1113. clone = div.clone( true );
  1114. assert.equal( clone.data( "a" ), true, "Data cloned." );
  1115. clone.data( "a", false );
  1116. assert.equal( clone.data( "a" ), false, "Ensure cloned element data object was correctly modified" );
  1117. assert.equal( div.data( "a" ), true, "Ensure cloned element data object is copied, not referenced" );
  1118. // manually clean up detached elements
  1119. div.remove();
  1120. clone.remove();
  1121. form = document.createElement( "form" );
  1122. form.action = "/test/";
  1123. div = document.createElement( "div" );
  1124. div.appendChild( document.createTextNode( "test" ) );
  1125. form.appendChild( div );
  1126. assert.equal( jQuery( form ).clone().children().length, 1, "Make sure we just get the form back." );
  1127. body = jQuery( "body" ).clone();
  1128. assert.equal( body.children()[ 0 ].id, "qunit", "Make sure cloning body works" );
  1129. body.remove();
  1130. } );
  1131. QUnit.test( "clone(script type=non-javascript) (#11359)", function( assert ) {
  1132. assert.expect( 3 );
  1133. var src = jQuery( "<script type='text/filler'>Lorem ipsum dolor sit amet</script><q><script type='text/filler'>consectetur adipiscing elit</script></q>" ),
  1134. dest = src.clone();
  1135. assert.equal( dest[ 0 ].text, "Lorem ipsum dolor sit amet", "Cloning preserves script text" );
  1136. assert.equal( dest.last().html(), src.last().html(), "Cloning preserves nested script text" );
  1137. assert.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" );
  1138. dest.remove();
  1139. } );
  1140. QUnit.test( "clone(form element) (Bug #3879, #6655)", function( assert ) {
  1141. assert.expect( 5 );
  1142. var clone, element;
  1143. element = jQuery( "<select><option>Foo</option><option value='selected' selected>Bar</option></select>" );
  1144. assert.equal( element.clone().find( "option" ).filter( function() { return this.selected; } ).val(), "selected", "Selected option cloned correctly" );
  1145. element = jQuery( "<input type='checkbox' value='foo'>" ).attr( "checked", "checked" );
  1146. clone = element.clone();
  1147. assert.equal( clone.is( ":checked" ), element.is( ":checked" ), "Checked input cloned correctly" );
  1148. assert.equal( clone[ 0 ].defaultValue, "foo", "Checked input defaultValue cloned correctly" );
  1149. element = jQuery( "<input type='text' value='foo'>" );
  1150. clone = element.clone();
  1151. assert.equal( clone[ 0 ].defaultValue, "foo", "Text input defaultValue cloned correctly" );
  1152. element = jQuery( "<textarea>foo</textarea>" );
  1153. clone = element.clone();
  1154. assert.equal( clone[ 0 ].defaultValue, "foo", "Textarea defaultValue cloned correctly" );
  1155. } );
  1156. QUnit.test( "clone(multiple selected options) (Bug #8129)", function( assert ) {
  1157. assert.expect( 1 );
  1158. var element = jQuery( "<select><option>Foo</option><option selected>Bar</option><option selected>Baz</option></select>" );
  1159. function getSelectedOptions( collection ) {
  1160. return collection.find( "option" ).filter( function( option ) {
  1161. return option.selected;
  1162. } );
  1163. }
  1164. assert.equal(
  1165. getSelectedOptions( element.clone() ).length,
  1166. getSelectedOptions( element ).length,
  1167. "Multiple selected options cloned correctly"
  1168. );
  1169. } );
  1170. QUnit.test( "clone() on XML nodes", function( assert ) {
  1171. assert.expect( 2 );
  1172. var xml = createDashboardXML(),
  1173. root = jQuery( xml.documentElement ).clone(),
  1174. origTab = jQuery( "tab", xml ).eq( 0 ),
  1175. cloneTab = jQuery( "tab", root ).eq( 0 );
  1176. origTab.text( "origval" );
  1177. cloneTab.text( "cloneval" );
  1178. assert.equal( origTab.text(), "origval", "Check original XML node was correctly set" );
  1179. assert.equal( cloneTab.text(), "cloneval", "Check cloned XML node was correctly set" );
  1180. } );
  1181. QUnit.test( "clone() on local XML nodes with html5 nodename", function( assert ) {
  1182. assert.expect( 2 );
  1183. var $xmlDoc = jQuery( jQuery.parseXML( "<root><meter /></root>" ) ),
  1184. $meter = $xmlDoc.find( "meter" ).clone();
  1185. assert.equal( $meter[ 0 ].nodeName, "meter", "Check if nodeName was not changed due to cloning" );
  1186. assert.equal( $meter[ 0 ].nodeType, 1, "Check if nodeType is not changed due to cloning" );
  1187. } );
  1188. QUnit.test( "html(undefined)", function( assert ) {
  1189. assert.expect( 1 );
  1190. assert.equal( jQuery( "#foo" ).html( "<i>test</i>" ).html( undefined ).html().toLowerCase(), "<i>test</i>", ".html(undefined) is chainable (#5571)" );
  1191. } );
  1192. QUnit.test( "html() on empty set", function( assert ) {
  1193. assert.expect( 1 );
  1194. assert.strictEqual( jQuery().html(), undefined, ".html() returns undefined for empty sets (#11962)" );
  1195. } );
  1196. function childNodeNames( node ) {
  1197. return jQuery.map( node.childNodes, function( child ) {
  1198. return child.nodeName.toUpperCase();
  1199. } ).join( " " );
  1200. }
  1201. function testHtml( valueObj, assert ) {
  1202. assert.expect( 40 );
  1203. var actual, expected, tmp,
  1204. div = jQuery( "<div></div>" ),
  1205. fixture = jQuery( "#qunit-fixture" );
  1206. div.html( valueObj( "<div id='parent_1'><div id='child_1'></div></div><div id='parent_2'></div>" ) );
  1207. assert.equal( div.children().length, 2, "Found children" );
  1208. assert.equal( div.children().children().length, 1, "Found grandchild" );
  1209. actual = []; expected = [];
  1210. tmp = jQuery( "<map></map>" ).html( valueObj( "<area alt='area'></area>" ) ).each( function() {
  1211. expected.push( "AREA" );
  1212. actual.push( childNodeNames( this ) );
  1213. } );
  1214. assert.equal( expected.length, 1, "Expecting one parent" );
  1215. assert.deepEqual( actual, expected, "Found the inserted area element" );
  1216. assert.equal( div.html( valueObj( 5 ) ).html(), "5", "Setting a number as html" );
  1217. assert.equal( div.html( valueObj( 0 ) ).html(), "0", "Setting a zero as html" );
  1218. assert.equal( div.html( valueObj( Infinity ) ).html(), "Infinity", "Setting Infinity as html" );
  1219. assert.equal( div.html( valueObj( NaN ) ).html(), "", "Setting NaN as html" );
  1220. assert.equal( div.html( valueObj( 1e2 ) ).html(), "100", "Setting exponential number notation as html" );
  1221. div.html( valueObj( "&#160;&amp;" ) );
  1222. assert.equal(
  1223. div[ 0 ].innerHTML.replace( /\xA0/, "&nbsp;" ),
  1224. "&nbsp;&amp;",
  1225. "Entities are passed through correctly"
  1226. );
  1227. tmp = "&lt;div&gt;hello1&lt;/div&gt;";
  1228. assert.equal( div.html( valueObj( tmp ) ).html().replace( />/g, "&gt;" ), tmp, "Escaped html" );
  1229. tmp = "x" + tmp;
  1230. assert.equal( div.html( valueObj( tmp ) ).html().replace( />/g, "&gt;" ), tmp, "Escaped html, leading x" );
  1231. tmp = " " + tmp.slice( 1 );
  1232. assert.equal( div.html( valueObj( tmp ) ).html().replace( />/g, "&gt;" ), tmp, "Escaped html, leading space" );
  1233. actual = []; expected = []; tmp = {};
  1234. jQuery( "#nonnodes" ).contents().html( valueObj( "<b>bold</b>" ) ).each( function() {
  1235. var html = jQuery( this ).html();
  1236. tmp[ this.nodeType ] = true;
  1237. expected.push( this.nodeType === 1 ? "<b>bold</b>" : undefined );
  1238. actual.push( html ? html.toLowerCase() : html );
  1239. } );
  1240. assert.deepEqual( actual, expected, "Set containing element, text node, comment" );
  1241. assert.ok( tmp[ 1 ], "element" );
  1242. assert.ok( tmp[ 3 ], "text node" );
  1243. assert.ok( tmp[ 8 ], "comment" );
  1244. actual = []; expected = [];
  1245. fixture.children( "div" ).html( valueObj( "<b>test</b>" ) ).each( function() {
  1246. expected.push( "B" );
  1247. actual.push( childNodeNames( this ) );
  1248. } );
  1249. assert.equal( expected.length, 7, "Expecting many parents" );
  1250. assert.deepEqual( actual, expected, "Correct childNodes after setting HTML" );
  1251. actual = []; expected = [];
  1252. fixture.html( valueObj( "<style>.foobar{color:green;}</style>" ) ).each( function() {
  1253. expected.push( "STYLE" );
  1254. actual.push( childNodeNames( this ) );
  1255. } );
  1256. assert.equal( expected.length, 1, "Expecting one parent" );
  1257. assert.deepEqual( actual, expected, "Found the inserted style element" );
  1258. fixture.html( valueObj( "<select></select>" ) );
  1259. jQuery( "#qunit-fixture select" ).html( valueObj( "<option>O1</option><option selected='selected'>O2</option><option>O3</option>" ) );
  1260. assert.equal( jQuery( "#qunit-fixture select" ).val(), "O2", "Selected option correct" );
  1261. tmp = fixture.html(
  1262. valueObj( [
  1263. "<script type='something/else'>QUnit.assert.ok( false, 'evaluated: non-script' );</script>",
  1264. "<script type='text/javascript'>QUnit.assert.ok( true, 'evaluated: text/javascript' );</script>",
  1265. "<script type='text/ecmascript'>QUnit.assert.ok( true, 'evaluated: text/ecmascript' );</script>",
  1266. "<script>QUnit.assert.ok( true, 'evaluated: no type' );</script>",
  1267. "<div>",
  1268. "<script type='something/else'>QUnit.assert.ok( false, 'evaluated: inner non-script' );</script>",
  1269. "<script type='text/javascript'>QUnit.assert.ok( true, 'evaluated: inner text/javascript' );</script>",
  1270. "<script type='text/ecmascript'>QUnit.assert.ok( true, 'evaluated: inner text/ecmascript' );</script>",
  1271. "<script>QUnit.assert.ok( true, 'evaluated: inner no type' );</script>",
  1272. "</div>"
  1273. ].join( "" ) )
  1274. ).find( "script" );
  1275. assert.equal( tmp.length, 8, "All script tags remain." );
  1276. assert.equal( tmp[ 0 ].type, "something/else", "Non-evaluated type." );
  1277. assert.equal( tmp[ 1 ].type, "text/javascript", "Evaluated type." );
  1278. fixture.html( valueObj( "<script type='text/javascript'>QUnit.assert.ok( true, 'Injection of identical script' );</script>" ) );
  1279. fixture.html( valueObj( "<script type='text/javascript'>QUnit.assert.ok( true, 'Injection of identical script' );</script>" ) );
  1280. fixture.html( valueObj( "<script type='text/javascript'>QUnit.assert.ok( true, 'Injection of identical script' );</script>" ) );
  1281. fixture.html( valueObj( "foo <form><script type='text/javascript'>QUnit.assert.ok( true, 'Injection of identical script (#975)' );</script></form>" ) );
  1282. jQuery.scriptorder = 0;
  1283. fixture.html( valueObj( [
  1284. "<script>",
  1285. "QUnit.assert.equal( jQuery('#scriptorder').length, 1,'Execute after html' );",
  1286. "QUnit.assert.equal( jQuery.scriptorder++, 0, 'Script is executed in order' );",
  1287. "</script>",
  1288. "<span id='scriptorder'><script>QUnit.assert.equal( jQuery.scriptorder++, 1, 'Script (nested) is executed in order');</script></span>",
  1289. "<script>QUnit.assert.equal( jQuery.scriptorder++, 2, 'Script (unnested) is executed in order' );</script>"
  1290. ].join( "" ) ) );
  1291. fixture.html( valueObj( fixture.text() ) );
  1292. assert.ok( /^[^<]*[^<\s][^<]*$/.test( fixture.html() ), "Replace html with text" );
  1293. }
  1294. QUnit.test( "html(String|Number)", function( assert ) {
  1295. testHtml( manipulationBareObj, assert );
  1296. } );
  1297. QUnit.test( "html(Function)", function( assert ) {
  1298. testHtml( manipulationFunctionReturningObj, assert );
  1299. } );
  1300. QUnit[
  1301. // Support: Edge 16-18+
  1302. // Edge sometimes doesn't execute module scripts so skip the test there.
  1303. ( QUnit.moduleTypeSupported && !/edge\//i.test( navigator.userAgent ) ) ?
  1304. "test" :
  1305. "skip"
  1306. ]( "html(script type module)", function( assert ) {
  1307. assert.expect( 4 );
  1308. var done = assert.async(),
  1309. $fixture = jQuery( "#qunit-fixture" );
  1310. $fixture.html(
  1311. [
  1312. "<script type='module'>QUnit.assert.ok( true, 'evaluated: module' );</script>",
  1313. "<script type='module' src='" + url( "module.js" ) + "'></script>",
  1314. "<div>",
  1315. "<script type='module'>QUnit.assert.ok( true, 'evaluated: inner module' );</script>",
  1316. "<script type='module' src='" + url( "inner_module.js" ) + "'></script>",
  1317. "</div>"
  1318. ].join( "" )
  1319. );
  1320. // Allow asynchronous script execution to generate assertions
  1321. setTimeout( function() {
  1322. done();
  1323. }, 1000 );
  1324. } );
  1325. QUnit[
  1326. // Support: IE 9-11 only, Android 4.0-4.4 only, iOS 7-10 only
  1327. // `nomodule` scripts should be executed by legacy browsers only.
  1328. // iOS 10 supports `<script type="module">` but doesn't support the nomodule attribute
  1329. // so let's skip it here; sites supporting it must handle `nomodule` in a custom way anyway.
  1330. !/iphone os 10_/i.test( navigator.userAgent ) ?
  1331. "test" :
  1332. "skip"
  1333. ]( "html(script nomodule)", function( assert ) {
  1334. assert.expect( QUnit.moduleTypeSupported ? 0 : 4 );
  1335. var done = assert.async(),
  1336. $fixture = jQuery( "#qunit-fixture" );
  1337. $fixture.html(
  1338. [
  1339. "<script nomodule>QUnit.assert.ok( !QUnit.moduleTypeSupported, 'evaluated: nomodule script' );</script>",
  1340. "<script nomodule src='" + url( "nomodule.js" ) + "'></script>",
  1341. "<div>",
  1342. "<script nomodule>QUnit.assert.ok( !QUnit.moduleTypeSupported, 'evaluated: inner nomodule script' );</script>",
  1343. "<script nomodule src='" + url( "inner_nomodule.js" ) + "'></script>",
  1344. "</div>"
  1345. ].join( "" )
  1346. );
  1347. // Allow asynchronous script execution to generate assertions
  1348. setTimeout( function() {
  1349. done();
  1350. }, 1000 );
  1351. } );
  1352. QUnit.test( "html(Function) with incoming value -- direct selection", function( assert ) {
  1353. assert.expect( 4 );
  1354. var els, actualhtml, pass;
  1355. els = jQuery( "#foo > p" );
  1356. actualhtml = els.map( function() {
  1357. return jQuery( this ).html();
  1358. } );
  1359. els.html( function( i, val ) {
  1360. assert.equal( val, actualhtml[ i ], "Make sure the incoming value is correct." );
  1361. return "<b>test</b>";
  1362. } );
  1363. pass = true;
  1364. els.each( function() {
  1365. if ( this.childNodes.length !== 1 ) {
  1366. pass = false;
  1367. }
  1368. } );
  1369. assert.ok( pass, "Set HTML" );
  1370. } );
  1371. QUnit.test( "html(Function) with incoming value -- jQuery.contents()", function( assert ) {
  1372. assert.expect( 14 );
  1373. var actualhtml, j, $div, $div2, insert;
  1374. j = jQuery( "#nonnodes" ).contents();
  1375. actualhtml = j.map( function() {
  1376. return jQuery( this ).html();
  1377. } );
  1378. j.html( function( i, val ) {
  1379. assert.equal( val, actualhtml[ i ], "Make sure the incoming value is correct." );
  1380. return "<b>bold</b>";
  1381. } );
  1382. // Handle the case where no comment is in the document
  1383. if ( j.length === 2 ) {
  1384. assert.equal( null, null, "Make sure the incoming value is correct." );
  1385. }
  1386. assert.equal( j.html().replace( / xmlns="[^"]+"/g, "" ).toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
  1387. $div = jQuery( "<div></div>" );
  1388. assert.equal( $div.html( function( i, val ) {
  1389. assert.equal( val, "", "Make sure the incoming value is correct." );
  1390. return 5;
  1391. } ).html(), "5", "Setting a number as html" );
  1392. assert.equal( $div.html( function( i, val ) {
  1393. assert.equal( val, "5", "Make sure the incoming value is correct." );
  1394. return 0;
  1395. } ).html(), "0", "Setting a zero as html" );
  1396. $div2 = jQuery( "<div></div>" );
  1397. insert = "&lt;div&gt;hello1&lt;/div&gt;";
  1398. assert.equal( $div2.html( function( i, val ) {
  1399. assert.equal( val, "", "Make sure the incoming value is correct." );
  1400. return insert;
  1401. } ).html().replace( />/g, "&gt;" ), insert, "Verify escaped insertion." );
  1402. assert.equal( $div2.html( function( i, val ) {
  1403. assert.equal( val.replace( />/g, "&gt;" ), insert, "Make sure the incoming value is correct." );
  1404. return "x" + insert;
  1405. } ).html().replace( />/g, "&gt;" ), "x" + insert, "Verify escaped insertion." );
  1406. assert.equal( $div2.html( function( i, val ) {
  1407. assert.equal( val.replace( />/g, "&gt;" ), "x" + insert, "Make sure the incoming value is correct." );
  1408. return " " + insert;
  1409. } ).html().replace( />/g, "&gt;" ), " " + insert, "Verify escaped insertion." );
  1410. } );
  1411. QUnit.test( "clone()/html() don't expose jQuery/Sizzle expandos (#12858)", function( assert ) {
  1412. assert.expect( 2 );
  1413. var $content = jQuery( "<div><b><i>text</i></b></div>" ).appendTo( "#qunit-fixture" ),
  1414. expected = /^<b><i>text<\/i><\/b>$/i;
  1415. // Attach jQuery and Sizzle data (the latter with a non-qSA nth-child)
  1416. try {
  1417. $content.find( ":nth-child(1):lt(4)" ).data( "test", true );
  1418. // But don't break on a non-Sizzle build
  1419. } catch ( e ) {
  1420. $content.find( "*" ).data( "test", true );
  1421. }
  1422. assert.ok( expected.test( $content.clone( false )[ 0 ].innerHTML ), "clone()" );
  1423. assert.ok( expected.test( $content.html() ), "html()" );
  1424. } );
  1425. QUnit.test( "remove() no filters", function( assert ) {
  1426. assert.expect( 2 );
  1427. var first = jQuery( "#ap" ).children().first();
  1428. first.data( "foo", "bar" );
  1429. jQuery( "#ap" ).children().remove();
  1430. assert.ok( jQuery( "#ap" ).text().length > 10, "Check text is not removed" );
  1431. assert.equal( jQuery( "#ap" ).children().length, 0, "Check remove" );
  1432. } );
  1433. QUnit.test( "remove() with filters", function( assert ) {
  1434. assert.expect( 8 );
  1435. var markup, div;
  1436. jQuery( "#ap" ).children().remove( "a" );
  1437. assert.ok( jQuery( "#ap" ).text().length > 10, "Check text is not removed" );
  1438. assert.equal( jQuery( "#ap" ).children().length, 1, "Check filtered remove" );
  1439. jQuery( "#ap" ).children().remove( "a, code" );
  1440. assert.equal( jQuery( "#ap" ).children().length, 0, "Check multi-filtered remove" );
  1441. // Positional and relative selectors
  1442. markup = "<div><span>1</span><span>2</span><span>3</span><span>4</span></div>";
  1443. div = jQuery( markup );
  1444. div.children().remove( "span:nth-child(2n)" );
  1445. assert.equal( div.text(), "13", "relative selector in remove" );
  1446. if ( jQuery.find.compile ) {
  1447. div = jQuery( markup );
  1448. div.children().remove( "span:first" );
  1449. assert.equal( div.text(), "234", "positional selector in remove" );
  1450. div = jQuery( markup );
  1451. div.children().remove( "span:last" );
  1452. assert.equal( div.text(), "123", "positional selector in remove" );
  1453. } else {
  1454. assert.ok( "skip", "Positional selectors not supported in selector-native" );
  1455. assert.ok( "skip", "Positional selectors not supported in selector-native" );
  1456. }
  1457. // using contents will get comments regular, text, and comment nodes
  1458. // Handle the case where no comment is in the document
  1459. assert.ok( jQuery( "#nonnodes" ).contents().length >= 2, "Check node,textnode,comment remove works" );
  1460. jQuery( "#nonnodes" ).contents().remove();
  1461. assert.equal( jQuery( "#nonnodes" ).contents().length, 0, "Check node,textnode,comment remove works" );
  1462. } );
  1463. QUnit.test( "remove() event cleaning ", function( assert ) {
  1464. assert.expect( 1 );
  1465. var count, first, cleanUp;
  1466. count = 0;
  1467. first = jQuery( "#ap" ).children().first();
  1468. cleanUp = first.on( "click", function() {
  1469. count++;
  1470. } ).remove().appendTo( "#qunit-fixture" ).trigger( "click" );
  1471. assert.strictEqual( 0, count, "Event handler has been removed" );
  1472. // Clean up detached data
  1473. cleanUp.remove();
  1474. } );
  1475. QUnit.test( "remove() in document order #13779", function( assert ) {
  1476. assert.expect( 1 );
  1477. var last,
  1478. cleanData = jQuery.cleanData;
  1479. jQuery.cleanData = function( nodes ) {
  1480. last = jQuery.text( nodes[ 0 ] );
  1481. cleanData.call( this, nodes );
  1482. };
  1483. jQuery( "#qunit-fixture" ).append(
  1484. jQuery.parseHTML(
  1485. "<div class='removal-fixture'>1</div>" +
  1486. "<div class='removal-fixture'>2</div>" +
  1487. "<div class='removal-fixture'>3</div>"
  1488. )
  1489. );
  1490. jQuery( ".removal-fixture" ).remove();
  1491. assert.equal( last, 3, "The removal fixtures were removed in document order" );
  1492. jQuery.cleanData = cleanData;
  1493. } );
  1494. QUnit.test( "detach() no filters", function( assert ) {
  1495. assert.expect( 3 );
  1496. var first = jQuery( "#ap" ).children().first();
  1497. first.data( "foo", "bar" );
  1498. jQuery( "#ap" ).children().detach();
  1499. assert.ok( jQuery( "#ap" ).text().length > 10, "Check text is not removed" );
  1500. assert.equal( jQuery( "#ap" ).children().length, 0, "Check remove" );
  1501. assert.equal( first.data( "foo" ), "bar" );
  1502. first.remove();
  1503. } );
  1504. QUnit.test( "detach() with filters", function( assert ) {
  1505. assert.expect( 8 );
  1506. var markup, div;
  1507. jQuery( "#ap" ).children().detach( "a" );
  1508. assert.ok( jQuery( "#ap" ).text().length > 10, "Check text is not removed" );
  1509. assert.equal( jQuery( "#ap" ).children().length, 1, "Check filtered remove" );
  1510. jQuery( "#ap" ).children().detach( "a, code" );
  1511. assert.equal( jQuery( "#ap" ).children().length, 0, "Check multi-filtered remove" );
  1512. // Positional and relative selectors
  1513. markup = "<div><span>1</span><span>2</span><span>3</span><span>4</span></div>";
  1514. div = jQuery( markup );
  1515. div.children().detach( "span:nth-child(2n)" );
  1516. assert.equal( div.text(), "13", "relative selector in detach" );
  1517. if ( jQuery.find.compile ) {
  1518. div = jQuery( markup );
  1519. div.children().detach( "span:first" );
  1520. assert.equal( div.text(), "234", "positional selector in detach" );
  1521. div = jQuery( markup );
  1522. div.children().detach( "span:last" );
  1523. assert.equal( div.text(), "123", "positional selector in detach" );
  1524. } else {
  1525. assert.ok( "skip", "positional selectors not supported in selector-native" );
  1526. assert.ok( "skip", "positional selectors not supported in selector-native" );
  1527. }
  1528. // using contents will get comments regular, text, and comment nodes
  1529. // Handle the case where no comment is in the document
  1530. assert.ok( jQuery( "#nonnodes" ).contents().length >= 2, "Check node,textnode,comment remove works" );
  1531. jQuery( "#nonnodes" ).contents().detach();
  1532. assert.equal( jQuery( "#nonnodes" ).contents().length, 0, "Check node,textnode,comment remove works" );
  1533. } );
  1534. QUnit.test( "detach() event cleaning ", function( assert ) {
  1535. assert.expect( 1 );
  1536. var count, first, cleanUp;
  1537. count = 0;
  1538. first = jQuery( "#ap" ).children().first();
  1539. cleanUp = first.on( "click", function() {
  1540. count++;
  1541. } ).detach().appendTo( "#qunit-fixture" ).trigger( "click" );
  1542. assert.strictEqual( 1, count, "Event handler has not been removed" );
  1543. // Clean up detached data
  1544. cleanUp.remove();
  1545. } );
  1546. QUnit.test( "empty()", function( assert ) {
  1547. assert.expect( 3 );
  1548. assert.equal( jQuery( "#ap" ).children().empty().text().length, 0, "Check text is removed" );
  1549. assert.equal( jQuery( "#ap" ).children().length, 4, "Check elements are not removed" );
  1550. // using contents will get comments regular, text, and comment nodes
  1551. var j = jQuery( "#nonnodes" ).contents();
  1552. j.empty();
  1553. assert.equal( j.html(), "", "Check node,textnode,comment empty works" );
  1554. } );
  1555. QUnit.test( "jQuery.cleanData", function( assert ) {
  1556. assert.expect( 14 );
  1557. var type, pos, div, child;
  1558. type = "remove";
  1559. // Should trigger 4 remove event
  1560. div = getDiv().remove();
  1561. // Should both do nothing
  1562. pos = "Outer";
  1563. div.trigger( "click" );
  1564. pos = "Inner";
  1565. div.children().trigger( "click" );
  1566. type = "empty";
  1567. div = getDiv();
  1568. child = div.children();
  1569. // Should trigger 2 remove event
  1570. div.empty();
  1571. // Should trigger 1
  1572. pos = "Outer";
  1573. div.trigger( "click" );
  1574. // Should do nothing
  1575. pos = "Inner";
  1576. child.trigger( "click" );
  1577. // Should trigger 2
  1578. div.remove();
  1579. type = "html";
  1580. div = getDiv();
  1581. child = div.children();
  1582. // Should trigger 2 remove event
  1583. div.html( "<div></div>" );
  1584. // Should trigger 1
  1585. pos = "Outer";
  1586. div.trigger( "click" );
  1587. // Should do nothing
  1588. pos = "Inner";
  1589. child.trigger( "click" );
  1590. // Should trigger 2
  1591. div.remove();
  1592. function getDiv() {
  1593. var div = jQuery( "<div class='outer'><div class='inner'></div></div>" ).on( "click", function() {
  1594. assert.ok( true, type + " " + pos + " Click event fired." );
  1595. } ).on( "focus", function() {
  1596. assert.ok( true, type + " " + pos + " Focus event fired." );
  1597. } ).find( "div" ).on( "click", function() {
  1598. assert.ok( false, type + " " + pos + " Click event fired." );
  1599. } ).on( "focus", function() {
  1600. assert.ok( false, type + " " + pos + " Focus event fired." );
  1601. } ).end().appendTo( "body" );
  1602. div[ 0 ].detachEvent = div[ 0 ].removeEventListener = function( t ) {
  1603. assert.ok( true, type + " Outer " + t + " event unbound" );
  1604. };
  1605. div[ 0 ].firstChild.detachEvent = div[ 0 ].firstChild.removeEventListener = function( t ) {
  1606. assert.ok( true, type + " Inner " + t + " event unbound" );
  1607. };
  1608. return div;
  1609. }
  1610. } );
  1611. QUnit.test( "jQuery.cleanData eliminates all private data (gh-2127)", function( assert ) {
  1612. assert.expect( 3 );
  1613. var div = jQuery( "<div></div>" ).appendTo( "#qunit-fixture" );
  1614. jQuery._data( div[ 0 ], "gh-2127", "testing" );
  1615. assert.ok( !jQuery.isEmptyObject( jQuery._data( div[ 0 ] ) ), "Ensure some private data exists" );
  1616. div.remove();
  1617. assert.ok( !jQuery.hasData( div[ 0 ] ), "Removed element hasData should return false" );
  1618. assert.ok( jQuery.isEmptyObject( jQuery._data( div[ 0 ] ) ),
  1619. "Private data is empty after node is removed" );
  1620. div.remove();
  1621. } );
  1622. QUnit.test( "jQuery.cleanData eliminates all public data", function( assert ) {
  1623. assert.expect( 3 );
  1624. var key,
  1625. div = jQuery( "<div></div>" );
  1626. div.data( "some", "data" );
  1627. assert.ok( !jQuery.isEmptyObject( jQuery.data( div[ 0 ] ) ), "Ensure some public data exists" );
  1628. div.remove();
  1629. assert.ok( !jQuery.hasData( div[ 0 ] ), "Removed element hasData should return false" );
  1630. // Make sure the expando is gone
  1631. for ( key in div[ 0 ] ) {
  1632. if ( /^jQuery/.test( key ) ) {
  1633. assert.strictEqual( div[ 0 ][ key ], undefined, "Expando was not removed when there was no more data" );
  1634. }
  1635. }
  1636. } );
  1637. QUnit.test( "domManip plain-text caching (trac-6779)", function( assert ) {
  1638. assert.expect( 1 );
  1639. // DOM manipulation fails if added text matches an Object method
  1640. var i,
  1641. $f = jQuery( "<div></div>" ).appendTo( "#qunit-fixture" ),
  1642. bad = [ "start-", "toString", "hasOwnProperty", "append", "here&there!", "-end" ];
  1643. for ( i = 0; i < bad.length; i++ ) {
  1644. try {
  1645. $f.append( bad[ i ] );
  1646. }
  1647. catch ( e ) {}
  1648. }
  1649. assert.equal( $f.text(), bad.join( "" ), "Cached strings that match Object properties" );
  1650. $f.remove();
  1651. } );
  1652. QUnit.test( "domManip executes scripts containing html comments or CDATA (trac-9221)", function( assert ) {
  1653. assert.expect( 3 );
  1654. jQuery( [
  1655. "<script type='text/javascript'>",
  1656. "<!--",
  1657. "QUnit.assert.ok( true, '<!-- handled' );",
  1658. "//-->",
  1659. "</script>"
  1660. ].join( "\n" ) ).appendTo( "#qunit-fixture" );
  1661. jQuery( [
  1662. "<script type='text/javascript'>",
  1663. "<![CDATA[",
  1664. "QUnit.assert.ok( true, '<![CDATA[ handled' );",
  1665. "//]]>",
  1666. "</script>"
  1667. ].join( "\n" ) ).appendTo( "#qunit-fixture" );
  1668. jQuery( [
  1669. "<script type='text/javascript'>",
  1670. "<!--//--><![CDATA[//><!--",
  1671. "QUnit.assert.ok( true, '<!--//--><![CDATA[//><!-- (Drupal case) handled' );",
  1672. "//--><!]]>",
  1673. "</script>"
  1674. ].join( "\n" ) ).appendTo( "#qunit-fixture" );
  1675. } );
  1676. testIframe(
  1677. "domManip tolerates window-valued document[0] in IE9/10 (trac-12266)",
  1678. "manipulation/iframe-denied.html",
  1679. function( assert, jQuery, window, document, test ) {
  1680. assert.expect( 1 );
  1681. assert.ok( test.status, test.description );
  1682. }
  1683. );
  1684. testIframe(
  1685. "domManip executes scripts in iframes in the iframes' context",
  1686. "manipulation/scripts-context.html",
  1687. function( assert, framejQuery, frameWindow, frameDocument ) {
  1688. assert.expect( 2 );
  1689. jQuery( frameDocument.body ).append( "<script>window.scriptTest = true;<\x2fscript>" );
  1690. assert.ok( !window.scriptTest, "script executed in iframe context" );
  1691. assert.ok( frameWindow.scriptTest, "script executed in iframe context" );
  1692. }
  1693. );
  1694. testIframe(
  1695. "domManip executes external scripts in iframes in the iframes' context",
  1696. "manipulation/scripts-context.html",
  1697. function( assert, framejQuery, frameWindow, frameDocument ) {
  1698. assert.expect( 2 );
  1699. Globals.register( "finishTest" );
  1700. return new Promise( function( resolve ) {
  1701. window.finishTest = resolve;
  1702. jQuery( frameDocument.body ).append(
  1703. "<script src='" + url( "manipulation/set-global-scripttest.js" ) + "'></script>" );
  1704. assert.ok( !window.scriptTest, "script executed in iframe context" );
  1705. assert.ok( frameWindow.scriptTest, "script executed in iframe context" );
  1706. } );
  1707. },
  1708. // The AJAX module is needed for jQuery._evalUrl.
  1709. QUnit[ jQuery.ajax ? "test" : "skip" ]
  1710. );
  1711. QUnit.test( "jQuery.clone - no exceptions for object elements #9587", function( assert ) {
  1712. assert.expect( 1 );
  1713. try {
  1714. jQuery( "#no-clone-exception" ).clone();
  1715. assert.ok( true, "cloned with no exceptions" );
  1716. } catch ( e ) {
  1717. assert.ok( false, e.message );
  1718. }
  1719. } );
  1720. QUnit.test( "Cloned, detached HTML5 elems (#10667,10670)", function( assert ) {
  1721. assert.expect( 7 );
  1722. var $clone,
  1723. $section = jQuery( "<section>" ).appendTo( "#qunit-fixture" );
  1724. // First clone
  1725. $clone = $section.clone();
  1726. // This branch tests a known behavior in modern browsers that should never fail.
  1727. // Included for expected test count symmetry (expecting 1)
  1728. assert.equal( $clone[ 0 ].nodeName, "SECTION", "detached clone nodeName matches 'SECTION'" );
  1729. // Bind an event
  1730. $section.on( "click", function() {
  1731. assert.ok( true, "clone fired event" );
  1732. } );
  1733. // Second clone (will have an event bound)
  1734. $clone = $section.clone( true );
  1735. // Trigger an event from the first clone
  1736. $clone.trigger( "click" );
  1737. $clone.off( "click" );
  1738. // Add a child node with text to the original
  1739. $section.append( "<p>Hello</p>" );
  1740. // Third clone (will have child node and text)
  1741. $clone = $section.clone( true );
  1742. assert.equal( $clone.find( "p" ).text(), "Hello", "Assert text in child of clone" );
  1743. // Trigger an event from the third clone
  1744. $clone.trigger( "click" );
  1745. $clone.off( "click" );
  1746. // Add attributes to copy
  1747. $section.attr( {
  1748. "class": "foo bar baz",
  1749. "title": "This is a title"
  1750. } );
  1751. // Fourth clone (will have newly added attributes)
  1752. $clone = $section.clone( true );
  1753. assert.equal( $clone.attr( "class" ), $section.attr( "class" ), "clone and element have same class attribute" );
  1754. assert.equal( $clone.attr( "title" ), $section.attr( "title" ), "clone and element have same title attribute" );
  1755. // Remove the original
  1756. $section.remove();
  1757. // Clone the clone
  1758. $section = $clone.clone( true );
  1759. // Remove the clone
  1760. $clone.remove();
  1761. // Trigger an event from the clone of the clone
  1762. $section.trigger( "click" );
  1763. // Unbind any remaining events
  1764. $section.off( "click" );
  1765. $clone.off( "click" );
  1766. } );
  1767. QUnit.test( "Guard against exceptions when clearing safeChildNodes", function( assert ) {
  1768. assert.expect( 1 );
  1769. var div;
  1770. try {
  1771. div = jQuery( "<div></div><hr/><code></code><b></b>" );
  1772. } catch ( e ) {}
  1773. assert.ok( div && div.jquery, "Created nodes safely, guarded against exceptions on safeChildNodes[ -1 ]" );
  1774. } );
  1775. QUnit.test( "Ensure oldIE creates a new set on appendTo (#8894)", function( assert ) {
  1776. assert.expect( 5 );
  1777. assert.strictEqual( jQuery( "<div></div>" ).clone().addClass( "test" ).appendTo( "<div></div>" ).end().end().hasClass( "test" ), false, "Check jQuery.fn.appendTo after jQuery.clone" );
  1778. assert.strictEqual( jQuery( "<div></div>" ).find( "p" ).end().addClass( "test" ).appendTo( "<div></div>" ).end().end().hasClass( "test" ), false, "Check jQuery.fn.appendTo after jQuery.fn.find" );
  1779. assert.strictEqual( jQuery( "<div></div>" ).text( "test" ).addClass( "test" ).appendTo( "<div></div>" ).end().end().hasClass( "test" ), false, "Check jQuery.fn.appendTo after jQuery.fn.text" );
  1780. assert.strictEqual( jQuery( "<bdi></bdi>" ).clone().addClass( "test" ).appendTo( "<div></div>" ).end().end().hasClass( "test" ), false, "Check jQuery.fn.appendTo after clone html5 element" );
  1781. assert.strictEqual( jQuery( "<p></p>" ).appendTo( "<div></div>" ).end().length, jQuery( "<p>test</p>" ).appendTo( "<div></div>" ).end().length, "Elements created with createElement and with createDocumentFragment should be treated alike" );
  1782. } );
  1783. QUnit.test( "html() - script exceptions bubble (#11743)", function( assert ) {
  1784. assert.expect( 2 );
  1785. var done = assert.async(),
  1786. onerror = window.onerror;
  1787. setTimeout( function() {
  1788. window.onerror = onerror;
  1789. done();
  1790. }, 1000 );
  1791. window.onerror = function() {
  1792. assert.ok( true, "Exception thrown" );
  1793. if ( jQuery.ajax ) {
  1794. window.onerror = function() {
  1795. assert.ok( true, "Exception thrown in remote script" );
  1796. };
  1797. jQuery( "#qunit-fixture" ).html( "<script src='" + baseURL + "badcall.js'></script>" );
  1798. assert.ok( true, "Exception ignored" );
  1799. } else {
  1800. assert.ok( true, "No jQuery.ajax" );
  1801. }
  1802. };
  1803. jQuery( "#qunit-fixture" ).html( "<script>undefined();</script>" );
  1804. } );
  1805. QUnit.test( "checked state is cloned with clone()", function( assert ) {
  1806. assert.expect( 2 );
  1807. var elem = jQuery.parseHTML( "<input type='checkbox' checked='checked'/>" )[ 0 ];
  1808. elem.checked = false;
  1809. assert.equal( jQuery( elem ).clone().attr( "id", "clone" )[ 0 ].checked, false, "Checked false state correctly cloned" );
  1810. elem = jQuery.parseHTML( "<input type='checkbox'/>" )[ 0 ];
  1811. elem.checked = true;
  1812. assert.equal( jQuery( elem ).clone().attr( "id", "clone" )[ 0 ].checked, true, "Checked true state correctly cloned" );
  1813. } );
  1814. QUnit.test( "manipulate mixed jQuery and text (#12384, #12346)", function( assert ) {
  1815. assert.expect( 2 );
  1816. var div = jQuery( "<div>a</div>" ).append( "&nbsp;", jQuery( "<span>b</span>" ), "&nbsp;", jQuery( "<span>c</span>" ) ),
  1817. nbsp = String.fromCharCode( 160 );
  1818. assert.equal( div.text(), "a" + nbsp + "b" + nbsp + "c", "Appending mixed jQuery with text nodes" );
  1819. div = jQuery( "<div><div></div></div>" )
  1820. .find( "div" )
  1821. .after( "<p>a</p>", "<p>b</p>" )
  1822. .parent();
  1823. assert.equal( div.find( "*" ).length, 3, "added 2 paragraphs after inner div" );
  1824. } );
  1825. QUnit.test( "script evaluation (#11795)", function( assert ) {
  1826. assert.expect( 13 );
  1827. var scriptsIn, scriptsOut,
  1828. fixture = jQuery( "#qunit-fixture" ).empty(),
  1829. objGlobal = ( function() {
  1830. return this;
  1831. } )(),
  1832. isOk = objGlobal.ok,
  1833. notOk = function() {
  1834. var args = arguments;
  1835. args[ 0 ] = !args[ 0 ];
  1836. return isOk.apply( this, args );
  1837. };
  1838. objGlobal.ok = notOk;
  1839. scriptsIn = jQuery( [
  1840. "<script type='something/else'>QUnit.assert.ok( false, 'evaluated: non-script' );</script>",
  1841. "<script type='text/javascript'>QUnit.assert.ok( true, 'evaluated: text/javascript' );</script>",
  1842. "<script type='text/ecmascript'>QUnit.assert.ok( true, 'evaluated: text/ecmascript' );</script>",
  1843. "<script>QUnit.assert.ok( true, 'evaluated: no type' );</script>",
  1844. "<div>",
  1845. "<script type='something/else'>QUnit.assert.ok( false, 'evaluated: inner non-script' );</script>",
  1846. "<script type='text/javascript'>QUnit.assert.ok( true, 'evaluated: inner text/javascript' );</script>",
  1847. "<script type='text/ecmascript'>QUnit.assert.ok( true, 'evaluated: inner text/ecmascript' );</script>",
  1848. "<script>QUnit.assert.ok( true, 'evaluated: inner no type' );</script>",
  1849. "</div>"
  1850. ].join( "" ) );
  1851. scriptsIn.appendTo( jQuery( "<div class='detached'></div>" ) );
  1852. objGlobal.ok = isOk;
  1853. scriptsOut = fixture.append( scriptsIn ).find( "script" );
  1854. assert.equal( scriptsOut[ 0 ].type, "something/else", "Non-evaluated type." );
  1855. assert.equal( scriptsOut[ 1 ].type, "text/javascript", "Evaluated type." );
  1856. assert.deepEqual( scriptsOut.get(), fixture.find( "script" ).get(), "All script tags remain." );
  1857. objGlobal.ok = notOk;
  1858. scriptsOut = scriptsOut.add( scriptsOut.clone() ).appendTo( fixture.find( "div" ) );
  1859. assert.deepEqual( fixture.find( "div script" ).get(), scriptsOut.get(), "Scripts cloned without reevaluation" );
  1860. fixture.append( scriptsOut.detach() );
  1861. assert.deepEqual( fixture.children( "script" ).get(), scriptsOut.get(), "Scripts detached without reevaluation" );
  1862. objGlobal.ok = isOk;
  1863. if ( jQuery.ajax ) {
  1864. Globals.register( "testBar" );
  1865. jQuery( "#qunit-fixture" ).append( "<script src='" + url( "mock.php?action=testbar" ) + "'></script>" );
  1866. assert.strictEqual( window.testBar, "bar", "Global script evaluation" );
  1867. } else {
  1868. assert.ok( true, "No jQuery.ajax" );
  1869. assert.ok( true, "No jQuery.ajax" );
  1870. }
  1871. } );
  1872. QUnit[ jQuery.ajax ? "test" : "skip" ]( "jQuery._evalUrl (#12838)", function( assert ) {
  1873. assert.expect( 5 );
  1874. var message, expectedArgument,
  1875. ajax = jQuery.ajax,
  1876. evalUrl = jQuery._evalUrl;
  1877. message = "jQuery.ajax implementation";
  1878. expectedArgument = 1;
  1879. jQuery.ajax = function( input ) {
  1880. assert.equal( ( input.url || input ).slice( -1 ), expectedArgument, message );
  1881. expectedArgument++;
  1882. };
  1883. jQuery( "#qunit-fixture" ).append( "<script src='1'></script><script src='2'></script>" );
  1884. assert.equal( expectedArgument, 3, "synchronous execution" );
  1885. message = "custom implementation";
  1886. expectedArgument = 3;
  1887. jQuery._evalUrl = jQuery.ajax;
  1888. jQuery.ajax = function( options ) {
  1889. assert.strictEqual( options, {}, "Unexpected call to jQuery.ajax" );
  1890. };
  1891. jQuery( "#qunit-fixture" ).append( "<script src='3'></script><script src='4'></script>" );
  1892. jQuery.ajax = ajax;
  1893. jQuery._evalUrl = evalUrl;
  1894. } );
  1895. QUnit.test( "jQuery.htmlPrefilter (gh-1747)", function( assert ) {
  1896. assert.expect( 5 );
  1897. var expectedArgument,
  1898. invocations = 0,
  1899. done = assert.async(),
  1900. htmlPrefilter = jQuery.htmlPrefilter,
  1901. fixture = jQuery( "<div></div>" ).appendTo( "#qunit-fixture" ),
  1902. poison = "<script>jQuery.htmlPrefilter.assert.ok( false, 'script not executed' );</script>";
  1903. jQuery.htmlPrefilter = function( html ) {
  1904. invocations++;
  1905. assert.equal( html, expectedArgument, "Expected input" );
  1906. // Remove <script> and <del> elements
  1907. return htmlPrefilter.apply( this, arguments )
  1908. .replace( /<(script|del)(?=[\s>])[\w\W]*?<\/\1\s*>/ig, "" );
  1909. };
  1910. jQuery.htmlPrefilter.assert = assert;
  1911. expectedArgument = "A-" + poison + "B-" + poison + poison + "C-";
  1912. fixture.html( expectedArgument );
  1913. expectedArgument = "D-" + poison + "E-" + "<del></del><div>" + poison + poison + "</div>" + "F-";
  1914. fixture.append( expectedArgument );
  1915. expectedArgument = poison;
  1916. fixture.find( "div" ).replaceWith( expectedArgument );
  1917. assert.equal( invocations, 3, "htmlPrefilter invoked for all DOM manipulations" );
  1918. assert.equal( fixture.html(), "A-B-C-D-E-F-", "htmlPrefilter modified HTML" );
  1919. // Allow asynchronous script execution to generate assertions
  1920. setTimeout( function() {
  1921. jQuery.htmlPrefilter = htmlPrefilter;
  1922. done();
  1923. }, 100 );
  1924. } );
  1925. QUnit.test( "insertAfter, insertBefore, etc do not work when destination is original element. Element is removed (#4087)", function( assert ) {
  1926. assert.expect( 10 );
  1927. var elems;
  1928. jQuery.each( [
  1929. "appendTo",
  1930. "prependTo",
  1931. "insertBefore",
  1932. "insertAfter",
  1933. "replaceAll"
  1934. ], function( index, name ) {
  1935. elems = jQuery( [
  1936. "<ul id='test4087-complex'><li class='test4087'><div>c1</div>h1</li><li><div>c2</div>h2</li></ul>",
  1937. "<div id='test4087-simple'><div class='test4087-1'>1<div class='test4087-2'>2</div><div class='test4087-3'>3</div></div></div>",
  1938. "<div id='test4087-multiple'><div class='test4087-multiple'>1</div><div class='test4087-multiple'>2</div></div>"
  1939. ].join( "" ) ).appendTo( "#qunit-fixture" );
  1940. // complex case based on https://jsfiddle.net/pbramos/gZ7vB/
  1941. jQuery( "#test4087-complex div" )[ name ]( "#test4087-complex li:last-child div:last-child" );
  1942. assert.equal( jQuery( "#test4087-complex li:last-child div" ).length, name === "replaceAll" ? 1 : 2, name + " a node to itself, complex case." );
  1943. // simple case
  1944. jQuery( ".test4087-1" )[ name ]( ".test4087-1" );
  1945. assert.equal( jQuery( ".test4087-1" ).length, 1, name + " a node to itself, simple case." );
  1946. // clean for next test
  1947. jQuery( "#test4087-complex" ).remove();
  1948. jQuery( "#test4087-simple" ).remove();
  1949. jQuery( "#test4087-multiple" ).remove();
  1950. } );
  1951. } );
  1952. QUnit.test( "Index for function argument should be received (#13094)", function( assert ) {
  1953. assert.expect( 2 );
  1954. var i = 0;
  1955. jQuery( "<div></div><div></div>" ).before( function( index ) {
  1956. assert.equal( index, i++, "Index should be correct" );
  1957. } );
  1958. } );
  1959. QUnit.test( "Make sure jQuery.fn.remove can work on elements in documentFragment", function( assert ) {
  1960. assert.expect( 1 );
  1961. var fragment = document.createDocumentFragment(),
  1962. div = fragment.appendChild( document.createElement( "div" ) );
  1963. jQuery( div ).remove();
  1964. assert.equal( fragment.childNodes.length, 0, "div element was removed from documentFragment" );
  1965. } );
  1966. QUnit.test( "Make sure specific elements with content created correctly (#13232)", function( assert ) {
  1967. assert.expect( 20 );
  1968. var results = [],
  1969. args = [],
  1970. elems = {
  1971. thead: "<tr><td>thead</td></tr>",
  1972. tbody: "<tr><td>tbody</td></tr>",
  1973. tfoot: "<tr><td>tfoot</td></tr>",
  1974. colgroup: "<col span='5'></col>",
  1975. caption: "caption",
  1976. tr: "<td>tr</td>",
  1977. th: "th",
  1978. td: "<div>td</div>",
  1979. optgroup: "<option>optgroup</option>",
  1980. option: "option"
  1981. };
  1982. jQuery.each( elems, function( name, value ) {
  1983. var html = "<" + name + ">" + value + "</" + name + ">";
  1984. assert.ok( jQuery.parseHTML( "<" + name + ">" + value + "</" + name + ">" )[ 0 ].nodeName.toLowerCase() === name, name + " is created correctly" );
  1985. results.push( name );
  1986. args.push( html );
  1987. } );
  1988. jQuery.fn.append.apply( jQuery( "<div></div>" ), args ).children().each( function( i ) {
  1989. assert.ok( this.nodeName.toLowerCase() === results[ i ] );
  1990. } );
  1991. } );
  1992. QUnit.test( "Validate creation of multiple quantities of certain elements (#13818)", function( assert ) {
  1993. assert.expect( 22 );
  1994. var tags = [ "thead", "tbody", "tfoot", "colgroup", "col", "caption", "tr", "th", "td", "optgroup", "option" ];
  1995. jQuery.each( tags, function( index, tag ) {
  1996. jQuery( "<" + tag + "></" + tag + "><" + tag + "></" + tag + ">" ).each( function() {
  1997. assert.ok( this.nodeName.toLowerCase() === tag, tag + " elements created correctly" );
  1998. } );
  1999. } );
  2000. } );
  2001. QUnit.test( "Make sure tr element will be appended to tbody element of table when present", function( assert ) {
  2002. assert.expect( 1 );
  2003. var html,
  2004. table = document.createElement( "table" );
  2005. table.appendChild( document.createElement( "tbody" ) );
  2006. document.getElementById( "qunit-fixture" ).appendChild( table );
  2007. jQuery( table ).append( "<tr><td>test</td></tr>" );
  2008. // Lowercase and replace spaces to remove possible browser inconsistencies
  2009. html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
  2010. assert.strictEqual( html, "<tbody><tr><td>test</td></tr></tbody>" );
  2011. } );
  2012. QUnit.test( "Make sure tr elements will be appended to tbody element of table when present", function( assert ) {
  2013. assert.expect( 1 );
  2014. var html,
  2015. table = document.createElement( "table" );
  2016. table.appendChild( document.createElement( "tbody" ) );
  2017. document.getElementById( "qunit-fixture" ).appendChild( table );
  2018. jQuery( table ).append( "<tr><td>1</td></tr><tr><td>2</td></tr>" );
  2019. // Lowercase and replace spaces to remove possible browser inconsistencies
  2020. html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
  2021. assert.strictEqual( html, "<tbody><tr><td>1</td></tr><tr><td>2</td></tr></tbody>" );
  2022. } );
  2023. QUnit.test( "Make sure tfoot element will not be appended to tbody element of table when present", function( assert ) {
  2024. assert.expect( 1 );
  2025. var html,
  2026. table = document.createElement( "table" );
  2027. table.appendChild( document.createElement( "tbody" ) );
  2028. document.getElementById( "qunit-fixture" ).appendChild( table );
  2029. jQuery( table ).append( "<tfoot></tfoot>" );
  2030. // Lowercase and replace spaces to remove possible browser inconsistencies
  2031. html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
  2032. assert.strictEqual( html, "<tbody></tbody><tfoot></tfoot>" );
  2033. } );
  2034. QUnit.test( "Make sure document fragment will be appended to tbody element of table when present", function( assert ) {
  2035. assert.expect( 1 );
  2036. var html,
  2037. fragment = document.createDocumentFragment(),
  2038. table = document.createElement( "table" ),
  2039. tr = document.createElement( "tr" ),
  2040. td = document.createElement( "td" );
  2041. table.appendChild( document.createElement( "tbody" ) );
  2042. document.getElementById( "qunit-fixture" ).appendChild( table );
  2043. fragment.appendChild( tr );
  2044. tr.appendChild( td );
  2045. td.innerHTML = "test";
  2046. jQuery( table ).append( fragment );
  2047. // Lowercase and replace spaces to remove possible browser inconsistencies
  2048. html = table.innerHTML.toLowerCase().replace( /\s/g, "" );
  2049. assert.strictEqual( html, "<tbody><tr><td>test</td></tr></tbody>" );
  2050. } );
  2051. QUnit.test( "Make sure col element is appended correctly", function( assert ) {
  2052. assert.expect( 1 );
  2053. var table = jQuery( "<table cellpadding='0'><tr><td>test</td></tr></table>" );
  2054. jQuery( table ).appendTo( "#qunit-fixture" );
  2055. jQuery( "<col width='150'></col>" ).prependTo( table );
  2056. assert.strictEqual( table.find( "td" ).width(), 150 );
  2057. } );
  2058. QUnit.test( "Make sure tr is not appended to the wrong tbody (gh-3439)", function( assert ) {
  2059. assert.expect( 1 );
  2060. var htmlOut,
  2061. htmlIn =
  2062. "<thead><tr><td>" +
  2063. "<table><tbody><tr><td>nested</td></tr></tbody></table>" +
  2064. "</td></tr></thead>",
  2065. newRow = "<tr><td>added</td></tr>",
  2066. htmlExpected = htmlIn.replace( "</thead>", "</thead>" + newRow ),
  2067. table = supportjQuery( "<table></table>" ).html( htmlIn ).appendTo( "#qunit-fixture" )[ 0 ];
  2068. jQuery( table ).append( newRow );
  2069. // Lowercase and replace spaces to remove possible browser inconsistencies
  2070. htmlOut = table.innerHTML.toLowerCase().replace( /\s/g, "" );
  2071. assert.strictEqual( htmlOut, htmlExpected );
  2072. } );
  2073. QUnit.test( "Make sure tags with single-character names are found (gh-4124)", function( assert ) {
  2074. assert.expect( 1 );
  2075. var htmlOut,
  2076. htmlIn = "<p>foo<!--<td>--></p>",
  2077. $el = jQuery( "<div></div>" );
  2078. $el.html( htmlIn );
  2079. // Lowercase and replace spaces to remove possible browser inconsistencies
  2080. htmlOut = $el[ 0 ].innerHTML.toLowerCase().replace( /\s/g, "" );
  2081. assert.strictEqual( htmlOut, htmlIn );
  2082. } );
  2083. // The AJAX module is needed for jQuery._evalUrl.
  2084. QUnit[ jQuery.ajax ? "test" : "skip" ]( "Insert script with data-URI (gh-1887)", function( assert ) {
  2085. assert.expect( 1 );
  2086. Globals.register( "testFoo" );
  2087. Globals.register( "testSrcFoo" );
  2088. var script = document.createElement( "script" ),
  2089. fixture = document.getElementById( "qunit-fixture" ),
  2090. done = assert.async();
  2091. script.src = "data:text/javascript,testSrcFoo = 'foo';";
  2092. fixture.appendChild( script );
  2093. jQuery( fixture ).append( "<script src=\"data:text/javascript,testFoo = 'foo';\"></script>" );
  2094. setTimeout( function() {
  2095. if ( window.testSrcFoo === "foo" ) {
  2096. assert.strictEqual( window.testFoo, window.testSrcFoo, "data-URI script executed" );
  2097. } else {
  2098. assert.ok( true, "data-URI script is not supported by this environment" );
  2099. }
  2100. done();
  2101. }, 100 );
  2102. } );
  2103. QUnit.test( "Ignore content from unsuccessful responses (gh-4126)", function( assert ) {
  2104. assert.expect( 1 );
  2105. var globalEval = jQuery.globalEval;
  2106. jQuery.globalEval = function( code ) {
  2107. assert.ok( false, "no attempt to evaluate code from an unsuccessful response" );
  2108. };
  2109. try {
  2110. jQuery( "#qunit-fixture" ).append(
  2111. "<script src='" + url( "mock.php?action=error" ) + "'></script>" );
  2112. assert.ok( true, "no error thrown from embedding script with unsuccessful-response src" );
  2113. } catch ( e ) {
  2114. throw e;
  2115. } finally {
  2116. jQuery.globalEval = globalEval;
  2117. }
  2118. } );
  2119. testIframe(
  2120. "Check if CSP nonce is preserved",
  2121. "mock.php?action=cspNonce",
  2122. function( assert, jQuery, window, document ) {
  2123. var done = assert.async();
  2124. assert.expect( 1 );
  2125. supportjQuery.get( baseURL + "support/csp.log" ).done( function( data ) {
  2126. assert.equal( data, "", "No log request should be sent" );
  2127. supportjQuery.get( baseURL + "mock.php?action=cspClean" ).done( done );
  2128. } );
  2129. },
  2130. // Support: Edge 18+, iOS 7-9 only, Android 4.0-4.4 only
  2131. // Edge doesn't support nonce in non-inline scripts.
  2132. // See https://web.archive.org/web/20171203124125/https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/13246371/
  2133. // Old iOS & Android Browser versions support script-src but not nonce, making this test
  2134. // impossible to run. Browsers not supporting CSP at all are not a problem as they'll skip
  2135. // script-src restrictions completely.
  2136. QUnit[ /\bedge\/|iphone os [789]|android 4\./i.test( navigator.userAgent ) ? "skip" : "test" ]
  2137. );
  2138. testIframe(
  2139. "Check if CSP nonce is preserved for external scripts with src attribute",
  2140. "mock.php?action=cspNonce&test=external",
  2141. function( assert, jQuery, window, document ) {
  2142. var done = assert.async();
  2143. assert.expect( 1 );
  2144. supportjQuery.get( baseURL + "support/csp.log" ).done( function( data ) {
  2145. assert.equal( data, "", "No log request should be sent" );
  2146. supportjQuery.get( baseURL + "mock.php?action=cspClean" ).done( done );
  2147. } );
  2148. },
  2149. // The AJAX module is needed for jQuery._evalUrl.
  2150. // Support: Edge 18+, iOS 7-9 only, Android 4.0-4.4 only
  2151. // Edge doesn't support nonce in non-inline scripts.
  2152. // See https://web.archive.org/web/20171203124125/https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/13246371/
  2153. // Old iOS & Android Browser versions support script-src but not nonce, making this test
  2154. // impossible to run. Browsers not supporting CSP at all are not a problem as they'll skip
  2155. // script-src restrictions completely.
  2156. QUnit[ jQuery.ajax && !/\bedge\/|iphone os [789]|android 4\./i.test( navigator.userAgent ) ? "test" : "skip" ]
  2157. );
  2158. testIframe(
  2159. "jQuery.globalEval supports nonce",
  2160. "mock.php?action=cspNonce&test=globaleval",
  2161. function( assert, jQuery, window, document ) {
  2162. var done = assert.async();
  2163. assert.expect( 1 );
  2164. supportjQuery.get( baseURL + "support/csp.log" ).done( function( data ) {
  2165. assert.equal( data, "", "No log request should be sent" );
  2166. supportjQuery.get( baseURL + "mock.php?action=cspClean" ).done( done );
  2167. } );
  2168. },
  2169. // Support: Edge 18+, iOS 7-9 only, Android 4.0-4.4 only
  2170. // Edge doesn't support nonce in non-inline scripts.
  2171. // See https://web.archive.org/web/20171203124125/https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/13246371/
  2172. // Old iOS & Android Browser versions support script-src but not nonce, making this test
  2173. // impossible to run. Browsers not supporting CSP at all are not a problem as they'll skip
  2174. // script-src restrictions completely.
  2175. QUnit[ /\bedge\/|iphone os [789]|android 4\./i.test( navigator.userAgent ) ? "skip" : "test" ]
  2176. );
  2177. QUnit.test( "Sanitized HTML doesn't get unsanitized", function( assert ) {
  2178. var container,
  2179. counter = 0,
  2180. oldIos = /iphone os (?:8|9|10|11|12)_/i.test( navigator.userAgent ),
  2181. assertCount = oldIos ? 12 : 13,
  2182. done = assert.async( assertCount );
  2183. assert.expect( assertCount );
  2184. Globals.register( "xss" );
  2185. window.xss = sinon.spy();
  2186. container = jQuery( "<div></div>" );
  2187. container.appendTo( "#qunit-fixture" );
  2188. function test( htmlString ) {
  2189. var currCounter = counter,
  2190. div = jQuery( "<div></div>" );
  2191. counter++;
  2192. div.appendTo( container );
  2193. div.html( htmlString );
  2194. setTimeout( function() {
  2195. assert.ok( window.xss.withArgs( currCounter ).notCalled,
  2196. "Insecure code wasn't executed, input: " + htmlString );
  2197. done();
  2198. }, 1000 );
  2199. }
  2200. // Note: below test cases need to invoke the xss function with consecutive
  2201. // decimal parameters for the assertion messages to be correct.
  2202. // Thanks to Masato Kinugawa from Cure53 for providing the following test cases.
  2203. test( "<img alt=\"<x\" title=\"/><img src=url404 onerror=xss(0)>\">" );
  2204. test( "<img alt=\"\n<x\" title=\"/>\n<img src=url404 onerror=xss(1)>\">" );
  2205. test( "<style><style/><img src=url404 onerror=xss(2)>" );
  2206. test( "<xmp><xmp/><img src=url404 onerror=xss(3)>" );
  2207. test( "<title><title /><img src=url404 onerror=xss(4)>" );
  2208. test( "<iframe><iframe/><img src=url404 onerror=xss(5)>" );
  2209. test( "<noframes><noframes/><img src=url404 onerror=xss(6)>" );
  2210. test( "<noscript><noscript/><img src=url404 onerror=xss(7)>" );
  2211. test( "<foo\" alt=\"\" title=\"/><img src=url404 onerror=xss(8)>\">" );
  2212. test( "<img alt=\"<x\" title=\"\" src=\"/><img src=url404 onerror=xss(9)>\">" );
  2213. test( "<noscript/><img src=url404 onerror=xss(10)>" );
  2214. test( "<option><style></option></select><img src=url404 onerror=xss(11)></style>" );
  2215. // Support: iOS 8 - 12 only.
  2216. // Old iOS parses `<noembed>` tags differently, executing this code. This is no
  2217. // different to native behavior on that OS, though, so just accept it.
  2218. if ( !oldIos ) {
  2219. test( "<noembed><noembed/><img src=url404 onerror=xss(12)>" );
  2220. }
  2221. } );