Skip to content

Commit c15fffc

Browse files
committed
Merge pull request twbs#14623 from twbs/fix-14561
Use container instead of parent for tooltip/popover auto-placement calcs
2 parents 7ce6824 + 4d9890e commit c15fffc

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

js/tests/unit/tooltip.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,47 @@ $(function () {
827827
$circle.bootstrapTooltip('show')
828828
})
829829

830+
test('should correctly determine auto placement based on container rather than parent', function () {
831+
stop()
832+
833+
var styles = '<style>'
834+
+ '.tooltip, .tooltip *, .tooltip *:before, .tooltip *:after { box-sizing: border-box; }'
835+
+ '.tooltip { position: absolute; display: block; font-size: 12px; line-height: 1.4; }'
836+
+ '.tooltip .tooltip-inner { max-width: 200px; padding: 3px 8px; font-family: Helvetica; text-align: center; }'
837+
+ '#trigger-parent {'
838+
+ ' position: fixed;'
839+
+ ' top: 100px;'
840+
+ ' right: 17px;'
841+
+ '}'
842+
+ '</style>'
843+
var $styles = $(styles).appendTo('head')
844+
845+
$('#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>')
846+
var $trigger = $('#tt-trigger')
847+
848+
$trigger
849+
.on('shown.bs.tooltip', function () {
850+
var $tip = $('.tooltip-inner')
851+
var tipXrightEdge = $tip.offset().left + $tip.width()
852+
var triggerXleftEdge = $trigger.offset().left
853+
ok(tipXrightEdge < triggerXleftEdge, 'tooltip with auto left placement, when near the right edge of the viewport, gets left placement')
854+
$trigger.bootstrapTooltip('hide')
855+
})
856+
.on('hidden.bs.tooltip', function () {
857+
$styles.remove()
858+
$(this).remove()
859+
equal($('.tooltip').length, 0, 'tooltip removed from dom')
860+
start()
861+
})
862+
.bootstrapTooltip({
863+
container: 'body',
864+
placement: 'auto left',
865+
trigger: 'manual'
866+
})
867+
868+
$trigger.bootstrapTooltip('show')
869+
})
870+
830871
test('should not reload the tooltip on subsequent mouseenter events', function () {
831872
var titleHtml = function () {
832873
var uid = $.fn.bootstrapTooltip.Constructor.prototype.getUID('tooltip')
@@ -909,6 +950,7 @@ $(function () {
909950
.on('hidden.bs.tooltip', function () {
910951
$styles.remove()
911952
$(this).remove()
953+
equal($('.tooltip').length, 0, 'tooltip removed from dom')
912954
start()
913955
})
914956
.bootstrapTooltip({

js/tooltip.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,13 @@
188188

189189
if (autoPlace) {
190190
var orgPlacement = placement
191-
var $parent = this.$element.parent()
192-
var parentDim = this.getPosition($parent)
191+
var $container = this.options.container ? $(this.options.container) : this.$element.parent()
192+
var containerDim = this.getPosition($container)
193193

194-
placement = placement == 'bottom' && pos.top + pos.height + actualHeight - parentDim.scroll > parentDim.height ? 'top' :
195-
placement == 'top' && pos.top - parentDim.scroll - actualHeight < parentDim.top ? 'bottom' :
196-
placement == 'right' && pos.right + actualWidth > parentDim.width ? 'left' :
197-
placement == 'left' && pos.left - actualWidth < parentDim.left ? 'right' :
194+
placement = placement == 'bottom' && pos.top + pos.height + actualHeight - containerDim.scroll > containerDim.height ? 'top' :
195+
placement == 'top' && pos.top - containerDim.scroll - actualHeight < containerDim.top ? 'bottom' :
196+
placement == 'right' && pos.right + actualWidth > containerDim.width ? 'left' :
197+
placement == 'left' && pos.left - actualWidth < containerDim.left ? 'right' :
198198
placement
199199

200200
$tip

0 commit comments

Comments
 (0)