reifyui 0.11.3 → 0.11.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chart.cjs +66 -2
- package/dist/chart.cjs.map +1 -1
- package/dist/chart.d.ts +8 -0
- package/dist/chart.js +12 -2
- package/dist/chart.js.map +1 -1
- package/dist/chunk-FPLC5KAD.js +62 -0
- package/dist/chunk-FPLC5KAD.js.map +1 -0
- package/dist/slides.cjs +62 -15
- package/dist/slides.cjs.map +1 -1
- package/dist/slides.js +6 -14
- package/dist/slides.js.map +1 -1
- package/package.json +1 -1
- package/src/chart/Chart.jsx +9 -2
- package/src/chart/index.d.ts +8 -0
- package/src/chart/index.js +2 -0
- package/src/chart/safe.js +66 -0
- package/src/slides/elements.jsx +7 -11
package/src/chart/Chart.jsx
CHANGED
|
@@ -11,6 +11,13 @@
|
|
|
11
11
|
// not in the root index — the same reason `reifyui/slides` is its own entry. An app that imports
|
|
12
12
|
// a Button must not resolve a charting library.
|
|
13
13
|
import { useEffect, useRef, useState } from 'react';
|
|
14
|
+
import { safeChartOption } from './safe.js';
|
|
15
|
+
|
|
16
|
+
// Every option reaches ECharts through here, made inert first (safe.js): a tooltip draws text, a link is http(s) or
|
|
17
|
+
// mailto, the data view's words are escaped. An option a host sets on the instance it got from onInit is its own.
|
|
18
|
+
export function applyOption(chart, option, notMerge) {
|
|
19
|
+
chart.setOption(safeChartOption(option), notMerge);
|
|
20
|
+
}
|
|
14
21
|
|
|
15
22
|
/**
|
|
16
23
|
* option the ECharts option. Applied whenever the REFERENCE changes, so build it in a
|
|
@@ -43,7 +50,7 @@ export function Chart(props) {
|
|
|
43
50
|
const chart = echarts.init(host.current, null, { renderer });
|
|
44
51
|
inst.current = chart;
|
|
45
52
|
const { option: opt, notMerge: nm, onInit: cb } = latest.current;
|
|
46
|
-
if (opt) { chart
|
|
53
|
+
if (opt) { applyOption(chart, opt, nm); applied.current = opt; }
|
|
47
54
|
if (cb) cb(chart);
|
|
48
55
|
} catch {
|
|
49
56
|
if (!dead) setFailed(true); // no chart code: say so, never draw a fake one
|
|
@@ -61,7 +68,7 @@ export function Chart(props) {
|
|
|
61
68
|
// arrives asynchronously, so both paths run on the first render that has one.
|
|
62
69
|
useEffect(() => {
|
|
63
70
|
if (!inst.current || !option || applied.current === option) return;
|
|
64
|
-
inst.current
|
|
71
|
+
applyOption(inst.current, option, notMerge);
|
|
65
72
|
applied.current = option;
|
|
66
73
|
}, [option, notMerge]);
|
|
67
74
|
|
package/src/chart/index.d.ts
CHANGED
|
@@ -55,3 +55,11 @@ export function chartTokens(el?: Element | null): ChartTokens;
|
|
|
55
55
|
/** chartTokens(), re-read when the theme changes (a class/data-* flip on <html> or <body>, or the
|
|
56
56
|
* OS preference). Put the returned ref on the element the chart lives in. */
|
|
57
57
|
export function useChartTokens(): [ChartTokens, RefObject<HTMLElement | null>];
|
|
58
|
+
|
|
59
|
+
/** A copy of an option with what ECharts would write as HTML or open as a URL made inert: every tooltip draws text
|
|
60
|
+
* (renderMode 'richText'), a link or sublink is kept only for http(s) or mailto, and the data view's title and labels
|
|
61
|
+
* are escaped. Chart applies it to every option; call it yourself before setOption on the instance from onInit. */
|
|
62
|
+
export function safeChartOption<T>(option: T): T;
|
|
63
|
+
/** The value when it is an http(s) or mailto URL, else ''. */
|
|
64
|
+
export function safeHref(value: unknown): string;
|
|
65
|
+
|
package/src/chart/index.js
CHANGED
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
// dependency graph of every consumer, so an app that renders a Button and no chart would have to
|
|
5
5
|
// resolve a charting library to build. Same rule, same reason, as 'reifyui/slides'.
|
|
6
6
|
export { Chart } from './Chart.jsx';
|
|
7
|
+
// What the chart does to every option before ECharts sees it, for a host that sets options on the instance itself.
|
|
8
|
+
export { safeChartOption, safeHref } from './safe.js';
|
|
7
9
|
// The theme as strings a chart option can hold — series colours, axis ink, tooltip chrome. CSS
|
|
8
10
|
// variables do not reach inside a canvas; these do.
|
|
9
11
|
export { chartTokens } from './tokens.js';
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// An ECharts option can come from anywhere (a person, a saved dashboard, an agent writing it), so the chart keeps it
|
|
2
|
+
// inert before ECharts sees it (found 2026-11-10). ECharts writes some option strings into the page as HTML or opens
|
|
3
|
+
// them as URLs:
|
|
4
|
+
// - a tooltip's content (its formatter, series and data names) in the default HTML render mode;
|
|
5
|
+
// - a title's link / sublink and a sunburst or treemap node's link, opened in a window that keeps the page's origin;
|
|
6
|
+
// - the data view's title and button labels (toolbox.feature.dataView.title / lang).
|
|
7
|
+
// So every tooltip draws in the chart itself (renderMode 'richText': text, never markup), a link is kept only when it is
|
|
8
|
+
// http(s) or mailto, and the data view's words are escaped. The option passed in is never changed: plain objects and
|
|
9
|
+
// arrays are copied, anything else (functions, typed arrays, dates, gradient objects) is passed through as it is.
|
|
10
|
+
const SAFE_HREF = /^(https?:\/\/|mailto:)/i;
|
|
11
|
+
|
|
12
|
+
export function safeHref(v) {
|
|
13
|
+
return typeof v === 'string' && SAFE_HREF.test(v.replace(/[\u0000- \u007f]/g, '')) ? v : '';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const ESC = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' };
|
|
17
|
+
const esc = (s) => String(s).replace(/[&<>"']/g, (c) => ESC[c]);
|
|
18
|
+
|
|
19
|
+
const isPlain = (v) => {
|
|
20
|
+
if (!v || typeof v !== 'object' || Array.isArray(v)) return false;
|
|
21
|
+
const p = Object.getPrototypeOf(v);
|
|
22
|
+
return p === Object.prototype || p === null;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export function safeChartOption(option) {
|
|
26
|
+
if (!isPlain(option) && !Array.isArray(option)) return option;
|
|
27
|
+
const seen = new Map();
|
|
28
|
+
const escaped = new Set();
|
|
29
|
+
const walk = (n) => {
|
|
30
|
+
if (Array.isArray(n)) {
|
|
31
|
+
if (seen.has(n)) return seen.get(n);
|
|
32
|
+
const out = []; seen.set(n, out);
|
|
33
|
+
for (const x of n) out.push(walk(x));
|
|
34
|
+
return out;
|
|
35
|
+
}
|
|
36
|
+
if (!isPlain(n)) return n;
|
|
37
|
+
if (seen.has(n)) return seen.get(n);
|
|
38
|
+
const out = {}; seen.set(n, out);
|
|
39
|
+
for (const k of Object.keys(n)) {
|
|
40
|
+
const v = n[k];
|
|
41
|
+
if ((k === 'link' || k === 'sublink') && typeof v === 'string') {
|
|
42
|
+
if (safeHref(v)) out[k] = v;
|
|
43
|
+
} else if (k === 'tooltip') {
|
|
44
|
+
out[k] = tooltip(v);
|
|
45
|
+
} else if (k === 'dataView' && isPlain(v)) {
|
|
46
|
+
out[k] = dataView(walk(v));
|
|
47
|
+
} else {
|
|
48
|
+
out[k] = walk(v);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return out;
|
|
52
|
+
};
|
|
53
|
+
const tooltip = (v) => {
|
|
54
|
+
if (v === true) return { renderMode: 'richText' };
|
|
55
|
+
if (Array.isArray(v)) return v.map((t) => (isPlain(t) ? { ...walk(t), renderMode: 'richText' } : walk(t)));
|
|
56
|
+
return isPlain(v) ? { ...walk(v), renderMode: 'richText' } : walk(v);
|
|
57
|
+
};
|
|
58
|
+
const dataView = (d) => {
|
|
59
|
+
if (escaped.has(d)) return d; // the same data view reached twice: escape once
|
|
60
|
+
escaped.add(d);
|
|
61
|
+
if (typeof d.title === 'string') d.title = esc(d.title);
|
|
62
|
+
if (Array.isArray(d.lang)) d.lang = d.lang.map((s) => (typeof s === 'string' ? esc(s) : s));
|
|
63
|
+
return d;
|
|
64
|
+
};
|
|
65
|
+
return walk(option);
|
|
66
|
+
}
|
package/src/slides/elements.jsx
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
// about placement. Charts (ECharts) and flowcharts (Mermaid) load lazily and
|
|
6
6
|
// render to SVG so they stay crisp in the canvas, thumbnails, and PDF.
|
|
7
7
|
import { useEffect, useRef, useState } from 'react';
|
|
8
|
+
import { safeChartOption } from '../chart/safe.js';
|
|
8
9
|
|
|
9
10
|
// A deck can come from anywhere (a save, the copilot, a peer writing the live document), so the renderer itself
|
|
10
11
|
// keeps it inert: a link is a link only when it is http(s) or mailto (React 18 renders a javascript: href), and a
|
|
@@ -13,17 +14,11 @@ const SAFE_HREF = /^(https?:\/\/|mailto:)/i;
|
|
|
13
14
|
export function safeHref(v) {
|
|
14
15
|
return typeof v === 'string' && SAFE_HREF.test(v.replace(/[\u0000-\u0020\u007f]/g, '')) ? v : '';
|
|
15
16
|
}
|
|
17
|
+
// A deck's chart spec goes through the same inert pass as reifyui/chart's options (chart/safe.js): every tooltip at
|
|
18
|
+
// any level (root, baseOption, timeline options, media, series, data) draws text, a link is http(s) or mailto, and the
|
|
19
|
+
// data view's words are escaped. 11-10: the top-level-only pass here left a baseOption tooltip and the data view as HTML.
|
|
16
20
|
export function safeChartSpec(spec) {
|
|
17
|
-
|
|
18
|
-
const walk = (n) => {
|
|
19
|
-
if (Array.isArray(n)) { n.forEach(walk); return; }
|
|
20
|
-
if (!n || typeof n !== 'object') return;
|
|
21
|
-
for (const k of ['link', 'sublink']) if (k in n && !safeHref(n[k])) delete n[k];
|
|
22
|
-
Object.values(n).forEach(walk);
|
|
23
|
-
};
|
|
24
|
-
walk(out);
|
|
25
|
-
for (const t of [].concat(out.tooltip || [])) if (t && typeof t === 'object') t.renderMode = 'richText';
|
|
26
|
-
return out;
|
|
21
|
+
return safeChartOption(spec);
|
|
27
22
|
}
|
|
28
23
|
|
|
29
24
|
// Rich-text runs → spans with marks. role drives default sizing (the stage is
|
|
@@ -107,7 +102,8 @@ function FlowchartEl({ el }) {
|
|
|
107
102
|
if (!src) return;
|
|
108
103
|
try {
|
|
109
104
|
const mermaid = (await import('mermaid')).default || (await import('mermaid'));
|
|
110
|
-
|
|
105
|
+
// 'strict' named, not left to mermaid's default: labels are text and click handlers are off (CAT 11-10)
|
|
106
|
+
mermaid.initialize({ startOnLoad: false, theme: 'neutral', securityLevel: 'strict' });
|
|
111
107
|
const id = 'mmd_' + Math.random().toString(36).slice(2);
|
|
112
108
|
const { svg } = await mermaid.render(id, src);
|
|
113
109
|
if (!dead) setSvg(svg);
|