Skip to content

Commit c1727a6

Browse files
committed
refactor(tooltip): add encodeHTMLContent option to specify whether the content should be encoded by default
1 parent ad947d6 commit c1727a6

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

src/component/tooltip/TooltipView.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { bind, each, clone, trim, isString, isFunction, isArray, isObject, exten
2020
import env from 'zrender/src/core/env';
2121
import TooltipHTMLContent from './TooltipHTMLContent';
2222
import TooltipRichContent from './TooltipRichContent';
23-
import { convertToColorString, formatTpl, TooltipMarker } from '../../util/format';
23+
import { convertToColorString, encodeHTML, formatTpl, TooltipMarker } from '../../util/format';
2424
import { parsePercent } from '../../util/number';
2525
import { Rect } from '../../util/graphic';
2626
import findPointFromSeries from '../axisPointer/findPointFromSeries';
@@ -724,16 +724,28 @@ class TooltipView extends ComponentView {
724724
el: ECElement,
725725
dispatchAction: ExtensionAPI['dispatchAction']
726726
) {
727+
const isHTMLRenderMode = this._renderMode === 'html';
727728
const ecData = getECData(el);
728729
const tooltipConfig = ecData.tooltipConfig;
729730
let tooltipOpt = tooltipConfig.option || {};
731+
let encodeHTMLContent = tooltipOpt.encodeHTMLContent;
730732
if (isString(tooltipOpt)) {
731733
const content = tooltipOpt;
732734
tooltipOpt = {
733735
content: content,
734736
// Fixed formatter
735737
formatter: content
736738
};
739+
// when `tooltipConfig.option` is a string rather than an object,
740+
// we can't know if the content needs to be encoded
741+
// for the sake of security, encode it by default.
742+
encodeHTMLContent = true;
743+
}
744+
745+
if (encodeHTMLContent && isHTMLRenderMode && tooltipOpt.content) {
746+
// clone might be unnecessary?
747+
tooltipOpt = clone(tooltipOpt);
748+
tooltipOpt.content = encodeHTML(tooltipOpt.content);
737749
}
738750

739751
const tooltipModelCascade = [tooltipOpt] as TooltipModelOptionCascade[];

src/util/graphic.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ import {
6767
} from 'zrender/src/core/util';
6868
import { getECData } from './innerStore';
6969
import ComponentModel from '../model/Component';
70-
import { encodeHTML } from 'zrender/src/core/dom';
71-
7270

7371
import {
7472
updateProps,
@@ -601,11 +599,11 @@ export function setTooltipConfig(opt: {
601599
const ecData = getECData(opt.el);
602600
ecData.componentMainType = mainType;
603601
ecData.componentIndex = componentIndex;
604-
605602
ecData.tooltipConfig = {
606603
name: itemName,
607604
option: defaults({
608-
content: encodeHTML(itemName),
605+
content: itemName,
606+
encodeHTMLContent: true,
609607
formatterParams: formatterParams
610608
}, itemTooltipOptionObj)
611609
};

src/util/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,12 @@ export interface CommonTooltipOption<FormatterParams> {
13361336
export type ComponentItemTooltipOption<T> = CommonTooltipOption<T> & {
13371337
// Default content HTML.
13381338
content?: string;
1339+
/**
1340+
* Whether to encode HTML content according to `tooltip.renderMode`.
1341+
*
1342+
* e.g. renderMode 'html' needs to encode but 'richText' does not.
1343+
*/
1344+
encodeHTMLContent?: boolean;
13391345
formatterParams?: ComponentItemTooltipLabelFormatterParams;
13401346
};
13411347
export type ComponentItemTooltipLabelFormatterParams = {

0 commit comments

Comments
 (0)