From 37a10d37c17c051830cdd2ecec698d28c0984603 Mon Sep 17 00:00:00 2001 From: Adrien Date: Tue, 24 Mar 2015 17:12:52 +0100 Subject: [PATCH] Compare tooltip right offset to viewport right offset (and not width) Closes #16142 by merging it. --- js/tests/unit/tooltip.js | 27 +++++++++++++++++++++++++++ js/tooltip.js | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/js/tests/unit/tooltip.js b/js/tests/unit/tooltip.js index 2c492a92bb..711eba8ec0 100644 --- a/js/tests/unit/tooltip.js +++ b/js/tests/unit/tooltip.js @@ -764,6 +764,33 @@ $(function () { $styles.remove() }) + QUnit.test('should not misplace the tip when the right edge offset is greater or equal than the viewport width', function (assert) { + assert.expect(2) + var styles = '' + var $styles = $(styles).appendTo('head') + + var $container = $('
').appendTo(document.body) + var $target = $('foobar') + .appendTo($container) + .bootstrapTooltip({ + viewport: '.container-viewport' + }) + + $target.bootstrapTooltip('show') + var $tooltip = $container.find('.tooltip') + assert.strictEqual(Math.round($tooltip.offset().left), Math.round($target.position().left + $target.width() / 2 - $tooltip[0].offsetWidth / 2)) + + $target.bootstrapTooltip('hide') + assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom') + + $container.remove() + $styles.remove() + }) + QUnit.test('should not error when trying to show an auto-placed tooltip that has been removed from the dom', function (assert) { assert.expect(1) var passed = true diff --git a/js/tooltip.js b/js/tooltip.js index 2d42cab24c..1f66413d80 100644 --- a/js/tooltip.js +++ b/js/tooltip.js @@ -378,7 +378,7 @@ var rightEdgeOffset = pos.left + viewportPadding + actualWidth if (leftEdgeOffset < viewportDimensions.left) { // left overflow delta.left = viewportDimensions.left - leftEdgeOffset - } else if (rightEdgeOffset > viewportDimensions.width) { // right overflow + } else if (rightEdgeOffset > viewportDimensions.right) { // right overflow delta.left = viewportDimensions.left + viewportDimensions.width - rightEdgeOffset } }