Skip to content

Commit fc37295

Browse files
committed
Throw error when tooltip/popover template has multiple top-level elements
Closes twbs#16219.
1 parent 8949bce commit fc37295

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

js/tests/unit/popover.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,16 @@ $(function () {
259259
assert.strictEqual($popover.data('bs.popover'), undefined, 'should not initialize the popover')
260260
})
261261

262+
QUnit.test('should throw an error when template contains multiple top-level elements', function (assert) {
263+
assert.expect(1)
264+
assert.throws(function () {
265+
$('<span data-toggle="popover" data-title="some title" data-content="some content">some text</span>')
266+
.appendTo('#qunit-fixture')
267+
.bootstrapPopover({ template: '<div>Foo</div><div>Bar</div>' })
268+
.bootstrapPopover('show')
269+
}, new Error('popover `template` option must consist of exactly 1 top-level element!'))
270+
})
271+
262272
QUnit.test('should fire inserted event', function (assert) {
263273
assert.expect(2)
264274
var done = assert.async()
@@ -276,4 +286,5 @@ $(function () {
276286
})
277287
.bootstrapPopover('show')
278288
})
289+
279290
})

js/tests/unit/tooltip.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,4 +1225,14 @@ $(function () {
12251225
assert.strictEqual($tooltip.data('bs.tooltip'), undefined, 'should not initialize the tooltip')
12261226
})
12271227

1228+
QUnit.test('should throw an error when template contains multiple top-level elements', function (assert) {
1229+
assert.expect(1)
1230+
assert.throws(function () {
1231+
$('<a href="#" data-toggle="tooltip" title="Another tooltip"></a>')
1232+
.appendTo('#qunit-fixture')
1233+
.bootstrapTooltip({ template: '<div>Foo</div><div>Bar</div>' })
1234+
.bootstrapTooltip('show')
1235+
}, new Error('tooltip `template` option must consist of exactly 1 top-level element!'))
1236+
})
1237+
12281238
})

js/tooltip.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,13 @@
404404
}
405405

406406
Tooltip.prototype.tip = function () {
407-
return (this.$tip = this.$tip || $(this.options.template))
407+
if (!this.$tip) {
408+
this.$tip = $(this.options.template)
409+
if (this.$tip.length != 1) {
410+
throw new Error(this.type + ' `template` option must consist of exactly 1 top-level element!')
411+
}
412+
}
413+
return this.$tip
408414
}
409415

410416
Tooltip.prototype.arrow = function () {

0 commit comments

Comments
 (0)