Skip to content

Commit cb9033c

Browse files
fix(tooltip): prevent XSS in default HTML rendering (#369)
* fix(tooltip): prevent XSS in default HTML rendering * fix: 修复 CR 问题 * fix: 简化代码 * chore: 更新测试截图 * fix: 兼容 G 新版本导致的 TS 报错问题 * Revert "chore: 更新测试截图" This reverts commit 996f8e5. * fix: 参考 g2 触发两次 RAF * fix: 修复坐标轴偏差的 bug --------- Co-authored-by: hustcc <i@hust.cc>
1 parent f860e37 commit cb9033c

20 files changed

Lines changed: 265 additions & 60 deletions

‎__tests__/integration/canvas.ts‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { DisplayObject } from '@antv/g';
22
import { Canvas, CanvasEvent, resetEntityCounter } from '@antv/g';
33
import { Renderer } from '@antv/g-svg';
44
import { OffscreenCanvasContext, measureText } from './utils/offscreen-canvas-context';
5-
import { setMockMeasureTextWidth } from '../../src';
5+
import { Layout, setMockMeasureTextWidth } from '../../src';
66

77
export function createGCanvas(width: number, height: number) {
88
resetEntityCounter();
@@ -38,6 +38,12 @@ export function sleep(n: number) {
3838
});
3939
}
4040

41+
function nextFrame(canvas: Canvas) {
42+
return new Promise<void>((resolve) => {
43+
canvas.requestAnimationFrame(() => resolve());
44+
});
45+
}
46+
4147
export async function renderCanvas(gshape: DisplayObject, wait = 300) {
4248
const bbox = gshape.getBBox();
4349
const width = gshape.attributes.width || bbox.x + bbox.width || 400;
@@ -48,8 +54,34 @@ export async function renderCanvas(gshape: DisplayObject, wait = 300) {
4854
canvas.addEventListener(CanvasEvent.READY, async () => {
4955
canvas.appendChild(gshape);
5056

51-
// Wait for the next tick.
57+
// Wait for components, animations and G's mutation queue to settle.
5258
await sleep(wait);
59+
60+
// Layout nodes are populated before being connected to the canvas, so
61+
// their insertion events can run before layout has measurable children.
62+
// Trigger a final layout after mounting, as G2 does during rendering.
63+
let layoutTriggered = false;
64+
canvas.getRoot().forEach((node) => {
65+
if (
66+
node instanceof Layout &&
67+
node.children.length > 0 &&
68+
node.children.every((child) => child.attributes.transform === undefined)
69+
) {
70+
node.layout();
71+
layoutTriggered = true;
72+
}
73+
});
74+
75+
// Flush the layout mutations before serializing the SVG. The explicit
76+
// render keeps this deterministic in Jest, where RAF scheduling differs
77+
// from a browser's render loop.
78+
if (layoutTriggered) {
79+
canvas.render();
80+
81+
// Match G2's render completion: wait two frames before taking a snapshot.
82+
await nextFrame(canvas);
83+
await nextFrame(canvas);
84+
}
5385
resolve(canvas);
5486
});
5587
});

‎__tests__/integration/snapshots/AxisLinearLabelRender1.svg‎

Lines changed: 4 additions & 4 deletions
Loading

‎__tests__/integration/snapshots/AxisLinearLabelRender2.svg‎

Lines changed: 4 additions & 4 deletions
Loading

‎__tests__/integration/snapshots/AxisLinearLabelRender3.svg‎

Lines changed: 4 additions & 4 deletions
Loading

‎__tests__/integration/snapshots/LayoutLegend2.svg‎

Lines changed: 3 additions & 3 deletions
Loading

‎__tests__/integration/snapshots/LayoutLegend3.svg‎

Lines changed: 3 additions & 3 deletions
Loading

‎__tests__/integration/snapshots/LayoutLegend5.svg‎

Lines changed: 3 additions & 3 deletions
Loading

‎__tests__/integration/snapshots/LayoutLegend6.svg‎

Lines changed: 3 additions & 3 deletions
Loading

‎__tests__/integration/snapshots/LayoutLegend8.svg‎

Lines changed: 3 additions & 3 deletions
Loading

‎__tests__/integration/snapshots/LayoutLegend9.svg‎

Lines changed: 3 additions & 3 deletions
Loading

0 commit comments

Comments
 (0)