From 37f74c70702c4e90e12f062e63358cb64670eb01 Mon Sep 17 00:00:00 2001 From: Johann-S Date: Wed, 5 Dec 2018 10:25:00 +0100 Subject: [PATCH] fix regression about using element for tooltip container option --- js/src/tooltip.js | 15 ++++++++++-- js/tests/unit/tooltip.js | 46 ++++++++++++++++++++++++++++++++++++ js/tests/visual/tooltip.html | 10 ++++++-- 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/js/src/tooltip.js b/js/src/tooltip.js index f428a79ebd..1c40dfed34 100644 --- a/js/src/tooltip.js +++ b/js/src/tooltip.js @@ -272,8 +272,7 @@ class Tooltip { const attachment = this._getAttachment(placement) this.addAttachmentClass(attachment) - const container = this.config.container === false ? document.body : $(document).find(this.config.container) - + const container = this._getContainer() $(tip).data(this.constructor.DATA_KEY, this) if (!$.contains(this.element.ownerDocument.documentElement, this.tip)) { @@ -450,6 +449,18 @@ class Tooltip { // Private + _getContainer() { + if (this.config.container === false) { + return document.body + } + + if (Util.isElement(this.config.container)) { + return $(this.config.container) + } + + return $(document).find(this.config.container) + } + _getAttachment(placement) { return AttachmentMap[placement.toUpperCase()] } diff --git a/js/tests/unit/tooltip.js b/js/tests/unit/tooltip.js index 7652d4cf86..289f8aebff 100644 --- a/js/tests/unit/tooltip.js +++ b/js/tests/unit/tooltip.js @@ -414,6 +414,52 @@ $(function () { .bootstrapTooltip('show') }) + QUnit.test('should place tooltips inside a specific container when container is an element', function (assert) { + assert.expect(3) + var done = assert.async() + var $container = $('
').appendTo('#qunit-fixture') + var $tooltip = $('') + .appendTo('#qunit-fixture') + .bootstrapTooltip({ + container: $container[0] + }) + + $tooltip + .one('shown.bs.tooltip', function () { + assert.strictEqual($container.find('.tooltip').length, 1) + assert.strictEqual($('#qunit-fixture > .tooltip').length, 0, 'tooltip is not in parent') + $tooltip.bootstrapTooltip('hide') + }) + .one('hidden.bs.tooltip', function () { + assert.strictEqual($container.find('.tooltip').length, 0, 'tooltip was removed from dom') + done() + }) + .bootstrapTooltip('show') + }) + + QUnit.test('should place tooltips inside a specific container when container is a selector', function (assert) { + assert.expect(3) + var done = assert.async() + var $container = $('
').appendTo('#qunit-fixture') + var $tooltip = $('
') + .appendTo('#qunit-fixture') + .bootstrapTooltip({ + container: '#container' + }) + + $tooltip + .one('shown.bs.tooltip', function () { + assert.strictEqual($container.find('.tooltip').length, 1) + assert.strictEqual($('#qunit-fixture > .tooltip').length, 0, 'tooltip is not in parent') + $tooltip.bootstrapTooltip('hide') + }) + .one('hidden.bs.tooltip', function () { + assert.strictEqual($container.find('.tooltip').length, 0, 'tooltip was removed from dom') + done() + }) + .bootstrapTooltip('show') + }) + QUnit.test('should add position class before positioning so that position-specific styles are taken into account', function (assert) { assert.expect(2) var done = assert.async() diff --git a/js/tests/visual/tooltip.html b/js/tests/visual/tooltip.html index 8759f3dcda..436f5c1cd8 100644 --- a/js/tests/visual/tooltip.html +++ b/js/tests/visual/tooltip.html @@ -51,8 +51,11 @@ - +