tooltip.js 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710
  1. $(function () {
  2. 'use strict';
  3. QUnit.module('tooltip plugin')
  4. QUnit.test('should be defined on jquery object', function (assert) {
  5. assert.expect(1)
  6. assert.ok($(document.body).tooltip, 'tooltip method is defined')
  7. })
  8. QUnit.module('tooltip', {
  9. beforeEach: function () {
  10. // Run all tests in noConflict mode -- it's the only way to ensure that the plugin works in noConflict mode
  11. $.fn.bootstrapTooltip = $.fn.tooltip.noConflict()
  12. },
  13. afterEach: function () {
  14. $.fn.tooltip = $.fn.bootstrapTooltip
  15. delete $.fn.bootstrapTooltip
  16. $('#qunit-fixture').html('')
  17. }
  18. })
  19. QUnit.test('should provide no conflict', function (assert) {
  20. assert.expect(1)
  21. assert.strictEqual($.fn.tooltip, undefined, 'tooltip was set back to undefined (org value)')
  22. })
  23. QUnit.test('should return jquery collection containing the element', function (assert) {
  24. assert.expect(2)
  25. var $el = $('<div/>').appendTo('#qunit-fixture')
  26. var $tooltip = $el.bootstrapTooltip()
  27. assert.ok($tooltip instanceof $, 'returns jquery collection')
  28. assert.strictEqual($tooltip[0], $el[0], 'collection contains element')
  29. })
  30. QUnit.test('should expose default settings', function (assert) {
  31. assert.expect(1)
  32. assert.ok($.fn.bootstrapTooltip.Constructor.DEFAULTS, 'defaults is defined')
  33. })
  34. QUnit.test('should empty title attribute', function (assert) {
  35. assert.expect(1)
  36. var $trigger = $('<a href="#" rel="tooltip" title="Another tooltip"/>')
  37. .appendTo('#qunit-fixture')
  38. .bootstrapTooltip()
  39. assert.strictEqual($trigger.attr('title'), '', 'title attribute was emptied')
  40. })
  41. QUnit.test('should add data attribute for referencing original title', function (assert) {
  42. assert.expect(1)
  43. var $trigger = $('<a href="#" rel="tooltip" title="Another tooltip"/>')
  44. .appendTo('#qunit-fixture')
  45. .bootstrapTooltip()
  46. assert.strictEqual($trigger.attr('data-original-title'), 'Another tooltip', 'original title preserved in data attribute')
  47. })
  48. QUnit.test('should add aria-describedby to the trigger on show', function (assert) {
  49. assert.expect(3)
  50. var done = assert.async()
  51. var $trigger = $('<a href="#" rel="tooltip" title="Another tooltip"/>')
  52. .appendTo('#qunit-fixture')
  53. $trigger
  54. .bootstrapTooltip()
  55. .on('shown.bs.tooltip', function () {
  56. var id = $('.tooltip').attr('id')
  57. assert.strictEqual($('#' + id).length, 1, 'has a unique id')
  58. assert.strictEqual($('.tooltip').attr('aria-describedby'), $trigger.attr('id'), 'tooltip id and aria-describedby on trigger match')
  59. assert.ok($trigger[0].hasAttribute('aria-describedby'), 'trigger has aria-describedby')
  60. done()
  61. })
  62. .bootstrapTooltip('show')
  63. })
  64. QUnit.test('should remove aria-describedby from trigger on hide', function (assert) {
  65. assert.expect(2)
  66. var done = assert.async()
  67. var $trigger = $('<a href="#" rel="tooltip" title="Another tooltip"/>')
  68. .bootstrapTooltip()
  69. .appendTo('#qunit-fixture')
  70. $trigger.one('shown.bs.tooltip', function () {
  71. assert.ok($trigger[0].hasAttribute('aria-describedby'), 'trigger has aria-describedby')
  72. $trigger.bootstrapTooltip('hide')
  73. })
  74. .one('hidden.bs.tooltip', function () {
  75. assert.ok(!$trigger[0].hasAttribute('aria-describedby'), 'trigger does not have aria-describedby')
  76. done()
  77. })
  78. .bootstrapTooltip('show')
  79. })
  80. QUnit.test('should assign a unique id tooltip element', function (assert) {
  81. assert.expect(2)
  82. var done = assert.async()
  83. $('<a href="#" rel="tooltip" title="Another tooltip"/>')
  84. .appendTo('#qunit-fixture')
  85. .on('shown.bs.tooltip', function () {
  86. var id = $('.tooltip').attr('id')
  87. assert.strictEqual($('#' + id).length, 1, 'tooltip has unique id')
  88. assert.strictEqual(id.indexOf('tooltip'), 0, 'tooltip id has prefix')
  89. done()
  90. })
  91. .bootstrapTooltip('show')
  92. })
  93. QUnit.test('should place tooltips relative to placement option', function (assert) {
  94. assert.expect(2)
  95. var done = assert.async()
  96. var $tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"/>')
  97. .appendTo('#qunit-fixture')
  98. .on('shown.bs.tooltip', function () {
  99. assert.ok($('.tooltip').is('.fade.bottom.in'), 'has correct classes applied')
  100. $tooltip.bootstrapTooltip('hide')
  101. })
  102. .on('hidden.bs.tooltip', function () {
  103. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed')
  104. done()
  105. })
  106. .bootstrapTooltip({ placement: 'bottom' })
  107. $tooltip.bootstrapTooltip('show')
  108. })
  109. QUnit.test('should allow html entities', function (assert) {
  110. assert.expect(2)
  111. var done = assert.async()
  112. var $tooltip = $('<a href="#" rel="tooltip" title="&lt;b&gt;@fat&lt;/b&gt;"/>')
  113. .appendTo('#qunit-fixture')
  114. .bootstrapTooltip({ html: true })
  115. $tooltip.one('shown.bs.tooltip', function () {
  116. assert.notEqual($('.tooltip b').length, 0, 'b tag was inserted')
  117. $tooltip.bootstrapTooltip('hide')
  118. })
  119. .one('hidden.bs.tooltip', function () {
  120. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed')
  121. done()
  122. })
  123. .bootstrapTooltip('show')
  124. })
  125. QUnit.test('should respect custom classes', function (assert) {
  126. assert.expect(2)
  127. var done = assert.async()
  128. var $tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"/>')
  129. .appendTo('#qunit-fixture')
  130. .bootstrapTooltip({ template: '<div class="tooltip some-class"><div class="tooltip-arrow"/><div class="tooltip-inner"/></div>' })
  131. $tooltip.one('shown.bs.tooltip', function () {
  132. assert.ok($('.tooltip').hasClass('some-class'), 'custom class is present')
  133. $tooltip.bootstrapTooltip('hide')
  134. })
  135. .one('hidden.bs.tooltip', function () {
  136. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed')
  137. done()
  138. })
  139. .bootstrapTooltip('show')
  140. })
  141. QUnit.test('should fire show event', function (assert) {
  142. assert.expect(1)
  143. var done = assert.async()
  144. $('<div title="tooltip title"/>')
  145. .on('show.bs.tooltip', function () {
  146. assert.ok(true, 'show event fired')
  147. done()
  148. })
  149. .bootstrapTooltip('show')
  150. })
  151. QUnit.test('should fire inserted event', function (assert) {
  152. assert.expect(2)
  153. var done = assert.async()
  154. $('<div title="tooltip title"/>')
  155. .appendTo('#qunit-fixture')
  156. .on('inserted.bs.tooltip', function () {
  157. assert.notEqual($('.tooltip').length, 0, 'tooltip was inserted')
  158. assert.ok(true, 'inserted event fired')
  159. done()
  160. })
  161. .bootstrapTooltip('show')
  162. })
  163. QUnit.test('should fire shown event', function (assert) {
  164. assert.expect(1)
  165. var done = assert.async()
  166. $('<div title="tooltip title"></div>')
  167. .appendTo('#qunit-fixture')
  168. .on('shown.bs.tooltip', function () {
  169. assert.ok(true, 'shown was called')
  170. done()
  171. })
  172. .bootstrapTooltip('show')
  173. })
  174. QUnit.test('should not fire shown event when show was prevented', function (assert) {
  175. assert.expect(1)
  176. var done = assert.async()
  177. $('<div title="tooltip title"/>')
  178. .on('show.bs.tooltip', function (e) {
  179. e.preventDefault()
  180. assert.ok(true, 'show event fired')
  181. done()
  182. })
  183. .on('shown.bs.tooltip', function () {
  184. assert.ok(false, 'shown event fired')
  185. })
  186. .bootstrapTooltip('show')
  187. })
  188. QUnit.test('should fire hide event', function (assert) {
  189. assert.expect(1)
  190. var done = assert.async()
  191. $('<div title="tooltip title"/>')
  192. .appendTo('#qunit-fixture')
  193. .on('shown.bs.tooltip', function () {
  194. $(this).bootstrapTooltip('hide')
  195. })
  196. .on('hide.bs.tooltip', function () {
  197. assert.ok(true, 'hide event fired')
  198. done()
  199. })
  200. .bootstrapTooltip('show')
  201. })
  202. QUnit.test('should fire hidden event', function (assert) {
  203. assert.expect(1)
  204. var done = assert.async()
  205. $('<div title="tooltip title"/>')
  206. .appendTo('#qunit-fixture')
  207. .on('shown.bs.tooltip', function () {
  208. $(this).bootstrapTooltip('hide')
  209. })
  210. .on('hidden.bs.tooltip', function () {
  211. assert.ok(true, 'hidden event fired')
  212. done()
  213. })
  214. .bootstrapTooltip('show')
  215. })
  216. QUnit.test('should not fire hidden event when hide was prevented', function (assert) {
  217. assert.expect(1)
  218. var done = assert.async()
  219. $('<div title="tooltip title"/>')
  220. .appendTo('#qunit-fixture')
  221. .on('shown.bs.tooltip', function () {
  222. $(this).bootstrapTooltip('hide')
  223. })
  224. .on('hide.bs.tooltip', function (e) {
  225. e.preventDefault()
  226. assert.ok(true, 'hide event fired')
  227. done()
  228. })
  229. .on('hidden.bs.tooltip', function () {
  230. assert.ok(false, 'hidden event fired')
  231. })
  232. .bootstrapTooltip('show')
  233. })
  234. QUnit.test('should destroy tooltip', function (assert) {
  235. assert.expect(7)
  236. var $tooltip = $('<div/>')
  237. .bootstrapTooltip()
  238. .on('click.foo', function () {})
  239. assert.ok($tooltip.data('bs.tooltip'), 'tooltip has data')
  240. assert.ok($._data($tooltip[0], 'events').mouseover && $._data($tooltip[0], 'events').mouseout, 'tooltip has hover events')
  241. assert.strictEqual($._data($tooltip[0], 'events').click[0].namespace, 'foo', 'tooltip has extra click.foo event')
  242. $tooltip.bootstrapTooltip('show')
  243. $tooltip.bootstrapTooltip('destroy')
  244. assert.ok(!$tooltip.hasClass('in'), 'tooltip is hidden')
  245. assert.ok(!$._data($tooltip[0], 'bs.tooltip'), 'tooltip does not have data')
  246. assert.strictEqual($._data($tooltip[0], 'events').click[0].namespace, 'foo', 'tooltip still has click.foo')
  247. assert.ok(!$._data($tooltip[0], 'events').mouseover && !$._data($tooltip[0], 'events').mouseout, 'tooltip does not have hover events')
  248. })
  249. QUnit.test('should show tooltip with delegate selector on click', function (assert) {
  250. assert.expect(2)
  251. var done = assert.async()
  252. var $div = $('<div><a href="#" rel="tooltip" title="Another tooltip"/></div>')
  253. .appendTo('#qunit-fixture')
  254. .bootstrapTooltip({
  255. selector: 'a[rel="tooltip"]',
  256. trigger: 'click'
  257. })
  258. $div.one('shown.bs.tooltip', function () {
  259. assert.ok($('.tooltip').is('.fade.in'), 'tooltip is faded in')
  260. $div.find('a').trigger('click')
  261. })
  262. .one('hidden.bs.tooltip', function () {
  263. assert.strictEqual($('.tooltip').length, 0, 'tooltip was removed from dom')
  264. done()
  265. })
  266. $div.find('a').trigger('click')
  267. })
  268. QUnit.test('should show tooltip when toggle is called', function (assert) {
  269. assert.expect(1)
  270. $('<a href="#" rel="tooltip" title="tooltip on toggle"/>')
  271. .appendTo('#qunit-fixture')
  272. .bootstrapTooltip({ trigger: 'manual' })
  273. .bootstrapTooltip('toggle')
  274. assert.ok($('.tooltip').is('.fade.in'), 'tooltip is faded in')
  275. })
  276. QUnit.test('should hide previously shown tooltip when toggle is called on tooltip', function (assert) {
  277. assert.expect(1)
  278. $('<a href="#" rel="tooltip" title="tooltip on toggle">@ResentedHook</a>')
  279. .appendTo('#qunit-fixture')
  280. .bootstrapTooltip({ trigger: 'manual' })
  281. .bootstrapTooltip('show')
  282. $('.tooltip').bootstrapTooltip('toggle')
  283. assert.ok($('.tooltip').not('.fade.in'), 'tooltip was faded out')
  284. })
  285. QUnit.test('should place tooltips inside body when container is body', function (assert) {
  286. assert.expect(3)
  287. var done = assert.async()
  288. $('<a href="#" rel="tooltip" title="Another tooltip"/>')
  289. .appendTo('#qunit-fixture')
  290. .bootstrapTooltip({ container: 'body' })
  291. .one('shown.bs.tooltip', function () {
  292. assert.notEqual($('body > .tooltip').length, 0, 'tooltip is direct descendant of body')
  293. assert.strictEqual($('#qunit-fixture > .tooltip').length, 0, 'tooltip is not in parent')
  294. $(this).bootstrapTooltip('hide')
  295. })
  296. .one('hidden.bs.tooltip', function () {
  297. assert.strictEqual($('body > .tooltip').length, 0, 'tooltip was removed from dom')
  298. done()
  299. })
  300. .bootstrapTooltip('show')
  301. })
  302. QUnit.test('should add position class before positioning so that position-specific styles are taken into account', function (assert) {
  303. assert.expect(1)
  304. var styles = '<style>'
  305. + '.tooltip.right { white-space: nowrap; }'
  306. + '.tooltip.right .tooltip-inner { max-width: none; }'
  307. + '</style>'
  308. var $styles = $(styles).appendTo('head')
  309. var $container = $('<div/>').appendTo('#qunit-fixture')
  310. var $target = $('<a href="#" rel="tooltip" title="very very very very very very very very long tooltip in one line"/>')
  311. .appendTo($container)
  312. .bootstrapTooltip({
  313. placement: 'right',
  314. viewport: null
  315. })
  316. .bootstrapTooltip('show')
  317. var $tooltip = $container.find('.tooltip')
  318. // this is some dumb hack shit because sub pixels in firefox
  319. var top = Math.round($target.offset().top + ($target[0].offsetHeight / 2) - ($tooltip[0].offsetHeight / 2))
  320. var top2 = Math.round($tooltip.offset().top)
  321. var topDiff = top - top2
  322. assert.ok(topDiff <= 1 && topDiff >= -1)
  323. $target.bootstrapTooltip('hide')
  324. $container.remove()
  325. $styles.remove()
  326. })
  327. QUnit.test('should use title attribute for tooltip text', function (assert) {
  328. assert.expect(2)
  329. var done = assert.async()
  330. $('<a href="#" rel="tooltip" title="Simple tooltip"/>')
  331. .appendTo('#qunit-fixture')
  332. .bootstrapTooltip()
  333. .one('shown.bs.tooltip', function () {
  334. assert.strictEqual($('.tooltip').children('.tooltip-inner').text(), 'Simple tooltip', 'title from title attribute is set')
  335. $(this).bootstrapTooltip('hide')
  336. })
  337. .one('hidden.bs.tooltip', function () {
  338. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
  339. done()
  340. })
  341. .bootstrapTooltip('show')
  342. })
  343. QUnit.test('should prefer title attribute over title option', function (assert) {
  344. assert.expect(2)
  345. var done = assert.async()
  346. $('<a href="#" rel="tooltip" title="Simple tooltip"/>')
  347. .appendTo('#qunit-fixture')
  348. .bootstrapTooltip({
  349. title: 'This is a tooltip with some content'
  350. })
  351. .one('shown.bs.tooltip', function () {
  352. assert.strictEqual($('.tooltip').children('.tooltip-inner').text(), 'Simple tooltip', 'title is set from title attribute while preferred over title option')
  353. $(this).bootstrapTooltip('hide')
  354. })
  355. .one('hidden.bs.tooltip', function () {
  356. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
  357. done()
  358. })
  359. .bootstrapTooltip('show')
  360. })
  361. QUnit.test('should use title option', function (assert) {
  362. assert.expect(2)
  363. var done = assert.async()
  364. $('<a href="#" rel="tooltip"/>')
  365. .appendTo('#qunit-fixture')
  366. .bootstrapTooltip({
  367. title: 'This is a tooltip with some content'
  368. })
  369. .one('shown.bs.tooltip', function () {
  370. assert.strictEqual($('.tooltip').children('.tooltip-inner').text(), 'This is a tooltip with some content', 'title from title option is set')
  371. $(this).bootstrapTooltip('hide')
  372. })
  373. .one('hidden.bs.tooltip', function () {
  374. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
  375. done()
  376. })
  377. .bootstrapTooltip('show')
  378. })
  379. QUnit.test('should be placed dynamically to viewport with the dynamic placement option', function (assert) {
  380. assert.expect(6)
  381. var done = assert.async()
  382. var $style = $('<style> div[rel="tooltip"] { position: absolute; } #qunit-fixture { top: inherit; left: inherit } </style>').appendTo('head')
  383. var $container = $('<div/>')
  384. .css({
  385. position: 'relative',
  386. height: '100%'
  387. })
  388. .appendTo('#qunit-fixture')
  389. var $topTooltip = $('<div style="left: 0; top: 0;" rel="tooltip" title="Top tooltip">Top Dynamic Tooltip</div>')
  390. .appendTo($container)
  391. .bootstrapTooltip({ placement: 'auto', viewport: '#qunit-fixture' })
  392. var topDone = false
  393. var rightDone = false
  394. var leftDone = false
  395. function isDone() {
  396. if (topDone && rightDone && leftDone) {
  397. $container.remove()
  398. $style.remove()
  399. done()
  400. }
  401. }
  402. function leftTooltip() {
  403. var $leftTooltip = $('<div style="left: 0;" rel="tooltip" title="Left tooltip">Left Dynamic Tooltip</div>')
  404. .appendTo($container)
  405. .bootstrapTooltip({ placement: 'auto left', viewport: '#qunit-fixture' })
  406. $leftTooltip
  407. .one('shown.bs.tooltip', function () {
  408. assert.ok($('.tooltip').is('.right'), 'left positioned tooltip is dynamically positioned right')
  409. $leftTooltip.bootstrapTooltip('hide')
  410. })
  411. .one('hidden.bs.tooltip', function () {
  412. assert.strictEqual($('.tooltip').length, 0, 'left positioned tooltip removed from dom')
  413. leftDone = true
  414. isDone()
  415. })
  416. .bootstrapTooltip('show')
  417. }
  418. function rightTooltip() {
  419. var $rightTooltip = $('<div style="right: 0;" rel="tooltip" title="Right tooltip">Right Dynamic Tooltip</div>')
  420. .appendTo($container)
  421. .bootstrapTooltip({ placement: 'right auto', viewport: '#qunit-fixture' })
  422. $rightTooltip
  423. .one('shown.bs.tooltip', function () {
  424. assert.ok($('.tooltip').is('.in'), 'right positioned tooltip is dynamically positioned left')
  425. $rightTooltip.bootstrapTooltip('hide')
  426. })
  427. .one('hidden.bs.tooltip', function () {
  428. assert.strictEqual($('.tooltip').length, 0, 'right positioned tooltip removed from dom')
  429. rightDone = true
  430. leftTooltip()
  431. })
  432. .bootstrapTooltip('show')
  433. }
  434. $topTooltip
  435. .one('shown.bs.tooltip', function () {
  436. assert.ok($('.tooltip').is('.bottom'), 'top positioned tooltip is dynamically positioned to bottom')
  437. $topTooltip.bootstrapTooltip('hide')
  438. })
  439. .one('hidden.bs.tooltip', function () {
  440. assert.strictEqual($('.tooltip').length, 0, 'top positioned tooltip removed from dom')
  441. topDone = true
  442. rightTooltip()
  443. })
  444. .bootstrapTooltip('show')
  445. })
  446. QUnit.test('should position tip on top if viewport has enough space and placement is "auto top"', function (assert) {
  447. assert.expect(2)
  448. var done = assert.async()
  449. var styles = '<style>'
  450. + 'body { padding-top: 100px; }'
  451. + '#section { height: 300px; border: 1px solid red; padding-top: 50px }'
  452. + 'div[rel="tooltip"] { width: 150px; border: 1px solid blue; }'
  453. + '</style>'
  454. var $styles = $(styles).appendTo('head')
  455. var $container = $('<div id="section"/>').appendTo('#qunit-fixture')
  456. var $target = $('<div rel="tooltip" title="tip"/>')
  457. .appendTo($container)
  458. .bootstrapTooltip({
  459. placement: 'auto top',
  460. viewport: '#section'
  461. })
  462. $target
  463. .on('shown.bs.tooltip', function () {
  464. assert.ok($('.tooltip').is('.top'), 'top positioned tooltip is dynamically positioned to top')
  465. $target.bootstrapTooltip('hide')
  466. })
  467. .on('hidden.bs.tooltip', function () {
  468. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
  469. $styles.remove()
  470. done()
  471. })
  472. .bootstrapTooltip('show')
  473. })
  474. QUnit.test('should position tip on top if viewport has enough space and is not parent', function (assert) {
  475. assert.expect(2)
  476. var done = assert.async()
  477. var styles = '<style>'
  478. + '#section { height: 300px; border: 1px solid red; margin-top: 100px; }'
  479. + 'div[rel="tooltip"] { width: 150px; border: 1px solid blue; }'
  480. + '</style>'
  481. var $styles = $(styles).appendTo('head')
  482. var $container = $('<div id="section"/>').appendTo('#qunit-fixture')
  483. var $target = $('<div rel="tooltip" title="tip"/>')
  484. .appendTo($container)
  485. .bootstrapTooltip({
  486. placement: 'auto top',
  487. viewport: '#qunit-fixture'
  488. })
  489. $target
  490. .on('shown.bs.tooltip', function () {
  491. assert.ok($('.tooltip').is('.top'), 'top positioned tooltip is dynamically positioned to top')
  492. $target.bootstrapTooltip('hide')
  493. })
  494. .on('hidden.bs.tooltip', function () {
  495. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
  496. $styles.remove()
  497. done()
  498. })
  499. .bootstrapTooltip('show')
  500. })
  501. QUnit.test('should position tip on bottom if the tip\'s dimension exceeds the viewport area and placement is "auto top"', function (assert) {
  502. assert.expect(2)
  503. var done = assert.async()
  504. var styles = '<style>'
  505. + 'body { padding-top: 100px; }'
  506. + '#section { height: 300px; border: 1px solid red; }'
  507. + 'div[rel="tooltip"] { width: 150px; border: 1px solid blue; }'
  508. + '</style>'
  509. var $styles = $(styles).appendTo('head')
  510. var $container = $('<div id="section"/>').appendTo('#qunit-fixture')
  511. var $target = $('<div rel="tooltip" title="tip"/>')
  512. .appendTo($container)
  513. .bootstrapTooltip({
  514. placement: 'auto top',
  515. viewport: '#section'
  516. })
  517. $target
  518. .on('shown.bs.tooltip', function () {
  519. assert.ok($('.tooltip').is('.bottom'), 'top positioned tooltip is dynamically positioned to bottom')
  520. $target.bootstrapTooltip('hide')
  521. })
  522. .on('hidden.bs.tooltip', function () {
  523. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
  524. $styles.remove()
  525. done()
  526. })
  527. .bootstrapTooltip('show')
  528. })
  529. QUnit.test('should display the tip on top whenever scrollable viewport has enough room if the given placement is "auto top"', function (assert) {
  530. assert.expect(2)
  531. var done = assert.async()
  532. var styles = '<style>'
  533. + '#scrollable-div { height: 200px; overflow: auto; }'
  534. + '.tooltip-item { margin: 200px 0 400px; width: 150px; }'
  535. + '</style>'
  536. var $styles = $(styles).appendTo('head')
  537. var $container = $('<div id="scrollable-div"/>').appendTo('#qunit-fixture')
  538. var $target = $('<div rel="tooltip" title="tip" class="tooltip-item">Tooltip Item</div>')
  539. .appendTo($container)
  540. .bootstrapTooltip({
  541. placement: 'top auto',
  542. viewport: '#scrollable-div'
  543. })
  544. $('#scrollable-div').scrollTop(100)
  545. $target
  546. .on('shown.bs.tooltip', function () {
  547. assert.ok($('.tooltip').is('.fade.top.in'), 'has correct classes applied')
  548. $target.bootstrapTooltip('hide')
  549. })
  550. .on('hidden.bs.tooltip', function () {
  551. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
  552. $styles.remove()
  553. done()
  554. })
  555. .bootstrapTooltip('show')
  556. })
  557. QUnit.test('should display the tip on bottom whenever scrollable viewport doesn\'t have enough room if the given placement is "auto top"', function (assert) {
  558. assert.expect(2)
  559. var done = assert.async()
  560. var styles = '<style>'
  561. + '#scrollable-div { height: 200px; overflow: auto; }'
  562. + '.tooltip-item { padding: 200px 0 400px; width: 150px; }'
  563. + '</style>'
  564. var $styles = $(styles).appendTo('head')
  565. var $container = $('<div id="scrollable-div"/>').appendTo('#qunit-fixture')
  566. var $target = $('<div rel="tooltip" title="tip" class="tooltip-item">Tooltip Item</div>')
  567. .appendTo($container)
  568. .bootstrapTooltip({
  569. placement: 'top auto',
  570. viewport: '#scrollable-div'
  571. })
  572. $('#scrollable-div').scrollTop(200)
  573. $target
  574. .on('shown.bs.tooltip', function () {
  575. assert.ok($('.tooltip').is('.fade.bottom.in'), 'has correct classes applied')
  576. $target.bootstrapTooltip('hide')
  577. })
  578. .on('hidden.bs.tooltip', function () {
  579. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
  580. $styles.remove()
  581. done()
  582. })
  583. .bootstrapTooltip('show')
  584. })
  585. QUnit.test('should display the tip on bottom whenever scrollable viewport has enough room if the given placement is "auto bottom"', function (assert) {
  586. assert.expect(2)
  587. var done = assert.async()
  588. var styles = '<style>'
  589. + '#scrollable-div { height: 200px; overflow: auto; }'
  590. + '.spacer { height: 400px; }'
  591. + '.spacer:first-child { height: 200px; }'
  592. + '.tooltip-item { width: 150px; }'
  593. + '</style>'
  594. var $styles = $(styles).appendTo('head')
  595. var $container = $('<div id="scrollable-div"/>').appendTo('#qunit-fixture')
  596. var $target = $('<div rel="tooltip" title="tip" class="tooltip-item">Tooltip Item</div>')
  597. .appendTo($container)
  598. .before('<div class="spacer"/>')
  599. .after('<div class="spacer"/>')
  600. .bootstrapTooltip({
  601. placement: 'bottom auto',
  602. viewport: '#scrollable-div'
  603. })
  604. $('#scrollable-div').scrollTop(200)
  605. $target
  606. .on('shown.bs.tooltip', function () {
  607. assert.ok($('.tooltip').is('.fade.bottom.in'), 'has correct classes applied')
  608. $target.bootstrapTooltip('hide')
  609. })
  610. .on('hidden.bs.tooltip', function () {
  611. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
  612. $styles.remove()
  613. done()
  614. })
  615. .bootstrapTooltip('show')
  616. })
  617. QUnit.test('should display the tip on top whenever scrollable viewport doesn\'t have enough room if the given placement is "auto bottom"', function (assert) {
  618. assert.expect(2)
  619. var done = assert.async()
  620. var styles = '<style>'
  621. + '#scrollable-div { height: 200px; overflow: auto; }'
  622. + '.tooltip-item { margin-top: 400px; width: 150px; }'
  623. + '</style>'
  624. var $styles = $(styles).appendTo('head')
  625. var $container = $('<div id="scrollable-div"/>').appendTo('#qunit-fixture')
  626. var $target = $('<div rel="tooltip" title="tip" class="tooltip-item">Tooltip Item</div>')
  627. .appendTo($container)
  628. .bootstrapTooltip({
  629. placement: 'bottom auto',
  630. viewport: '#scrollable-div'
  631. })
  632. $('#scrollable-div').scrollTop(400)
  633. $target
  634. .on('shown.bs.tooltip', function () {
  635. assert.ok($('.tooltip').is('.fade.top.in'), 'has correct classes applied')
  636. $target.bootstrapTooltip('hide')
  637. })
  638. .on('hidden.bs.tooltip', function () {
  639. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
  640. $styles.remove()
  641. done()
  642. })
  643. .bootstrapTooltip('show')
  644. })
  645. QUnit.test('should adjust the tip\'s top position when up against the top of the viewport', function (assert) {
  646. assert.expect(2)
  647. var done = assert.async()
  648. var styles = '<style>'
  649. + '.tooltip .tooltip-inner { width: 200px; height: 200px; max-width: none; }'
  650. + 'a[rel="tooltip"] { position: fixed; }'
  651. + '</style>'
  652. var $styles = $(styles).appendTo('head')
  653. var $container = $('<div/>').appendTo('#qunit-fixture')
  654. var $target = $('<a href="#" rel="tooltip" title="tip" style="top: 0px; left: 0px;"/>')
  655. .appendTo($container)
  656. .bootstrapTooltip({
  657. placement: 'right',
  658. viewport: {
  659. selector: 'body',
  660. padding: 12
  661. }
  662. })
  663. $target
  664. .on('shown.bs.tooltip', function () {
  665. assert.strictEqual(Math.round($container.find('.tooltip').offset().top), 12)
  666. $target.bootstrapTooltip('hide')
  667. })
  668. .on('hidden.bs.tooltip', function () {
  669. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
  670. $styles.remove()
  671. done()
  672. })
  673. .bootstrapTooltip('show')
  674. })
  675. QUnit.test('should adjust the tip\'s top position when up against the bottom of the viewport', function (assert) {
  676. assert.expect(2)
  677. var done = assert.async()
  678. var styles = '<style>'
  679. + '.tooltip .tooltip-inner { width: 200px; height: 200px; max-width: none; }'
  680. + 'a[rel="tooltip"] { position: fixed; }'
  681. + '</style>'
  682. var $styles = $(styles).appendTo('head')
  683. var $container = $('<div/>').appendTo('#qunit-fixture')
  684. var $target = $('<a href="#" rel="tooltip" title="tip" style="bottom: 0px; left: 0px;"/>')
  685. .appendTo($container)
  686. .bootstrapTooltip({
  687. placement: 'right',
  688. viewport: {
  689. selector: 'body',
  690. padding: 12
  691. }
  692. })
  693. $target
  694. .on('shown.bs.tooltip', function () {
  695. var $tooltip = $container.find('.tooltip')
  696. var padding = 50
  697. var result = Math.round($tooltip.offset().top)
  698. var resultMax = result + padding
  699. var resultMin = result - padding
  700. var expected = Math.round($(window).height() - 12 - $tooltip[0].offsetHeight)
  701. assert.ok(expected < resultMax && expected > resultMin)
  702. $target.bootstrapTooltip('hide')
  703. })
  704. .on('hidden.bs.tooltip', function () {
  705. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
  706. $container.remove()
  707. $styles.remove()
  708. done()
  709. })
  710. .bootstrapTooltip('show')
  711. })
  712. QUnit.test('should adjust the tip\'s left position when up against the left of the viewport', function (assert) {
  713. assert.expect(2)
  714. var done = assert.async()
  715. var styles = '<style>'
  716. + '.tooltip .tooltip-inner { width: 200px; height: 200px; max-width: none; }'
  717. + 'a[rel="tooltip"] { position: fixed; }'
  718. + '</style>'
  719. var $styles = $(styles).appendTo('head')
  720. var $container = $('<div/>').appendTo('#qunit-fixture')
  721. var $target = $('<a href="#" rel="tooltip" title="tip" style="top: 0px; left: 0px;"/>')
  722. .appendTo($container)
  723. .bootstrapTooltip({
  724. placement: 'bottom',
  725. viewport: {
  726. selector: 'body',
  727. padding: 12
  728. }
  729. })
  730. $target
  731. .on('shown.bs.tooltip', function () {
  732. assert.strictEqual(Math.round($container.find('.tooltip').offset().left), 12)
  733. $target.bootstrapTooltip('hide')
  734. })
  735. .on('hidden.bs.tooltip', function () {
  736. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
  737. $container.remove()
  738. $styles.remove()
  739. done()
  740. })
  741. .bootstrapTooltip('show')
  742. })
  743. QUnit.test('should adjust the tip\'s left position when up against the right of the viewport', function (assert) {
  744. assert.expect(2)
  745. var done = assert.async()
  746. var styles = '<style>'
  747. + '.tooltip .tooltip-inner { width: 200px; height: 200px; max-width: none; }'
  748. + 'a[rel="tooltip"] { position: fixed; }'
  749. + '</style>'
  750. var $styles = $(styles).appendTo('head')
  751. var $container = $('<div/>').appendTo('body')
  752. var $target = $('<a href="#" rel="tooltip" title="tip" style="top: 0px; right: 0px;"/>')
  753. .appendTo($container)
  754. .bootstrapTooltip({
  755. placement: 'bottom',
  756. viewport: {
  757. selector: 'body',
  758. padding: 12
  759. }
  760. })
  761. $target
  762. .on('shown.bs.tooltip', function () {
  763. var $tooltip = $container.find('.tooltip')
  764. assert.strictEqual(Math.round($tooltip.offset().left), Math.round($(window).width() - 12 - $tooltip[0].offsetWidth))
  765. $target.bootstrapTooltip('hide')
  766. })
  767. .on('hidden.bs.tooltip', function () {
  768. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
  769. $container.remove()
  770. $styles.remove()
  771. done()
  772. })
  773. .bootstrapTooltip('show')
  774. })
  775. QUnit.test('should adjust the tip when up against the right of an arbitrary viewport', function (assert) {
  776. assert.expect(2)
  777. var done = assert.async()
  778. var styles = '<style>'
  779. + '.tooltip, .tooltip .tooltip-inner { width: 200px; height: 200px; max-width: none; }'
  780. + '.container-viewport { position: absolute; top: 50px; left: 60px; width: 300px; height: 300px; }'
  781. + 'a[rel="tooltip"] { position: fixed; }'
  782. + '</style>'
  783. var $styles = $(styles).appendTo('head')
  784. var $container = $('<div class="container-viewport"/>').appendTo(document.body)
  785. var $target = $('<a href="#" rel="tooltip" title="tip" style="top: 50px; left: 350px;"/>')
  786. .appendTo($container)
  787. .bootstrapTooltip({
  788. placement: 'bottom',
  789. viewport: '.container-viewport'
  790. })
  791. $target
  792. .on('shown.bs.tooltip', function () {
  793. var $tooltip = $container.find('.tooltip')
  794. assert.strictEqual(Math.round($tooltip.offset().left), Math.round(60 + $container.width() - $tooltip[0].offsetWidth))
  795. $target.bootstrapTooltip('hide')
  796. })
  797. .on('hidden.bs.tooltip', function () {
  798. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
  799. $container.remove()
  800. $styles.remove()
  801. done()
  802. })
  803. .bootstrapTooltip('show')
  804. })
  805. QUnit.test('should get viewport element from function', function (assert) {
  806. assert.expect(3)
  807. var done = assert.async()
  808. var styles = '<style>'
  809. + '.tooltip, .tooltip .tooltip-inner { width: 200px; height: 200px; max-width: none; }'
  810. + '.container-viewport { position: absolute; top: 50px; left: 60px; width: 300px; height: 300px; }'
  811. + 'a[rel="tooltip"] { position: fixed; }'
  812. + '</style>'
  813. var $styles = $(styles).appendTo('head')
  814. var $container = $('<div class="container-viewport"/>').appendTo(document.body)
  815. var $target = $('<a href="#" rel="tooltip" title="tip" style="top: 50px; left: 350px;"/>').appendTo($container)
  816. $target
  817. .bootstrapTooltip({
  818. placement: 'bottom',
  819. viewport: function ($element) {
  820. assert.strictEqual($element[0], $target[0], 'viewport function was passed target as argument')
  821. return ($element.closest('.container-viewport'))
  822. }
  823. })
  824. $target
  825. .on('shown.bs.tooltip', function () {
  826. var $tooltip = $container.find('.tooltip')
  827. assert.strictEqual(Math.round($tooltip.offset().left), Math.round(60 + $container.width() - $tooltip[0].offsetWidth))
  828. $target.bootstrapTooltip('hide')
  829. })
  830. .on('hidden.bs.tooltip', function () {
  831. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
  832. $container.remove()
  833. $styles.remove()
  834. done()
  835. })
  836. .bootstrapTooltip('show')
  837. })
  838. QUnit.test('should not misplace the tip when the right edge offset is greater or equal than the viewport width', function (assert) {
  839. assert.expect(2)
  840. var done = assert.async()
  841. var styles = '<style>'
  842. + '.tooltip, .tooltip *, .tooltip *:before, .tooltip *:after { box-sizing: border-box; }'
  843. + '.container-viewport, .container-viewport *, .container-viewport *:before, .container-viewport *:after { box-sizing: border-box; }'
  844. + '.tooltip, .tooltip .tooltip-inner { width: 50px; height: 50px; max-width: none; background: red; }'
  845. + '.container-viewport { padding: 100px; margin-left: 100px; width: 100px; }'
  846. + '</style>'
  847. var $styles = $(styles).appendTo('head')
  848. var $container = $('<div class="container-viewport"/>').appendTo(document.body)
  849. var $target = $('<a href="#" rel="tooltip" title="tip">foobar</a>')
  850. .appendTo($container)
  851. .bootstrapTooltip({
  852. viewport: '.container-viewport'
  853. })
  854. $target
  855. .on('shown.bs.tooltip', function () {
  856. var $tooltip = $container.find('.tooltip')
  857. var expected = Math.round($target.position().left + $target.width() / 2 - $tooltip[0].offsetWidth / 2)
  858. var result = Math.round($tooltip.offset().left)
  859. assert.ok(result === (expected - 1) || result === expected)
  860. $target.bootstrapTooltip('hide')
  861. })
  862. .on('hidden.bs.tooltip', function () {
  863. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
  864. $container.remove()
  865. $styles.remove()
  866. done()
  867. })
  868. .bootstrapTooltip('show')
  869. })
  870. QUnit.test('should not error when trying to show an auto-placed tooltip that has been removed from the dom', function (assert) {
  871. assert.expect(1)
  872. var passed = true
  873. var $tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"/>')
  874. .appendTo('#qunit-fixture')
  875. .one('show.bs.tooltip', function () {
  876. $(this).remove()
  877. })
  878. .bootstrapTooltip({ placement: 'auto' })
  879. try {
  880. $tooltip.bootstrapTooltip('show')
  881. } catch (err) {
  882. passed = false
  883. console.log(err)
  884. }
  885. assert.ok(passed, '.tooltip(\'show\') should not throw an error if element no longer is in dom')
  886. })
  887. QUnit.test('should place tooltip on top of element', function (assert) {
  888. assert.expect(1)
  889. var done = assert.async()
  890. var containerHTML = '<div>'
  891. + '<p style="margin-top: 200px">'
  892. + '<a href="#" title="very very very very very very very long tooltip">Hover me</a>'
  893. + '</p>'
  894. + '</div>'
  895. var $container = $(containerHTML)
  896. .css({
  897. position: 'absolute',
  898. bottom: 0,
  899. left: 0,
  900. textAlign: 'right',
  901. width: 300,
  902. height: 300
  903. })
  904. .appendTo('#qunit-fixture')
  905. var $trigger = $container
  906. .find('a')
  907. .css('margin-top', 200)
  908. .bootstrapTooltip({
  909. placement: 'top',
  910. animate: false
  911. })
  912. .bootstrapTooltip('show')
  913. var $tooltip = $container.find('.tooltip')
  914. setTimeout(function () {
  915. assert.ok(Math.round($tooltip.offset().top + $tooltip.outerHeight()) <= Math.round($trigger.offset().top))
  916. done()
  917. }, 0)
  918. })
  919. QUnit.test('should place tooltip inside viewport', function (assert) {
  920. assert.expect(1)
  921. var done = assert.async()
  922. var $container = $('<div/>')
  923. .css({
  924. position: 'absolute',
  925. width: 200,
  926. height: 200,
  927. bottom: 0,
  928. left: 0
  929. })
  930. .appendTo('#qunit-fixture')
  931. $('<a href="#" title="Very very very very very very very very long tooltip">Hover me</a>')
  932. .css({
  933. position: 'absolute',
  934. top: 0,
  935. left: 0
  936. })
  937. .appendTo($container)
  938. .bootstrapTooltip({
  939. placement: 'top'
  940. })
  941. .bootstrapTooltip('show')
  942. setTimeout(function () {
  943. assert.ok($('.tooltip').offset().left >= 0)
  944. done()
  945. }, 0)
  946. })
  947. QUnit.test('should show tooltip if leave event hasn\'t occurred before delay expires', function (assert) {
  948. assert.expect(2)
  949. var done = assert.async()
  950. var $tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"/>')
  951. .appendTo('#qunit-fixture')
  952. .bootstrapTooltip({ delay: 150 })
  953. setTimeout(function () {
  954. assert.ok(!$('.tooltip').is('.fade.in'), '100ms: tooltip is not faded in')
  955. }, 100)
  956. setTimeout(function () {
  957. assert.ok($('.tooltip').is('.fade.in'), '200ms: tooltip is faded in')
  958. done()
  959. }, 200)
  960. $tooltip.trigger('mouseenter')
  961. })
  962. QUnit.test('should not show tooltip if leave event occurs before delay expires', function (assert) {
  963. assert.expect(2)
  964. var done = assert.async()
  965. var $tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"/>')
  966. .appendTo('#qunit-fixture')
  967. .bootstrapTooltip({ delay: 150 })
  968. setTimeout(function () {
  969. assert.ok(!$('.tooltip').is('.fade.in'), '100ms: tooltip not faded in')
  970. $tooltip.trigger('mouseout')
  971. }, 100)
  972. setTimeout(function () {
  973. assert.ok(!$('.tooltip').is('.fade.in'), '200ms: tooltip not faded in')
  974. done()
  975. }, 200)
  976. $tooltip.trigger('mouseenter')
  977. })
  978. QUnit.test('should not hide tooltip if leave event occurs and enter event occurs within the hide delay', function (assert) {
  979. assert.expect(3)
  980. var done = assert.async()
  981. var $tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"/>')
  982. .appendTo('#qunit-fixture')
  983. .bootstrapTooltip({ delay: { show: 0, hide: 150 }})
  984. setTimeout(function () {
  985. assert.ok($('.tooltip').is('.fade.in'), '1ms: tooltip faded in')
  986. $tooltip.trigger('mouseout')
  987. setTimeout(function () {
  988. assert.ok($('.tooltip').is('.fade.in'), '100ms: tooltip still faded in')
  989. $tooltip.trigger('mouseenter')
  990. }, 100)
  991. setTimeout(function () {
  992. assert.ok($('.tooltip').is('.fade.in'), '200ms: tooltip still faded in')
  993. done()
  994. }, 200)
  995. }, 0)
  996. $tooltip.trigger('mouseenter')
  997. })
  998. QUnit.test('should not show tooltip if leave event occurs before delay expires', function (assert) {
  999. assert.expect(2)
  1000. var done = assert.async()
  1001. var $tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"/>')
  1002. .appendTo('#qunit-fixture')
  1003. .bootstrapTooltip({ delay: 150 })
  1004. setTimeout(function () {
  1005. assert.ok(!$('.tooltip').is('.fade.in'), '100ms: tooltip not faded in')
  1006. $tooltip.trigger('mouseout')
  1007. }, 100)
  1008. setTimeout(function () {
  1009. assert.ok(!$('.tooltip').is('.fade.in'), '200ms: tooltip not faded in')
  1010. done()
  1011. }, 200)
  1012. $tooltip.trigger('mouseenter')
  1013. })
  1014. QUnit.test('should not show tooltip if leave event occurs before delay expires, even if hide delay is 0', function (assert) {
  1015. assert.expect(2)
  1016. var done = assert.async()
  1017. var $tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"/>')
  1018. .appendTo('#qunit-fixture')
  1019. .bootstrapTooltip({ delay: { show: 150, hide: 0 }})
  1020. setTimeout(function () {
  1021. assert.ok(!$('.tooltip').is('.fade.in'), '100ms: tooltip not faded in')
  1022. $tooltip.trigger('mouseout')
  1023. }, 100)
  1024. setTimeout(function () {
  1025. assert.ok(!$('.tooltip').is('.fade.in'), '250ms: tooltip not faded in')
  1026. done()
  1027. }, 250)
  1028. $tooltip.trigger('mouseenter')
  1029. })
  1030. QUnit.test('should wait 200ms before hiding the tooltip', function (assert) {
  1031. if (window.__karma__) {
  1032. assert.expect(0)
  1033. return
  1034. }
  1035. assert.expect(3)
  1036. var done = assert.async()
  1037. var $tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"/>')
  1038. .appendTo('#qunit-fixture')
  1039. .bootstrapTooltip({ delay: { show: 0, hide: 150 }})
  1040. setTimeout(function () {
  1041. assert.ok($tooltip.data('bs.tooltip').$tip.is('.fade.in'), '1ms: tooltip faded in')
  1042. $tooltip.trigger('mouseout')
  1043. setTimeout(function () {
  1044. assert.ok($tooltip.data('bs.tooltip').$tip.is('.fade.in'), '100ms: tooltip still faded in')
  1045. }, 100)
  1046. setTimeout(function () {
  1047. assert.ok(!$tooltip.data('bs.tooltip').$tip.is('.in'), '200ms: tooltip removed')
  1048. done()
  1049. }, 200)
  1050. }, 0)
  1051. $tooltip.trigger('mouseenter')
  1052. })
  1053. QUnit.test('should correctly position tooltips on SVG elements', function (assert) {
  1054. if (!window.SVGElement) {
  1055. // Skip IE8 since it doesn't support SVG
  1056. assert.expect(0)
  1057. return
  1058. }
  1059. assert.expect(2)
  1060. var done = assert.async()
  1061. var styles = '<style>'
  1062. + '.tooltip, .tooltip *, .tooltip *:before, .tooltip *:after { box-sizing: border-box; }'
  1063. + '.tooltip { position: absolute; }'
  1064. + '.tooltip .tooltip-inner { width: 24px; height: 24px; font-family: Helvetica; }'
  1065. + '</style>'
  1066. var $styles = $(styles).appendTo('head')
  1067. $('#qunit-fixture').append(
  1068. '<div style="position: fixed; top: 0; left: 0;">'
  1069. + ' <svg width="200" height="200">'
  1070. + ' <circle cx="100" cy="100" r="10" title="m" id="theCircle" />'
  1071. + ' </svg>'
  1072. + '</div>')
  1073. var $circle = $('#theCircle')
  1074. $circle
  1075. .on('shown.bs.tooltip', function () {
  1076. var offset = $('.tooltip').offset()
  1077. $styles.remove()
  1078. assert.ok(Math.abs(offset.left - 88) <= 1, 'tooltip has correct horizontal location')
  1079. $circle.bootstrapTooltip('hide')
  1080. })
  1081. .on('hidden.bs.tooltip', function () {
  1082. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
  1083. done()
  1084. })
  1085. .bootstrapTooltip({ container: 'body', placement: 'top', trigger: 'manual' })
  1086. $circle.bootstrapTooltip('show')
  1087. })
  1088. QUnit.test('should correctly determine auto placement based on container rather than parent', function (assert) {
  1089. assert.expect(2)
  1090. var done = assert.async()
  1091. var styles = '<style>'
  1092. + '.tooltip, .tooltip *, .tooltip *:before, .tooltip *:after { box-sizing: border-box; }'
  1093. + '.tooltip { position: absolute; display: block; font-size: 12px; line-height: 1.4; }'
  1094. + '.tooltip .tooltip-inner { max-width: 200px; padding: 3px 8px; font-family: Helvetica; text-align: center; }'
  1095. + '#trigger-parent {'
  1096. + ' position: fixed;'
  1097. + ' top: 100px;'
  1098. + ' right: 17px;'
  1099. + '}'
  1100. + '</style>'
  1101. var $styles = $(styles).appendTo('head')
  1102. $('#qunit-fixture').append('<span id="trigger-parent"><a id="tt-trigger" title="If a_larger_text is written here, it won\'t fit using older broken version of BS">HOVER OVER ME</a></span>')
  1103. var $trigger = $('#tt-trigger')
  1104. $trigger
  1105. .on('shown.bs.tooltip', function () {
  1106. var $tip = $('.tooltip-inner')
  1107. var tipXrightEdge = $tip.offset().left + $tip.width()
  1108. var triggerXleftEdge = $trigger.offset().left
  1109. assert.ok(tipXrightEdge < triggerXleftEdge, 'tooltip with auto left placement, when near the right edge of the viewport, gets left placement')
  1110. $trigger.bootstrapTooltip('hide')
  1111. })
  1112. .on('hidden.bs.tooltip', function () {
  1113. $styles.remove()
  1114. $(this).remove()
  1115. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
  1116. done()
  1117. })
  1118. .bootstrapTooltip({
  1119. container: 'body',
  1120. placement: 'auto left',
  1121. trigger: 'manual'
  1122. })
  1123. $trigger.bootstrapTooltip('show')
  1124. })
  1125. QUnit.test('should not reload the tooltip on subsequent mouseenter events', function (assert) {
  1126. assert.expect(1)
  1127. var titleHtml = function () {
  1128. var uid = $.fn.bootstrapTooltip.Constructor.prototype.getUID('tooltip')
  1129. return '<p id="tt-content">' + uid + '</p><p>' + uid + '</p><p>' + uid + '</p>'
  1130. }
  1131. var $tooltip = $('<span id="tt-outer" rel="tooltip" data-trigger="hover" data-placement="top">some text</span>')
  1132. .appendTo('#qunit-fixture')
  1133. $tooltip.bootstrapTooltip({
  1134. html: true,
  1135. animation: false,
  1136. trigger: 'hover',
  1137. delay: { show: 0, hide: 500 },
  1138. container: $tooltip,
  1139. title: titleHtml
  1140. })
  1141. $('#tt-outer').trigger('mouseenter')
  1142. var currentUid = $('#tt-content').text()
  1143. $('#tt-content').trigger('mouseenter')
  1144. assert.strictEqual(currentUid, $('#tt-content').text())
  1145. })
  1146. QUnit.test('should not reload the tooltip if the mouse leaves and re-enters before hiding', function (assert) {
  1147. assert.expect(4)
  1148. var titleHtml = function () {
  1149. var uid = $.fn.bootstrapTooltip.Constructor.prototype.getUID('tooltip')
  1150. return '<p id="tt-content">' + uid + '</p><p>' + uid + '</p><p>' + uid + '</p>'
  1151. }
  1152. var $tooltip = $('<span id="tt-outer" rel="tooltip" data-trigger="hover" data-placement="top">some text</span>')
  1153. .appendTo('#qunit-fixture')
  1154. $tooltip.bootstrapTooltip({
  1155. html: true,
  1156. animation: false,
  1157. trigger: 'hover',
  1158. delay: { show: 0, hide: 500 },
  1159. container: $tooltip,
  1160. title: titleHtml
  1161. })
  1162. var obj = $tooltip.data('bs.tooltip')
  1163. $('#tt-outer').trigger('mouseenter')
  1164. var currentUid = $('#tt-content').text()
  1165. $('#tt-outer').trigger('mouseleave')
  1166. assert.strictEqual(currentUid, $('#tt-content').text())
  1167. assert.ok(obj.hoverState == 'out', 'the tooltip hoverState should be set to "out"')
  1168. $('#tt-content').trigger('mouseenter')
  1169. assert.ok(obj.hoverState == 'in', 'the tooltip hoverState should be set to "in"')
  1170. assert.strictEqual(currentUid, $('#tt-content').text())
  1171. })
  1172. QUnit.test('should position arrow correctly when tooltip is moved to not appear offscreen', function (assert) {
  1173. assert.expect(2)
  1174. var done = assert.async()
  1175. var styles = '<style>'
  1176. + '.tooltip, .tooltip *, .tooltip *:before, .tooltip *:after { box-sizing: border-box; }'
  1177. + '.tooltip { position: absolute; }'
  1178. + '.tooltip-arrow { position: absolute; width: 0; height: 0; }'
  1179. + '.tooltip .tooltip-inner { max-width: 200px; padding: 3px 8px; }'
  1180. + '</style>'
  1181. var $styles = $(styles).appendTo('head')
  1182. $('<a href="#" title="tooltip title" style="position: absolute; bottom: 0; right: 0;">Foobar</a>')
  1183. .appendTo('body')
  1184. .on('shown.bs.tooltip', function () {
  1185. var arrowStyles = $(this).data('bs.tooltip').$tip.find('.tooltip-arrow').attr('style')
  1186. assert.ok(/left/i.test(arrowStyles) && !/top/i.test(arrowStyles), 'arrow positioned correctly')
  1187. $(this).bootstrapTooltip('hide')
  1188. })
  1189. .on('hidden.bs.tooltip', function () {
  1190. $styles.remove()
  1191. $(this).remove()
  1192. assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
  1193. done()
  1194. })
  1195. .bootstrapTooltip({
  1196. container: 'body',
  1197. placement: 'top',
  1198. trigger: 'manual'
  1199. })
  1200. .bootstrapTooltip('show')
  1201. })
  1202. QUnit.test('should correctly position tooltips on transformed elements', function (assert) {
  1203. var styleProps = document.documentElement.style
  1204. if ((!('transform' in styleProps) && !('webkitTransform' in styleProps) && !('msTransform' in styleProps)) || window.__karma__) {
  1205. assert.expect(0)
  1206. return
  1207. }
  1208. assert.expect(2)
  1209. var done = assert.async()
  1210. var styles = '<style>'
  1211. + '#qunit-fixture { top: 0; left: 0; }'
  1212. + '.tooltip, .tooltip *, .tooltip *:before, .tooltip *:after { box-sizing: border-box; }'
  1213. + '.tooltip { position: absolute; }'
  1214. + '.tooltip .tooltip-inner { width: 24px; height: 24px; font-family: Helvetica; }'
  1215. + '#target { position: absolute; top: 100px; left: 50px; width: 100px; height: 200px; -webkit-transform: rotate(270deg); -ms-transform: rotate(270deg); transform: rotate(270deg); }'
  1216. + '</style>'
  1217. var $styles = $(styles).appendTo('head')
  1218. var $element = $('<div id="target" title="1"/>').appendTo('#qunit-fixture')
  1219. $element
  1220. .on('shown.bs.tooltip', function () {
  1221. var offset = $('.tooltip').offset()
  1222. $styles.remove()
  1223. assert.ok(Math.abs(offset.left - 88) <= 1, 'tooltip has correct horizontal location')
  1224. assert.ok(Math.abs(offset.top - 126) <= 1, 'tooltip has correct vertical location')
  1225. $element.bootstrapTooltip('hide')
  1226. done()
  1227. })
  1228. .bootstrapTooltip({
  1229. container: 'body',
  1230. placement: 'top',
  1231. trigger: 'manual'
  1232. })
  1233. $element.bootstrapTooltip('show')
  1234. })
  1235. QUnit.test('should throw an error when initializing tooltip on the document object without specifying a delegation selector', function (assert) {
  1236. assert.expect(1)
  1237. assert.throws(function () {
  1238. $(document).bootstrapTooltip({ title: 'What am I on?' })
  1239. }, new Error('`selector` option must be specified when initializing tooltip on the window.document object!'))
  1240. })
  1241. QUnit.test('should do nothing when an attempt is made to hide an uninitialized tooltip', function (assert) {
  1242. assert.expect(1)
  1243. var $tooltip = $('<span data-toggle="tooltip" title="some tip">some text</span>')
  1244. .appendTo('#qunit-fixture')
  1245. .on('hidden.bs.tooltip shown.bs.tooltip', function () {
  1246. assert.ok(false, 'should not fire any tooltip events')
  1247. })
  1248. .bootstrapTooltip('hide')
  1249. assert.strictEqual($tooltip.data('bs.tooltip'), undefined, 'should not initialize the tooltip')
  1250. })
  1251. QUnit.test('should throw an error when template contains multiple top-level elements', function (assert) {
  1252. assert.expect(1)
  1253. assert.throws(function () {
  1254. $('<a href="#" data-toggle="tooltip" title="Another tooltip"></a>')
  1255. .appendTo('#qunit-fixture')
  1256. .bootstrapTooltip({ template: '<div>Foo</div><div>Bar</div>' })
  1257. .bootstrapTooltip('show')
  1258. }, new Error('tooltip `template` option must consist of exactly 1 top-level element!'))
  1259. })
  1260. QUnit.test('should not remove tooltip if multiple triggers are set and one is still active', function (assert) {
  1261. assert.expect(41)
  1262. var $el = $('<button>Trigger</button>')
  1263. .appendTo('#qunit-fixture')
  1264. .bootstrapTooltip({ trigger: 'click hover focus', animation: false })
  1265. var tooltip = $el.data('bs.tooltip')
  1266. var $tooltip = tooltip.tip()
  1267. function showingTooltip() { return $tooltip.hasClass('in') || tooltip.hoverState == 'in' }
  1268. var tests = [
  1269. ['mouseenter', 'mouseleave'],
  1270. ['focusin', 'focusout'],
  1271. ['click', 'click'],
  1272. ['mouseenter', 'focusin', 'focusout', 'mouseleave'],
  1273. ['mouseenter', 'focusin', 'mouseleave', 'focusout'],
  1274. ['focusin', 'mouseenter', 'mouseleave', 'focusout'],
  1275. ['focusin', 'mouseenter', 'focusout', 'mouseleave'],
  1276. ['click', 'focusin', 'mouseenter', 'focusout', 'mouseleave', 'click'],
  1277. ['mouseenter', 'click', 'focusin', 'focusout', 'mouseleave', 'click'],
  1278. ['mouseenter', 'focusin', 'click', 'click', 'mouseleave', 'focusout']
  1279. ]
  1280. assert.ok(!showingTooltip())
  1281. $.each(tests, function (idx, triggers) {
  1282. for (var i = 0, len = triggers.length; i < len; i++) {
  1283. $el.trigger(triggers[i])
  1284. assert.equal(i < (len - 1), showingTooltip())
  1285. }
  1286. })
  1287. })
  1288. QUnit.test('should disable sanitizer', function (assert) {
  1289. assert.expect(1)
  1290. var $trigger = $('<a href="#" rel="tooltip" data-trigger="click" title="Another tooltip"/>')
  1291. .appendTo('#qunit-fixture')
  1292. .bootstrapTooltip({
  1293. sanitize: false
  1294. })
  1295. var tooltip = $trigger.data('bs.tooltip')
  1296. assert.strictEqual(tooltip.options.sanitize, false)
  1297. })
  1298. QUnit.test('should sanitize template by removing disallowed tags', function (assert) {
  1299. if (!document.implementation || !document.implementation.createHTMLDocument) {
  1300. assert.expect(0)
  1301. return
  1302. }
  1303. assert.expect(1)
  1304. var $trigger = $('<a href="#" rel="tooltip" data-trigger="click" title="Another tooltip"/>')
  1305. .appendTo('#qunit-fixture')
  1306. .bootstrapTooltip({
  1307. template: [
  1308. '<div>',
  1309. ' <script>console.log("oups script inserted")</script>',
  1310. ' <span>Some content</span>',
  1311. '</div>'
  1312. ].join('')
  1313. })
  1314. var tooltip = $trigger.data('bs.tooltip')
  1315. assert.strictEqual(tooltip.options.template.indexOf('script'), -1)
  1316. })
  1317. QUnit.test('should sanitize template by removing disallowed attributes', function (assert) {
  1318. if (!document.implementation || !document.implementation.createHTMLDocument) {
  1319. assert.expect(0)
  1320. return
  1321. }
  1322. assert.expect(1)
  1323. var $trigger = $('<a href="#" rel="tooltip" data-trigger="click" title="Another tooltip"/>')
  1324. .appendTo('#qunit-fixture')
  1325. .bootstrapTooltip({
  1326. template: [
  1327. '<div>',
  1328. ' <img src="x" onError="alert(\'test\')">Some content</img>',
  1329. '</div>'
  1330. ].join('')
  1331. })
  1332. var tooltip = $trigger.data('bs.tooltip')
  1333. assert.strictEqual(tooltip.options.template.indexOf('onError'), -1)
  1334. })
  1335. QUnit.test('should sanitize template by removing tags with XSS', function (assert) {
  1336. if (!document.implementation || !document.implementation.createHTMLDocument) {
  1337. assert.expect(0)
  1338. return
  1339. }
  1340. assert.expect(1)
  1341. var $trigger = $('<a href="#" rel="tooltip" data-trigger="click" title="Another tooltip"/>')
  1342. .appendTo('#qunit-fixture')
  1343. .bootstrapTooltip({
  1344. template: [
  1345. '<div>',
  1346. ' <a href="javascript:alert(7)">Click me</a>',
  1347. ' <span>Some content</span>',
  1348. '</div>'
  1349. ].join('')
  1350. })
  1351. var tooltip = $trigger.data('bs.tooltip')
  1352. assert.strictEqual(tooltip.options.template.indexOf('javascript'), -1)
  1353. })
  1354. QUnit.test('should allow custom sanitization rules', function (assert) {
  1355. if (!document.implementation || !document.implementation.createHTMLDocument) {
  1356. assert.expect(0)
  1357. return
  1358. }
  1359. assert.expect(2)
  1360. var $trigger = $('<a href="#" rel="tooltip" data-trigger="click" title="Another tooltip"/>')
  1361. .appendTo('#qunit-fixture')
  1362. .bootstrapTooltip({
  1363. template: [
  1364. '<a href="javascript:alert(7)">Click me</a>',
  1365. '<span>Some content</span>'
  1366. ].join(''),
  1367. whiteList: {
  1368. span: null
  1369. }
  1370. })
  1371. var tooltip = $trigger.data('bs.tooltip')
  1372. assert.strictEqual(tooltip.options.template.indexOf('<a'), -1)
  1373. assert.ok(tooltip.options.template.indexOf('span') !== -1)
  1374. })
  1375. QUnit.test('should allow passing a custom function for sanitization', function (assert) {
  1376. if (!document.implementation || !document.implementation.createHTMLDocument) {
  1377. assert.expect(0)
  1378. return
  1379. }
  1380. assert.expect(1)
  1381. var $trigger = $('<a href="#" rel="tooltip" data-trigger="click" title="Another tooltip"/>')
  1382. .appendTo('#qunit-fixture')
  1383. .bootstrapTooltip({
  1384. template: [
  1385. '<span>Some content</span>'
  1386. ].join(''),
  1387. sanitizeFn: function (input) {
  1388. return input
  1389. }
  1390. })
  1391. var tooltip = $trigger.data('bs.tooltip')
  1392. assert.ok(tooltip.options.template.indexOf('span') !== -1)
  1393. })
  1394. QUnit.test('should allow passing aria attributes', function (assert) {
  1395. if (!document.implementation || !document.implementation.createHTMLDocument) {
  1396. assert.expect(0)
  1397. return
  1398. }
  1399. assert.expect(1)
  1400. var $trigger = $('<a href="#" rel="tooltip" data-trigger="click" title="Another tooltip"/>')
  1401. .appendTo('#qunit-fixture')
  1402. .bootstrapTooltip({
  1403. template: [
  1404. '<span aria-pressed="true">Some content</span>'
  1405. ].join('')
  1406. })
  1407. var tooltip = $trigger.data('bs.tooltip')
  1408. assert.ok(tooltip.options.template.indexOf('aria-pressed') !== -1)
  1409. })
  1410. QUnit.test('should not take into account sanitize in data attributes', function (assert) {
  1411. if (!document.implementation || !document.implementation.createHTMLDocument) {
  1412. assert.expect(0)
  1413. return
  1414. }
  1415. assert.expect(1)
  1416. var $trigger = $('<a href="#" rel="tooltip" data-sanitize="false" data-trigger="click" title="Another tooltip"/>')
  1417. .appendTo('#qunit-fixture')
  1418. .bootstrapTooltip({
  1419. template: [
  1420. '<span aria-pressed="true">Some content</span>'
  1421. ].join('')
  1422. })
  1423. var tooltip = $trigger.data('bs.tooltip')
  1424. assert.strictEqual(tooltip.options.sanitize, true)
  1425. })
  1426. })