mirror of
https://github.com/twbs/bootstrap.git
synced 2025-09-26 05:19:15 +02:00
Add sanitize template option for tooltip/popover plugins.
This commit is contained in:
@@ -5,6 +5,10 @@
|
||||
* --------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
import {
|
||||
DefaultWhitelist,
|
||||
sanitizeHtml
|
||||
} from './tools/sanitizer'
|
||||
import $ from 'jquery'
|
||||
import Popper from 'popper.js'
|
||||
import Util from './util'
|
||||
@@ -15,13 +19,14 @@ import Util from './util'
|
||||
* ------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
const NAME = 'tooltip'
|
||||
const VERSION = '4.3.0'
|
||||
const DATA_KEY = 'bs.tooltip'
|
||||
const EVENT_KEY = `.${DATA_KEY}`
|
||||
const JQUERY_NO_CONFLICT = $.fn[NAME]
|
||||
const CLASS_PREFIX = 'bs-tooltip'
|
||||
const BSCLS_PREFIX_REGEX = new RegExp(`(^|\\s)${CLASS_PREFIX}\\S+`, 'g')
|
||||
const NAME = 'tooltip'
|
||||
const VERSION = '4.3.0'
|
||||
const DATA_KEY = 'bs.tooltip'
|
||||
const EVENT_KEY = `.${DATA_KEY}`
|
||||
const JQUERY_NO_CONFLICT = $.fn[NAME]
|
||||
const CLASS_PREFIX = 'bs-tooltip'
|
||||
const BSCLS_PREFIX_REGEX = new RegExp(`(^|\\s)${CLASS_PREFIX}\\S+`, 'g')
|
||||
const DISALLOWED_ATTRIBUTES = ['sanitize', 'whiteList', 'sanitizeFn']
|
||||
|
||||
const DefaultType = {
|
||||
animation : 'boolean',
|
||||
@@ -35,7 +40,10 @@ const DefaultType = {
|
||||
offset : '(number|string|function)',
|
||||
container : '(string|element|boolean)',
|
||||
fallbackPlacement : '(string|array)',
|
||||
boundary : '(string|element)'
|
||||
boundary : '(string|element)',
|
||||
sanitize : 'boolean',
|
||||
sanitizeFn : '(null|function)',
|
||||
whiteList : 'object'
|
||||
}
|
||||
|
||||
const AttachmentMap = {
|
||||
@@ -60,7 +68,10 @@ const Default = {
|
||||
offset : 0,
|
||||
container : false,
|
||||
fallbackPlacement : 'flip',
|
||||
boundary : 'scrollParent'
|
||||
boundary : 'scrollParent',
|
||||
sanitize : true,
|
||||
sanitizeFn : null,
|
||||
whiteList : DefaultWhitelist
|
||||
}
|
||||
|
||||
const HoverState = {
|
||||
@@ -419,18 +430,27 @@ class Tooltip {
|
||||
}
|
||||
|
||||
setElementContent($element, content) {
|
||||
const html = this.config.html
|
||||
if (typeof content === 'object' && (content.nodeType || content.jquery)) {
|
||||
// Content is a DOM node or a jQuery
|
||||
if (html) {
|
||||
if (this.config.html) {
|
||||
if (!$(content).parent().is($element)) {
|
||||
$element.empty().append(content)
|
||||
}
|
||||
} else {
|
||||
$element.text($(content).text())
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (this.config.html) {
|
||||
if (this.config.sanitize) {
|
||||
content = sanitizeHtml(content, this.config.whiteList, this.config.sanitizeFn)
|
||||
}
|
||||
|
||||
$element.html(content)
|
||||
} else {
|
||||
$element[html ? 'html' : 'text'](content)
|
||||
$element.text(content)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -636,9 +656,18 @@ class Tooltip {
|
||||
}
|
||||
|
||||
_getConfig(config) {
|
||||
const dataAttributes = $(this.element).data()
|
||||
|
||||
Object.keys(dataAttributes)
|
||||
.forEach((dataAttr) => {
|
||||
if (DISALLOWED_ATTRIBUTES.indexOf(dataAttr) !== -1) {
|
||||
delete dataAttributes[dataAttr]
|
||||
}
|
||||
})
|
||||
|
||||
config = {
|
||||
...this.constructor.Default,
|
||||
...$(this.element).data(),
|
||||
...dataAttributes,
|
||||
...typeof config === 'object' && config ? config : {}
|
||||
}
|
||||
|
||||
@@ -663,6 +692,10 @@ class Tooltip {
|
||||
this.constructor.DefaultType
|
||||
)
|
||||
|
||||
if (config.sanitize) {
|
||||
config.template = sanitizeHtml(config.template, config.whiteList, config.sanitizeFn)
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user