⌈⌋ ⎇ branch:  Bitrhythm


Hex Artifact Content

Artifact f85154c92b754efc9cfd3da612dcd6113b02d9d6cba5728b33ed57b39c80eb90:


0000: 63 6f 6e 73 74 20 65 78 70 65 63 74 20 3d 20 63  const expect = c
0010: 68 61 69 2e 65 78 70 65 63 74 0a 0a 65 78 70 6f  hai.expect..expo
0020: 72 74 20 63 6f 6e 73 74 20 49 45 5f 56 45 52 53  rt const IE_VERS
0030: 49 4f 4e 20 3d 20 28 77 69 6e 64 6f 77 20 26 26  ION = (window &&
0040: 20 77 69 6e 64 6f 77 2e 64 6f 63 75 6d 65 6e 74   window.document
0050: 20 7c 7c 20 7b 7d 29 2e 64 6f 63 75 6d 65 6e 74   || {}).document
0060: 4d 6f 64 65 20 7c 20 30 0a 0a 2f 2f 20 74 68 69  Mode | 0..// thi
0070: 73 20 65 78 70 6f 72 74 20 66 75 6e 63 74 69 6f  s export functio
0080: 6e 20 69 73 20 6e 65 65 64 65 64 20 74 6f 20 72  n is needed to r
0090: 75 6e 20 74 68 65 20 74 65 73 74 73 20 61 6c 73  un the tests als
00a0: 6f 20 6f 6e 20 69 65 38 0a 2f 2f 20 69 65 38 20  o on ie8.// ie8 
00b0: 72 65 74 75 72 6e 73 20 73 6f 6d 65 20 77 65 69  returns some wei
00c0: 72 64 20 73 74 72 69 6e 67 73 20 77 68 65 6e 20  rd strings when 
00d0: 77 65 20 74 72 79 20 74 6f 20 67 65 74 20 74 68  we try to get th
00e0: 65 20 69 6e 6e 65 72 48 54 4d 4c 20 6f 66 20 61  e innerHTML of a
00f0: 20 74 61 67 0a 65 78 70 6f 72 74 20 66 75 6e 63   tag.export func
0100: 74 69 6f 6e 20 6e 6f 72 6d 61 6c 69 7a 65 48 54  tion normalizeHT
0110: 4d 4c 28 68 74 6d 6c 29 20 7b 0a 20 20 72 65 74  ML(html) {.  ret
0120: 75 72 6e 20 68 74 6d 6c 0a 20 20 20 20 2e 74 72  urn html.    .tr
0130: 69 6d 28 29 0a 20 20 20 20 2f 2f 20 63 68 61 6e  im().    // chan
0140: 67 65 20 61 6c 6c 20 74 68 65 20 74 61 67 73 20  ge all the tags 
0150: 70 72 6f 70 65 72 74 69 65 73 20 61 6e 64 20 6e  properties and n
0160: 61 6d 65 73 20 74 6f 20 6c 6f 77 65 72 63 61 73  ames to lowercas
0170: 65 20 62 65 63 61 75 73 65 20 61 20 3c 6c 69 3e  e because a <li>
0180: 20 66 6f 72 20 69 65 38 20 69 73 20 61 20 3c 4c   for ie8 is a <L
0190: 49 3e 0a 20 20 20 20 2e 72 65 70 6c 61 63 65 28  I>.    .replace(
01a0: 2f 3c 28 5b 5e 3e 5d 2a 29 3e 2f 67 2c 20 66 75  /<([^>]*)>/g, fu
01b0: 6e 63 74 69 6f 6e 28 74 61 67 29 20 7b 20 72 65  nction(tag) { re
01c0: 74 75 72 6e 20 74 61 67 2e 74 6f 4c 6f 77 65 72  turn tag.toLower
01d0: 43 61 73 65 28 29 20 7d 29 0a 20 20 20 20 2e 72  Case() }).    .r
01e0: 65 70 6c 61 63 65 28 2f 5b 5c 72 5c 6e 5c 74 5d  eplace(/[\r\n\t]
01f0: 2b 2f 67 2c 20 27 27 29 0a 20 20 20 20 2e 72 65  +/g, '').    .re
0200: 70 6c 61 63 65 28 2f 3e 5c 73 2b 3c 2f 67 2c 20  place(/>\s+</g, 
0210: 27 3e 3c 27 29 0a 7d 0a 0a 65 78 70 6f 72 74 20  '><').}..export 
0220: 66 75 6e 63 74 69 6f 6e 20 65 78 70 65 63 74 48  function expectH
0230: 54 4d 4c 28 74 61 67 4f 72 44 6f 6d 29 20 7b 0a  TML(tagOrDom) {.
0240: 20 20 76 61 72 20 64 6f 6d 20 3d 20 74 61 67 4f    var dom = tagO
0250: 72 44 6f 6d 2e 72 6f 6f 74 20 3f 20 74 61 67 4f  rDom.root ? tagO
0260: 72 44 6f 6d 2e 72 6f 6f 74 20 3a 20 74 61 67 4f  rDom.root : tagO
0270: 72 44 6f 6d 0a 20 20 72 65 74 75 72 6e 20 65 78  rDom.  return ex
0280: 70 65 63 74 28 6e 6f 72 6d 61 6c 69 7a 65 48 54  pect(normalizeHT
0290: 4d 4c 28 64 6f 6d 2e 69 6e 6e 65 72 48 54 4d 4c  ML(dom.innerHTML
02a0: 29 29 0a 7d 0a 0a 65 78 70 6f 72 74 20 66 75 6e  )).}..export fun
02b0: 63 74 69 6f 6e 20 67 65 74 50 72 65 76 69 6f 75  ction getPreviou
02c0: 73 53 69 62 6c 69 6e 67 28 6e 29 20 7b 0a 20 20  sSibling(n) {.  
02d0: 76 61 72 20 78 20 3d 20 6e 2e 70 72 65 76 69 6f  var x = n.previo
02e0: 75 73 53 69 62 6c 69 6e 67 0a 20 20 77 68 69 6c  usSibling.  whil
02f0: 65 20 28 78 2e 6e 6f 64 65 54 79 70 65 20 21 3d  e (x.nodeType !=
0300: 3d 20 31 29 20 7b 0a 20 20 20 20 78 20 3d 20 78  = 1) {.    x = x
0310: 2e 70 72 65 76 69 6f 75 73 53 69 62 6c 69 6e 67  .previousSibling
0320: 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 78 0a  .  }.  return x.
0330: 7d 0a 0a 65 78 70 6f 72 74 20 66 75 6e 63 74 69  }..export functi
0340: 6f 6e 20 67 65 74 4e 65 78 74 53 69 62 6c 69 6e  on getNextSiblin
0350: 67 28 6e 29 20 7b 0a 20 20 76 61 72 20 78 20 3d  g(n) {.  var x =
0360: 20 6e 2e 70 72 65 76 69 6f 75 73 53 69 62 6c 69   n.previousSibli
0370: 6e 67 0a 20 20 77 68 69 6c 65 20 28 78 2e 6e 6f  ng.  while (x.no
0380: 64 65 54 79 70 65 20 21 3d 3d 20 31 29 20 7b 0a  deType !== 1) {.
0390: 20 20 20 20 78 20 3d 20 78 2e 70 72 65 76 69 6f      x = x.previo
03a0: 75 73 53 69 62 6c 69 6e 67 0a 20 20 7d 0a 20 20  usSibling.  }.  
03b0: 72 65 74 75 72 6e 20 78 0a 7d 0a 0a 65 78 70 6f  return x.}..expo
03c0: 72 74 20 66 75 6e 63 74 69 6f 6e 20 67 65 74 52  rt function getR
03d0: 69 6f 74 53 74 79 6c 65 73 28 72 69 6f 74 29 20  iotStyles(riot) 
03e0: 7b 0a 20 20 2f 2f 20 75 74 69 6c 2e 69 6e 6a 65  {.  // util.inje
03f0: 63 74 53 74 79 6c 65 20 6d 75 73 74 20 61 64 64  ctStyle must add
0400: 20 3c 73 74 79 6c 65 3e 20 69 6e 20 68 65 61 64   <style> in head
0410: 2c 20 6e 6f 74 20 69 6e 20 62 6f 64 79 20 2d 2d  , not in body --
0420: 20 63 6f 72 72 65 63 74 65 64 0a 20 20 76 61 72   corrected.  var
0430: 20 73 74 61 67 20 3d 20 72 69 6f 74 2e 75 74 69   stag = riot.uti
0440: 6c 2e 73 74 79 6c 65 4e 6f 64 65 20 7c 7c 20 64  l.styleNode || d
0450: 6f 63 75 6d 65 6e 74 2e 71 75 65 72 79 53 65 6c  ocument.querySel
0460: 65 63 74 6f 72 28 27 73 74 79 6c 65 27 29 0a 20  ector('style'). 
0470: 20 72 65 74 75 72 6e 20 6e 6f 72 6d 61 6c 69 7a   return normaliz
0480: 65 48 54 4d 4c 28 73 74 61 67 2e 73 74 79 6c 65  eHTML(stag.style
0490: 53 68 65 65 74 20 3f 20 73 74 61 67 2e 73 74 79  Sheet ? stag.sty
04a0: 6c 65 53 68 65 65 74 2e 63 73 73 54 65 78 74 20  leSheet.cssText 
04b0: 3a 20 73 74 61 67 2e 69 6e 6e 65 72 48 54 4d 4c  : stag.innerHTML
04c0: 29 0a 7d 0a 0a 2f 2a 2a 0a 20 2a 20 53 68 6f 72  ).}../**. * Shor
04d0: 74 65 72 20 61 6e 64 20 66 61 73 74 20 77 61 79  ter and fast way
04e0: 20 74 6f 20 73 65 6c 65 63 74 20 6d 75 6c 74 69   to select multi
04f0: 70 6c 65 20 6e 6f 64 65 73 20 69 6e 20 74 68 65  ple nodes in the
0500: 20 44 4f 4d 0a 20 2a 20 40 70 61 72 61 6d 20 20   DOM. * @param  
0510: 20 7b 20 53 74 72 69 6e 67 20 7d 20 73 65 6c 65   { String } sele
0520: 63 74 6f 72 20 2d 20 44 4f 4d 20 73 65 6c 65 63  ctor - DOM selec
0530: 74 6f 72 0a 20 2a 20 40 70 61 72 61 6d 20 20 20  tor. * @param   
0540: 7b 20 4f 62 6a 65 63 74 20 7d 20 63 74 78 20 2d  { Object } ctx -
0550: 20 44 4f 4d 20 6e 6f 64 65 20 77 68 65 72 65 20   DOM node where 
0560: 74 68 65 20 74 61 72 67 65 74 73 20 6f 66 20 6f  the targets of o
0570: 75 72 20 73 65 61 72 63 68 20 77 69 6c 6c 20 69  ur search will i
0580: 73 20 6c 6f 63 61 74 65 64 0a 20 2a 20 40 72 65  s located. * @re
0590: 74 75 72 6e 73 20 7b 20 4f 62 6a 65 63 74 20 7d  turns { Object }
05a0: 20 64 6f 6d 20 6e 6f 64 65 73 20 66 6f 75 6e 64   dom nodes found
05b0: 0a 20 2a 2f 0a 65 78 70 6f 72 74 20 66 75 6e 63  . */.export func
05c0: 74 69 6f 6e 20 24 24 28 29 20 7b 0a 20 20 72 65  tion $$() {.  re
05d0: 74 75 72 6e 20 72 69 6f 74 2e 75 74 69 6c 2e 64  turn riot.util.d
05e0: 6f 6d 2e 24 24 2e 61 70 70 6c 79 28 74 68 69 73  om.$$.apply(this
05f0: 2c 20 61 72 67 75 6d 65 6e 74 73 29 0a 7d 0a 0a  , arguments).}..
0600: 2f 2a 2a 0a 20 2a 20 53 68 6f 72 74 65 72 20 61  /**. * Shorter a
0610: 6e 64 20 66 61 73 74 20 77 61 79 20 74 6f 20 73  nd fast way to s
0620: 65 6c 65 63 74 20 61 20 73 69 6e 67 6c 65 20 6e  elect a single n
0630: 6f 64 65 20 69 6e 20 74 68 65 20 44 4f 4d 0a 20  ode in the DOM. 
0640: 2a 20 40 70 61 72 61 6d 20 20 20 7b 20 53 74 72  * @param   { Str
0650: 69 6e 67 20 7d 20 73 65 6c 65 63 74 6f 72 20 2d  ing } selector -
0660: 20 75 6e 69 71 75 65 20 64 6f 6d 20 73 65 6c 65   unique dom sele
0670: 63 74 6f 72 0a 20 2a 20 40 70 61 72 61 6d 20 20  ctor. * @param  
0680: 20 7b 20 4f 62 6a 65 63 74 20 7d 20 63 74 78 20   { Object } ctx 
0690: 2d 20 44 4f 4d 20 6e 6f 64 65 20 77 68 65 72 65  - DOM node where
06a0: 20 74 68 65 20 74 61 72 67 65 74 20 6f 66 20 6f   the target of o
06b0: 75 72 20 73 65 61 72 63 68 20 77 69 6c 6c 20 69  ur search will i
06c0: 73 20 6c 6f 63 61 74 65 64 0a 20 2a 20 40 72 65  s located. * @re
06d0: 74 75 72 6e 73 20 7b 20 4f 62 6a 65 63 74 20 7d  turns { Object }
06e0: 20 64 6f 6d 20 6e 6f 64 65 20 66 6f 75 6e 64 0a   dom node found.
06f0: 20 2a 2f 0a 65 78 70 6f 72 74 20 66 75 6e 63 74   */.export funct
0700: 69 6f 6e 20 24 28 29 20 7b 0a 20 20 72 65 74 75  ion $() {.  retu
0710: 72 6e 20 72 69 6f 74 2e 75 74 69 6c 2e 64 6f 6d  rn riot.util.dom
0720: 2e 24 2e 61 70 70 6c 79 28 74 68 69 73 2c 20 61  .$.apply(this, a
0730: 72 67 75 6d 65 6e 74 73 29 0a 7d 0a 0a 0a 65 78  rguments).}...ex
0740: 70 6f 72 74 20 66 75 6e 63 74 69 6f 6e 20 69 6e  port function in
0750: 6a 65 63 74 48 54 4d 4c 28 68 74 6d 6c 29 20 7b  jectHTML(html) {
0760: 0a 20 20 76 61 72 20 64 69 76 20 3d 20 64 6f 63  .  var div = doc
0770: 75 6d 65 6e 74 2e 63 72 65 61 74 65 45 6c 65 6d  ument.createElem
0780: 65 6e 74 28 27 64 69 76 27 29 0a 20 20 64 69 76  ent('div').  div
0790: 2e 69 6e 6e 65 72 48 54 4d 4c 20 3d 20 68 74 6d  .innerHTML = htm
07a0: 6c 20 69 6e 73 74 61 6e 63 65 6f 66 20 41 72 72  l instanceof Arr
07b0: 61 79 20 3f 20 68 74 6d 6c 2e 6a 6f 69 6e 28 27  ay ? html.join('
07c0: 5c 6e 27 29 20 3a 20 68 74 6d 6c 0a 20 20 77 68  \n') : html.  wh
07d0: 69 6c 65 20 28 64 69 76 2e 66 69 72 73 74 43 68  ile (div.firstCh
07e0: 69 6c 64 29 20 7b 0a 20 20 20 20 64 6f 63 75 6d  ild) {.    docum
07f0: 65 6e 74 2e 62 6f 64 79 2e 61 70 70 65 6e 64 43  ent.body.appendC
0800: 68 69 6c 64 28 64 69 76 2e 66 69 72 73 74 43 68  hild(div.firstCh
0810: 69 6c 64 29 0a 20 20 7d 0a 7d 0a 0a 65 78 70 6f  ild).  }.}..expo
0820: 72 74 20 66 75 6e 63 74 69 6f 6e 20 67 65 74 43  rt function getC
0830: 61 72 72 6f 74 50 6f 73 28 64 6f 6d 29 20 7b 0a  arrotPos(dom) {.
0840: 20 20 69 66 20 28 64 6f 6d 2e 73 65 6c 65 63 74    if (dom.select
0850: 69 6f 6e 53 74 61 72 74 20 21 3d 3d 20 6e 75 6c  ionStart !== nul
0860: 6c 29 0a 20 20 20 20 72 65 74 75 72 6e 20 64 6f  l).    return do
0870: 6d 2e 73 65 6c 65 63 74 69 6f 6e 53 74 61 72 74  m.selectionStart
0880: 0a 0a 20 20 69 66 20 28 64 6f 63 75 6d 65 6e 74  ..  if (document
0890: 2e 73 65 6c 65 63 74 69 6f 6e 20 3d 3d 3d 20 6e  .selection === n
08a0: 75 6c 6c 29 0a 20 20 20 20 72 65 74 75 72 6e 20  ull).    return 
08b0: 6e 75 6c 6c 0a 0a 20 20 76 61 72 20 72 61 6e 67  null..  var rang
08c0: 65 20 3d 20 64 6f 63 75 6d 65 6e 74 2e 73 65 6c  e = document.sel
08d0: 65 63 74 69 6f 6e 2e 63 72 65 61 74 65 52 61 6e  ection.createRan
08e0: 67 65 28 29 0a 20 20 72 61 6e 67 65 2e 6d 6f 76  ge().  range.mov
08f0: 65 53 74 61 72 74 28 27 63 68 61 72 61 63 74 65  eStart('characte
0900: 72 27 2c 20 2d 64 6f 6d 2e 76 61 6c 75 65 2e 6c  r', -dom.value.l
0910: 65 6e 67 74 68 29 0a 20 20 72 65 74 75 72 6e 20  ength).  return 
0920: 72 61 6e 67 65 2e 74 65 78 74 2e 6c 65 6e 67 74  range.text.lengt
0930: 68 0a 7d 0a 0a 65 78 70 6f 72 74 20 66 75 6e 63  h.}..export func
0940: 74 69 6f 6e 20 73 65 74 43 61 72 72 6f 74 50 6f  tion setCarrotPo
0950: 73 28 64 6f 6d 2c 20 70 6f 73 29 20 7b 0a 20 20  s(dom, pos) {.  
0960: 69 66 20 28 64 6f 6d 2e 73 65 74 53 65 6c 65 63  if (dom.setSelec
0970: 74 69 6f 6e 52 61 6e 67 65 20 21 3d 3d 20 6e 75  tionRange !== nu
0980: 6c 6c 29 20 7b 0a 20 20 20 20 64 6f 6d 2e 73 65  ll) {.    dom.se
0990: 74 53 65 6c 65 63 74 69 6f 6e 52 61 6e 67 65 28  tSelectionRange(
09a0: 70 6f 73 2c 20 70 6f 73 29 0a 20 20 20 20 72 65  pos, pos).    re
09b0: 74 75 72 6e 0a 20 20 7d 0a 0a 20 20 76 61 72 20  turn.  }..  var 
09c0: 72 61 6e 67 65 20 3d 20 64 6f 6d 2e 63 72 65 61  range = dom.crea
09d0: 74 65 54 65 78 74 52 61 6e 67 65 28 29 0a 20 20  teTextRange().  
09e0: 72 61 6e 67 65 2e 63 6f 6c 6c 61 70 73 65 28 74  range.collapse(t
09f0: 72 75 65 29 0a 20 20 72 61 6e 67 65 2e 6d 6f 76  rue).  range.mov
0a00: 65 45 6e 64 28 27 63 68 61 72 61 63 74 65 72 27  eEnd('character'
0a10: 2c 20 70 6f 73 29 0a 20 20 72 61 6e 67 65 2e 6d  , pos).  range.m
0a20: 6f 76 65 53 74 61 72 74 28 27 63 68 61 72 61 63  oveStart('charac
0a30: 74 65 72 27 2c 20 70 6f 73 29 0a 20 20 72 61 6e  ter', pos).  ran
0a40: 67 65 2e 73 65 6c 65 63 74 28 29 0a 7d 0a 0a 65  ge.select().}..e
0a50: 78 70 6f 72 74 20 66 75 6e 63 74 69 6f 6e 20 66  xport function f
0a60: 69 72 65 45 76 65 6e 74 28 65 6c 2c 20 6e 61 6d  ireEvent(el, nam
0a70: 65 29 20 7b 0a 20 20 76 61 72 20 65 20 3d 20 64  e) {.  var e = d
0a80: 6f 63 75 6d 65 6e 74 2e 63 72 65 61 74 65 45 76  ocument.createEv
0a90: 65 6e 74 28 27 48 54 4d 4c 45 76 65 6e 74 73 27  ent('HTMLEvents'
0aa0: 29 0a 20 20 65 2e 69 6e 69 74 45 76 65 6e 74 28  ).  e.initEvent(
0ab0: 6e 61 6d 65 2c 20 66 61 6c 73 65 2c 20 74 72 75  name, false, tru
0ac0: 65 29 0a 20 20 65 6c 2e 64 69 73 70 61 74 63 68  e).  el.dispatch
0ad0: 45 76 65 6e 74 28 65 29 0a 7d 0a                 Event(e).}.