Skip to content

Commit 37a10d3

Browse files
Intrepiddcvrebert
authored andcommitted
Compare tooltip right offset to viewport right offset (and not width)
Closes twbs#16142 by merging it.
1 parent d0926f2 commit 37a10d3

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

js/tests/unit/tooltip.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,33 @@ $(function () {
764764
$styles.remove()
765765
})
766766

767+
QUnit.test('should not misplace the tip when the right edge offset is greater or equal than the viewport width', function (assert) {
768+
assert.expect(2)
769+
var styles = '<style>'
770+
+ '.tooltip, .tooltip *, .tooltip *:before, .tooltip *:after { box-sizing: border-box; }'
771+
+ '.tooltip, .tooltip .tooltip-inner { width: 50px; height: 50px; max-width: none; background: red; }'
772+
+ '.container-viewport { padding: 100px; margin-left: 100px; width: 100px; }'
773+
+ '</style>'
774+
var $styles = $(styles).appendTo('head')
775+
776+
var $container = $('<div class="container-viewport"/>').appendTo(document.body)
777+
var $target = $('<a href="#" rel="tooltip" title="tip">foobar</a>')
778+
.appendTo($container)
779+
.bootstrapTooltip({
780+
viewport: '.container-viewport'
781+
})
782+
783+
$target.bootstrapTooltip('show')
784+
var $tooltip = $container.find('.tooltip')
785+
assert.strictEqual(Math.round($tooltip.offset().left), Math.round($target.position().left + $target.width() / 2 - $tooltip[0].offsetWidth / 2))
786+
787+
$target.bootstrapTooltip('hide')
788+
assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
789+
790+
$container.remove()
791+
$styles.remove()
792+
})
793+
767794
QUnit.test('should not error when trying to show an auto-placed tooltip that has been removed from the dom', function (assert) {
768795
assert.expect(1)
769796
var passed = true

js/tooltip.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@
378378
var rightEdgeOffset = pos.left + viewportPadding + actualWidth
379379
if (leftEdgeOffset < viewportDimensions.left) { // left overflow
380380
delta.left = viewportDimensions.left - leftEdgeOffset
381-
} else if (rightEdgeOffset > viewportDimensions.width) { // right overflow
381+
} else if (rightEdgeOffset > viewportDimensions.right) { // right overflow
382382
delta.left = viewportDimensions.left + viewportDimensions.width - rightEdgeOffset
383383
}
384384
}

0 commit comments

Comments
 (0)