zepto.html 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
  6. <link rel="stylesheet" href="test.css">
  7. <title>Zepto Core unit tests</title>
  8. <script src="../vendor/evidence.js"></script>
  9. <script src="evidence_runner.js"></script>
  10. <script>
  11. // avoid caching
  12. (function(){
  13. function load(scripts){
  14. scripts.split(' ').forEach(function(script){
  15. document.write('<script src="../src/'+script+'.js?'+(+new Date)+'"></scr'+'ipt>')
  16. })
  17. }
  18. load('zepto event ie')
  19. })()
  20. </script>
  21. <style>
  22. #get_style_element {
  23. font-size: 48px;
  24. color: black;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <h1>Zepto Core unit tests</h1>
  30. <p id="results">
  31. Running… see browser console for results
  32. </p>
  33. <div id="fixtures">
  34. <div id="some_element"></div>
  35. <p>
  36. <span class="yay">yay</span>
  37. <span></span>
  38. <span class="nay" id="nay">nay</span>
  39. </p>
  40. <div id="toggle_element"></div>
  41. <div id="get_style_wrapper" style="font-size: 16px;">
  42. <div id="get_style_element">Derp</div>
  43. </div>
  44. <div class="replacewith">
  45. <div class="inner first">Hello</div>
  46. <div class="inner second">And</div>
  47. <div class="inner third">Goodbye</div>
  48. </div>
  49. <div id="attr_1" data-id="someId1" data-name="someName1"></div>
  50. <div id="attr_2" data-id="someId2" data-name="someName2"></div>
  51. <div id="attr_remove" data-name="boom"></div>
  52. <div id="attr_remove_multi" data-id="someId1" data-name="someName1"></div>
  53. <form><input id="attr_val" value="Hello World"></form>
  54. <div id="data_attr" data-foo="bar" data-foo-bar="baz" data-empty></div>
  55. <form id="attr_with_text_input">
  56. </form>
  57. <label for="prop_test2" id="prop_test1"></label>
  58. <input id="prop_test2" type="text" tabindex="-1" maxlength="10" readonly class="propTest" />
  59. <table id="prop_test3" cellspacing="10" cellpadding="5">
  60. <tr>
  61. <td id="prop_test4" rowspan="2"></td>
  62. <td></td>
  63. </tr>
  64. <tr>
  65. <td></td>
  66. </tr>
  67. <tr>
  68. <td id="prop_test5" colspan="2"></td>
  69. </tr>
  70. </table>
  71. <img id="prop_test6" usemap="#imgMap" />
  72. <map name="imgMap">
  73. </map>
  74. <div id="prop_test7" contenteditable="true"></div>
  75. <div class="htmltest" id="htmltest1"></div>
  76. <div class="htmltest" id="htmltest2"></div>
  77. <div id="htmltest3"></div>
  78. <table id="htmltest4"></table>
  79. <div id="texttest1" class="texttest"><span>Here <strong>is</strong> some text</span></div>
  80. <div id="texttest2" class="texttest">And <em>some more</em></div>
  81. <div id="texttest3" class="texttest"></div>
  82. <div id="beforeafter_container"><div id="beforeafter"></div></div>
  83. <div id="appendtoprependto_container"><div id="appendtoprependto"></div></div>
  84. <div id="insertbeforeinsertafter_container"><div id="insertbeforeinsertafter"></div></div>
  85. <div id="empty_test">
  86. <div id="empty_1"></div>
  87. <div id="empty_2"></div>
  88. <div id="empty_3"></div>
  89. <span id="empty_4">test</span>
  90. </div>
  91. <p id="find1">
  92. <span class="findme">1</span>
  93. <span class="findme">2</span>
  94. <b>3<span class="findme">4</span></b>
  95. <span class="findme">5<span>6</span></span>
  96. </p>
  97. <p id="find2">
  98. <span>1</span>
  99. <span>2</span>
  100. <span>3<span>4</span></span>
  101. <span>5<span>6</span></span>
  102. </p>
  103. <div id="eachtest">
  104. <span></span><b></b><br>
  105. </div>
  106. <div style="position:absolute;width:100px;height:50px" id="offset">test</div>
  107. <ul id="parents">
  108. <li id="li1">
  109. <ul id="nested">
  110. <li id="li2">one</li>
  111. <li>two</li>
  112. <li>three</li>
  113. </ul>
  114. <ul>
  115. <li></li>
  116. </ul>
  117. </li>
  118. </ul>
  119. <ul id="childrenTest">
  120. <li class="child one"><a class="childOfOne" href="#">gchild1</a></li>
  121. <li class="child two"><a class="childOfTwo" href="#">gchild2</a></li>
  122. <li class="child three"><a class="childOfThree" href="#">gchild3</a></li>
  123. <li class="child four"><a class="childOfFour" href="#">gchild4</a></li>
  124. </ul>
  125. <ul id="contentsTest">1<span>2</span><span id="contentsEmptyTest"></span></ul>
  126. <iframe id="contentsIframeTest" src="fixtures/iframe_document.html"></iframe>
  127. <ul id="siblingsTest">
  128. <li class="child one"><span class="b"></span><em></em><b></b></li>
  129. <li class="child two"><span class="c"></span><em></em><b></b></li>
  130. <li class="child three"><span class="d"></span><em></em><b></b></li>
  131. <li class="child four"><span class="e"></span></li>
  132. </ul>
  133. <ul id="notTest">
  134. <li class="child one"><span class="b"></span></li>
  135. <li class="child two"><span class="c"></span></li>
  136. <li class="child three"><span class="d" id="notTestExclude"></span></li>
  137. <li class="child four"><span class="e"></span></li>
  138. </ul>
  139. <div id="addTest">
  140. <span class="add_span"></span>
  141. <span class="add_span"></span>
  142. <span class="add_span"></span>
  143. <span class="add_span_exclude"></span>
  144. <div id="addTestDiv"></div>
  145. </div>
  146. <style>
  147. .hidden {
  148. display: none;
  149. }
  150. #show_hide_span1,
  151. #show_hide_span2 {
  152. display: block;
  153. }
  154. #show_hide_div1 {
  155. display: inline-block;
  156. }
  157. </style>
  158. <div id="show_hide_div1" style="display:none"></div>
  159. <div id="show_hide_div2" class="hidden"></div>
  160. <div id="show_hide_div3"></div>
  161. <span id="show_hide_span1" style="display:none"></span>
  162. <span id="show_hide_span2" class="hidden"></span>
  163. <span id="show_hide_span3"></span>
  164. <div class="filtertest" id="filtertest1"></div>
  165. <div class="filtertest" id="filtertest2"></div>
  166. <div id="delegate_test"><span class="first-level"><span class="second-level">hi</span></span></div>
  167. <div id="undelegate_test"><span class="first-level"><span class="second-level">hi</span></span></div>
  168. <div id="delegate_blur_test"><input type="text"></div>
  169. <div id="delegate_focus_test"><input type="text"></div>
  170. <div id="another_element"></div>
  171. <div id="namespace_test"></div>
  172. <input type="text" id="BooleanInput" required />
  173. <form id="some_form"></form>
  174. <div class="replace_test_div">test</div>
  175. <div id="wrap_test"><span>hi</span><a></a><span>hello</span></div>
  176. <div id="wrapall_test"><b></b><span>hi</span><span>hello</span><i></i></div>
  177. <div id="wrapinner_test">1<span>2</span><span id="wrapinner_empty_test"></span></div>
  178. <div id="unwrap_test">
  179. <div class="unwrap_one"><b><span></span></b></div>
  180. <div class="unwrap_two"><b><span>1</span><span>2</span></b></div>
  181. </div>
  182. <div id="slice_test">
  183. <div class="slice1"></div>
  184. <div class="slice2"></div>
  185. <div class="slice3"></div>
  186. </div>
  187. <div id="eq_test">
  188. <div class="eq0"></div>
  189. <div class="eq1"></div>
  190. <div class="eq2"></div>
  191. </div>
  192. <div id="end_test">
  193. <div class="end_one"><b><span></span></b></div>
  194. <div class="end_two"><b><span>1</span><span>2</span></b></div>
  195. </div>
  196. <div id="andself_test">
  197. <div class="one"></div>
  198. <div class="two"></div>
  199. <div class="three"></div>
  200. <div class="four"></div>
  201. </div>
  202. <div id="index_test">
  203. <div class="index0"></div>
  204. <div class="index1"></div>
  205. </div>
  206. <div id="trigger_handler">
  207. <form method="get">
  208. </form>
  209. </div>
  210. <svg></svg>
  211. <iframe src="fixtures/iframe_document.html"></iframe>
  212. <iframe id="link_target_iframe" name="link_target_iframe" width="10" height="10"></iframe>
  213. <a id="link_that_will_be_prevented" href="fixtures/iframe_document.html" target="link_target_iframe"></a>
  214. <div id="test_dollar_with_shadow_root" style="display:none;"></div>
  215. </div><!-- fixtures -->
  216. <script>
  217. (function(){
  218. function click(el){
  219. var event = document.createEvent('MouseEvents')
  220. event.initMouseEvent('click', true, true, document.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null)
  221. el.dispatchEvent(event)
  222. }
  223. function mousedown(el){
  224. var event = document.createEvent('MouseEvents')
  225. event.initMouseEvent('mousedown', true, true, document.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null)
  226. el.dispatchEvent(event)
  227. }
  228. function outerHTML(node) {
  229. return node.outerHTML || (function(n) {
  230. var div = document.createElement('div')
  231. div.appendChild(n.cloneNode(true))
  232. var html = div.innerHTML
  233. div = null
  234. return html
  235. })(node)
  236. }
  237. function testDocumentFragmentOrShadowRootContext($fragment, t) {
  238. t.assertLength(1, $fragment)
  239. t.assertEqual(Node.DOCUMENT_FRAGMENT_NODE, $fragment.get(0).nodeType)
  240. var fragment = $fragment[0]
  241. var $div = $('<div id="child"><div class="inner"></div></div>')
  242. t.assertLength(0, $('#child', fragment))
  243. fragment.appendChild($div[0])
  244. var expectedElement = fragment.querySelector('#child')
  245. t.assertLength(1, $('#child', fragment))
  246. t.assertEqual($div[0], $('#child', fragment).get(0))
  247. t.assertEqual(expectedElement, $('#child', fragment).get(0))
  248. expectedElement = fragment.querySelector('.inner')
  249. t.assertLength(1, $('.inner', fragment))
  250. t.assertEqual(expectedElement, $('.inner', fragment).get(0))
  251. var expectedElements = fragment.querySelectorAll('div')
  252. t.assertLength(2, $('div', fragment))
  253. t.assertEqual(expectedElements[0], $('div', fragment).get(0))
  254. t.assertEqual(expectedElements[1], $('div', fragment).get(1))
  255. }
  256. var globalVarSetFromReady = ""
  257. $(document).ready(function(){ globalVarSetFromReady = 'hi!' })
  258. var globalVarSetFromReady2 = ""
  259. $(function(){ globalVarSetFromReady2 = 'hi!' })
  260. var globalVarSetFromReady3 = ""
  261. $(document).on('ready', function(){ globalVarSetFromReady3 = 'hi!' })
  262. var globalVarSetFromReady4 = ""
  263. $(document).on('foo ready bar', function(){ globalVarSetFromReady4 = 'hi!' })
  264. Evidence.Assertions.assertSame = function(expected, actual, message) {
  265. var expectedKeyCount = 0, actualKeyCount = 0, key, passed = true
  266. for (key in expected) expectedKeyCount++
  267. for (key in actual) actualKeyCount++
  268. if (expectedKeyCount == actualKeyCount)
  269. for (key in expected)
  270. passed &= expected[key] == actual[key]
  271. else
  272. passed = false
  273. this._assertExpression(passed, message || 'Failed assertion.',
  274. 'Expected %o to be the same as %o.', actual, expected)
  275. }
  276. Evidence.Assertions.assertLength = function(expected, object, message) {
  277. var actual = object.length
  278. this._assertExpression(expected === actual, message || 'Failed assertion.',
  279. 'Expected length %d, got %d.', expected, actual)
  280. }
  281. Evidence.Assertions.assertZeptoCollection = function(expectedLength, object, message) {
  282. if (!$.zepto.isZ(object))
  283. this._assertExpression(false, message || 'Failed assertion.',
  284. 'Expected %o to be a Zepto collection.', object)
  285. else
  286. this.assertLength(expectedLength, object, message)
  287. }
  288. Evidence.Assertions.assertEqualCollection = function(expectedCollection, actualCollection, message) {
  289. var expected = expectedCollection, actual = actualCollection,
  290. passed = expected.length == actual.length
  291. if (typeof expected.get == 'function') expected = expected.get()
  292. if (typeof actual.get == 'function') actual = actual.get()
  293. if (passed) for (var i=0; i<expected.length; i++) passed &= expected[i] == actual[i]
  294. this._assertExpression(passed, message || 'Failed assertion.',
  295. 'Expected %o, got %o.', expected, actual)
  296. }
  297. Evidence('ZeptoTest', {
  298. testIsFunction: function(t) {
  299. t.assertTrue($.isFunction(function(){}))
  300. t.assertTrue($.isFunction(new Function()))
  301. var f1 = function(){}
  302. function f2(){}
  303. t.assertTrue($.isFunction(f1))
  304. t.assertTrue($.isFunction(f2))
  305. t.assertFalse($.isFunction())
  306. t.assertFalse($.isFunction(undefined))
  307. t.assertFalse($.isFunction({}))
  308. t.assertFalse($.isFunction(new Object()))
  309. t.assertFalse($.isFunction(null))
  310. t.assertFalse($.isFunction([]))
  311. t.assertFalse($.isFunction(1))
  312. t.assertFalse($.isFunction('a'))
  313. t.assertFalse($.isFunction(new Date()))
  314. t.assertFalse($.isFunction(window))
  315. t.assertFalse($.isFunction($('body')))
  316. },
  317. testIsPlainObject: function(t) {
  318. t.assertTrue($.isPlainObject(new Object()), 'Object is plain object')
  319. t.assertTrue($.isPlainObject({}), '{} is plain object')
  320. t.assertTrue($.isPlainObject({one : 1}), '{one : 1} is plain object')
  321. t.assertTrue($.isPlainObject({one : 1, two: [1,2]}), '{one : 1, two: [1,2]} is plain object')
  322. t.assertFalse($.isPlainObject(new Array()), 'Array object is not plain object')
  323. t.assertFalse($.isPlainObject([]), '[] is not plain object')
  324. t.assertFalse($.isPlainObject(null), 'null is not plain object')
  325. t.assertFalse($.isPlainObject(), 'undefined is not plain object')
  326. t.assertFalse($.isPlainObject(new String()), 'empty String object is not plain object')
  327. t.assertFalse($.isPlainObject(new String('moe')), 'String object is not plain object')
  328. t.assertFalse($.isPlainObject(''), 'the empty string is not plain object')
  329. t.assertFalse($.isPlainObject('moe'), 'a string is not plain object')
  330. t.assertFalse($.isPlainObject(new RegExp('test')), 'RegExp object is not plain object')
  331. t.assertFalse($.isPlainObject(/test/), 'regex is not plain object')
  332. t.assertFalse($.isPlainObject(new Boolean(true)), 'Boolean object is not plain object')
  333. t.assertFalse($.isPlainObject(true), 'a boolean is not plain object')
  334. t.assertFalse($.isPlainObject(new Number(2)), 'Number object is not plain object')
  335. t.assertFalse($.isPlainObject(2), 'a number is not plain object')
  336. t.assertFalse($.isPlainObject(new Function()), 'Function object is not plain object')
  337. t.assertFalse($.isPlainObject(function() {}), 'a function is not plain object')
  338. t.assertFalse($.isPlainObject(new Date()), 'Date object is not plain object')
  339. t.assertFalse($.isPlainObject(window), 'window is not a plain object')
  340. t.assertFalse($.isPlainObject($("html")[0]), 'html node is not a plain object')
  341. var F = function(){}, obj
  342. F.prototype = {'a':1}
  343. obj = new F()
  344. t.assertFalse($.isPlainObject(obj), 'function with prototype is not a plain object')
  345. },
  346. testIsWindow: function(t){
  347. t.assertFalse($.isWindow())
  348. t.assertFalse($.isWindow({}))
  349. t.assertFalse($.isWindow(document.body))
  350. t.assertTrue($.isWindow(window))
  351. t.assertTrue($.isWindow($('iframe').get(0).contentWindow))
  352. },
  353. testDollarIsNumeric: function(t){
  354. t.assertTrue($.isNumeric( '-10' ))
  355. t.assertTrue($.isNumeric( '0' ))
  356. t.assertTrue($.isNumeric( 0xFF ))
  357. t.assertTrue($.isNumeric( '0xFF' ))
  358. t.assertTrue($.isNumeric( '8e5' ))
  359. t.assertTrue($.isNumeric( '3.1415' ))
  360. t.assertTrue($.isNumeric( +10 ))
  361. t.assertTrue($.isNumeric( 0144 ))
  362. t.assertFalse($.isNumeric( '-0x42' ), '-0x42')
  363. t.assertFalse($.isNumeric( '7.2acdgs' ), '7.2acdgs')
  364. t.assertFalse($.isNumeric( '' ), 'empty string')
  365. t.assertFalse($.isNumeric( {} ), 'empty hash')
  366. t.assertFalse($.isNumeric( NaN ), 'NaN')
  367. t.assertFalse($.isNumeric( null ), 'null')
  368. t.assertFalse($.isNumeric( true ), 'true')
  369. t.assertFalse($.isNumeric( Infinity ), 'Infinity')
  370. t.assertFalse($.isNumeric( undefined ), 'undefined')
  371. },
  372. // test to see if we augment iOS 3.2 with String#trim()
  373. testTrim: function(t){
  374. t.assertEqual("blah", " blah ".trim())
  375. t.assertIdentical("", $.trim(undefined))
  376. t.assertIdentical("", $.trim(null))
  377. t.assertIdentical("", $.trim(""))
  378. t.assertIdentical("0", $.trim(0))
  379. },
  380. testCamelCase: function(t){
  381. t.assertEqual("hello", $.camelCase("hello"))
  382. t.assertEqual("HELLO", $.camelCase("HELLO"))
  383. t.assertEqual("helloNiceWorld", $.camelCase("hello-nice-world"))
  384. t.assertEqual("helloWorld", $.camelCase("helloWorld"))
  385. },
  386. testExtend: function(t){
  387. t.assertSame({}, $.extend({}))
  388. t.assertSame(
  389. {a: "b", c: "d", e: "f"},
  390. $.extend({a: "1", e: "f"}, {a: "b", c: "d"})
  391. )
  392. var obj = {}
  393. t.assertIdentical(obj, $.extend(obj, {a: 1}))
  394. t.assertEqual(1, obj.a)
  395. obj = {}
  396. t.assertIdentical(obj, $.extend(obj, {a: 1}, {b: 2}))
  397. t.assertEqual(2, obj.b)
  398. // undefined values are not copied over
  399. t.assertSame({a:1}, $.extend({a:1}, {b:undefined}))
  400. // shallow by default
  401. obj = $.extend({ a:{b:"c"} }, { a:{d:"e"} })
  402. t.assertSame({d:"e"}, obj.a)
  403. },
  404. testExtendDeep: function(t){
  405. var obj = { a:{b:"c", x:{y:"z"}} }
  406. $.extend(true, obj, { a:{d:"e"} }, { a:{b:"B", f:"g", x:{q:"x"}} })
  407. t.assertEqual('a', Object.keys(obj).join(','))
  408. t.assertEqual('b,d,f,x', Object.keys(obj.a).sort().join(','))
  409. t.assertEqual('B', obj.a.b)
  410. t.assertEqual('e', obj.a.d)
  411. t.assertEqual('g', obj.a.f)
  412. t.assertEqual('z', obj.a.x.y)
  413. t.assertEqual('x', obj.a.x.q)
  414. // creates non-existing keys on target object
  415. obj = {}
  416. $.extend(true, obj, { a:{b:"c"} })
  417. t.assertEqual('a', Object.keys(obj).join(','))
  418. t.assertEqual('c', obj.a.b)
  419. // skips iterating over DOM elements
  420. obj = {}
  421. var dom = $('#some_element').get(0)
  422. $.extend(true, obj, { element: dom })
  423. t.assertIdentical(dom, obj.element)
  424. // can override DOM element
  425. $.extend(true, obj, { element:{a:'b'} })
  426. t.assertEqual('b', obj.element.a)
  427. // deep copy with array
  428. obj = {}
  429. var initial = { array: [1,2,3,4], object:{a:{b:["c","d"]}} }
  430. $.extend(true, obj, initial)
  431. t.assertTrue($.isArray(obj.array))
  432. t.assertEqual(JSON.stringify(obj), JSON.stringify(initial))
  433. t.refuteIdentical(obj, initial)
  434. t.refuteIdentical(obj.array, initial.array)
  435. t.refuteIdentical(obj.object, initial.object)
  436. t.refuteIdentical(obj.object.a, initial.object.a)
  437. t.refuteIdentical(obj.object.a.b, initial.object.a.b)
  438. },
  439. testExtensionAPI: function(t) {
  440. t.assert('init' in $.zepto)
  441. t.assert('fragment' in $.zepto)
  442. t.assert('Z' in $.zepto)
  443. t.assert('isZ' in $.zepto)
  444. // redefine Z and log some debug information
  445. var oldZ = $.zepto.Z, calls = []
  446. $.zepto.Z = function Z(dom, selector) {
  447. var value = oldZ(dom, selector)
  448. calls.push(dom)
  449. return value
  450. }
  451. // now select some stuff
  452. var Z1 = $(''), Z2 = $('#find1 .findme')
  453. // check if $.fn methods are still there
  454. t.assert('pluck' in Z1)
  455. t.assert('width' in Z2)
  456. // two calls should be logged
  457. t.assertLength(2, calls)
  458. // restore old Z
  459. $.zepto.Z = oldZ
  460. var Z3 = $('')
  461. t.assertLength(2, calls)
  462. t.assertFalse($.zepto.isZ())
  463. t.assertFalse($.zepto.isZ([]))
  464. t.assertTrue($.zepto.isZ($('body')))
  465. },
  466. testDollar: function(t){
  467. var expectedElement = document.getElementById('some_element')
  468. t.assertLength(1, $('#some_element'))
  469. t.assertEqual(expectedElement, $('#some_element').get(0))
  470. t.assertEqual(expectedElement, $(expectedElement).get(0))
  471. t.assertLength(4, $('p'))
  472. t.assertLength(1, $('p > span.yay'))
  473. },
  474. testDollarUnique: function(t){
  475. t.refuteIdentical($('#some_element'), $('#some_element'))
  476. t.refuteIdentical($('#nonexistent'), $('#nonexistent'))
  477. },
  478. testDollarWithNil: function(t){
  479. t.assertZeptoCollection(0, $(null))
  480. t.assertZeptoCollection(0, $(undefined))
  481. t.assertZeptoCollection(0, $(false))
  482. t.assertZeptoCollection(0, $(''))
  483. t.assertZeptoCollection(0, $('#'))
  484. var Z1 = $(null), Z2 = $(null)
  485. t.assert(Z1 !== Z2)
  486. },
  487. testDollarWithNonDOM: function(t){
  488. var zepto = $(['a', 'b', 'c'])
  489. t.assertZeptoCollection(3, zepto)
  490. t.assertEqualCollection(['a', 'b', 'c'], zepto)
  491. t.assert($({}))
  492. t.assertTrue($({ a: true })[0].a)
  493. // Plain objects wrapped by a Zepto collection
  494. // should still refer to the original object
  495. // This is required for events on plain objects
  496. var plainObject = { a: 1 }
  497. $(plainObject).get(0).a = 2
  498. t.assertEqual(2, plainObject.a)
  499. t.assertEqual(2, $(plainObject).get(0).a)
  500. },
  501. testGetWithoutIndex: function(t){
  502. var zepto = $('#find1 .findme')
  503. var array = zepto.get()
  504. t.assertFalse(zepto === array)
  505. t.assertTrue($.isArray(array))
  506. t.assertTrue(array.pop === ([]).pop)
  507. },
  508. testGetWithIndex: function(t){
  509. var zepto = $('#find1 .findme')
  510. t.assertEqual(zepto[0], zepto.get(0))
  511. t.assertEqual(zepto[zepto.length - 1], zepto.get(-1))
  512. t.assertUndefined(zepto.get(zepto.length))
  513. },
  514. testSize: function(t){
  515. t.assertEqual(4, $('#find1 .findme').size())
  516. },
  517. testDollarWithMultipleInstances: function(t){
  518. var instance1 = $('#some_element'),
  519. instance2 = $('p')
  520. t.assertLength(1, instance1)
  521. t.assertLength(4, instance2)
  522. t.refuteIdentical(instance1.get(0), instance2.get(0))
  523. },
  524. testDollarWithArrays: function(t){
  525. var element = document.getElementById('some_element')
  526. var z1 = $([element])
  527. t.assertLength(1, z1)
  528. t.assertEqual(element, z1.get(0))
  529. var z2 = $([element, null, undefined])
  530. t.assertLength(1, z2)
  531. t.assertEqual(element, z2.get(0))
  532. var z3 = $([null, element, null])
  533. t.assertLength(1, z3)
  534. t.assertEqual(element, z3.get(0))
  535. },
  536. testDollarWithContext: function(t){
  537. // Zepto object
  538. var zepto = $('p#find1, #find2')
  539. t.assertLength(11, $('span', zepto))
  540. // DOM Element
  541. var domElement = document.getElementById('find1')
  542. t.assertLength(4, $('span.findme', domElement))
  543. // Selector with DOM Element Context
  544. var domElement = document.getElementById('find1')
  545. t.assertLength(4, $('span.findme', domElement))
  546. // DOM Element with DOM Element Context
  547. t.assertLength(1, $(domElement, domElement))
  548. },
  549. testDollarWithDocument: function(t){
  550. var z = $(document)
  551. t.assertLength(1, z)
  552. t.assertEqual('', z.selector)
  553. },
  554. testDollarWithAppcache: function(t){
  555. if ('applicationCache' in window) {
  556. var z = $(window.applicationCache)
  557. t.assertLength(1, z)
  558. t.assertIdentical(window.applicationCache, z.get(0))
  559. t.assertEqual('', z.selector)
  560. }
  561. },
  562. testDollarWithDocumentFragment: function(t){
  563. var documentFragment = $(document.createDocumentFragment())
  564. t.assertLength(1, documentFragment)
  565. t.assertEqual(Node.DOCUMENT_FRAGMENT_NODE, documentFragment.get(0).nodeType)
  566. },
  567. testDollarWithElementInIframe: function(t){
  568. var iframe = $('#fixtures iframe').get(0),
  569. iframeWin = iframe.contentWindow,
  570. iframeDoc = iframe.contentDocument,
  571. iframeBody = iframeDoc.body,
  572. iframeEl = $(iframeBody).find('b')
  573. t.assertIdentical(iframeWin, $(iframeWin).get(0))
  574. t.assertIdentical(iframeDoc, $(iframeDoc).get(0))
  575. t.assertIdentical(iframeBody, $(iframeBody).get(0))
  576. t.assertEqual('B', iframeEl.pluck('tagName').join(','))
  577. t.assertEqual('Hello from iframe!', iframeEl.text())
  578. },
  579. testDollarWithFragment: function(t){
  580. var fragment = $("<div>")
  581. t.assertLength(1, fragment)
  582. t.assertEqual("<div></div>", outerHTML(fragment.get(0)))
  583. t.assertEqual('', fragment.selector)
  584. t.assertNull(fragment.get(0).parentNode)
  585. fragment = $("<div>hello world</div>")
  586. t.assertLength(1, fragment)
  587. t.assertEqual("<div>hello world</div>", outerHTML(fragment.get(0)))
  588. t.assertEqual('', fragment.selector)
  589. fragment = $("<div>hello</div> <span>world</span>")
  590. t.assertLength(3, fragment)
  591. t.assertEqual("<div>hello</div>", outerHTML(fragment.get(0)))
  592. t.assertEqual(Node.TEXT_NODE, fragment.get(1).nodeType)
  593. t.assertEqual("<span>world</span>", outerHTML(fragment.get(2)))
  594. t.assertEqual('', fragment.selector)
  595. fragment = $("<div>\nhello</div> \n<span>world</span>")
  596. t.assertLength(3, fragment)
  597. t.assertEqual("<div>\nhello</div>", outerHTML(fragment.get(0)))
  598. t.assertEqual(Node.TEXT_NODE, fragment.get(1).nodeType)
  599. t.assertEqual("<span>world</span>", outerHTML(fragment.get(2)))
  600. t.assertEqual('', fragment.selector)
  601. fragment = $("<div /><div />")
  602. t.assertLength(2, fragment)
  603. fragment = $("<div>hello</div> ")
  604. t.assertLength(1, fragment)
  605. },
  606. testDollarFragmentAndProperties: function(t){
  607. var el = $('<p id=hi />', {
  608. id: 'hello', 'class': 'one two',
  609. text: 'world', css: {color: 'red'}
  610. })
  611. t.assertEqual('hello', el.attr('id'))
  612. t.assert(el.hasClass('one'))
  613. t.assert(el.hasClass('two'))
  614. t.assertEqual('world', el.text())
  615. t.assertEqual('red', el.css('color'))
  616. },
  617. testDollarNonemptyFragmentAndProperties: function(t){
  618. var zepto = $('<a>Goodbye</a>', { text: "Hello", href: "http://zeptojs.com" })
  619. t.assertLength(1, zepto)
  620. t.assertEqual('Hello', zepto.text())
  621. t.assertEqual('http://zeptojs.com', zepto.attr("href"))
  622. },
  623. testDollarWithTextNode: function(t){
  624. var textNode = $(document.createTextNode('hi there'))
  625. t.assertLength(1, textNode)
  626. t.assertEqual(Node.TEXT_NODE, textNode.get(0).nodeType)
  627. },
  628. testDollarWithCommentInFragment: function(t){
  629. var comment = $('<!-- -->')
  630. t.assertLength(1, comment)
  631. t.assertEqual(Node.COMMENT_NODE, comment.get(0).nodeType)
  632. },
  633. testDollarWithDoctypeInFragment: function(t){
  634. t.assertZeptoCollection(0, $('<!DOCTYPE html>'))
  635. },
  636. testNodeCreationViaDollar: function (t) {
  637. t.assertEqual('<div></div>', outerHTML($('<div></div>').get(0)))
  638. t.assertEqual('<div></div>', outerHTML($('<div/>').get(0)))
  639. t.assertEqual('<div><div></div></div>', outerHTML($('<div><div></div></div>').get(0)))
  640. t.assertEqual('<div><div></div></div>', outerHTML($('<div><div/></div>').get(0)))
  641. t.assertEqual('<div><div></div><div></div></div>', outerHTML($('<div><div></div><div></div></div>').get(0)))
  642. },
  643. testCreateTableCell: function(t) {
  644. t.assertEqual('TD', $('<td></td>').pluck('nodeName').join(','))
  645. },
  646. testCreateTableHeaderCell: function(t) {
  647. t.assertEqual('TH', $('<th></th>').pluck('nodeName').join(','))
  648. },
  649. testCreateTableRow: function(t) {
  650. t.assertEqual('TR', $('<tr></tr>').pluck('nodeName').join(','))
  651. },
  652. testCreateTableHeader: function(t) {
  653. t.assertEqual('THEAD', $('<thead></thead>').pluck('nodeName').join(','))
  654. },
  655. testCreateTableBody: function(t) {
  656. t.assertEqual('TBODY', $('<tbody></tbody>').pluck('nodeName').join(','))
  657. },
  658. testCreateTableFooter: function(t) {
  659. t.assertEqual('TFOOT', $('<tfoot></tfoot>').pluck('nodeName').join(','))
  660. },
  661. testCreateSelectOptgroup: function(t) {
  662. t.assertEqual('OPTGROUP', $('<optgroup></optgroup>').pluck('nodeName').join(','))
  663. },
  664. testCreateSelectOption: function(t) {
  665. t.assertEqual('OPTION', $('<option></option>').pluck('nodeName').join(','))
  666. },
  667. testReady: function(t){
  668. t.assertEqual('hi!', globalVarSetFromReady)
  669. t.assertEqual('hi!', globalVarSetFromReady2)
  670. t.assertEqual('hi!', globalVarSetFromReady3)
  671. t.assertEqual('hi!', globalVarSetFromReady4)
  672. },
  673. testNext: function(t){
  674. t.assertEqual('P', $('#some_element').next().get(0).tagName)
  675. t.assertEqual('DIV', $('p').next().get(0).tagName)
  676. t.assertEqual(0, $('span.yay').next('.nay').size())
  677. t.assertEqual(1, $('span.yay').next().size())
  678. t.assertEqual(1, $('span.yay').next().next('.nay').size())
  679. },
  680. testPrev: function(t){
  681. t.assertEqual('H1', $('p').prev().get(0).tagName)
  682. t.assertEqual('DIV', $('ul').prev().get(0).tagName)
  683. t.assertEqual(0, $('span.nay').prev('.yay').size())
  684. t.assertEqual(1, $('span.nay').prev().size())
  685. t.assertEqual(1, $('span.nay').prev().prev('.yay').size())
  686. },
  687. testEach: function(t){
  688. var index, tagnames = []
  689. $('#eachtest > *').each(function(idx, el){
  690. index = idx
  691. t.assertIdentical(el, this)
  692. tagnames.push(el.tagName.toUpperCase())
  693. })
  694. t.assertEqual('SPAN, B, BR', tagnames.join(', '))
  695. t.assertEqual(2, index)
  696. },
  697. testEachBreak: function(t){
  698. var index, tagnames = []
  699. $('#eachtest > *').each(function(idx, el){
  700. index = idx
  701. t.assertIdentical(el, this)
  702. tagnames.push(el.tagName.toUpperCase())
  703. if (idx == 1) return false
  704. })
  705. t.assertEqual('SPAN, B', tagnames.join(', '))
  706. t.assertEqual(1, index)
  707. },
  708. testNoop: function(t){
  709. t.assertUndefined($.noop())
  710. },
  711. testConcat: function(t){
  712. var $collection1 = $('#eachtest > *')
  713. var $collection2 = $('#find1 .findme')
  714. var $collection3 = $('#some_element, #another_element')
  715. var result, array = $collection1.toArray().concat($collection2.toArray(), $collection3.toArray())
  716. result = $collection1.concat($collection2, $collection3.toArray())
  717. t.assertEqualCollection(array, result)
  718. result = $.fn.concat.apply($collection1.toArray(), [$collection2, $collection3.toArray()])
  719. t.assertEqualCollection(array, result)
  720. },
  721. testMap: function(t){
  722. var results = $('#eachtest > *').map(function(idx, el) {
  723. t.assertIdentical(el, this)
  724. return idx + ':' + this.nodeName.toUpperCase()
  725. })
  726. t.assertEqual(3, results.size())
  727. t.assertEqual('0:SPAN, 1:B, 2:BR', results.get().join(', '))
  728. },
  729. testDollarMap: function(t){
  730. var fruits = ['apples', 'oranges', 'pineapple', 'peach', ['grape', 'melon']]
  731. var results = $.map(fruits, function(item, i) {
  732. if (item instanceof Array) return item
  733. else if (!/apple/.test(item)) return i + ':' + item
  734. })
  735. t.assertEqual('1:oranges,3:peach,grape,melon', results.join(','))
  736. },
  737. testDollarMapObject: function(t){
  738. var fruit = { name: 'banana', taste: 'sweet' }
  739. var results = $.map(fruit, function(value, key) {
  740. return key + '=' + value
  741. })
  742. t.assertEqual('name=banana,taste=sweet', results.sort().join(','))
  743. },
  744. testDollarMapArrayLike: function(t){
  745. var results
  746. results = $.map({ length: 1, a: 'b' }, function(value, key) {
  747. return key + '=' + value
  748. })
  749. t.assertEqual('a=b,length=1', results.sort().join(','))
  750. results = $.map({ length: 0, a: 'b' }, function(value, key) {
  751. return key + '=' + value
  752. })
  753. t.assertEqual(0, results.length)
  754. results = $.map(function(){}, function(value, key) {
  755. return key + '=' + value
  756. })
  757. t.assertEqual(0, results.length)
  758. },
  759. testDollarGrep: function(t){
  760. var fruits = ['apples', 'oranges', 'pineapple', 'peach']
  761. var result = $.grep(fruits, function(name){ return /apple/.test(name) })
  762. t.assertEqualCollection(['apples', 'pineapple'], result)
  763. },
  764. testDollarEach: function(t){
  765. var array = ['a','b','c'], object = { a: 1, b: 2, c: 3 }, result
  766. result = []
  767. $.each(array, function(idx, val){
  768. result.push(idx)
  769. result.push(val)
  770. })
  771. t.assertEqual('0a1b2c', result.join(''))
  772. result = []
  773. $.each(object, function(key, val){
  774. result.push(key)
  775. result.push(val)
  776. })
  777. t.assertEqual('a1b2c3', result.join(''))
  778. result = []
  779. $.each(array, function(idx, val){
  780. result.push(idx)
  781. result.push(val)
  782. return idx<1
  783. })
  784. t.assertEqual('0a1b', result.join(''))
  785. t.assertEqual('abc', $.each(array, function(){}).join(''))
  786. },
  787. testDollarEachContext: function(t){
  788. $.each(['a'], function(key, val) {
  789. t.assertEqual(this, val)
  790. })
  791. $.each({a:'b'}, function(key, val) {
  792. t.assertEqual(this, val)
  793. })
  794. },
  795. testDollarInArray: function(t) {
  796. t.assertIdentical( 0, $.inArray(1, [1,2,3]) )
  797. t.assertIdentical( 1, $.inArray(2, [1,2,3]) )
  798. t.assertIdentical( -1, $.inArray(4, [1,2,3]) )
  799. t.assertIdentical( 3, $.inArray(1, [1,2,3,1], 1) )
  800. },
  801. testDollarParseJSON: function(t) {
  802. t.assertSame({a:'b'}, $.parseJSON('{"a":"b"}'))
  803. },
  804. testDollarWithDocumentFragmentContext: function(t){
  805. testDocumentFragmentOrShadowRootContext($(document.createDocumentFragment()), t)
  806. },
  807. testDollarWithShadowRootContext: function(t){
  808. if ('createShadowRoot' in document.body) {
  809. var $subject = $('#test_dollar_with_shadow_root')
  810. t.assertLength(1, $subject)
  811. testDocumentFragmentOrShadowRootContext($($subject[0].createShadowRoot()), t)
  812. }
  813. },
  814. testEq: function(t){
  815. var $els = $('#eq_test div')
  816. t.assertZeptoCollection(1, $els.eq(0))
  817. t.assertZeptoCollection(1, $els.eq(-1))
  818. t.assertEqual($els.eq(-1)[0].className, 'eq2')
  819. t.assertUndefined($els.eq(-1).tagName)
  820. t.assertZeptoCollection(0, $('nonexistent').eq(0))
  821. },
  822. testFirst: function(t){
  823. var zepto = $('h1,p')
  824. t.assertLength(5, zepto)
  825. var zepto2 = zepto.first()
  826. t.refuteIdentical(zepto, zepto2)
  827. t.assertLength(5, zepto)
  828. t.assertLength(1, zepto2)
  829. t.assertEqual('H1', zepto2.get(0).tagName)
  830. t.assertLength(0, $('nonexistent').first())
  831. },
  832. testFirstNonDOM: function(t){
  833. t.assertEqual('a', $(['a', 'b', 'c']).first())
  834. },
  835. testLast: function(t){
  836. var zepto = $('h1,p')
  837. t.assertLength(5, zepto)
  838. var zepto2 = zepto.last()
  839. t.refuteIdentical(zepto, zepto2)
  840. t.assertLength(5, zepto)
  841. t.assertLength(1, zepto2)
  842. t.assertEqual('P', zepto2.get(0).tagName)
  843. t.assertLength(0, $('nonexistent').last())
  844. },
  845. testLastNonDOM: function(t){
  846. t.assertEqual('c', $(['a', 'b', 'c']).last())
  847. },
  848. testPluck: function(t){
  849. t.assertEqual('H1DIVDIV', $('h1,div.htmltest').pluck('tagName').join(''))
  850. },
  851. testShow: function(t){
  852. $('#show_hide_div1').show()
  853. t.assertEqual('inline-block', getComputedStyle($('#show_hide_div1').get(0)).display)
  854. $('#show_hide_div2').show()
  855. t.assertEqual('block', getComputedStyle($('#show_hide_div2').get(0)).display)
  856. $('#show_hide_div3').show()
  857. t.assertEqual('block', getComputedStyle($('#show_hide_div3').get(0)).display)
  858. $('#show_hide_span1').show()
  859. t.assertEqual('block', getComputedStyle($('#show_hide_span1').get(0)).display)
  860. $('#show_hide_span2').show()
  861. t.assertEqual('block', getComputedStyle($('#show_hide_span2').get(0)).display)
  862. $('#show_hide_span3').show()
  863. t.assertEqual('inline', getComputedStyle($('#show_hide_span3').get(0)).display)
  864. },
  865. testHide: function(t){
  866. $('#show_hide_div1').hide()
  867. t.assertEqual('none', $('#show_hide_div1').get(0).style.display)
  868. $('#show_hide_div2').hide()
  869. t.assertEqual('none', $('#show_hide_div2').get(0).style.display)
  870. $('#show_hide_div3').hide()
  871. t.assertEqual('none', $('#show_hide_div3').get(0).style.display)
  872. $('#show_hide_span1').hide()
  873. t.assertEqual('none', $('#show_hide_span1').get(0).style.display)
  874. $('#show_hide_span2').hide()
  875. t.assertEqual('none', $('#show_hide_span2').get(0).style.display)
  876. $('#show_hide_span3').hide()
  877. t.assertEqual('none', $('#show_hide_span3').get(0).style.display)
  878. },
  879. testToggle: function(t){
  880. var el = $('#show_hide_div1').hide(),
  881. domStyle = el.get(0).style
  882. t.assertEqual('none', domStyle.display)
  883. var result = el.toggle()
  884. t.assertIdentical(el, result, 'expected toggle() to return self')
  885. t.assertIdentical('', domStyle.display)
  886. el.toggle()
  887. t.assertEqual('none', domStyle.display)
  888. el.toggle(true)
  889. t.assertIdentical('', domStyle.display)
  890. el.toggle(true)
  891. t.assertIdentical('', domStyle.display)
  892. el.toggle(false)
  893. t.assertEqual('none', domStyle.display)
  894. el.toggle(false)
  895. t.assertEqual('none', domStyle.display)
  896. },
  897. testToggleMultiple: function(t){
  898. var el1 = $('#show_hide_div1').hide(),
  899. el2 = $('#show_hide_div2').show(),
  900. both = $('#show_hide_div1, #show_hide_div2')
  901. both.toggle()
  902. t.assertIdentical('', el1.get(0).style.display)
  903. t.assertEqual('none', el2.get(0).style.display)
  904. both.toggle()
  905. t.assertEqual('none', el1.get(0).style.display)
  906. t.assertEqual('block', el2.get(0).style.display)
  907. },
  908. testOffset: function(t){
  909. // TODO
  910. t.assertNull($('#doesnotexist').offset())
  911. var el = $('#some_element')
  912. t.assertIdentical(el, el.offset({}))
  913. t.assertSame($('<div>').offset(), {left: 0, top: 0})
  914. },
  915. testOffsetOnHTML: function(t){
  916. var offset = $('html').offset()
  917. t.assertEqual(offset.left, 0)
  918. t.assertEqual(offset.top, 0)
  919. t.assert('width' in offset)
  920. t.assert('height' in offset)
  921. },
  922. testWidth: function(t){
  923. t.assertNull($('#doesnotexist').width())
  924. // can't check values here, but make sure it doesn't error out
  925. var viewportWidth = $(window).width()
  926. t.assert(viewportWidth > 0 || viewportWidth === 0)
  927. t.assert($(document).width())
  928. t.assertIdentical(100, $('#offset').width())
  929. $('#offset').width('90px')
  930. t.assertIdentical(90, $('#offset').width())
  931. $('#offset').width(110)
  932. t.assertIdentical(110, $('#offset').width())
  933. $('#offset').width(function(i, oldWidth) { return oldWidth + 5 })
  934. t.assertIdentical(115, $('#offset').width())
  935. },
  936. testHeight: function(t){
  937. t.assertNull($('#doesnotexist').height())
  938. // can't check values here, but make sure it doesn't error out
  939. var viewportHeight = $(window).height()
  940. t.assert(viewportHeight > 0 || viewportHeight === 0)
  941. t.assert($(document).height())
  942. // with a tall element on the page,
  943. // the window (viewport) should be shorter than the total
  944. // document height
  945. $('<div style="height:9999px" id="very_high"></div>').appendTo('body')
  946. t.assert(
  947. $(window).height() < $(document).height(),
  948. "'window' height was not smaller than 'document' height?")
  949. $('#very_high').remove()
  950. t.assertIdentical(50, $('#offset').height())
  951. $('#offset').height('60px')
  952. t.assertIdentical(60, $('#offset').height())
  953. $('#offset').height(70)
  954. t.assertIdentical(70, $('#offset').height())
  955. $('#offset').height(function(i, oldHeight) { return oldHeight + 5 })
  956. t.assertIdentical(75, $('#offset').height())
  957. },
  958. testClosest: function(t){
  959. var el = $('#li2')
  960. t.assertEqualCollection(el, el.closest('li'))
  961. t.assertEqualCollection($('#nested'), el.closest('ul'))
  962. // with context
  963. t.assertEqualCollection($('#nested'), el.closest('ul', $('#li1').get(0)))
  964. t.assertLength(0, el.closest('#parents', $('#li1').get(0)))
  965. // no ancestor matched
  966. t.assertLength(0, el.closest('form'))
  967. },
  968. testClosestMultiple: function(t){
  969. var els = $('#parents ul li')
  970. var uls = els.closest('ul')
  971. t.assertEqualCollection($('#parents ul'), uls)
  972. t.assertEqualCollection($('#parents'), uls.closest('#parents'))
  973. },
  974. testClosestWithCollection: function(t){
  975. var targets = $('#parents > li')
  976. var result = $('#li2').closest(targets)
  977. t.assertLength(1, result)
  978. t.assertEqual('li1', result.get(0).id)
  979. t.assertLength(0, $('#li1').closest('#li2'))
  980. },
  981. testClosestWithElement: function(t){
  982. var target = $('#li1').get(0)
  983. var result = $('#li2').closest(target)
  984. t.assertLength(1, result)
  985. t.assertIdentical(target, result.get(0))
  986. t.assertLength(0, $('#li1').closest($('#li2').get(0)))
  987. },
  988. testClosestOnDetached: function(t){
  989. var el = $('<div><p><a></a></p></div>'),
  990. para = el.children(),
  991. link = para.children()
  992. t.assertEqualCollection(para, link.closest('p'))
  993. t.assertEqualCollection(el, link.closest('div'))
  994. t.assertEqualCollection(el, el.closest('div'))
  995. },
  996. testContains: function(t){
  997. var el1 = $('#li1'), el2 = $('#li2')
  998. t.assertTrue($.contains(el1.get(0), el2.get(0)))
  999. t.assertFalse($.contains(el1.get(0), $('#parents').get(0)))
  1000. },
  1001. testContainsOnDetached: function(t){
  1002. var el = $('<div><p><a></a></p></div>'),
  1003. para = el.children(),
  1004. link = para.children()
  1005. t.assertTrue($.contains(para.get(0), link.get(0)))
  1006. t.assertFalse($.contains(document.body, el.get(0)))
  1007. },
  1008. testParents: function(t){
  1009. var body = document.body, html = body.parentNode, container = $('#parents'),
  1010. wrapper = $('#fixtures').get(0)
  1011. t.assertEqualCollection($([wrapper, body, html]), container.parents())
  1012. var expected = $('#li1 > ul').get()
  1013. expected.push($('#li1').get(0))
  1014. expected.push(container.get(0))
  1015. expected = expected.concat([wrapper, body, html])
  1016. t.assertEqualCollection($(expected), $('#li1').find('li').parents())
  1017. expected = [$('#nested').get(0), $('#parents').get(0)]
  1018. t.assertEqualCollection($(expected), $('#li2').parents('ul'))
  1019. },
  1020. testParentsIframe: function(t){
  1021. var iframeBody = $('iframe').get(0).contentDocument.body
  1022. t.assertEqualCollection(
  1023. [iframeBody, iframeBody.parentNode],
  1024. $(iframeBody).find('b').first().parents()
  1025. )
  1026. },
  1027. testParent: function(t){
  1028. var el = $('#li1')
  1029. t.assertEqualCollection($('#parents'), el.parent())
  1030. t.assertEqualCollection($('#li1 > ul'), el.find('li').parent())
  1031. t.assertLength(0, $(document.createElement('div')).parent())
  1032. },
  1033. testParentOnDetached: function(t){
  1034. t.assertLength(0, $('<ul />').parent())
  1035. },
  1036. testChildren: function(t){
  1037. var el=$("#childrenTest"), lis=$("li.child",el)
  1038. //basic form
  1039. t.assertEqualCollection(lis, el.children())
  1040. //filtered by selector
  1041. t.assertEqualCollection(lis.filter(".two"), el.children(".two"))
  1042. //children == null
  1043. t.assertLength(4,lis.children(null))
  1044. //across multiple parents
  1045. t.assertEqualCollection(el.find("li a"), lis.children("a"))
  1046. //chainabilty
  1047. t.assertEqual(el.find("li a.childOfTwo").text(), lis.children(".childOfTwo").text())
  1048. //non-existent children
  1049. t.assertLength(0,lis.children(".childOfTwo").children())
  1050. },
  1051. testContents: function(t){
  1052. var $contents = $("#contentsTest").contents()
  1053. t.assertLength(3, $contents)
  1054. t.assertLength(2, $contents.filter('span'))
  1055. t.assertLength(0, $("#contentsEmptyTest").contents())
  1056. },
  1057. testIframeContents: function(t){
  1058. var el = $("#contentsIframeTest")
  1059. t.assertEqual("Hello from iframe!", el.contents().find("b").first().text())
  1060. },
  1061. testSiblings: function(t){
  1062. var el=$("#siblingsTest")
  1063. //basic form
  1064. t.assertEqualCollection($("li.one,li.three,li.four",el), $("li.two",el).siblings())
  1065. //filtered by selector
  1066. t.assertEqualCollection($("li.three",el), $("li.two",el).siblings(".three"))
  1067. //across multiple parents
  1068. t.assertEqualCollection(el.find("li b"), $("li em",el).siblings("b"))
  1069. t.assertLength(6,$("li span",el).siblings())
  1070. //non-existent siblings
  1071. t.assertLength(0,$("li span.e",el).siblings())
  1072. },
  1073. testNot: function(t){
  1074. var el=$("#notTest")
  1075. //selector form
  1076. t.assertEqualCollection($("li.one,li.three,li.four",el), $("li",el).not(".two"))
  1077. //element or NodeList form
  1078. t.assertEqualCollection($("span.b,span.c,span.e",el), $("span",el).not(document.getElementById("notTestExclude")))
  1079. t.assertEqualCollection($("li",el), $("li, span",el).not(document.getElementsByTagName("span")))
  1080. //function form
  1081. t.assertEqualCollection($("span.b,span.c",el),$("span",el).not(function(i){
  1082. var $this=$(this)
  1083. $this.html(i)
  1084. return ($this.hasClass("d") || $this.hasClass("e")) ? true : false
  1085. }))
  1086. //test the index was passed in properly in previous test
  1087. t.assertEqual("0",$("span.b",el).text())
  1088. t.assertEqual("1",$("span.c",el).text())
  1089. },
  1090. testReplaceWith: function(t) {
  1091. $('div.first').replaceWith('<h2 id="replace_test">New heading</h2>')
  1092. t.assertUndefined($('div.first').get(0))
  1093. t.assert(document.getElementById("replace_test").nodeType)
  1094. t.assertEqual($('.replacewith h2#replace_test').get(0), document.getElementById("replace_test"))
  1095. $('#replace_test').replaceWith($('.replace_test_div'))
  1096. t.assertUndefined($('#replace_test').get(0))
  1097. t.assert(document.getElementsByClassName("replace_test_div")[0].nodeType)
  1098. t.assertEqual($('.replacewith h2#replace_test').get(0), document.getElementsByClassName("replace_test")[0])
  1099. //Multiple elements
  1100. $('.replacewith .replace_test_div').replaceWith('<div class="inner first">hi</div><div class="inner fourth">hello</div>')
  1101. t.assertLength(4,$('.replacewith div'))
  1102. t.assertEqual("inner first", $('.replacewith div')[0].className)
  1103. t.assertEqual("inner fourth", $('.replacewith div')[1].className)
  1104. },
  1105. testReplaceWithFragment: function(t) {
  1106. var orphanDiv = $("<div />")
  1107. orphanDiv.replaceWith($("<div class='different' />"))
  1108. t.assert(!orphanDiv.hasClass('different'))
  1109. },
  1110. testWrap: function(t) {
  1111. var el = $('#wrap_test')
  1112. el.find('span').wrap('<p><i/></p>')
  1113. t.assertEqual(
  1114. '<p><i><span>hi</span></i></p><a></a><p><i><span>hello</span></i></p>',
  1115. el.html()
  1116. )
  1117. // avoids unnecessary cloning of dom structure for wrapping
  1118. el = $('<div><a/></div>')
  1119. var structure = $('<span/>')
  1120. el.find('a').wrap(structure)
  1121. t.assertIdentical(structure.get(0), el.find('span').get(0))
  1122. t.assert(el.find('a').parent().is('span'))
  1123. },
  1124. testWrapFunction: function(t) {
  1125. var el = $('<div><b>A</b><b>B</b></div>')
  1126. el.find('b').wrap(function(index){
  1127. return '<a class=link' + index + $(this).text() + ' />'
  1128. })
  1129. t.assertEqual(
  1130. '<a class="link0A"><b>A</b></a><a class="link1B"><b>B</b></a>',
  1131. el.html()
  1132. )
  1133. },
  1134. testWrapAll: function(t) {
  1135. var el = $('#wrapall_test')
  1136. el.find('span').wrapAll('<p><a/></p>')
  1137. t.assertEqual(
  1138. '<b></b><p><a><span>hi</span><span>hello</span></a></p><i></i>',
  1139. el.html()
  1140. )
  1141. },
  1142. testWrapFragment: function(t) {
  1143. var fragment = $('<div id="fragment" />')
  1144. fragment.wrapAll('<div id="wrap_test" />')
  1145. t.assertEqual('wrap_test', fragment.parent().attr('id'))
  1146. t.assertEqual(0, fragment.children().length)
  1147. fragment = $('<div id="fragment" />')
  1148. fragment.wrap('<div id="wrap_test" />')
  1149. t.assertEqual('wrap_test', fragment.parent().attr('id'))
  1150. t.assertEqual(0, fragment.children().length)
  1151. },
  1152. testWrapInner: function(t) {
  1153. var $el = $('#wrapinner_test')
  1154. $el.wrapInner('<div>')
  1155. t.assertLength(1, $el.children())
  1156. t.assertLength(1, $el.children('div'))
  1157. t.assertLength(3, $el.find('div').contents())
  1158. $el = $('#wrapinner_empty_test')
  1159. $el.wrapInner('<div>')
  1160. t.assertLength(1, $el.children())
  1161. t.assertLength(1, $el.children('div'))
  1162. t.assertLength(0, $el.find('div').contents())
  1163. },
  1164. testWrapInnerFunction: function(t) {
  1165. var el = $('<div><b>A</b><b>B</b></div>')
  1166. el.find('b').wrapInner(function(index){
  1167. return '<a class=link' + index + $(this).text() + ' />'
  1168. })
  1169. t.assertEqual(
  1170. '<b><a class="link0A">A</a></b><b><a class="link1B">B</a></b>',
  1171. el.html()
  1172. )
  1173. },
  1174. testUnwrap: function(t){
  1175. var context=$("#unwrap_test")
  1176. //Element With no siblings
  1177. $(".unwrap_one span",context).unwrap()
  1178. t.assertLength(1,$("b",context))
  1179. //Element with siblings
  1180. $(".unwrap_two span",context).unwrap()
  1181. t.assertLength(0,$("b",context))
  1182. //make sure siblings are unaffected
  1183. t.assertLength(3,$("span",context))
  1184. //make sure parents are what they should be
  1185. t.assertEqual($("span",context).parent().get(0), document.getElementsByClassName("unwrap_one")[0])
  1186. },
  1187. testUnwrapFragment: function(t){
  1188. var fragment = $('<div id=outer><div id=inner></div><div id=uninvolved></div></div>'),
  1189. innerFragment = fragment.find("#inner"),
  1190. uninvolved = fragment.find("#uninvolved")
  1191. innerFragment.unwrap()
  1192. t.assertLength(0, innerFragment.parent(), '#inner should be orphan')
  1193. t.assertLength(0, uninvolved.parent(), '#uninvolved should be orphan')
  1194. t.assertLength(0, fragment.children(), 'fragment should be empty')
  1195. },
  1196. testClone: function(t){
  1197. var el = $('<div class=sheep><span></span></div>'),
  1198. el2 = el.clone()
  1199. t.assert(el2.hasClass('sheep'))
  1200. el2.addClass('black')
  1201. t.refute(el.hasClass('black'))
  1202. el2.find('span').text('baa')
  1203. t.assertIdentical('', el.find('span').text())
  1204. },
  1205. testFind: function(t){
  1206. var found = $('p#find1').find('span.findme')
  1207. t.assertLength(4, found)
  1208. t.assertEqual('1', found.get(0).innerHTML)
  1209. t.assertEqual('2', found.get(1).innerHTML)
  1210. t.assertEqual('4', found.get(2).innerHTML)
  1211. t.assertEqual('5<span>6</span>', found.get(3).innerHTML)
  1212. var found = $('p#find1, #find2').find('span')
  1213. t.assertLength(11, found)
  1214. },
  1215. testFindWithCollection: function(t){
  1216. var targets = $('#find1 span span, #find1 b, #find2 span')
  1217. var found = $('p#find1').find(targets)
  1218. t.assertLength(2, found)
  1219. t.assertEqual('B', found.get(0).tagName)
  1220. t.assertEqual('6', found.get(1).innerHTML)
  1221. },
  1222. testFindWithElement: function(t){
  1223. var target = $('#find1 span span').get(0)
  1224. var found = $('p#find1').find(target)
  1225. t.assertLength(1, found)
  1226. t.assertEqual('6', found.get(0).innerHTML)
  1227. found = $('p#find1').find(document.body)
  1228. t.assertLength(0, found, "no elements should have matched")
  1229. },
  1230. testFindWithInvalidNode: function(t) {
  1231. var found = $('<div><a>1</a></div>\n<div></div>').find('a')
  1232. t.assertLength(1, found)
  1233. t.assertEqual('1', found.get(0).innerHTML)
  1234. },
  1235. testFindWithFalsyValue: function(t){
  1236. var element = '<div><a>1</a></div>'
  1237. t.assertZeptoCollection(0, $(element).find(undefined))
  1238. t.assertZeptoCollection(0, $(element).find(false))
  1239. t.assertZeptoCollection(0, $(element).find(0))
  1240. t.assertZeptoCollection(0, $(element).find(''))
  1241. },
  1242. testFilter: function(t){
  1243. var found = $('div')
  1244. t.assertLength(2, found.filter('.filtertest'))
  1245. t.assertLength(0, found.filter('.doesnotexist'))
  1246. t.assertLength(1, found.filter('.filtertest').filter(':nth-child(2n)'))
  1247. var nodes = $('<select><option value=1>test1</option><option value=2>test2</option><option value=1>test1</option></select>')
  1248. t.assertLength(2, nodes.find('option').filter(function(){ return this.value == '1' }))
  1249. var indexes = []
  1250. nodes.find('option').filter(function(index){ if (this.value=='1') indexes.push(index) })
  1251. t.assertEqualCollection([0,2], indexes)
  1252. },
  1253. testFilterWithNonNativeArrayFilter: function(t){
  1254. var nativeFilter = Array.prototype.filter
  1255. try {
  1256. // apply broken filter
  1257. Array.prototype.filter = function(){ return [] }
  1258. t.assertLength(2, $('div').filter('.filtertest'))
  1259. } finally {
  1260. Array.prototype.filter = nativeFilter
  1261. }
  1262. },
  1263. testHas: function(t){
  1264. var result, el = $('<b id=one><a></a></b><b id=two><i></i></b><b id=three><i></i></b>')
  1265. result = el.has('a')
  1266. t.assertEqual('one', result.pluck('id').join(' '))
  1267. result = el.has('i')
  1268. t.assertEqual('two three', result.pluck('id').join(' '))
  1269. result = el.has(el.find('i').get(0))
  1270. t.assertEqual('two', result.pluck('id').join(' '))
  1271. },
  1272. testAdd: function(t){
  1273. var lis=$("li"),spans=$("span"),
  1274. together=lis.add("span"),
  1275. duplicates=spans.add("span"),
  1276. disconnected=$("<div></div>").add("<span></span>"),
  1277. mainContext=$("#addTest")
  1278. //uniquness of collection
  1279. t.assertLength(spans.length, duplicates)
  1280. //selector only
  1281. t.assertLength((lis.length + spans.length), together)
  1282. //selector with context
  1283. t.assertEqualCollection($("span",mainContext), $(".add_span").add(".add_span_exclude",mainContext))
  1284. //DOM Element + Chaining test
  1285. t.assertEqualCollection(mainContext.children(), $(".add_span").add(".add_span_exclude").add(document.getElementById("addTestDiv")))
  1286. //Disconnected
  1287. t.assert(!disconnected.get(0).parentNode)
  1288. $("#addTestDiv").append(disconnected)
  1289. t.assertEqual('<div></div><span></span>', document.getElementById("addTestDiv").innerHTML)
  1290. },
  1291. testIs: function(t){
  1292. t.assert($('#find1').is('p'))
  1293. t.assert($('#li2').is(':first-child'))
  1294. t.assert(!$('#find1').is('doesnotexist'))
  1295. t.assert(!$('#find1').is())
  1296. t.assert($('#fixtures div').is('#some_element'))
  1297. t.assert(!$('#doesnotexist').is('p'))
  1298. t.assert(!$(window).is('p'))
  1299. },
  1300. testIsWithoutParent: function(t){
  1301. var elem = $('<div id=outOfDOM />')
  1302. t.assert(elem.is('div'))
  1303. t.assert(elem.is('#outOfDOM'))
  1304. t.assert(!elem.is('p'))
  1305. t.assert(!elem.is())
  1306. },
  1307. testCSS: function(t){
  1308. var el = $('#some_element').get(0)
  1309. // single assignments
  1310. $('#some_element').css('color', '#f00')
  1311. $('#some_element').css('margin-top', '10px')
  1312. $('#some_element').css('marginBottom', '5px')
  1313. $('#some_element').css('left', 42)
  1314. $('#some_element').css('z-index', 10)
  1315. $('#some_element').css('fontWeight', 300)
  1316. $('#some_element').css('border', '1px solid rgba(255,0,0,0)')
  1317. t.assertEqual('rgb(255, 0, 0)', el.style.color)
  1318. t.assertEqual('rgba(255, 0, 0, 0)', el.style.borderLeftColor)
  1319. t.assertEqual('1px', el.style.borderLeftWidth)
  1320. t.assertEqual('10px', el.style.marginTop)
  1321. t.assertEqual('5px', el.style.marginBottom)
  1322. t.assertEqual('42px', el.style.left)
  1323. t.assertEqual(300, el.style.fontWeight)
  1324. t.assertEqual(10, el.style.zIndex)
  1325. // read single values, including shorthands
  1326. t.assertEqual('rgb(255, 0, 0)',
  1327. $('#some_element').css('color'))
  1328. t.assertEqual('1px solid rgba(255, 0, 0, 0)',
  1329. $('#some_element').css('border'))
  1330. // multiple assignments
  1331. $('#some_element').css({
  1332. 'border': '2px solid #000',
  1333. 'color': 'rgb(0,255,0)',
  1334. 'padding-left': '2px'
  1335. })
  1336. t.assertEqual('2px', $('#some_element').css('borderLeftWidth'))
  1337. t.assertEqual('solid', $('#some_element').css('borderLeftStyle'))
  1338. t.assertEqual('rgb(0, 0, 0)', $('#some_element').css('borderLeftColor'))
  1339. t.assertEqual('rgb(0, 255, 0)', $('#some_element').css('color'))
  1340. t.assertEqual('2px', $('#some_element').css('paddingLeft'))
  1341. t.assertEqual('2px', $('#some_element').css('border-left-width'))
  1342. t.assertEqual('solid', $('#some_element').css('border-left-style'))
  1343. t.assertEqual('rgb(0, 0, 0)', $('#some_element').css('border-left-color'))
  1344. t.assertEqual('rgb(0, 255, 0)', $('#some_element').css('color'))
  1345. t.assertEqual('2px', $('#some_element').css('padding-left'))
  1346. // read multiple values, camelCased CSS
  1347. var arrCamelCss = $('#some_element').css(['borderLeftWidth', 'borderLeftStyle', 'borderLeftColor', 'color'])
  1348. t.assertEqual('2px', arrCamelCss['borderLeftWidth'])
  1349. t.assertEqual('solid', arrCamelCss['borderLeftStyle'])
  1350. t.assertEqual('rgb(0, 0, 0)', arrCamelCss['borderLeftColor'])
  1351. t.assertEqual('rgb(0, 255, 0)', arrCamelCss['color'])
  1352. t.assertUndefined(arrCamelCss['paddingLeft'])
  1353. // read multiple values, dashed CSS property names
  1354. var arrDashedCss = $('#some_element').css(['border-left-width', 'border-left-style', 'border-left-color', 'color'])
  1355. t.assertEqual('2px', arrDashedCss['border-left-width'])
  1356. t.assertEqual('solid', arrDashedCss['border-left-style'])
  1357. t.assertEqual('rgb(0, 0, 0)', arrDashedCss['border-left-color'])
  1358. t.assertEqual('rgb(0, 255, 0)', arrDashedCss['color'])
  1359. t.assertUndefined(arrDashedCss['padding-left'])
  1360. // make sure reads from empty Zepto collections just return undefined
  1361. t.assertUndefined($().css(['border-left-width']))
  1362. var div = $('#get_style_element')
  1363. t.assertEqual('48px', div.css('font-size'))
  1364. t.assertEqual('rgb(0, 0, 0)', div.css('color'))
  1365. },
  1366. testCSSUnset: function (t) {
  1367. var el = $('#some_element').css({ 'margin-top': '1px', 'margin-bottom': '1px' }),
  1368. dom = el.get(0)
  1369. el.css('color', '#000')
  1370. el.css('color', '')
  1371. t.assertIdentical('', dom.style.color)
  1372. el.css('color', '#000')
  1373. el.css('color', undefined)
  1374. t.assertIdentical('', dom.style.color)
  1375. el.css('color', '#000')
  1376. el.css('color', null)
  1377. t.assertIdentical('', dom.style.color)
  1378. el.css('color', '#000')
  1379. el.css({ color: '', 'margin-top': undefined, 'marginBottom': null })
  1380. t.assertIdentical('', dom.style.color)
  1381. t.assertIdentical('', dom.style.marginTop)
  1382. t.assertIdentical('', dom.style.marginBottom)
  1383. },
  1384. testCSSZeroValue: function (t) {
  1385. var el = $('#some_element'), dom = el.get(0)
  1386. el.css('opacity', 0)
  1387. t.assertIdentical('0', dom.style.opacity)
  1388. el.css('opacity', 1)
  1389. el.css({ opacity: 0 })
  1390. t.assertIdentical('0', dom.style.opacity)
  1391. },
  1392. testCSSOnNonExistingElement: function (t) {
  1393. var el = $('.some-non-exist-elm')
  1394. t.assertUndefined(el.css('color'))
  1395. },
  1396. testCSSBatchSetOnNonExistingElementReturnsSelf: function (t) {
  1397. var el = $('.some-non-exist-elm')
  1398. var ret = el.css({
  1399. 'border': '2px solid #000',
  1400. 'color': 'rgb(0,255,0)'
  1401. })
  1402. t.assertIdentical(ret, el)
  1403. },
  1404. testHtml: function(t){
  1405. var div = $('div.htmltest')
  1406. div.text(undefined)
  1407. t.assertEqual('', div.html())
  1408. t.assertIdentical(div, div.html('yowza'))
  1409. t.assertEqual('yowza', document.getElementById('htmltest1').innerHTML)
  1410. t.assertEqual('yowza', document.getElementById('htmltest2').innerHTML)
  1411. t.assertEqual('yowza', $('div.htmltest').html())
  1412. div.html('')
  1413. t.assertEqual('', document.getElementById('htmltest2').innerHTML)
  1414. t.assertEqual("", $('#htmltest3').html())
  1415. t.assertNull($('doesnotexist').html())
  1416. div.html('yowza')
  1417. div.html(function(idx, html){
  1418. return html.toUpperCase()
  1419. })
  1420. t.assertEqual('YOWZA', div.html())
  1421. div.html('<u>a</u><u>b</u><u>c</u>')
  1422. $('u').html(function(idx,html){
  1423. return idx+html
  1424. })
  1425. t.assertEqual('<u>0a</u><u>1b</u><u>2c</u>', div.html())
  1426. var table = $('#htmltest4'),
  1427. html = '<tbody><tr><td>ok</td></tr></tbody>'
  1428. table.html('<tbody><tr><td>ok</td></tr></tbody>')
  1429. t.assertEqual(html, table.html())
  1430. },
  1431. testText: function(t){
  1432. // test basics with Zepto-created DOM elements
  1433. t.assertEqual('', $('<h1/>').text())
  1434. t.assertEqual('', $('<h1/>').text('').text())
  1435. t.assertEqual('', $('<h1/>').text(undefined).text())
  1436. t.assertEqual('', $('<h1/>').text(null).text())
  1437. t.assertEqual('false', $('<h1/>').text(false).text())
  1438. t.assertEqual('1', $('<h1/>').text(1).text())
  1439. t.assertEqual('<b>a</b>', $('<h1/>').text('<b>a</b>').text())
  1440. t.assertEqual('&lt;b&gt;a&lt;/b&gt;',
  1441. $('<h1/>').text('<b>a</b>').html())
  1442. // now test with some existing DOM elements
  1443. $('#texttest3').text(undefined)
  1444. t.assertEqual('', $('#texttest3').text())
  1445. t.assertEqual('Here is some text', $('#texttest1').text())
  1446. t.assertEqual('And some more', $('#texttest2').text())
  1447. // gets all text of selectors matching
  1448. t.assertEqual('Here is some textAnd some more', $('div.texttest').text())
  1449. $('div.texttest').text("Let's set it")
  1450. t.assertEqual("Let's set it", $('#texttest1').text())
  1451. t.assertEqual("Let's set it", $('#texttest2').text())
  1452. $('#texttest2').text('')
  1453. t.assertEqual("Let's set it", $('#texttest1').text())
  1454. t.assertEqual('', $('#texttest2').text())
  1455. },
  1456. testTextWithFunction: function(t) {
  1457. var el = $('<div><span>hello</span> <span></span> <span>world</span> <span>again</span></div>'),
  1458. els = el.find('span')
  1459. els.text(function(idx, oldText){
  1460. if (idx > 2) return null
  1461. if (oldText) return oldText.toUpperCase() + ' ' + idx
  1462. })
  1463. t.assertEqual('HELLO 0', els[0].textContent)
  1464. t.assertEqual('', els[1].textContent)
  1465. t.assertEqual('WORLD 2', els[2].textContent)
  1466. t.assertEqual('', els[3].textContent)
  1467. },
  1468. testEmpty: function(t) {
  1469. $('#empty_test').empty()
  1470. t.assertEqual(document.getElementById('empty_1'), null)
  1471. t.assertEqual(document.getElementById('empty_2'), null)
  1472. t.assertEqual(document.getElementById('empty_3'), null)
  1473. t.assertEqual(document.getElementById('empty_4'), null)
  1474. },
  1475. testAttr: function(t){
  1476. var els = $('#attr_1, #attr_2')
  1477. t.assertEqual('someId1', els.attr("data-id"))
  1478. t.assertEqual('someName1', els.attr("data-name"))
  1479. els.attr("data-id","someOtherId")
  1480. els.attr("data-name","someOtherName")
  1481. t.assertEqual('someOtherId', els.attr("data-id"))
  1482. t.assertEqual('someOtherName', els.attr("data-name"))
  1483. t.assertEqual('someOtherId', $('#attr_2').attr('data-id'))
  1484. t.assertUndefined(els.attr("nonExistentAttribute"))
  1485. els.attr("data-id", false)
  1486. t.assertEqual("false", els.attr("data-id"))
  1487. els.attr("data-id", 0)
  1488. t.assertEqual("0", els.attr("data-id"))
  1489. els.attr({ 'data-id': 'id', 'data-name': 'name' })
  1490. t.assertEqual('id', els.attr("data-id"))
  1491. t.assertEqual('name', els.attr("data-name"))
  1492. t.assertEqual('id', $('#attr_2').attr('data-id'))
  1493. els.attr('data-id', function(idx,oldvalue){
  1494. return idx+oldvalue
  1495. })
  1496. t.assertEqual('0id', els.attr('data-id'))
  1497. t.assertEqual('1id', $('#attr_2').attr('data-id'))
  1498. },
  1499. testAttrSetterErase: function(t){
  1500. var el = $('<div data-name="foo">')
  1501. t.assertIdentical(el, el.attr('data-name', undefined), 'setter should return self')
  1502. t.assertNull(el.get(0).getAttribute('data-name'), 'attribute should be erased')
  1503. t.assertUndefined(el.attr('data-name'), 'attr should reflect erased attribute')
  1504. },
  1505. testProp: function(t){
  1506. var label = $('#prop_test1')
  1507. var input = $('#prop_test2')
  1508. var table = $('#prop_test3')
  1509. var td1 = $('#prop_test4')
  1510. var td2 = $('#prop_test5')
  1511. var img = $('#prop_test6')
  1512. var div = $('#prop_test7')
  1513. t.assertEqual(input.prop('tabindex'), -1)
  1514. t.assertEqual(input.prop('readonly'), true)
  1515. t.assertEqual(label.prop('for'), 'prop_test2')
  1516. t.assertEqual(input.prop('class'), 'propTest')
  1517. t.assertEqual(input.prop('maxlength'), 10)
  1518. t.assertEqual(table.prop('cellspacing'), 10)
  1519. t.assertEqual(table.prop('cellpadding'), 5)
  1520. t.assertEqual(td1.prop('rowspan'), 2)
  1521. t.assertEqual(td2.prop('colspan'), 2)
  1522. t.assertEqual(img.prop('usemap'), '#imgMap')
  1523. t.assertEqual(div.prop('contenteditable'), 'true')
  1524. },
  1525. testRemoveProp: function(t){
  1526. var el = $('<span>')
  1527. el.prop('hello', 'world')
  1528. t.assertEqual('world', el[0].hello)
  1529. el.removeProp('hello')
  1530. t.assertUndefined(el[0].hello)
  1531. },
  1532. testPropSetterErase: function(t){
  1533. var input = $('<input readonly>')
  1534. t.assertIdentical(input, input.prop('readonly', false))
  1535. t.assertFalse(input.prop('readonly'))
  1536. input.get(0)._foo = 'bar'
  1537. t.assertIdentical(input, input.prop('_foo', undefined))
  1538. t.assertUndefined(input.get(0)._foo, 'custom property should be cleared')
  1539. t.assertUndefined(input.prop('_foo'), 'prop should reflect cleared property')
  1540. },
  1541. testAttrNoElement: function(t){
  1542. t.assertUndefined($().attr('yo'))
  1543. t.assertUndefined($(document.createTextNode('')).attr('yo'))
  1544. t.assertUndefined($(document.createComment('')).attr('yo'))
  1545. var els = $('<b></b> <i></i>').attr('id', function(i){ return this.nodeName + i })
  1546. t.assertEqual('B0', els.eq(0).attr('id'))
  1547. t.assertEqual('I2', els.eq(2).attr('id'))
  1548. t.assertUndefined(els.eq(1).attr('id'))
  1549. },
  1550. testAttrEmpty: function(t){
  1551. var el = $('#data_attr')
  1552. t.assertIdentical('', el.attr('data-empty'))
  1553. },
  1554. testAttrOnTextInputField: function(t) {
  1555. var inputs, values
  1556. // HTML is set here because IE does not reset
  1557. // values of input fields on page reload
  1558. document.getElementById('attr_with_text_input').innerHTML =
  1559. '<input value="Default input">'+
  1560. '<input type="text" value="Text input">'+
  1561. '<input type="email" value="Email input">'+
  1562. '<input type="search" value="Search input">'
  1563. inputs = $('#attr_with_text_input input')
  1564. values = $.map(inputs, function(i){ return $(i).attr('value') })
  1565. t.assertEqual('Default input, Text input, Email input, Search input', values.join(', '))
  1566. // Only .attr('value', v) changes .attr('value')
  1567. // rather than .val(v)
  1568. inputs.attr('value', function(i, value){ return value.replace('input', 'changed') })
  1569. values = $.map(inputs, function(i){ return $(i).attr('value') })
  1570. t.assertEqual('Default changed, Text changed, Email changed, Search changed', values.join(', '))
  1571. },
  1572. testAttrNullUnset: function(t){
  1573. var el = $('<div id=hi>')
  1574. el.attr('id', null)
  1575. t.assertUndefined(el.attr('id'))
  1576. el.attr('id', 'hello')
  1577. el.attr({ id:null })
  1578. t.assertUndefined(el.attr('id'))
  1579. },
  1580. testRemoveAttr: function(t) {
  1581. var el = $('#attr_remove')
  1582. t.assertEqual('boom', el.attr('data-name'))
  1583. el.removeAttr('data-name')
  1584. t.assertUndefined(el.attr('data-name'))
  1585. },
  1586. testRemoveMultipleAttr: function(t) {
  1587. var el = $('#attr_remove_multi')
  1588. t.assertEqual('someId1', el.attr('data-id'))
  1589. t.assertEqual('someName1', el.attr('data-name'))
  1590. el.removeAttr('data-id data-name')
  1591. t.assertUndefined(el.attr('data-id'))
  1592. t.assertUndefined(el.attr('data-name'))
  1593. },
  1594. testRemoveAttrNoElement: function(t){
  1595. t.assert($().removeAttr('rel'))
  1596. t.assert($(document.createTextNode('')).removeAttr('rel'))
  1597. var els = $('<b rel=up></b> <i rel=next></i>')
  1598. t.assertIdentical(els, els.removeAttr('rel'))
  1599. t.assertUndefined(els.eq(0).attr('rel'))
  1600. t.assertUndefined(els.eq(1).attr('rel'))
  1601. t.assertUndefined(els.eq(2).attr('rel'))
  1602. },
  1603. testData: function(t) {
  1604. var el = $('#data_attr')
  1605. // existing attribute
  1606. t.assertEqual('bar', el.data('foo'))
  1607. t.assertEqual('baz', el.data('foo-bar'))
  1608. t.assertEqual('baz', el.data('fooBar'))
  1609. // camelCase
  1610. el.data('fooBar', 'bam')
  1611. t.assertEqual('bam', el.data('fooBar'))
  1612. t.assertEqual('bam', el.data('foo-bar'))
  1613. // new attribute
  1614. el.data('fun', 'hello')
  1615. t.assertEqual('hello', el.attr('data-fun'))
  1616. t.assertEqual('hello', el.data('fun'))
  1617. // blank values
  1618. t.assertIdentical('', el.data('empty'))
  1619. t.assertUndefined(el.data('does-not-exist'))
  1620. },
  1621. testDataSetterErase: function(t) {
  1622. var el = $('<div data-name="foo">')
  1623. t.assertIdentical(el, el.data('name', undefined))
  1624. t.assertUndefined(el.data('name'))
  1625. },
  1626. testDataNumberType: function(t){
  1627. var el = $('<div data-num=42 />')
  1628. t.assertIdentical(42, el.data('num'))
  1629. t.assertIdentical(42.5,
  1630. $('<div data-float=42.5 />').data('float'))
  1631. t.assertIdentical("08",
  1632. $('<div data-notnum=08 />').data('notnum'))
  1633. t.assertIdentical("5903509451651483504",
  1634. $('<div data-bignum="5903509451651483504" />').data('bignum'))
  1635. },
  1636. testDataBooleanType: function(t){
  1637. var el = $('<div data-true=true data-false=false />')
  1638. t.assertTrue(el.data('true'))
  1639. t.assertFalse(el.data('false'))
  1640. },
  1641. testDataNullType: function(t){
  1642. var el = $('<div data-nil=null />')
  1643. t.assertNull(el.data('nil'))
  1644. },
  1645. testDataJsonType: function(t){
  1646. var el = $('<div data-json=\'["one", "two"]\' data-invalid=\'[boom]\' />')
  1647. var json = el.data('json')
  1648. t.assertEqual(2, json.length)
  1649. t.assertEqual("one", json[0])
  1650. t.assertEqual("two", json[1])
  1651. t.assertEqual('[boom]', el.data('invalid'))
  1652. },
  1653. testVal: function(t) {
  1654. var input = $('#attr_val')
  1655. // some browsers like IE don't reset input values on reload
  1656. // which messes up repeated test runs, so set the start value
  1657. // directly via the DOM API
  1658. document.getElementById('attr_val').value = 'Hello World'
  1659. t.assertEqual('Hello World', input.val())
  1660. input.val(undefined)
  1661. t.assertEqual('', input.val())
  1662. input.val(null)
  1663. t.assertEqual('', input.val())
  1664. input.val('')
  1665. t.assertEqual('', input.val())
  1666. input.get(0).value = 'Hello again'
  1667. t.assertEqual('Hello again', input.val())
  1668. input.val(function(i, val){ return val.replace('Hello', 'Bye') })
  1669. t.assertEqual('Bye again', input.val())
  1670. t.assertUndefined($('non-existent').val())
  1671. var multiple =
  1672. $('<select multiple><option selected>1</option><option value=2 selected="selected">a</option><option>3</option></select>')
  1673. t.assertEqualCollection(['1','2'], multiple.val())
  1674. // FIXME
  1675. // This is the "approved" way of de-selecting an option
  1676. // Unfortunately, this fails on Chrome 29 for Android
  1677. multiple.find('option')[0].selected = false
  1678. t.assertEqualCollection(['2'], multiple.val(),
  1679. "Expected val() to reflect changes to selected options in a <select multiple> element")
  1680. },
  1681. testValVsValueAttr: function(t) {
  1682. var input
  1683. input = $('<input type="text" value="Original">')
  1684. input.val('By .val(v)')
  1685. t.assertEqual('Original', input.attr('value'))
  1686. t.assertEqual('By .val(v)', input.val())
  1687. input.attr('value', 'By .attr("value", v)')
  1688. t.assertEqual('By .attr("value", v)', input.attr('value'))
  1689. t.assertEqual('By .val(v)', input.val())
  1690. // .attr('value', v) will change both
  1691. // without applying .val(v) first
  1692. input = $('<input type="text" value="Original">')
  1693. input.attr('value', 'By .attr("value", v)')
  1694. t.assertEqual('By .attr("value", v)', input.attr('value'))
  1695. t.assertEqual('By .attr("value", v)', input.val())
  1696. },
  1697. testChaining: function(t){
  1698. t.assert(document.getElementById('nay').innerHTML == "nay")
  1699. $('span.nay').css('color', 'red').html('test')
  1700. t.assert(document.getElementById('nay').innerHTML == "test")
  1701. },
  1702. testCachingForLater: function(t){
  1703. var one = $('div')
  1704. var two = $('span')
  1705. t.assert(one.get(0) !== two.get(0))
  1706. },
  1707. testPlugins: function(t){
  1708. var el = $('#some_element').get(0)
  1709. $.fn.plugin = function(){
  1710. return this.each(function(){ this.innerHTML = 'plugin!' })
  1711. }
  1712. $('#some_element').plugin()
  1713. t.assertEqual('plugin!', el.innerHTML)
  1714. // test if existing Zepto objects receive new plugins
  1715. if ('__proto__' in {}) {
  1716. var $some_element = $('#some_element')
  1717. $.fn.anotherplugin = function(){
  1718. return this.each(function(){ this.innerHTML = 'anotherplugin!' })
  1719. }
  1720. t.assert(typeof $some_element.anotherplugin == 'function')
  1721. $some_element.anotherplugin()
  1722. t.assertEqual('anotherplugin!', el.innerHTML)
  1723. } else
  1724. window.console && console.warn &&
  1725. console.warn("Browser doesn't support __proto__, skipping test of live extension of existing Zepto objects with plugins")
  1726. },
  1727. testAppendPrependBeforeAfter: function(t){
  1728. $('#beforeafter').append('append')
  1729. $('#beforeafter').prepend('prepend')
  1730. $('#beforeafter').before('before')
  1731. $('#beforeafter').after('after')
  1732. t.assertEqual('before<div id="beforeafter">prependappend</div>after', $('#beforeafter_container').html())
  1733. //testing with TextNode as parameter
  1734. $('#beforeafter_container').html('<div id="beforeafter"></div>')
  1735. function text(contents){
  1736. return document.createTextNode(contents)
  1737. }
  1738. $('#beforeafter').append(text('append'))
  1739. $('#beforeafter').prepend(text('prepend'))
  1740. $('#beforeafter').before(text('before'))
  1741. $('#beforeafter').after(text('after'))
  1742. t.assertEqual('before<div id="beforeafter">prependappend</div>after', $('#beforeafter_container').html())
  1743. $('#beforeafter_container').html('<div id="beforeafter"></div>')
  1744. function div(contents){
  1745. var el = document.createElement('div')
  1746. el.innerHTML = contents
  1747. return el
  1748. }
  1749. $('#beforeafter').append(div('append'))
  1750. $('#beforeafter').prepend(div('prepend'))
  1751. $('#beforeafter').before(div('before'))
  1752. $('#beforeafter').after(div('after'))
  1753. t.assertEqual(
  1754. '<div>before</div><div id="beforeafter"><div>prepend</div>'+
  1755. '<div>append</div></div><div>after</div>',
  1756. $('#beforeafter_container').html()
  1757. )
  1758. //testing with Zepto object as parameter
  1759. $('#beforeafter_container').html('<div id="beforeafter"></div>')
  1760. $('#beforeafter').append($(div('append')))
  1761. $('#beforeafter').prepend($(div('prepend')))
  1762. $('#beforeafter').before($(div('before')))
  1763. $('#beforeafter').after($(div('after')))
  1764. t.assertEqual(
  1765. '<div>before</div><div id="beforeafter"><div>prepend</div>'+
  1766. '<div>append</div></div><div>after</div>',
  1767. $('#beforeafter_container').html()
  1768. )
  1769. //testing with a zepto object of more than one element as parameter
  1770. $(document.body).append('<div class="append">append1</div><div class="append">append2</div>')
  1771. $(document.body).append('<div class="prepend">prepend1</div><div class="prepend">prepend2</div>')
  1772. $(document.body).append('<div class="before">before1</div><div class="before">before2</div>')
  1773. $(document.body).append('<div class="after">after1</div><div class="after">after2</div>')
  1774. $('#beforeafter_container').html('<div id="beforeafter"></div>')
  1775. $('#beforeafter').append($('.append'))
  1776. $('#beforeafter').prepend($('.prepend'))
  1777. $('#beforeafter').before($('.before'))
  1778. $('#beforeafter').after($('.after'))
  1779. t.assertEqual(
  1780. '<div class="before">before1</div><div class="before">before2</div><div id="beforeafter"><div class="prepend">prepend1</div><div class="prepend">prepend2</div>'+
  1781. '<div class="append">append1</div><div class="append">append2</div></div><div class="after">after1</div><div class="after">after2</div>',
  1782. $('#beforeafter_container').html()
  1783. )
  1784. //
  1785. var helloWorlds = [], appendContainer1 = $('<div> <div>Hello</div> <div>Hello</div> </div>'),
  1786. helloDivs = appendContainer1.find('div')
  1787. helloDivs.append(' world!')
  1788. helloDivs.each(function() { helloWorlds.push($(this).text()) })
  1789. t.assertEqual('Hello world!,Hello world!', helloWorlds.join(','))
  1790. //
  1791. var spans = [], appendContainer2 = $('<div> <div></div> <div></div> </div>'),
  1792. appendDivs = appendContainer2.find('div')
  1793. appendDivs.append($('<span>Test</span>'))
  1794. appendDivs.each(function() { spans.push($(this).html()) })
  1795. t.assertEqual('<span>Test</span>,<span>Test</span>', spans.join(','))
  1796. },
  1797. testAppendNull: function(t){
  1798. var el = $(document.body)
  1799. t.assertIdentical(el, el.append(null))
  1800. },
  1801. testBeforeAfterFragment: function(t){
  1802. var fragment = $('<div class=fragment />')
  1803. fragment.before('before').after('after')
  1804. t.assertLength(1, fragment)
  1805. t.assert(fragment.hasClass('fragment'))
  1806. },
  1807. testAppendMultipleArguments: function(t){
  1808. var el = $('<div><span>original</span></div>')
  1809. el.append(
  1810. $('<b>one</b>').get(0),
  1811. $('<b>two</b><b>three</b>').get(),
  1812. $('<b>four</b><b>five</b>'),
  1813. '<b>six</b>'
  1814. )
  1815. t.assertEqual('original one two three four five six',
  1816. $.map(el.children(), function(c){ return $(c).text() }).join(' '))
  1817. },
  1818. testAppendWithArrayParams: function(t){
  1819. var zpElement = $('<b>five</b>')
  1820. var multElements = $('<b>six</b><b>seven</b>')
  1821. var node = document.createElement('b')
  1822. node.appendChild(document.createTextNode('four'))
  1823. // append elements on this array
  1824. var arrElements = ['<b>one</b><b>two</b>', '<b>three</b>', node, zpElement, multElements ]
  1825. var el = $('<div></div>')
  1826. el.append(arrElements)
  1827. t.assertEqual('<b>one</b><b>two</b><b>three</b><b>four</b><b>five</b><b>six</b><b>seven</b>', el.html())
  1828. },
  1829. testAppendToPrependTo: function(t){
  1830. // testing with Zepto object
  1831. function div(contents){
  1832. var el = document.createElement('div')
  1833. el.innerHTML = contents
  1834. return el
  1835. }
  1836. var ap = $(div('appendto'))
  1837. var pr = $(div('prependto'))
  1838. var ap2 = ap.appendTo($('#appendtoprependto'))
  1839. var pr2 = pr.prependTo($('#appendtoprependto'))
  1840. // the object returned is the correct one for method chaining
  1841. t.assertSame(ap, ap2)
  1842. t.assertSame(pr, pr2)
  1843. t.assertEqual(
  1844. '<div id="appendtoprependto"><div>prependto</div>'+
  1845. '<div>appendto</div></div>',
  1846. $('#appendtoprependto_container').html()
  1847. )
  1848. // zepto object with more than one element
  1849. $(document.body).append('<div class="appendto">appendto1</div><div class="appendto">appendto2</div>')
  1850. $(document.body).append('<div class="prependto">prependto1</div><div class="prependto">prependto2</div>')
  1851. // selector
  1852. // Note that on IE resetting the parent element to be empty will
  1853. // cause inserted elements to be emptied out, so we have to re-create
  1854. // them. This is the same behavior as on jQuery.
  1855. // (Other browsers don't exhibit this problem.)
  1856. ap = $(div('appendto'))
  1857. pr = $(div('prependto'))
  1858. $('#appendtoprependto_container').html('<div id="appendtoprependto"></div>')
  1859. ap.appendTo('#appendtoprependto')
  1860. pr.prependTo('#appendtoprependto')
  1861. t.assertEqual(
  1862. '<div id="appendtoprependto"><div>prependto</div>'+
  1863. '<div>appendto</div></div>',
  1864. $('#appendtoprependto_container').html()
  1865. )
  1866. // reset test elements
  1867. ap = $(div('appendto'))
  1868. pr = $(div('prependto'))
  1869. $('#appendtoprependto_container').html('<div id="appendtoprependto"></div>')
  1870. $('.appendto').appendTo($('#appendtoprependto'))
  1871. $('.prependto').prependTo($('#appendtoprependto'))
  1872. t.assertEqual(
  1873. '<div id="appendtoprependto"><div class="prependto">prependto1</div><div class="prependto">prependto2</div><div class="appendto">appendto1</div><div class="appendto">appendto2</div></div>',
  1874. $('#appendtoprependto_container').html()
  1875. )
  1876. },
  1877. testInsertBeforeInsertAfter: function(t){
  1878. // testing with Zepto object
  1879. function div(contents){
  1880. var el = document.createElement('div')
  1881. el.innerHTML = contents
  1882. return el
  1883. }
  1884. var ib = $(div('insertbefore'))
  1885. var ia = $(div('insertafter'))
  1886. var ibia = $('#insertbeforeinsertafter')
  1887. var ib2 = ib.insertBefore(ibia)
  1888. var ia2 = ia.insertAfter(ibia)
  1889. // test the object returned is correct for method chaining
  1890. t.assertEqual(
  1891. '<div>insertbefore</div><div id="insertbeforeinsertafter">'+
  1892. '</div><div>insertafter</div>',
  1893. $('#insertbeforeinsertafter_container').html()
  1894. )
  1895. // testing with a zepto object of more than one element as parameter
  1896. $(document.body).append('<div class="insertbefore">insertbefore1</div><div class="insertbefore">insertbefore2</div>')
  1897. $(document.body).append('<div class="insertafter">insertafter1</div><div class="insertafter">insertafter2</div>')
  1898. $('#insertbeforeinsertafter_container').html('<div id="insertbeforeinsertafter"></div>')
  1899. $('.insertbefore').insertBefore($('#insertbeforeinsertafter'))
  1900. $('.insertafter').insertAfter($('#insertbeforeinsertafter'))
  1901. t.assertEqual(
  1902. '<div class="insertbefore">insertbefore1</div><div class="insertbefore">insertbefore2</div>'+
  1903. '<div id="insertbeforeinsertafter"></div><div class="insertafter">insertafter1</div>'+
  1904. '<div class="insertafter">insertafter2</div>',
  1905. $('#insertbeforeinsertafter_container').html()
  1906. )
  1907. // testing with a selector as parameter
  1908. $('#insertbeforeinsertafter_container').html('<div id="insertbeforeinsertafter"></div>')
  1909. // reset test elements
  1910. ib = $(div('insertbefore'))
  1911. ia = $(div('insertafter'))
  1912. ib.insertBefore('#insertbeforeinsertafter')
  1913. ia.insertAfter('#insertbeforeinsertafter')
  1914. t.assertEqual(
  1915. '<div>insertbefore</div><div id="insertbeforeinsertafter">'+
  1916. '</div><div>insertafter</div>',
  1917. $('#insertbeforeinsertafter_container').html()
  1918. )
  1919. },
  1920. testAppendEval: function (t) {
  1921. window.someGlobalVariable = 0
  1922. try {
  1923. $('#fixtures').append(
  1924. '<div><b id="newByAppend">Hi</b>' +
  1925. '<\script>this.someGlobalVariable += $("#newByAppend").size()<\/script></div>'
  1926. )
  1927. t.assertIdentical(1, window.someGlobalVariable)
  1928. } finally {
  1929. delete window.someGlobalVariable
  1930. }
  1931. },
  1932. testNoEvalWithSrc: function (t) {
  1933. try {
  1934. window.someGlobalVariable = false
  1935. $('<\script src="remote.js">window.someGlobalVariable = true<\/script>').appendTo('body')
  1936. t.assert(!window.someGlobalVariable, 'expected SCRIPT with src not to be evaled')
  1937. } finally {
  1938. delete window.someGlobalVariable
  1939. }
  1940. },
  1941. testHtmlEval: function (t) {
  1942. window.someGlobalVariable = 0
  1943. try {
  1944. $('<div>').appendTo('#fixtures').html(
  1945. '<div><b id="newByHtml">Hi</b>' +
  1946. '<\script>this.someGlobalVariable += $("#newByHtml").size()<\/script></div>'
  1947. )
  1948. t.assertIdentical(1, window.someGlobalVariable)
  1949. } finally {
  1950. delete window.someGlobalVariable
  1951. }
  1952. },
  1953. testPrependEval: function (t) {
  1954. window.someGlobalVariable = 0
  1955. try {
  1956. $('<div>').appendTo('#fixtures').prepend(
  1957. '<b id="newByPrepend">Hi</b>' +
  1958. '<\script>this.someGlobalVariable += $("#newByPrepend").size()<\/script>'
  1959. )
  1960. t.assertIdentical(1, window.someGlobalVariable)
  1961. } finally {
  1962. delete window.someGlobalVariable
  1963. }
  1964. },
  1965. testAppendTemplateNonEval: function (t) {
  1966. try {
  1967. window.someGlobalVariable = true
  1968. $('<' + 'script type="text/template">window.someGlobalVariable = false</script' + '>').appendTo('body')
  1969. t.assert(window.someGlobalVariable)
  1970. window.someGlobalVariable = true
  1971. $('<' + 'script type="text/template">this.someGlobalVariable = false</script' + '>').appendTo('body')
  1972. t.assert(window.someGlobalVariable)
  1973. } finally {
  1974. delete window.someGlobalVariable
  1975. }
  1976. },
  1977. testHtmlTemplateNonEval: function (t) {
  1978. try {
  1979. window.someGlobalVariable = true
  1980. $('<div></div>').appendTo('body')
  1981. .html('<' + 'script type="text/template">window.someGlobalVariable = false</script' + '>')
  1982. t.assert(window.someGlobalVariable)
  1983. } finally {
  1984. delete window.someGlobalVariable
  1985. }
  1986. },
  1987. testRemove: function(t) {
  1988. var el = $('<div>').appendTo(document.body)
  1989. t.assertLength(1, el.parent())
  1990. t.assertIdentical(el, el.remove())
  1991. t.assertLength(0, el.parent())
  1992. t.assertIdentical(el, el.remove()) //=> ensure an error isn't raised
  1993. },
  1994. testNotInDocumentNonEval: function (t) {
  1995. try {
  1996. window.someGlobalVariable = 0
  1997. $('<div></div>')
  1998. .html('<\script>window.someGlobalVariable += 1<\/script>')
  1999. .appendTo('body')
  2000. t.assertIdentical(1, window.someGlobalVariable)
  2001. } finally {
  2002. delete window.someGlobalVariable
  2003. }
  2004. },
  2005. testAddRemoveClass: function(t){
  2006. var el = $('#some_element').get(0)
  2007. $('#some_element').addClass('green')
  2008. t.assertEqual('green', el.className)
  2009. $('#some_element').addClass('green')
  2010. t.assertEqual('green', el.className)
  2011. $('#some_element').addClass('red')
  2012. t.assertEqual('green red', el.className)
  2013. $('#some_element').addClass('blue red')
  2014. t.assertEqual('green red blue', el.className)
  2015. $('#some_element').removeClass('green blue')
  2016. t.assertEqual('red', el.className)
  2017. $('#some_element').attr('class', ' red green blue ')
  2018. t.assertEqual(' red green blue ', el.className) // sanity check that WebKit doesn't change original input
  2019. $('#some_element').removeClass('green')
  2020. t.assertEqual('red blue', el.className)
  2021. //addClass with function argument
  2022. $('#some_element').addClass(function(idx,classes){
  2023. //test the value of "this"
  2024. t.assertEqualCollection($('#some_element'), $(this))
  2025. //test original classes are being passed
  2026. t.assertEqual('red blue', this.className)
  2027. return "green"
  2028. })
  2029. t.assertEqual('red blue green', el.className)
  2030. // addClass with no argument
  2031. t.assertEqualCollection($('#some_element'), $('#some_element').addClass())
  2032. t.assertEqual('red blue green', el.className)
  2033. t.assertEqualCollection($('#some_element'), $('#some_element').addClass(''))
  2034. t.assertEqual('red blue green', el.className)
  2035. //removeClass with function argument
  2036. $('#some_element').removeClass(function(idx,classes){
  2037. //test the value of "this"
  2038. t.assertEqualCollection($('#some_element'), $(this))
  2039. //test original classes are being passed
  2040. t.assertEqual('red blue green', this.className)
  2041. return "blue"
  2042. })
  2043. t.assertEqual('red green', el.className)
  2044. $('#some_element').removeClass()
  2045. t.assertEqual('', el.className)
  2046. },
  2047. testWindowClasses: function(t){
  2048. // check that window is agnostic to class related functions
  2049. $(window).removeClass('non-existing-class')
  2050. t.refute('className' in window)
  2051. $(window).addClass('some-class')
  2052. t.refute('className' in window)
  2053. t.refute($(window).hasClass('some-class'))
  2054. $(window).toggleClass('some-class')
  2055. t.refute('className' in window)
  2056. },
  2057. testHasClass: function(t){
  2058. var el = $('#some_element')
  2059. el.addClass('green')
  2060. t.assert(el.hasClass('green'))
  2061. t.assert(!el.hasClass('orange'))
  2062. el.addClass('orange')
  2063. t.assert(el.hasClass('green'))
  2064. t.assert(el.hasClass('orange'))
  2065. el = $(document.body)
  2066. t.assert(!el.hasClass('orange'), "body shouldn't have the class")
  2067. el = el.add('#some_element')
  2068. t.assert(el.hasClass('orange'), "an element in collection has the class")
  2069. t.assertFalse(el.hasClass())
  2070. t.assertFalse(el.hasClass(''))
  2071. },
  2072. testHasClassEmpty: function(t){
  2073. var z = $('#doesnotexist')
  2074. t.assertEqual(0, z.size())
  2075. t.assertFalse(z.hasClass('a'))
  2076. },
  2077. testToggleClass: function(t){
  2078. var el = $('#toggle_element').removeClass()
  2079. t.assertIdentical(el, el.toggleClass('green'))
  2080. t.assertTrue(el.hasClass('green'))
  2081. t.assertFalse(el.hasClass('orange'))
  2082. el.toggleClass('orange')
  2083. t.assertTrue(el.hasClass('green'))
  2084. t.assertTrue(el.hasClass('orange'))
  2085. el.toggleClass('green')
  2086. t.assertFalse(el.hasClass('green'))
  2087. t.assertTrue(el.hasClass('orange'))
  2088. el.toggleClass('orange')
  2089. t.assertFalse(el.hasClass('green'))
  2090. t.assertFalse(el.hasClass('orange'))
  2091. el.toggleClass('orange green')
  2092. t.assertTrue(el.hasClass('orange'))
  2093. t.assertTrue(el.hasClass('green'))
  2094. el.toggleClass('orange green blue')
  2095. t.assertFalse(el.hasClass('orange'))
  2096. t.assertFalse(el.hasClass('green'))
  2097. t.assertTrue(el.hasClass('blue'))
  2098. el.removeClass()
  2099. el.toggleClass('orange', false)
  2100. t.assertFalse(el.hasClass('orange'))
  2101. el.toggleClass('orange', false)
  2102. t.assertFalse(el.hasClass('orange'))
  2103. el.toggleClass('orange', true)
  2104. t.assertTrue(el.hasClass('orange'))
  2105. el.toggleClass('orange', true)
  2106. t.assertTrue(el.hasClass('orange'))
  2107. //function argument
  2108. el.toggleClass(function(idx,classes){
  2109. t.assertIdentical(el.get(0), this)
  2110. //test original classes are being passed
  2111. t.assertEqual('orange', this.className)
  2112. return "brown"
  2113. })
  2114. t.assertTrue(el.hasClass('brown'))
  2115. el.toggleClass(function(idx,classes){
  2116. return "yellow"
  2117. }, false)
  2118. t.assertFalse(el.hasClass('yellow'))
  2119. el.toggleClass(function(idx,classes){
  2120. return "yellow"
  2121. }, true)
  2122. t.assert(el.hasClass('yellow'))
  2123. // no/empty argument
  2124. t.assertIdentical(el, el.toggleClass())
  2125. t.assertEqual('orange brown yellow', el.get(0).className)
  2126. t.assertIdentical(el, el.toggleClass(''))
  2127. t.assertEqual('orange brown yellow', el.get(0).className)
  2128. },
  2129. testClassSVG: function(t){
  2130. var svg = $('svg')
  2131. t.assert(!svg.hasClass('foo'))
  2132. svg.addClass('foo bar')
  2133. t.assert(svg.hasClass('foo'))
  2134. t.assert(svg.hasClass('bar'))
  2135. svg.removeClass('foo')
  2136. t.assert(!svg.hasClass('foo'))
  2137. t.assert(svg.hasClass('bar'))
  2138. svg.toggleClass('bar')
  2139. t.assert(!svg.hasClass('foo'))
  2140. t.assert(!svg.hasClass('bar'))
  2141. },
  2142. testIndex: function(t){
  2143. t.assertEqual($("p > span").index("#nay"), 2)
  2144. t.assertEqual($("p > span").index(".yay"), 0)
  2145. t.assertEqual($("span").index("span"), 0)
  2146. t.assertEqual($("span").index("boo"), -1)
  2147. t.assertEqual($('#index_test > *').eq(-1).index(), 1)
  2148. },
  2149. testBoolAttr: function (t) {
  2150. var el = $('#BooleanInput')
  2151. t.assertEqual('', el.attr('required'))
  2152. t.assertUndefined(el.attr('non_existant_attr'))
  2153. },
  2154. testDocumentReady: function (t) {
  2155. // Check that if document is already loaded, ready() immediately executes callback
  2156. var arg1, arg2, arg3, arg4, fired = false
  2157. $(function (Z1) {
  2158. arg1 = Z1
  2159. $(document).ready(function (Z2) {
  2160. arg2 = Z2
  2161. $(document).on('ready', function (Z3) {
  2162. arg3 = Z3
  2163. $(document).on('foo ready bar', function (Z4) {
  2164. arg4 = Z4
  2165. fired = true
  2166. })
  2167. })
  2168. })
  2169. })
  2170. t.assertTrue(fired)
  2171. t.assertIdentical(Zepto, arg1)
  2172. t.assertIdentical(Zepto, arg2)
  2173. t.assertIdentical(Zepto, arg3)
  2174. t.assertIdentical(Zepto, arg4)
  2175. },
  2176. testSlice: function (t) {
  2177. var $els = $("#slice_test div")
  2178. t.assertEqual($els.slice().length, 3)
  2179. t.assertEqual(typeof $els.slice().ready, 'function')
  2180. t.assertEqual($els.slice(-1)[0].className, 'slice3')
  2181. },
  2182. testScrollTop: function(t) {
  2183. var $window = $(window), $body = $(document.body)
  2184. t.assert($window.scrollTop() >= 0)
  2185. t.assert($body.scrollTop() >= 0)
  2186. t.assertIdentical($window.scrollTop(20), $window)
  2187. t.assertIdentical($body.scrollTop(20), $body)
  2188. },
  2189. testScrollLeft: function(t) {
  2190. var $window = $(window), $body = $(document.body)
  2191. t.assert($window.scrollLeft() >= 0)
  2192. t.assert($body.scrollLeft() >= 0)
  2193. t.assertIdentical($window.scrollLeft(20), $window)
  2194. t.assertIdentical($body.scrollLeft(20), $body)
  2195. },
  2196. testSort: function(t){
  2197. var els = $(['eight', 'nine', 'ten', 'eleven'])
  2198. var result = els.sort(function(a,b){ return b.length > a.length ? -1 : 1 })
  2199. t.assertIdentical(els, result)
  2200. t.assertEqual(4, result.size())
  2201. t.assertEqualCollection(['ten', 'nine', 'eight', 'eleven'], result)
  2202. }
  2203. })
  2204. Evidence('EventTest', {
  2205. tearDown: function(){
  2206. $('*').unbind()
  2207. },
  2208. testBind: function(t){
  2209. var counter = 0
  2210. $(document.body).bind('click', function(){ counter++ })
  2211. click($('#some_element').get(0))
  2212. t.assertEqual(1, counter)
  2213. counter = 0
  2214. $('#some_element').bind('click mousedown', function(){ counter++ })
  2215. click($('#some_element').get(0))
  2216. mousedown($('#some_element').get(0))
  2217. t.assertEqual(3, counter) // 1 = body click, 2 = element click, 3 = element mousedown
  2218. },
  2219. testBindWithObject: function(t){
  2220. var counter = 0, keyCounter = 0, el = $('#some_element'),
  2221. eventData = {
  2222. click: function(){ counter++ },
  2223. keypress: function(){ keyCounter++ }
  2224. }
  2225. $(document.body).bind(eventData)
  2226. el.trigger('click')
  2227. el.trigger('click')
  2228. t.assertEqual(2, counter)
  2229. el.trigger('keypress')
  2230. t.assertEqual(1, keyCounter)
  2231. $(document.body).unbind({ keypress: eventData.keypress })
  2232. el.trigger('click')
  2233. t.assertEqual(3, counter)
  2234. el.trigger('keypress')
  2235. t.assertEqual(1, keyCounter)
  2236. },
  2237. testBindContext: function(t){
  2238. var context, handler = function(){
  2239. context = $(this)
  2240. }
  2241. $('#empty_test').bind("click",handler)
  2242. $('#empty_test').bind("mousedown",handler)
  2243. click($('#empty_test').get(0))
  2244. t.assertEqualCollection($('#empty_test'), context)
  2245. context = null
  2246. mousedown($('#empty_test').get(0))
  2247. t.assertEqualCollection($('#empty_test'), context)
  2248. },
  2249. testBindWithCustomArgument: function(t) {
  2250. var data, numArgs, counter = 0,
  2251. handler = function(ev, arg) {
  2252. numArgs = arguments.length,
  2253. data = ev.data
  2254. counter = arg.counter
  2255. }
  2256. $('#some_element').bind('custom', handler)
  2257. $('#some_element').trigger('custom', { counter: 10 })
  2258. t.assertEqual(10, counter)
  2259. t.assertEqual(2, numArgs)
  2260. t.assertUndefined(data)
  2261. },
  2262. testBindPreventDefault: function (t) {
  2263. var link = $('<a href="#"></a>'),
  2264. prevented = false
  2265. link
  2266. .appendTo('body')
  2267. .bind('click', function () {
  2268. return false
  2269. })
  2270. .bind('click', function (e) {
  2271. prevented = e.isDefaultPrevented()
  2272. })
  2273. .trigger('click')
  2274. t.assert(prevented)
  2275. },
  2276. testCreateEventObject: function(t){
  2277. var e = $.Event('custom')
  2278. t.assertEqual('custom', e.type)
  2279. var e2 = new $.Event('custom')
  2280. t.assertEqual('custom', e2.type)
  2281. var e3 = $.Event('custom', {customKey: 'customValue'})
  2282. t.assertEqual('custom', e3.type)
  2283. t.assertEqual('customValue', e3.customKey)
  2284. var e4 = $.Event('custom', {bubbles: false})
  2285. t.assertFalse(e4.bubbles)
  2286. var e5 = $.Event({ type: 'keyup', keyCode: 40 })
  2287. t.assertEqual('keyup', e5.type)
  2288. t.assertEqual(40, e5.keyCode)
  2289. },
  2290. testTriggerObject: function(t){
  2291. var el = $('#some_element'),
  2292. eventType, eventCode
  2293. el.on('keyup', function(e){
  2294. eventType = e.type
  2295. eventCode = e.keyCode
  2296. })
  2297. el.trigger({ type: 'keyup', keyCode: 40 })
  2298. t.assertEqual('keyup', eventType)
  2299. t.assertEqual(40, eventCode)
  2300. },
  2301. testTriggerEventObject: function(t){
  2302. var data, counter = 0,
  2303. customEventKey = 0
  2304. var handler = function(ev,customData) {
  2305. data = ev.data
  2306. counter = customData.counter
  2307. customEventKey = ev.customKey
  2308. }
  2309. var customEventObject = $.Event('custom', { customKey: 20 })
  2310. $('#some_element').bind('custom', handler)
  2311. $('#some_element').trigger(customEventObject, { counter: 10 })
  2312. t.assertEqual(10, counter)
  2313. t.assertEqual(20, customEventKey)
  2314. t.assertUndefined(data)
  2315. },
  2316. testTriggerEventCancelled: function(t){
  2317. var event = $.Event('custom'),
  2318. element = $('<div/>'),
  2319. isDefaultPrevented = false
  2320. t.refute(event.isDefaultPrevented())
  2321. element.bind('custom', function(e){
  2322. e.preventDefault()
  2323. isDefaultPrevented = e.isDefaultPrevented()
  2324. })
  2325. element.trigger(event)
  2326. t.assertTrue(event.isDefaultPrevented())
  2327. t.assertTrue(isDefaultPrevented)
  2328. },
  2329. testTriggerHandler: function(t){
  2330. t.assertUndefined($('doesnotexist').triggerHandler('submit'))
  2331. var form = $('#trigger_handler form').get(0)
  2332. $('#trigger_handler').bind('submit', function(e) {
  2333. t.fail("triggerHandler shouldn't bubble")
  2334. })
  2335. var executed = []
  2336. $(form).bind('submit', function(e) {
  2337. executed.push("1")
  2338. t.assertEqual(form, e.target)
  2339. return 1
  2340. })
  2341. $(form).bind('submit', function(e) {
  2342. executed.push("2")
  2343. t.assertEqual(form, e.target)
  2344. e.stopImmediatePropagation()
  2345. return 2
  2346. })
  2347. $(form).bind('submit', function(e) {
  2348. t.fail("triggerHandler shouldn't continue after stopImmediatePropagation")
  2349. })
  2350. t.assertIdentical(2, $(form).triggerHandler('submit'))
  2351. t.assertEqual('1 2', executed.join(' '))
  2352. },
  2353. testUnbind: function(t){
  2354. var counter = 0, el = $('#another_element').get(0)
  2355. var handler = function(){ counter++ }
  2356. $('#another_element').bind('click mousedown', handler)
  2357. click(el)
  2358. mousedown(el)
  2359. t.assertEqual(2, counter)
  2360. $('#another_element').unbind('click', handler)
  2361. click(el)
  2362. t.assertEqual(2, counter)
  2363. mousedown(el)
  2364. t.assertEqual(3, counter)
  2365. $('#another_element').unbind('mousedown')
  2366. mousedown(el)
  2367. t.assertEqual(3, counter)
  2368. $('#another_element').bind('click mousedown', handler)
  2369. click(el)
  2370. mousedown(el)
  2371. t.assertEqual(5, counter)
  2372. $('#another_element').unbind()
  2373. click(el)
  2374. mousedown(el)
  2375. t.assertEqual(5, counter)
  2376. },
  2377. testUnbindWithNamespace: function(t){
  2378. var count = 0
  2379. $("#namespace_test").bind("click.bar", function() { count++ })
  2380. $("#namespace_test").bind("click.foo", function() { count++ })
  2381. $("#namespace_test").bind("mousedown.foo.bar", function() { count++ })
  2382. $("#namespace_test").trigger("click")
  2383. t.assertEqual(2, count)
  2384. $("#namespace_test").unbind("click.baz")
  2385. $("#namespace_test").trigger("click")
  2386. t.assertEqual(4, count)
  2387. $("#namespace_test").unbind("click.foo")
  2388. $("#namespace_test").trigger("click")
  2389. t.assertEqual(5, count)
  2390. $("#namespace_test").trigger("mousedown")
  2391. t.assertEqual(6, count)
  2392. $("#namespace_test").unbind(".bar")
  2393. $("#namespace_test").trigger("click").trigger("mousedown")
  2394. t.assertEqual(6, count)
  2395. },
  2396. testDelegate: function(t){
  2397. var counter = 0, pcounter = 0
  2398. $(document.body).delegate('#some_element', 'click', function(){ counter++ })
  2399. $('p').delegate('span.yay', 'click', function(){ counter++ })
  2400. $(document.body).delegate('p', 'click', function(){ pcounter++ })
  2401. click($('#some_element').get(0))
  2402. t.assertEqual(1, counter)
  2403. click($('span.yay').get(0))
  2404. t.assertEqual(2, counter)
  2405. click($('span.nay').get(0))
  2406. t.assertEqual(2, counter)
  2407. click($('p').get(0))
  2408. t.assertEqual(3, pcounter)
  2409. },
  2410. testDelegateBlurFocus: function(t) {
  2411. var counter = 0
  2412. $('#delegate_blur_test').delegate('input', 'blur', function(){ counter++ })
  2413. $('#delegate_blur_test').find('input').focus()
  2414. $('#delegate_blur_test').find('input').blur()
  2415. t.assertEqual(1, counter)
  2416. $('#delegate_blur_test').find('input').focus()
  2417. $('#delegate_blur_test').find('input').blur()
  2418. t.assertEqual(2, counter)
  2419. $('#delegate_focus_test').delegate('input', 'focus', function(){ counter++ })
  2420. $('#delegate_focus_test').find('input').focus()
  2421. $('#delegate_focus_test').find('input').blur()
  2422. t.assertEqual(3, counter)
  2423. $('#delegate_focus_test').find('input').focus()
  2424. $('#delegate_focus_test').find('input').blur()
  2425. t.assertEqual(4, counter)
  2426. },
  2427. testDelegateNamespacedBlurFocus: function(t) {
  2428. var counter = 0
  2429. $('#delegate_blur_test').delegate('input', 'blur.namespace_test', function(){ counter++ })
  2430. $('#delegate_blur_test').find('input').focus()
  2431. $('#delegate_blur_test').find('input').blur()
  2432. t.assertEqual(1, counter)
  2433. $('#delegate_blur_test').find('input').focus()
  2434. $('#delegate_blur_test').find('input').blur()
  2435. t.assertEqual(2, counter)
  2436. $('#delegate_focus_test').delegate('input', 'focus.namespace_test', function(){ counter++ })
  2437. $('#delegate_focus_test').find('input').focus()
  2438. $('#delegate_focus_test').find('input').blur()
  2439. t.assertEqual(3, counter)
  2440. $('#delegate_focus_test').find('input').focus()
  2441. $('#delegate_focus_test').find('input').blur()
  2442. t.assertEqual(4, counter)
  2443. },
  2444. testUndelegateNamespacedBlurFocus: function(t) {
  2445. var el, counter = 0
  2446. el = $('#delegate_blur_test')
  2447. el.delegate('input', 'blur.test', function(){ counter++ })
  2448. el.find('input').focus().blur()
  2449. t.assertEqual(1, counter, 'expected handler to be triggered')
  2450. el.undelegate('input', '.test')
  2451. el.find('input').focus().blur()
  2452. t.assertEqual(1, counter, 'expected handler to unbind')
  2453. el = $('#delegate_focus_test')
  2454. el.delegate('input', 'focus.test', function(){ counter++ })
  2455. el.find('input').focus().blur()
  2456. t.assertEqual(2, counter, 'expected handler to be triggered')
  2457. el.undelegate('input', '.test')
  2458. el.find('input').focus().blur()
  2459. t.assertEqual(2, counter, 'expected handler to unbind')
  2460. },
  2461. testDelegateReturnFalse: function(t){
  2462. $(document.body).delegate('#link_that_will_be_prevented', 'click', function(){
  2463. return false
  2464. })
  2465. var event = $.Event('click')
  2466. $('#link_that_will_be_prevented').trigger(event)
  2467. t.assertTrue(event.isDefaultPrevented())
  2468. t.pause()
  2469. setTimeout(function(){
  2470. t.resume(function(){
  2471. var text = $('#link_target_iframe')[0].contentDocument.body.textContent
  2472. t.assert(!text.match(/Hello from iframe/))
  2473. })
  2474. }, 500)
  2475. },
  2476. testDelegateReturnValueShouldntPreventDefault: function(t){
  2477. $(document.body).delegate('#link_that_will_be_prevented', 'click', function(){
  2478. })
  2479. var event = $.Event('click')
  2480. $('#link_that_will_be_prevented').trigger(event)
  2481. t.assertFalse(event.isDefaultPrevented())
  2482. t.pause()
  2483. setTimeout(function(){
  2484. t.resume(function(){
  2485. var text = $('#link_target_iframe')[0].contentDocument.body.textContent
  2486. t.assert(text.match(/Hello from iframe/))
  2487. })
  2488. }, 500)
  2489. },
  2490. testDelegateWithObject: function(t){
  2491. var counter = 0, received, el = $('p').first(),
  2492. eventData = {
  2493. click: function(){ counter++ },
  2494. custom: function(e, arg){ received = arg }
  2495. }
  2496. $(document.body).delegate('p', eventData)
  2497. el.trigger('click')
  2498. t.assertEqual(1, counter)
  2499. el.trigger('click')
  2500. t.assertEqual(2, counter)
  2501. el.trigger('custom', 'boo')
  2502. t.assertEqual('boo', received)
  2503. $(document.body).undelegate('p', {custom: eventData.custom})
  2504. el.trigger('click')
  2505. t.assertEqual(3, counter)
  2506. el.trigger('custom', 'bam')
  2507. t.assertEqual('boo', received)
  2508. },
  2509. testDelegateWithCustomArgument: function(t) {
  2510. var received
  2511. $(document).delegate('#some_element', 'custom', function(e, arg, more){ received = arg + more })
  2512. $('p').delegate('span.yay', 'custom', function(e, arg){ received = arg })
  2513. $(document).delegate('p', 'custom', function(e, arg){ received = arg })
  2514. $('#some_element').trigger('custom', 'one')
  2515. t.assertEqual('oneundefined', received)
  2516. $('#some_element').trigger('custom', ['one', 'two'])
  2517. t.assertEqual('onetwo', received)
  2518. $('span.yay').trigger('custom', 'boom')
  2519. t.assertEqual('boom', received)
  2520. $('span.yay').trigger('custom', ['bam', 'boom'])
  2521. t.assertEqual('bam', received)
  2522. $('span.nay').trigger('custom', 'noes')
  2523. t.assertEqual('noes', received)
  2524. $('p').first().trigger('custom', 'para')
  2525. t.assertEqual('para', received)
  2526. },
  2527. testDelegateEventProxy: function(t){
  2528. var content
  2529. $('div#delegate_test').delegate('span.second-level', 'click', function(e){
  2530. t.assertEqual($('span.second-level').get(0), this)
  2531. t.assertEqual($('span.second-level').get(0), e.currentTarget)
  2532. t.refuteEqual($('span.second-level').get(0), e.originalEvent.currentTarget)
  2533. t.assertEqual($('div#delegate_test').get(0), e.liveFired)
  2534. content = $(this).html()
  2535. })
  2536. click($('span.second-level').get(0))
  2537. t.assertEqual('hi', content)
  2538. var fired = false
  2539. if (window.location.hash.length) window.location.hash = ''
  2540. $('div#delegate_test').html('<a href="#foo"></a>')
  2541. $('div#delegate_test').delegate('a', 'click', function(e){
  2542. e.preventDefault()
  2543. fired = true
  2544. })
  2545. click($('div#delegate_test a').get(0))
  2546. t.assert(fired)
  2547. t.refuteEqual('#foo', window.location.hash)
  2548. fired = false
  2549. if (window.location.hash.length) window.location.hash = ''
  2550. $('div#delegate_test').html('<a href="#bar"></a>')
  2551. $('div#delegate_test a').trigger('click')
  2552. t.assert(fired)
  2553. t.refuteEqual('#bar', window.location.hash)
  2554. },
  2555. testUndelegate: function(t){
  2556. var count = 0, handler = function() { count++ }
  2557. $("#undelegate_test").bind("click mousedown", handler)
  2558. $("#undelegate_test").delegate("span.first-level", "click mousedown", handler)
  2559. $("#undelegate_test").delegate("span.second-level", "click mousedown", handler)
  2560. $("#undelegate_test span.second-level").trigger("click")
  2561. t.assertEqual(3, count)
  2562. $("#undelegate_test").undelegate("span.second-level", "click", handler)
  2563. $("#undelegate_test span.second-level").trigger("click")
  2564. t.assertEqual(5, count)
  2565. $("#undelegate_test").undelegate("span.first-level")
  2566. $("#undelegate_test span.second-level").trigger("click")
  2567. t.assertEqual(6, count)
  2568. $("#undelegate_test").unbind("click")
  2569. $("#undelegate_test span.second-level").trigger("click")
  2570. t.assertEqual(6, count)
  2571. $("#undelegate_test span.second-level").trigger("mousedown")
  2572. t.assertEqual(8, count)
  2573. $("#undelegate_test").undelegate()
  2574. $("#undelegate_test span.second-level").trigger("mousedown")
  2575. t.assertEqual(8, count)
  2576. },
  2577. testLive: function(t){
  2578. var counter = 0
  2579. $('p.live').live('click', function(){ counter++ })
  2580. $(document.body).append('<p class="live" id="live_1"></p>')
  2581. $(document.body).append('<p class="live" id="live_2"></p>')
  2582. click($('#live_1').get(0))
  2583. click($('#live_2').get(0))
  2584. $('p.live').remove()
  2585. $(document.body).append('<p class="live" id="live_this_test"></p>')
  2586. $('p.live').live('click', function(){
  2587. t.assertEqual(document.getElementById('live_this_test'), this)
  2588. })
  2589. click($('#live_this_test').get(0))
  2590. t.assertEqual(3, counter)
  2591. },
  2592. testDie: function(t){
  2593. var count = 0, handler = function() { count++ }
  2594. $("#another_element").live("click mousedown", handler)
  2595. $("#another_element").trigger("click")
  2596. t.assertEqual(1, count)
  2597. $("#another_element").die("click", handler)
  2598. $("#another_element").trigger("click")
  2599. t.assertEqual(1, count)
  2600. $("#another_element").trigger("mousedown")
  2601. t.assertEqual(2, count)
  2602. $("#another_element").die()
  2603. $("#another_element").trigger("mousedown")
  2604. t.assertEqual(2, count)
  2605. },
  2606. testOn: function(t){
  2607. var el = $('#some_element'), node = el.get(0), ret,
  2608. bindTriggered = 0, delegateTriggered = 0
  2609. ret = el.on('click', function(e){
  2610. bindTriggered++
  2611. t.assertIdentical(node, this)
  2612. })
  2613. .on({ click: function(){bindTriggered++} })
  2614. t.assertIdentical(el, ret)
  2615. ret = $(document.body).on('click', 'div', function(e){
  2616. delegateTriggered++
  2617. t.assertIdentical(node, this)
  2618. })
  2619. .on({ click: function(){delegateTriggered++} }, '*[id^=some]')
  2620. t.assertIdentical(document.body, ret.get(0))
  2621. click(node)
  2622. t.assertEqual(2, bindTriggered, "bind handlers")
  2623. t.assertEqual(2, delegateTriggered, "delegate handlers")
  2624. },
  2625. testOnReturnFalse: function(t){
  2626. var el = $('#some_element')
  2627. el.on('click', function(){
  2628. setTimeout(function(){
  2629. t.resume(function(){})
  2630. }, 10)
  2631. t.assert(true, "should have been called")
  2632. return false
  2633. })
  2634. $(document.body).on('click', function(){
  2635. t.refute(true, "the event should have been stopped")
  2636. })
  2637. t.pause()
  2638. click(el.get(0))
  2639. },
  2640. testOff: function(t){
  2641. var el = $('#some_element'), bindTriggered = 0, delegateTriggered = 0,
  2642. handler = function(){ bindTriggered++ }
  2643. el.bind('click', handler).bind('click', function(){ bindTriggered++ })
  2644. el.live('click', function(){ delegateTriggered++ })
  2645. click(el.get(0))
  2646. t.assertEqual(2, bindTriggered, "bind handlers before unbind")
  2647. t.assertEqual(1, delegateTriggered, "delegate handlers before unbind")
  2648. el.off('click', handler)
  2649. $(document.body).off('click', '#some_element')
  2650. click(el.get(0))
  2651. t.assertEqual(3, bindTriggered, "bind handlers")
  2652. t.assertEqual(1, delegateTriggered, "delegate handlers")
  2653. },
  2654. testOne: function(t){
  2655. var counter = 0, context, received, el = $('#some_element')
  2656. $(document.body).one('click', function(e, arg, more){
  2657. context = this
  2658. counter++
  2659. received = arg + more
  2660. t.assertIn('preventDefault', e)
  2661. return false
  2662. })
  2663. var evt = $.Event('click')
  2664. el.trigger(evt, ['one', 'two'])
  2665. t.assertEqual(1, counter)
  2666. t.assertEqual('onetwo', received)
  2667. t.assertIdentical(document.body, context)
  2668. t.assertTrue(evt.isDefaultPrevented())
  2669. el.trigger('click')
  2670. t.assertEqual(1, counter, "the event handler didn't unbind itself")
  2671. },
  2672. testOneWithObject: function(t){
  2673. var counter = 0, el = $('#some_element')
  2674. $(document.body).one({
  2675. click: function() { counter++ },
  2676. custom: function() { counter-- }
  2677. })
  2678. el.trigger('click')
  2679. t.assertEqual(1, counter)
  2680. el.trigger('click')
  2681. t.assertEqual(1, counter)
  2682. el.trigger('custom')
  2683. t.assertEqual(0, counter)
  2684. el.trigger('custom')
  2685. t.assertEqual(0, counter)
  2686. },
  2687. testDOMEventWrappers: function(t){
  2688. var events = ('blur focus focusin focusout load resize scroll unload click dblclick '+
  2689. 'mousedown mouseup mousemove mouseover mouseout '+
  2690. 'change select keydown keypress keyup error').split(' ')
  2691. var el = $('#another_element'), count = 0
  2692. events.forEach(function(event){
  2693. t.assertTrue($.isFunction(el[event]), 'event type: ' + event)
  2694. })
  2695. el.click(function(){ count++ })
  2696. click(el.get(0))
  2697. t.assertEqual(1, count)
  2698. },
  2699. testCustomEvents: function (t) {
  2700. var el = $(document.body)
  2701. el.bind('custom', function(evt, a, b) {
  2702. t.assertEqual(a, 1)
  2703. t.assertEqual(b, 2)
  2704. el.unbind()
  2705. })
  2706. el.trigger('custom', [1, 2])
  2707. el.bind('custom', function(evt, a) {
  2708. t.assertEqual(a, 1)
  2709. el.unbind()
  2710. })
  2711. el.trigger('custom', 1)
  2712. var eventData = {z: 1}
  2713. el.bind('custom', function(evt, a) {
  2714. t.assertEqual(a, eventData)
  2715. el.unbind()
  2716. })
  2717. el.trigger('custom', eventData)
  2718. },
  2719. testSpecialEvent: function (t) {
  2720. var clickEvent = $.Event('click'),
  2721. mouseDownEvent = $.Event('mousedown'),
  2722. mouseUpEvent = $.Event('mouseup'),
  2723. mouseMoveEvent = $.Event('mousemove'),
  2724. submitEvent = $.Event('submit')
  2725. t.assertEqual(MouseEvent, clickEvent.constructor)
  2726. t.assertEqual(MouseEvent, mouseDownEvent.constructor)
  2727. t.assertEqual(MouseEvent, mouseUpEvent.constructor)
  2728. t.assertEqual(MouseEvent, mouseMoveEvent.constructor)
  2729. t.assertEqual(Event, submitEvent.constructor)
  2730. }
  2731. })
  2732. })()
  2733. </script>
  2734. </body>
  2735. </html>