@@ -263,7 +263,7 @@ export class SlickCustomTooltip {
263263 */
264264 protected renderRegularTooltip ( formatterOrText : Formatter | string | undefined , cell : { row : number ; cell : number ; } , value : any , columnDef : Column , item : any ) {
265265 const tmpDiv = document . createElement ( 'div' ) ;
266- tmpDiv . innerHTML = this . parseFormatterAndSanitize ( formatterOrText , cell , value , columnDef , item ) ;
266+ this . _grid . applyHtmlCode ( tmpDiv , this . parseFormatterAndSanitize ( formatterOrText , cell , value , columnDef , item ) ) ;
267267 let tooltipText = columnDef . toolTip || '' ;
268268 let tmpTitleElm ;
269269
@@ -427,12 +427,12 @@ export class SlickCustomTooltip {
427427 * Parse the Custom Formatter (when provided) or return directly the text when it is already a string.
428428 * We will also sanitize the text in both cases before returning it so that it can be used safely.
429429 */
430- protected parseFormatterAndSanitize ( formatterOrText : Formatter | string | undefined , cell : { row : number ; cell : number ; } , value : any , columnDef : Column , item : unknown ) : string {
430+ protected parseFormatterAndSanitize ( formatterOrText : Formatter | string | undefined , cell : { row : number ; cell : number ; } , value : any , columnDef : Column , item : unknown ) : string | HTMLElement {
431431 if ( typeof formatterOrText === 'function' ) {
432432 const tooltipResult = formatterOrText ( cell . row , cell . cell , value , columnDef , item , this . _grid ) ;
433- let formatterText = ( Object . prototype . toString . call ( tooltipResult ) !== '[object Object]' ? tooltipResult : ( tooltipResult as FormatterResultWithHtml ) . html || ( tooltipResult as FormatterResultWithText ) . text ) ;
433+ const formatterText = ( Object . prototype . toString . call ( tooltipResult ) !== '[object Object]' ? tooltipResult : ( tooltipResult as FormatterResultWithHtml ) . html || ( tooltipResult as FormatterResultWithText ) . text ) ;
434434 if ( formatterText instanceof HTMLElement ) {
435- formatterText = formatterText . outerHTML ;
435+ return formatterText ;
436436 }
437437 return this . _grid . sanitizeHtmlString ( formatterText as string ) ;
438438 } else if ( typeof formatterOrText === 'string' ) {
@@ -450,15 +450,27 @@ export class SlickCustomTooltip {
450450 this . _tooltipElm . classList . add ( 'l' + cell . cell ) ;
451451 this . _tooltipElm . classList . add ( 'r' + cell . cell ) ;
452452 let outputText = tooltipText || this . parseFormatterAndSanitize ( formatter , cell , value , columnDef , item ) || '' ;
453- outputText = ( this . _cellTooltipOptions . tooltipTextMaxLength && outputText . length > this . _cellTooltipOptions . tooltipTextMaxLength ) ? outputText . substring ( 0 , this . _cellTooltipOptions . tooltipTextMaxLength - 3 ) + '...' : outputText ;
453+ if ( outputText instanceof HTMLElement ) {
454+ const content = outputText . textContent || '' ;
455+ if ( this . _cellTooltipOptions . tooltipTextMaxLength && content . length > this . _cellTooltipOptions . tooltipTextMaxLength ) {
456+ outputText . textContent = content . substring ( 0 , this . _cellTooltipOptions . tooltipTextMaxLength - 3 ) + '...' ;
457+ }
458+ } else {
459+ outputText = ( this . _cellTooltipOptions . tooltipTextMaxLength && outputText . length > this . _cellTooltipOptions . tooltipTextMaxLength ) ? outputText . substring ( 0 , this . _cellTooltipOptions . tooltipTextMaxLength - 3 ) + '...' : outputText ;
460+ }
454461
455462 let finalOutputText = '' ;
456463 if ( ! tooltipText || ( this . _cellTooltipOptions ?. renderRegularTooltipAsHtml ) ) {
457- finalOutputText = this . _grid . sanitizeHtmlString ( outputText ) ;
458- this . _tooltipElm . innerHTML = finalOutputText ;
464+ if ( outputText instanceof HTMLElement ) {
465+ this . _grid . applyHtmlCode ( this . _tooltipElm , outputText ) ;
466+ finalOutputText = this . _grid . sanitizeHtmlString ( outputText . textContent || '' ) ;
467+ } else {
468+ finalOutputText = this . _grid . sanitizeHtmlString ( outputText ) ;
469+ this . _tooltipElm . innerHTML = finalOutputText ;
470+ }
459471 this . _tooltipElm . style . whiteSpace = this . _cellTooltipOptions ?. whiteSpace ?? this . _defaults . whiteSpace as string ;
460472 } else {
461- finalOutputText = outputText || '' ;
473+ finalOutputText = ( outputText instanceof HTMLElement ? outputText . textContent : outputText ) || '' ;
462474 this . _tooltipElm . textContent = finalOutputText ;
463475 this . _tooltipElm . style . whiteSpace = this . _cellTooltipOptions ?. regularTooltipWhiteSpace ?? this . _defaults . regularTooltipWhiteSpace as string ; // use `pre` so that sequences of white space are collapsed. Lines are broken at newline characters
464476 }
@@ -484,7 +496,7 @@ export class SlickCustomTooltip {
484496 }
485497
486498 // also clear any "title" attribute to avoid showing a 2nd browser tooltip
487- this . swapAndClearTitleAttribute ( inputTitleElm , outputText ) ;
499+ this . swapAndClearTitleAttribute ( inputTitleElm , ( outputText instanceof HTMLElement ? outputText . textContent : outputText ) || '' ) ;
488500 }
489501 }
490502
0 commit comments