aboutsummaryrefslogtreecommitdiff
path: root/js/src/tooltip.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/src/tooltip.js')
-rw-r--r--js/src/tooltip.js73
1 files changed, 39 insertions, 34 deletions
diff --git a/js/src/tooltip.js b/js/src/tooltip.js
index 78b7c478b..e09a53b5c 100644
--- a/js/src/tooltip.js
+++ b/js/src/tooltip.js
@@ -1,6 +1,6 @@
/**
* --------------------------------------------------------------------------
- * Bootstrap (v5.0.1): tooltip.js
+ * Bootstrap (v5.0.2): tooltip.js
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
* --------------------------------------------------------------------------
*/
@@ -17,10 +17,7 @@ import {
noop,
typeCheckConfig
} from './util/index'
-import {
- DefaultAllowlist,
- sanitizeHtml
-} from './util/sanitizer'
+import { DefaultAllowlist, sanitizeHtml } from './util/sanitizer'
import Data from './dom/data'
import EventHandler from './dom/event-handler'
import Manipulator from './dom/manipulator'
@@ -37,7 +34,6 @@ const NAME = 'tooltip'
const DATA_KEY = 'bs.tooltip'
const EVENT_KEY = `.${DATA_KEY}`
const CLASS_PREFIX = 'bs-tooltip'
-const BSCLS_PREFIX_REGEX = new RegExp(`(^|\\s)${CLASS_PREFIX}\\S+`, 'g')
const DISALLOWED_ATTRIBUTES = new Set(['sanitize', 'allowList', 'sanitizeFn'])
const DefaultType = {
@@ -273,7 +269,7 @@ class Tooltip extends BaseComponent {
tip.classList.add(CLASS_NAME_SHOW)
- const customClass = typeof this._config.customClass === 'function' ? this._config.customClass() : this._config.customClass
+ const customClass = this._resolvePossibleFunction(this._config.customClass)
if (customClass) {
tip.classList.add(...customClass.split(' '))
}
@@ -371,14 +367,27 @@ class Tooltip extends BaseComponent {
const element = document.createElement('div')
element.innerHTML = this._config.template
- this.tip = element.children[0]
+ const tip = element.children[0]
+ tip.classList.remove(CLASS_NAME_FADE, CLASS_NAME_SHOW)
+
+ this.tip = tip
return this.tip
}
setContent() {
const tip = this.getTipElement()
- this.setElementContent(SelectorEngine.findOne(SELECTOR_TOOLTIP_INNER, tip), this.getTitle())
- tip.classList.remove(CLASS_NAME_FADE, CLASS_NAME_SHOW)
+ this._sanitizeAndSetContent(tip, this.getTitle(), SELECTOR_TOOLTIP_INNER)
+ }
+
+ _sanitizeAndSetContent(template, content, selector) {
+ const templateElement = SelectorEngine.findOne(selector, template)
+ if (!content) {
+ templateElement.remove()
+ return
+ }
+
+ // we use append for html objects to maintain js events
+ this.setElementContent(templateElement, content)
}
setElementContent(element, content) {
@@ -414,15 +423,9 @@ class Tooltip extends BaseComponent {
}
getTitle() {
- let title = this._element.getAttribute('data-bs-original-title')
+ const title = this._element.getAttribute('data-bs-original-title') || this._config.title
- if (!title) {
- title = typeof this._config.title === 'function' ?
- this._config.title.call(this._element) :
- this._config.title
- }
-
- return title
+ return this._resolvePossibleFunction(title)
}
updateAttachment(attachment) {
@@ -440,15 +443,7 @@ class Tooltip extends BaseComponent {
// Private
_initializeOnDelegatedTarget(event, context) {
- const dataKey = this.constructor.DATA_KEY
- context = context || Data.get(event.delegateTarget, dataKey)
-
- if (!context) {
- context = new this.constructor(event.delegateTarget, this._getDelegateConfig())
- Data.set(event.delegateTarget, dataKey, context)
- }
-
- return context
+ return context || this.constructor.getOrCreateInstance(event.delegateTarget, this._getDelegateConfig())
}
_getOffset() {
@@ -465,6 +460,10 @@ class Tooltip extends BaseComponent {
return offset
}
+ _resolvePossibleFunction(content) {
+ return typeof content === 'function' ? content.call(this._element) : content
+ }
+
_getPopperConfig(attachment) {
const defaultBsPopperConfig = {
placement: attachment,
@@ -514,7 +513,7 @@ class Tooltip extends BaseComponent {
}
_addAttachmentClass(attachment) {
- this.getTipElement().classList.add(`${CLASS_PREFIX}-${this.updateAttachment(attachment)}`)
+ this.getTipElement().classList.add(`${this._getBasicClassPrefix()}-${this.updateAttachment(attachment)}`)
}
_getAttachment(placement) {
@@ -686,26 +685,32 @@ class Tooltip extends BaseComponent {
_getDelegateConfig() {
const config = {}
- if (this._config) {
- for (const key in this._config) {
- if (this.constructor.Default[key] !== this._config[key]) {
- config[key] = this._config[key]
- }
+ for (const key in this._config) {
+ if (this.constructor.Default[key] !== this._config[key]) {
+ config[key] = this._config[key]
}
}
+ // In the future can be replaced with:
+ // const keysWithDifferentValues = Object.entries(this._config).filter(entry => this.constructor.Default[entry[0]] !== this._config[entry[0]])
+ // `Object.fromEntries(keysWithDifferentValues)`
return config
}
_cleanTipClass() {
const tip = this.getTipElement()
- const tabClass = tip.getAttribute('class').match(BSCLS_PREFIX_REGEX)
+ const basicClassPrefixRegex = new RegExp(`(^|\\s)${this._getBasicClassPrefix()}\\S+`, 'g')
+ const tabClass = tip.getAttribute('class').match(basicClassPrefixRegex)
if (tabClass !== null && tabClass.length > 0) {
tabClass.map(token => token.trim())
.forEach(tClass => tip.classList.remove(tClass))
}
}
+ _getBasicClassPrefix() {
+ return CLASS_PREFIX
+ }
+
_handlePopperPlacementChange(popperData) {
const { state } = popperData