1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-09-28 06:20:15 +02:00

Accept elements as the tooltip / popover content

When a DOM node is passed to an HTML tooltip, the `title` node is only
moved if it is not already in the tooltip. Otherwise, `empty()` is used
instead of `detach()` before appending the `title` to avoid memory
leaks. If a DOM node is passed to a plain text tooltip, its text is
copied via jQuery `.text()`.

Replaces `.detach()` with `.empty()`, as `.detach()` is almost never
useful but instead leaks memory. The difference between `empty` and
`detach` is that the latter keeps all the attached jQuery events/data.
However, since we do not return the previous children, the user would
have to keep these themselves, thus they can `detach()` if necessary.

This is a port of https://github.com/twbs/bootstrap/pull/14552 to v4.
This commit is contained in:
Gleb Mazovetskiy
2015-08-31 00:57:16 +01:00
parent 8941bdfbda
commit c7d8e7a077
6 changed files with 93 additions and 25 deletions

View File

@@ -34,7 +34,7 @@ const Popover = (($) => {
})
const DefaultType = $.extend({}, Tooltip.DefaultType, {
content : '(string|function)'
content : '(string|element|function)'
})
const ClassName = {
@@ -113,24 +113,13 @@ const Popover = (($) => {
}
setContent() {
let tip = this.getTipElement()
let title = this.getTitle()
let content = this._getContent()
let $titleElement = $(tip).find(Selector.TITLE)
if ($titleElement) {
$titleElement[
this.config.html ? 'html' : 'text'
](title)
}
let $tip = $(this.getTipElement())
// we use append for html objects to maintain js events
$(tip).find(Selector.CONTENT).children().detach().end()[
this.config.html ?
(typeof content === 'string' ? 'html' : 'append') : 'text'
](content)
this.setElementContent($tip.find(Selector.TITLE), this.getTitle())
this.setElementContent($tip.find(Selector.CONTENT), this._getContent())
$(tip)
$tip
.removeClass(ClassName.FADE)
.removeClass(ClassName.IN)