5
5
* --------------------------------------------------------------------------
6
6
*/
7
7
8
+ import {
9
+ DefaultWhitelist ,
10
+ sanitizeHtml
11
+ } from './tools/sanitizer'
8
12
import $ from 'jquery'
9
13
import Popper from 'popper.js'
10
14
import Util from './util'
@@ -15,13 +19,14 @@ import Util from './util'
15
19
* ------------------------------------------------------------------------
16
20
*/
17
21
18
- const NAME = 'tooltip'
19
- const VERSION = '4.3.0'
20
- const DATA_KEY = 'bs.tooltip'
21
- const EVENT_KEY = `.${ DATA_KEY } `
22
- const JQUERY_NO_CONFLICT = $ . fn [ NAME ]
23
- const CLASS_PREFIX = 'bs-tooltip'
24
- const BSCLS_PREFIX_REGEX = new RegExp ( `(^|\\s)${ CLASS_PREFIX } \\S+` , 'g' )
22
+ const NAME = 'tooltip'
23
+ const VERSION = '4.3.0'
24
+ const DATA_KEY = 'bs.tooltip'
25
+ const EVENT_KEY = `.${ DATA_KEY } `
26
+ const JQUERY_NO_CONFLICT = $ . fn [ NAME ]
27
+ const CLASS_PREFIX = 'bs-tooltip'
28
+ const BSCLS_PREFIX_REGEX = new RegExp ( `(^|\\s)${ CLASS_PREFIX } \\S+` , 'g' )
29
+ const DISALLOWED_ATTRIBUTES = [ 'sanitize' , 'whiteList' , 'sanitizeFn' ]
25
30
26
31
const DefaultType = {
27
32
animation : 'boolean' ,
@@ -35,7 +40,10 @@ const DefaultType = {
35
40
offset : '(number|string|function)' ,
36
41
container : '(string|element|boolean)' ,
37
42
fallbackPlacement : '(string|array)' ,
38
- boundary : '(string|element)'
43
+ boundary : '(string|element)' ,
44
+ sanitize : 'boolean' ,
45
+ sanitizeFn : '(null|function)' ,
46
+ whiteList : 'object'
39
47
}
40
48
41
49
const AttachmentMap = {
@@ -60,7 +68,10 @@ const Default = {
60
68
offset : 0 ,
61
69
container : false ,
62
70
fallbackPlacement : 'flip' ,
63
- boundary : 'scrollParent'
71
+ boundary : 'scrollParent' ,
72
+ sanitize : true ,
73
+ sanitizeFn : null ,
74
+ whiteList : DefaultWhitelist
64
75
}
65
76
66
77
const HoverState = {
@@ -419,18 +430,27 @@ class Tooltip {
419
430
}
420
431
421
432
setElementContent ( $element , content ) {
422
- const html = this . config . html
423
433
if ( typeof content === 'object' && ( content . nodeType || content . jquery ) ) {
424
434
// Content is a DOM node or a jQuery
425
- if ( html ) {
435
+ if ( this . config . html ) {
426
436
if ( ! $ ( content ) . parent ( ) . is ( $element ) ) {
427
437
$element . empty ( ) . append ( content )
428
438
}
429
439
} else {
430
440
$element . text ( $ ( content ) . text ( ) )
431
441
}
442
+
443
+ return
444
+ }
445
+
446
+ if ( this . config . html ) {
447
+ if ( this . config . sanitize ) {
448
+ content = sanitizeHtml ( content , this . config . whiteList , this . config . sanitizeFn )
449
+ }
450
+
451
+ $element . html ( content )
432
452
} else {
433
- $element [ html ? 'html' : ' text' ] ( content )
453
+ $element . text ( content )
434
454
}
435
455
}
436
456
@@ -636,9 +656,18 @@ class Tooltip {
636
656
}
637
657
638
658
_getConfig ( config ) {
659
+ const dataAttributes = $ ( this . element ) . data ( )
660
+
661
+ Object . keys ( dataAttributes )
662
+ . forEach ( ( dataAttr ) => {
663
+ if ( DISALLOWED_ATTRIBUTES . indexOf ( dataAttr ) !== - 1 ) {
664
+ delete dataAttributes [ dataAttr ]
665
+ }
666
+ } )
667
+
639
668
config = {
640
669
...this . constructor . Default ,
641
- ...$ ( this . element ) . data ( ) ,
670
+ ...dataAttributes ,
642
671
...typeof config === 'object' && config ? config : { }
643
672
}
644
673
@@ -663,6 +692,10 @@ class Tooltip {
663
692
this . constructor . DefaultType
664
693
)
665
694
695
+ if ( config . sanitize ) {
696
+ config . template = sanitizeHtml ( config . template , config . whiteList , config . sanitizeFn )
697
+ }
698
+
666
699
return config
667
700
}
668
701
0 commit comments