@micha.bigler/ui-core-micha 3.2.1 → 3.3.1
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/CHANGELOG.md +44 -0
- package/WORK_ORDERS.md +2 -0
- package/dist/components/charts/ChartFrame.js +7 -3
- package/dist/components/charts/exportChart.js +199 -23
- package/dist/i18n/chartsTranslations.js +17 -0
- package/package.json +1 -1
- package/src/components/charts/ChartFrame.jsx +29 -20
- package/src/components/charts/exportChart.js +210 -23
- package/src/i18n/chartsTranslations.ts +17 -0
- package/tests/ChartFrame.test.jsx +15 -0
- package/tests/exportChart.test.js +313 -0
- package/work-orders/UCM-CHART-17.md +195 -0
- package/work-orders/UCM-CHART-18.md +200 -0
|
@@ -1,12 +1,133 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
// UCM-CHART-17: the two exports promise two different things and now keep them.
|
|
2
|
+
//
|
|
3
|
+
// SVG stays a true, portable vector: only the chart's own MUI x-charts surface, but with the
|
|
4
|
+
// computed styles that render it correctly (font, colour, stroke) written inline, since nothing
|
|
5
|
+
// downstream of a cloned+serialised node carries the page's emotion CSS classes. PNG rasterises
|
|
6
|
+
// the whole container -- chart, hand-built legend, size key, footnotes -- because that is what
|
|
7
|
+
// the screen actually shows (`AccessGapScatterPanel` and its siblings render the legend/size key
|
|
8
|
+
// as HTML/SVG siblings of the chart since `HRAM-RES-39`/`HRAM-VIS-2`, outside the chart's own SVG).
|
|
9
|
+
//
|
|
10
|
+
// Both paths route DOM through an SVG (`<foreignObject>` for the PNG's arbitrary HTML+SVG mix,
|
|
11
|
+
// direct serialisation for the chart-only SVG) into an `Image`, then a `<canvas>` -- the platform's
|
|
12
|
+
// own APIs, no rasteriser dependency added (this package's only runtime dependency stays
|
|
13
|
+
// `@fontsource/dm-sans`). A library was seriously considered, not just skipped: the naive version
|
|
14
|
+
// of this technique (an `Image` loaded from a `blob:` URL) taints the canvas on `toBlob` in Chrome
|
|
15
|
+
// the moment the SVG contains a `<foreignObject>`, unconditionally -- see `rasterize`'s own comment
|
|
16
|
+
// for how that was confirmed live and worked around with a `data:` URI instead, which is what
|
|
17
|
+
// makes the platform-only approach actually viable rather than merely attempted.
|
|
18
|
+
//
|
|
19
|
+
// Font embedding: NOT done. Inlined `font-family` records the real value (e.g. `"DM Sans", ...`
|
|
20
|
+
// with its documented fallback stack) instead of nothing at all, which is already a correctness
|
|
21
|
+
// improvement over the pre-fix export (unstyled, arbitrary browser default). A machine without DM
|
|
22
|
+
// Sans installed falls back through that stack -- an honest, visible degradation, not a silent one,
|
|
23
|
+
// and embedding the font file was judged not worth the added complexity/weight for this WO.
|
|
24
|
+
|
|
25
|
+
const SVG_NS = 'http://www.w3.org/2000/svg';
|
|
26
|
+
const XHTML_NS = 'http://www.w3.org/1999/xhtml';
|
|
27
|
+
|
|
28
|
+
// Typography and SVG paint -- what actually makes a chart LOOK like the theme instead of bare SVG
|
|
29
|
+
// defaults (tick fonts and sizes, axis/grid colours, text fill). Deliberately curated, not a full
|
|
30
|
+
// `getComputedStyle` dump: every extra property is bytes in a file a human may open in Illustrator
|
|
31
|
+
// or Inkscape.
|
|
32
|
+
// UCM-CHART-18: `transform`/`transform-origin` added -- a rotated element (e.g. a legend swatch
|
|
33
|
+
// rotated into a diamond) previously exported unrotated, since nothing wrote the resolved CSS
|
|
34
|
+
// transform onto the clone. `getComputedStyle` returns the FINAL, resolved matrix -- already
|
|
35
|
+
// incorporating an SVG element's own `transform` PRESENTATION ATTRIBUTE, which `cloneNode` also
|
|
36
|
+
// keeps -- and an inline `style` declaration always wins over a presentation attribute (SVG2:
|
|
37
|
+
// presentation attributes carry the lowest possible specificity), so writing the computed value
|
|
38
|
+
// inline overrides rather than composes with it. Verified live against a rotated legend swatch and
|
|
39
|
+
// against `xLabels="angled"` tick labels: neither double-rotates or double-displaces.
|
|
40
|
+
const CHART_STYLE_PROPERTIES = [
|
|
41
|
+
'font-family', 'font-size', 'font-weight', 'font-style', 'line-height', 'letter-spacing',
|
|
42
|
+
'fill', 'stroke', 'stroke-width', 'stroke-dasharray', 'stroke-linecap', 'stroke-linejoin',
|
|
43
|
+
'opacity', 'fill-opacity', 'stroke-opacity', 'text-anchor', 'dominant-baseline', 'color',
|
|
44
|
+
'transform', 'transform-origin',
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
// Box layout/appearance for the HTML legend and size key a panel renders beside the chart --
|
|
48
|
+
// only needed for the PNG's whole-container rasterisation, where a hand-built legend is plain
|
|
49
|
+
// HTML with no chart-specific paint properties of its own. `overflow` and the grid properties
|
|
50
|
+
// (reviewer finding) cover MUI's own composed layouts too, not only a hand-built flex legend --
|
|
51
|
+
// `ChartsSurface` itself sits in a `grid-area`, so a container that lets MUI position multiple
|
|
52
|
+
// parts via CSS Grid needs those to reposition correctly once cloned out of the page.
|
|
53
|
+
const CONTAINER_LAYOUT_STYLE_PROPERTIES = [
|
|
54
|
+
'background-color', 'border-color', 'border-width', 'border-style', 'border-radius',
|
|
55
|
+
'display', 'flex-direction', 'flex-wrap', 'flex', 'align-items', 'justify-content',
|
|
56
|
+
'justify-items', 'align-content', 'gap', 'overflow',
|
|
57
|
+
'grid-template-columns', 'grid-template-rows', 'grid-template-areas', 'grid-area',
|
|
58
|
+
'padding-top', 'padding-right', 'padding-bottom', 'padding-left',
|
|
59
|
+
'margin-top', 'margin-right', 'margin-bottom', 'margin-left',
|
|
60
|
+
'width', 'height', 'box-sizing', 'text-align', 'white-space',
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
// Deliberate, not positional (UCM-CHART-17 scope item 3): MUI x-charts' own root `<svg>` always
|
|
64
|
+
// carries `MuiChartsSurface-root` (`@mui/x-charts/ChartsSurface`) -- a stable selector, unlike "the
|
|
65
|
+
// first svg in the container", which silently exported a size-key SVG once one existed ahead of
|
|
66
|
+
// the chart. Falls back to the first SVG for a bespoke (non-MUI) SVG child, which `ChartFrame`'s
|
|
67
|
+
// own docblock explicitly allows.
|
|
68
|
+
function findChartSvg(chartContainer) {
|
|
69
|
+
const svg = chartContainer?.querySelector('svg.MuiChartsSurface-root') || chartContainer?.querySelector('svg');
|
|
3
70
|
if (!svg) throw new Error('Chart container does not contain an SVG element.');
|
|
4
71
|
return svg;
|
|
5
72
|
}
|
|
6
73
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
74
|
+
// UCM-CHART-18: an exported image shows what the chart COMMUNICATES, not what can be clicked --
|
|
75
|
+
// a panel's own controls (an info `IconButton`, a drill-down `Button`, ...) live in `children`
|
|
76
|
+
// alongside the chart (`ChartFrame`'s `chartRef` wraps `children` only, not `toolbar`/`controls`/
|
|
77
|
+
// `meta`), so the whole-container PNG picked them up once it started rasterising `chartRef` in
|
|
78
|
+
// full (`UCM-CHART-17`). `[role="button"]` catches MUI's own non-native button-like elements (e.g.
|
|
79
|
+
// an `IconButton` can render as a `<span>`/`<div>` with that role) that a tag-name selector alone
|
|
80
|
+
// would miss; `a[href]`/`[role="link"]` (reviewer finding) cover a drill-down built as a link
|
|
81
|
+
// rather than a `<button>`.
|
|
82
|
+
const INTERACTIVE_CONTROL_SELECTOR = 'button, input, select, textarea, [role="button"], a[href], [role="link"]';
|
|
83
|
+
|
|
84
|
+
// ui_reviewer + reviewer finding, independently: MUI's OWN chart legend can be interactive
|
|
85
|
+
// (`onItemClick`/`toggleVisibilityOnClick`) and, when it is, X-Charts renders each legend item as
|
|
86
|
+
// `<button role="button" class="...MuiChartsLegend-series...">`, WRAPPING the series swatch and
|
|
87
|
+
// label -- that markup IS the legend's explanatory content, not chrome, and the WO's own risk
|
|
88
|
+
// section says exactly this: "if it vanishes, that is a finding that changes the approach, not a
|
|
89
|
+
// detail to paper over." A blanket `.remove()` would delete the swatch+label along with the click
|
|
90
|
+
// handler. Unwrapped instead: replace the interactive element with a plain, non-interactive one
|
|
91
|
+
// carrying the SAME class/style/children, so the legend keeps every pixel of content while losing
|
|
92
|
+
// only its clickability (irrelevant anyway -- a rasterised PNG has no click handlers regardless).
|
|
93
|
+
const MUI_INTERACTIVE_LEGEND_ITEM_SELECTOR = '.MuiChartsLegend-series';
|
|
94
|
+
|
|
95
|
+
function stripInteractiveControls(clone) {
|
|
96
|
+
clone.querySelectorAll(INTERACTIVE_CONTROL_SELECTOR).forEach((node) => {
|
|
97
|
+
if (node.matches(MUI_INTERACTIVE_LEGEND_ITEM_SELECTOR)) {
|
|
98
|
+
const unwrapped = document.createElement('span');
|
|
99
|
+
unwrapped.className = node.className;
|
|
100
|
+
const style = node.getAttribute('style');
|
|
101
|
+
if (style) unwrapped.setAttribute('style', style);
|
|
102
|
+
while (node.firstChild) unwrapped.appendChild(node.firstChild);
|
|
103
|
+
node.replaceWith(unwrapped);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
node.remove();
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Walks `sourceRoot` and `cloneRoot` in lockstep (both traversed in the same, deterministic
|
|
111
|
+
// document order, since `cloneRoot` is a structural copy of `sourceRoot`) and writes each node's
|
|
112
|
+
// RESOLVED style for `properties` onto the clone as an inline `style` attribute. This is what
|
|
113
|
+
// carries the page's emotion CSS classes (which the clone keeps by name but not by rule) into a
|
|
114
|
+
// standalone file that has no access to that stylesheet.
|
|
115
|
+
function inlineComputedStyles(sourceRoot, cloneRoot, properties) {
|
|
116
|
+
const sourceNodes = [sourceRoot, ...sourceRoot.querySelectorAll('*')];
|
|
117
|
+
const cloneNodes = [cloneRoot, ...cloneRoot.querySelectorAll('*')];
|
|
118
|
+
sourceNodes.forEach((sourceNode, index) => {
|
|
119
|
+
const cloneNode = cloneNodes[index];
|
|
120
|
+
if (!cloneNode || cloneNode.nodeType !== Node.ELEMENT_NODE) return;
|
|
121
|
+
const computed = window.getComputedStyle(sourceNode);
|
|
122
|
+
const declarations = properties
|
|
123
|
+
.map((property) => `${property}: ${computed.getPropertyValue(property)}`)
|
|
124
|
+
.join('; ');
|
|
125
|
+
const existing = cloneNode.getAttribute('style');
|
|
126
|
+
cloneNode.setAttribute('style', existing ? `${existing}; ${declarations}` : declarations);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function svgToBlob(svg) {
|
|
10
131
|
return new Blob([new XMLSerializer().serializeToString(svg)], { type: 'image/svg+xml;charset=utf-8' });
|
|
11
132
|
}
|
|
12
133
|
|
|
@@ -21,26 +142,78 @@ function download(blob, filename) {
|
|
|
21
142
|
URL.revokeObjectURL(url);
|
|
22
143
|
}
|
|
23
144
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
145
|
+
// The chart's own surface, faithfully styled -- true vector, chart only. Does not gain the
|
|
146
|
+
// legend/size key rendered beside it; that is correct for what this format is for (Part A).
|
|
147
|
+
function chartSvgBlob(chartContainer) {
|
|
148
|
+
const svg = findChartSvg(chartContainer);
|
|
149
|
+
const clone = svg.cloneNode(true);
|
|
150
|
+
inlineComputedStyles(svg, clone, CHART_STYLE_PROPERTIES);
|
|
151
|
+
// reviewer finding: MUI's ChartsSurface only ever gives the root <svg> a `viewBox` -- its actual
|
|
152
|
+
// pixel size comes from CSS (`width: 100%; height: 100%` against the page's own layout), which
|
|
153
|
+
// has nothing to resolve against once cloned out to a standalone file. Without an explicit
|
|
154
|
+
// width/height a standalone viewer falls back to the SVG UA default (typically 300x150).
|
|
155
|
+
const rect = svg.getBoundingClientRect();
|
|
156
|
+
clone.setAttribute('width', Math.ceil(rect.width) || svg.clientWidth || 1);
|
|
157
|
+
clone.setAttribute('height', Math.ceil(rect.height) || svg.clientHeight || 1);
|
|
158
|
+
clone.setAttribute('xmlns', SVG_NS);
|
|
159
|
+
return svgToBlob(clone);
|
|
29
160
|
}
|
|
30
161
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
162
|
+
// The whole container, as seen -- chart plus everything the panel renders beside it, via an SVG
|
|
163
|
+
// `<foreignObject>` wrapping a styled clone of the container's actual DOM. Standard platform
|
|
164
|
+
// technique for DOM rasterisation without a library: the intermediate SVG is never downloaded, only
|
|
165
|
+
// decoded into an `Image` and drawn onto a `<canvas>`.
|
|
166
|
+
function containerSvgBlob(chartContainer) {
|
|
167
|
+
const rect = chartContainer.getBoundingClientRect();
|
|
168
|
+
const width = Math.ceil(rect.width) || chartContainer.scrollWidth || 1;
|
|
169
|
+
const height = Math.ceil(rect.height) || chartContainer.scrollHeight || 1;
|
|
34
170
|
|
|
171
|
+
const clone = chartContainer.cloneNode(true);
|
|
172
|
+
inlineComputedStyles(chartContainer, clone, [...CHART_STYLE_PROPERTIES, ...CONTAINER_LAYOUT_STYLE_PROPERTIES]);
|
|
173
|
+
// UCM-CHART-18 scope item 2, deliberately AFTER inlineComputedStyles, never before:
|
|
174
|
+
// inlineComputedStyles walks `chartContainer` and `clone` IN LOCKSTEP BY INDEX
|
|
175
|
+
// (`sourceNodes[i]` <-> `cloneNodes[i]`, both from `querySelectorAll('*')`). Removing nodes from
|
|
176
|
+
// the clone first would shift every subsequent index out of alignment with the still-intact
|
|
177
|
+
// source tree, so elements downstream of the first removal would silently receive a DIFFERENT
|
|
178
|
+
// element's computed style -- wrong, but not obviously wrong, since the result still looks like a
|
|
179
|
+
// styled export.
|
|
180
|
+
stripInteractiveControls(clone);
|
|
181
|
+
clone.setAttribute('xmlns', XHTML_NS);
|
|
182
|
+
|
|
183
|
+
const svg = document.createElementNS(SVG_NS, 'svg');
|
|
184
|
+
svg.setAttribute('xmlns', SVG_NS);
|
|
185
|
+
svg.setAttribute('width', width);
|
|
186
|
+
svg.setAttribute('height', height);
|
|
187
|
+
svg.setAttribute('viewBox', `0 0 ${width} ${height}`);
|
|
188
|
+
const foreignObject = document.createElementNS(SVG_NS, 'foreignObject');
|
|
189
|
+
foreignObject.setAttribute('width', '100%');
|
|
190
|
+
foreignObject.setAttribute('height', '100%');
|
|
191
|
+
foreignObject.appendChild(clone);
|
|
192
|
+
svg.appendChild(foreignObject);
|
|
193
|
+
|
|
194
|
+
return { svg, width, height };
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// UCM-CHART-17 risk section, confirmed live (not assumed): an SVG containing a `<foreignObject>`
|
|
198
|
+
// taints the canvas on `toBlob` in Chrome -- unconditionally, even with zero external references
|
|
199
|
+
// (verified: a foreignObject holding nothing but plain styled text already triggers
|
|
200
|
+
// `SecurityError: Tainted canvases may not be exported`) -- but ONLY when the image is loaded via
|
|
201
|
+
// a `blob:` object URL. The identical SVG loaded via a `data:` URI does not taint the canvas at
|
|
202
|
+
// all. This is why the SVG-only export (`chartSvgBlob`, no foreignObject) can keep using
|
|
203
|
+
// `URL.createObjectURL` for its own download, while the PNG path's `Image` source specifically
|
|
204
|
+
// must be a `data:` URI. No rasteriser dependency needed once this is accounted for.
|
|
205
|
+
async function svgToDataUri(svg) {
|
|
206
|
+
const svgString = await svgToBlob(svg).text();
|
|
207
|
+
return `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svgString)}`;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
async function rasterize(svg, width, height, backgroundColour) {
|
|
211
|
+
const dataUri = await svgToDataUri(svg);
|
|
35
212
|
return new Promise((resolve, reject) => {
|
|
36
|
-
const url = URL.createObjectURL(source);
|
|
37
213
|
const image = new Image();
|
|
38
214
|
|
|
39
215
|
image.onload = () => {
|
|
40
216
|
try {
|
|
41
|
-
const svg = findSvg(chartContainer);
|
|
42
|
-
const width = Number(svg.getAttribute('width')) || svg.clientWidth || 1;
|
|
43
|
-
const height = Number(svg.getAttribute('height')) || svg.clientHeight || 1;
|
|
44
217
|
const canvas = document.createElement('canvas');
|
|
45
218
|
canvas.width = width * 2;
|
|
46
219
|
canvas.height = height * 2;
|
|
@@ -49,23 +222,37 @@ export function exportChartPng(chartContainer, filename = 'chart.png', backgroun
|
|
|
49
222
|
context.fillRect(0, 0, canvas.width, canvas.height);
|
|
50
223
|
context.drawImage(image, 0, 0, canvas.width, canvas.height);
|
|
51
224
|
canvas.toBlob((blob) => {
|
|
52
|
-
URL.revokeObjectURL(url);
|
|
53
225
|
if (!blob) {
|
|
54
226
|
reject(new Error('Unable to create PNG export.'));
|
|
55
227
|
return;
|
|
56
228
|
}
|
|
57
|
-
download(blob, filename);
|
|
58
229
|
resolve(blob);
|
|
59
230
|
}, 'image/png');
|
|
60
231
|
} catch (error) {
|
|
61
|
-
URL.revokeObjectURL(url);
|
|
62
232
|
reject(error);
|
|
63
233
|
}
|
|
64
234
|
};
|
|
65
235
|
image.onerror = () => {
|
|
66
|
-
|
|
67
|
-
reject(new Error('Unable to load chart SVG for PNG export.'));
|
|
236
|
+
reject(new Error('Unable to rasterize chart container for PNG export.'));
|
|
68
237
|
};
|
|
69
|
-
image.src =
|
|
238
|
+
image.src = dataUri;
|
|
70
239
|
});
|
|
71
240
|
}
|
|
241
|
+
|
|
242
|
+
/** Serializes the chart's own SVG surface, styles inlined, and downloads it. */
|
|
243
|
+
export function exportChartSvg(chartContainer, filename = 'chart.svg') {
|
|
244
|
+
const blob = chartSvgBlob(chartContainer);
|
|
245
|
+
download(blob, filename);
|
|
246
|
+
return blob;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Rasterizes the whole chart container -- chart, legend, size key, footnotes, everything the panel
|
|
251
|
+
* renders beside the chart -- at two-times scale and downloads it.
|
|
252
|
+
*/
|
|
253
|
+
export async function exportChartPng(chartContainer, filename = 'chart.png', backgroundColour) {
|
|
254
|
+
const { svg, width, height } = containerSvgBlob(chartContainer);
|
|
255
|
+
const blob = await rasterize(svg, width, height, backgroundColour);
|
|
256
|
+
download(blob, filename);
|
|
257
|
+
return blob;
|
|
258
|
+
}
|
|
@@ -23,12 +23,29 @@ export const chartsTranslations = {
|
|
|
23
23
|
en: 'Export SVG',
|
|
24
24
|
sw: 'Hamisha SVG',
|
|
25
25
|
},
|
|
26
|
+
// UCM-CHART-17: the two formats now genuinely differ (SVG = chart only, vector; PNG = the whole
|
|
27
|
+
// panel as shown, legend included) -- the label alone no longer says so, the tooltip does.
|
|
28
|
+
'ChartFrame.EXPORT_SVG_TOOLTIP': {
|
|
29
|
+
de: 'Skalierbare Vektorgrafik des Diagramms allein, ohne Legende.',
|
|
30
|
+
fr: 'Image vectorielle du graphique seul, sans la légende.',
|
|
31
|
+
en: 'Scalable vector image of the chart alone, without the legend.',
|
|
32
|
+
sw: 'Picha ya vekta inayoweza kubadilishwa ukubwa ya chati pekee, bila maelezo.',
|
|
33
|
+
},
|
|
26
34
|
'ChartFrame.EXPORT_PNG_LABEL': {
|
|
27
35
|
de: 'PNG exportieren',
|
|
28
36
|
fr: 'Exporter en PNG',
|
|
29
37
|
en: 'Export PNG',
|
|
30
38
|
sw: 'Hamisha PNG',
|
|
31
39
|
},
|
|
40
|
+
// ui_reviewer finding: the original wording named the legend but not the size key/footnotes
|
|
41
|
+
// the PNG also carries -- named explicitly now, matching the Envelope's own Goal wording
|
|
42
|
+
// ("legend, size key and footnotes").
|
|
43
|
+
'ChartFrame.EXPORT_PNG_TOOLTIP': {
|
|
44
|
+
de: 'Bild des ganzen Kartenbereichs, wie angezeigt, inklusive Legende, Grössenschlüssel und Fussnoten.',
|
|
45
|
+
fr: 'Image de toute la zone de la carte, telle qu’affichée, légende, clé de taille et notes de bas de page comprises.',
|
|
46
|
+
en: 'Image of the whole card area as shown, including the legend, size key, and footnotes.',
|
|
47
|
+
sw: 'Picha ya eneo lote la kadi kama linavyoonyeshwa, ikijumuisha maelezo, kifunguo cha ukubwa, na maelezo ya chini.',
|
|
48
|
+
},
|
|
32
49
|
'ChartFrame.EXPORT_ERROR': {
|
|
33
50
|
de: 'Das Diagramm konnte nicht exportiert werden.',
|
|
34
51
|
fr: 'Impossible d’exporter le graphique.',
|
|
@@ -22,7 +22,9 @@ const resources = {
|
|
|
22
22
|
'ChartFrame.EMPTY_DEFAULT': 'No data available.',
|
|
23
23
|
'ChartFrame.ERROR_DEFAULT': 'The chart could not be loaded.',
|
|
24
24
|
'ChartFrame.EXPORT_SVG_LABEL': 'Export SVG',
|
|
25
|
+
'ChartFrame.EXPORT_SVG_TOOLTIP': 'Scalable vector image of the chart alone, without the legend.',
|
|
25
26
|
'ChartFrame.EXPORT_PNG_LABEL': 'Export PNG',
|
|
27
|
+
'ChartFrame.EXPORT_PNG_TOOLTIP': 'Image of the whole card area as shown, including the legend, size key, and footnotes.',
|
|
26
28
|
'ChartFrame.EXPORT_ERROR': 'The chart could not be exported.',
|
|
27
29
|
},
|
|
28
30
|
},
|
|
@@ -32,7 +34,9 @@ const resources = {
|
|
|
32
34
|
'ChartFrame.EMPTY_DEFAULT': 'Aucune donnée disponible.',
|
|
33
35
|
'ChartFrame.ERROR_DEFAULT': 'Impossible de charger le graphique.',
|
|
34
36
|
'ChartFrame.EXPORT_SVG_LABEL': 'Exporter en SVG',
|
|
37
|
+
'ChartFrame.EXPORT_SVG_TOOLTIP': 'Image vectorielle du graphique seul, sans la légende.',
|
|
35
38
|
'ChartFrame.EXPORT_PNG_LABEL': 'Exporter en PNG',
|
|
39
|
+
'ChartFrame.EXPORT_PNG_TOOLTIP': 'Image de toute la zone de la carte, telle qu’affichée, légende, clé de taille et notes de bas de page comprises.',
|
|
36
40
|
'ChartFrame.EXPORT_ERROR': 'Impossible d’exporter le graphique.',
|
|
37
41
|
},
|
|
38
42
|
},
|
|
@@ -162,6 +166,17 @@ describe('ChartFrame', () => {
|
|
|
162
166
|
await vi.waitFor(() => expect(onExportPng).toHaveBeenCalledOnce());
|
|
163
167
|
});
|
|
164
168
|
|
|
169
|
+
// UCM-CHART-17: the two formats now genuinely differ (chart-only vector vs. the whole card as
|
|
170
|
+
// shown) -- each button must say so, without silently renaming itself in the process (MUI
|
|
171
|
+
// Tooltip's default `aria-label` behaviour would do exactly that; `describeChild` avoids it).
|
|
172
|
+
it('describes what each export format contains without replacing the button\'s own accessible name', () => {
|
|
173
|
+
renderFrame({ exportOptions: true });
|
|
174
|
+
const svgButton = screen.getByRole('button', { name: 'Export SVG' });
|
|
175
|
+
const pngButton = screen.getByRole('button', { name: 'Export PNG' });
|
|
176
|
+
expect(svgButton.getAttribute('title')).toBe('Scalable vector image of the chart alone, without the legend.');
|
|
177
|
+
expect(pngButton.getAttribute('title')).toBe('Image of the whole card area as shown, including the legend, size key, and footnotes.');
|
|
178
|
+
});
|
|
179
|
+
|
|
165
180
|
it('keeps the controls row rendered in the loading, error and empty states', () => {
|
|
166
181
|
const controls = <button type="button">View selector</button>;
|
|
167
182
|
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import {
|
|
3
|
+
afterEach, beforeEach, describe, expect, it, vi,
|
|
4
|
+
} from 'vitest';
|
|
5
|
+
|
|
6
|
+
import { exportChartPng, exportChartSvg } from '../src/components/charts/exportChart';
|
|
7
|
+
|
|
8
|
+
// UCM-CHART-17: a container shaped like `AccessGapScatterPanel` -- the chart's own MUI surface,
|
|
9
|
+
// PLUS a second SVG (the hand-built size key) and an HTML legend beside it, exactly the shape that
|
|
10
|
+
// broke "the first SVG" (scope item 3) and dropped the legend from every export (Part A, defect 2).
|
|
11
|
+
function buildContainer() {
|
|
12
|
+
const container = document.createElement('div');
|
|
13
|
+
container.innerHTML = `
|
|
14
|
+
<div class="legend">Legend HTML</div>
|
|
15
|
+
<svg class="MuiChartsSurface-root" width="300" height="200">
|
|
16
|
+
<g><text class="MuiChartsAxis-tickLabel">Alpha</text></g>
|
|
17
|
+
<line class="MuiChartsAxis-line" x1="0" x2="300" y1="10" y2="10"></line>
|
|
18
|
+
</svg>
|
|
19
|
+
<svg class="size-key" width="40" height="40"><circle r="4"></circle></svg>
|
|
20
|
+
`;
|
|
21
|
+
document.body.appendChild(container);
|
|
22
|
+
return container;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
describe('exportChartSvg (UCM-CHART-17)', () => {
|
|
26
|
+
afterEach(() => {
|
|
27
|
+
document.body.innerHTML = '';
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('resolves with its existing signature and returns a Blob', () => {
|
|
31
|
+
const container = buildContainer();
|
|
32
|
+
const blob = exportChartSvg(container, 'chart.svg');
|
|
33
|
+
expect(blob).toBeInstanceOf(Blob);
|
|
34
|
+
expect(blob.type).toBe('image/svg+xml;charset=utf-8');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// reviewer finding: MUI's ChartsSurface only ever carries a `viewBox` -- its real pixel size is
|
|
38
|
+
// CSS (`width: 100%`), which has nothing to resolve against once cloned out of the page. Without
|
|
39
|
+
// an explicit width/height a standalone viewer falls back to the SVG UA default (300x150), i.e.
|
|
40
|
+
// the wrong size regardless of how faithfully everything else is styled.
|
|
41
|
+
it('sets explicit width/height attributes on the exported SVG, not just the source viewBox', async () => {
|
|
42
|
+
const container = buildContainer();
|
|
43
|
+
const blob = exportChartSvg(container);
|
|
44
|
+
const text = await blob.text();
|
|
45
|
+
expect(text).toMatch(/<svg[^>]*\bwidth="\d+"/);
|
|
46
|
+
expect(text).toMatch(/<svg[^>]*\bheight="\d+"/);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// WO scope item 3: the legend sits BEFORE the chart's own svg in this fixture (as it does in
|
|
50
|
+
// `AccessGapScatterPanel`), and a second, unrelated SVG (the size key) sits after it -- "the
|
|
51
|
+
// first SVG" would grab neither of those correctly. `MuiChartsSurface-root` is the deliberate,
|
|
52
|
+
// non-positional signal.
|
|
53
|
+
it('takes the chart surface deliberately, not the first (or any other) SVG in the container', async () => {
|
|
54
|
+
const container = buildContainer();
|
|
55
|
+
const blob = exportChartSvg(container);
|
|
56
|
+
const text = await blob.text();
|
|
57
|
+
expect(text).toContain('MuiChartsSurface-root');
|
|
58
|
+
expect(text).toContain('Alpha');
|
|
59
|
+
expect(text).not.toContain('size-key');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// WO defect 1 + Unit tests: the clone must carry the RESOLVED style, not just the (rule-less,
|
|
63
|
+
// once serialised standalone) class name -- this is the whole point of the fix.
|
|
64
|
+
it('inlines computed styles for tick labels and axis strokes onto the cloned SVG', async () => {
|
|
65
|
+
const container = buildContainer();
|
|
66
|
+
const tickLabel = container.querySelector('.MuiChartsAxis-tickLabel');
|
|
67
|
+
tickLabel.style.fontFamily = 'DM Sans';
|
|
68
|
+
tickLabel.style.fill = 'rgb(10, 20, 30)';
|
|
69
|
+
const axisLine = container.querySelector('.MuiChartsAxis-line');
|
|
70
|
+
axisLine.style.stroke = 'rgb(200, 200, 200)';
|
|
71
|
+
|
|
72
|
+
const blob = exportChartSvg(container);
|
|
73
|
+
const text = await blob.text();
|
|
74
|
+
expect(text).toContain('font-family: "DM Sans"');
|
|
75
|
+
expect(text).toContain('fill: rgb(10, 20, 30)');
|
|
76
|
+
expect(text).toContain('stroke: rgb(200, 200, 200)');
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('does not gain the legend or size key -- true vector, chart only', async () => {
|
|
80
|
+
const container = buildContainer();
|
|
81
|
+
const blob = exportChartSvg(container);
|
|
82
|
+
const text = await blob.text();
|
|
83
|
+
expect(text).not.toContain('Legend HTML');
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// UCM-CHART-18 scope item 3: a rotated element (e.g. a legend swatch rotated into a diamond)
|
|
87
|
+
// must export rotated -- `transform` was missing from the inlined properties entirely. Applied
|
|
88
|
+
// via a STYLESHEET rule, not an inline style directly on the source node -- `cloneNode` copies
|
|
89
|
+
// an element's own inline `style` ATTRIBUTE verbatim regardless of what `inlineComputedStyles`
|
|
90
|
+
// does, which would make a test that sets `tickLabel.style.transform` pass whether or not
|
|
91
|
+
// `transform` is actually in the inlined property list (exactly the "asserts a number, proves
|
|
92
|
+
// nothing" trap the WO's risk section warns about) -- a class-based rule is only ever picked up
|
|
93
|
+
// through `getComputedStyle`, so it genuinely exercises `inlineComputedStyles`.
|
|
94
|
+
it('inlines the resolved transform onto the cloned chart surface', async () => {
|
|
95
|
+
const container = buildContainer();
|
|
96
|
+
const style = document.createElement('style');
|
|
97
|
+
style.textContent = '.rotated-probe { transform: rotate(45deg); transform-origin: 20% 30%; }';
|
|
98
|
+
document.head.appendChild(style);
|
|
99
|
+
const tickLabel = container.querySelector('.MuiChartsAxis-tickLabel');
|
|
100
|
+
tickLabel.classList.add('rotated-probe');
|
|
101
|
+
|
|
102
|
+
const blob = exportChartSvg(container);
|
|
103
|
+
const text = await blob.text();
|
|
104
|
+
document.head.removeChild(style);
|
|
105
|
+
expect(text).toMatch(/transform:\s*rotate\(45deg\)/);
|
|
106
|
+
expect(text).toMatch(/transform-origin:\s*20%\s*30%/);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('throws when the container has no SVG at all', () => {
|
|
110
|
+
const container = document.createElement('div');
|
|
111
|
+
document.body.appendChild(container);
|
|
112
|
+
expect(() => exportChartSvg(container)).toThrow(/does not contain an SVG/);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe('exportChartPng (UCM-CHART-17)', () => {
|
|
117
|
+
let originalGetContext;
|
|
118
|
+
let originalToBlob;
|
|
119
|
+
let originalImage;
|
|
120
|
+
|
|
121
|
+
beforeEach(() => {
|
|
122
|
+
originalGetContext = HTMLCanvasElement.prototype.getContext;
|
|
123
|
+
originalToBlob = HTMLCanvasElement.prototype.toBlob;
|
|
124
|
+
// jsdom has no real canvas/image-decoding backend (`getContext('2d')` returns null, `Image`
|
|
125
|
+
// never fires `onload`) -- this stands in for the platform pipeline `exportChartPng` drives,
|
|
126
|
+
// to test its OWN plumbing (does it resolve, with the right Blob type, for the documented
|
|
127
|
+
// input shape), not visual output. Visual correctness is the WO's own explicit non-unit-test
|
|
128
|
+
// gate: open the exported files.
|
|
129
|
+
HTMLCanvasElement.prototype.getContext = vi.fn(() => ({
|
|
130
|
+
fillStyle: '',
|
|
131
|
+
fillRect: vi.fn(),
|
|
132
|
+
drawImage: vi.fn(),
|
|
133
|
+
}));
|
|
134
|
+
HTMLCanvasElement.prototype.toBlob = function toBlob(callback) {
|
|
135
|
+
callback(new Blob(['png-bytes'], { type: 'image/png' }));
|
|
136
|
+
};
|
|
137
|
+
originalImage = global.Image;
|
|
138
|
+
global.Image = class MockImage {
|
|
139
|
+
set src(value) {
|
|
140
|
+
this._src = value;
|
|
141
|
+
queueMicrotask(() => this.onload?.());
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// eslint-disable-next-line class-methods-use-this
|
|
145
|
+
get src() {
|
|
146
|
+
return this._src;
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
afterEach(() => {
|
|
152
|
+
document.body.innerHTML = '';
|
|
153
|
+
HTMLCanvasElement.prototype.getContext = originalGetContext;
|
|
154
|
+
HTMLCanvasElement.prototype.toBlob = originalToBlob;
|
|
155
|
+
global.Image = originalImage;
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// Captures the actual intermediate SVG `exportChartPng` builds (intercepting the `Image` src it
|
|
159
|
+
// loads, per `rasterize`'s own data:-URI choice -- see the taint-avoidance test above) and
|
|
160
|
+
// decodes it back to markup, so a test can inspect exactly what got serialised rather than only
|
|
161
|
+
// whether the call resolved.
|
|
162
|
+
async function captureExportedMarkup(container, backgroundColour = '#ffffff') {
|
|
163
|
+
let capturedSrc;
|
|
164
|
+
global.Image = class CapturingImage {
|
|
165
|
+
set src(value) {
|
|
166
|
+
capturedSrc = value;
|
|
167
|
+
this._src = value;
|
|
168
|
+
queueMicrotask(() => this.onload?.());
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// eslint-disable-next-line class-methods-use-this
|
|
172
|
+
get src() {
|
|
173
|
+
return this._src;
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
await exportChartPng(container, 'chart.png', backgroundColour);
|
|
177
|
+
return decodeURIComponent(capturedSrc.replace(/^data:image\/svg\+xml;charset=utf-8,/, ''));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// WO Unit tests: "the PNG path resolves for a container holding more than one SVG" -- the exact
|
|
181
|
+
// shape (chart surface + size key) that the pre-fix `findSvg`'s "first SVG" logic depended on
|
|
182
|
+
// being ordered correctly.
|
|
183
|
+
it('resolves with its existing signature for a container holding more than one SVG', async () => {
|
|
184
|
+
const container = buildContainer();
|
|
185
|
+
const blob = await exportChartPng(container, 'chart.png', '#ffffff');
|
|
186
|
+
expect(blob).toBeInstanceOf(Blob);
|
|
187
|
+
expect(blob.type).toBe('image/png');
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
// Risk section, confirmed live against a real browser (see `rasterize`'s own comment): a
|
|
191
|
+
// `blob:` object URL taints the canvas the moment the SVG carries a `<foreignObject>`, in every
|
|
192
|
+
// case tested, `toBlob` then throws regardless of content. A `data:` URI does not. Locking this
|
|
193
|
+
// in so a future edit reaching back for `URL.createObjectURL` here regresses to the taint, not
|
|
194
|
+
// just to a style change.
|
|
195
|
+
it('loads the container SVG as a data: URI, never a blob: URL, to avoid canvas tainting', async () => {
|
|
196
|
+
const container = buildContainer();
|
|
197
|
+
let capturedSrc;
|
|
198
|
+
global.Image = class CapturingImage {
|
|
199
|
+
set src(value) {
|
|
200
|
+
capturedSrc = value;
|
|
201
|
+
this._src = value;
|
|
202
|
+
queueMicrotask(() => this.onload?.());
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// eslint-disable-next-line class-methods-use-this
|
|
206
|
+
get src() {
|
|
207
|
+
return this._src;
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
await exportChartPng(container, 'chart.png', '#ffffff');
|
|
211
|
+
expect(capturedSrc).toMatch(/^data:image\/svg\+xml/);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
// UCM-CHART-18 defect 1: `chartRef` wraps `children` only, so a panel's own controls (e.g. an
|
|
215
|
+
// info `IconButton` in the footer) are inside what gets rasterised. An exported image should
|
|
216
|
+
// show what the chart communicates, not what can be clicked. The button is placed BEFORE a
|
|
217
|
+
// later, unrelated sibling (reviewer finding: the original fixture had the button as the LAST
|
|
218
|
+
// descendant, so a "strip before inline" ordering regression would still have left every
|
|
219
|
+
// asserted node's index aligned and the test would have passed either way) -- `After the button`
|
|
220
|
+
// is what actually exercises the lockstep-index trap the WO names as the single most likely way
|
|
221
|
+
// to get this wrong.
|
|
222
|
+
it('drops interactive controls from the whole-container PNG, keeping non-interactive siblings both before and after it', async () => {
|
|
223
|
+
const container = buildContainer();
|
|
224
|
+
// The distinctive colour on `#after-button`, applied via a stylesheet rule (so it is only
|
|
225
|
+
// ever picked up through `getComputedStyle`, never copied verbatim by `cloneNode`), is what
|
|
226
|
+
// actually exercises the lockstep-index trap: content-presence assertions alone pass whether
|
|
227
|
+
// or not the button is removed before or after inlining (removing a node shifts which style
|
|
228
|
+
// string lands on which SURVIVING element, but every surviving TEXT NODE is still there
|
|
229
|
+
// either way) -- only checking that THIS specific element carries THIS specific colour proves
|
|
230
|
+
// its index wasn't shifted onto a neighbour's style by an out-of-order removal.
|
|
231
|
+
const style = document.createElement('style');
|
|
232
|
+
style.textContent = '#after-button { color: rgb(1, 2, 3); }';
|
|
233
|
+
document.head.appendChild(style);
|
|
234
|
+
const footer = document.createElement('div');
|
|
235
|
+
footer.innerHTML = '<span>Caveat note</span><button type="button">More about these caveats</button><span id="after-button">After the button</span>';
|
|
236
|
+
container.appendChild(footer);
|
|
237
|
+
|
|
238
|
+
const svgMarkup = await captureExportedMarkup(container);
|
|
239
|
+
document.head.removeChild(style);
|
|
240
|
+
expect(svgMarkup).not.toContain('<button');
|
|
241
|
+
expect(svgMarkup).not.toContain('More about these caveats');
|
|
242
|
+
expect(svgMarkup).toContain('Caveat note');
|
|
243
|
+
const afterButtonMatch = svgMarkup.match(/<span id="after-button"[^>]*style="([^"]*)"/);
|
|
244
|
+
expect(afterButtonMatch).not.toBeNull();
|
|
245
|
+
expect(afterButtonMatch[1]).toContain('color: rgb(1, 2, 3)');
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it('drops an anchor-based drill-down link, keeping its non-interactive siblings', async () => {
|
|
249
|
+
const container = buildContainer();
|
|
250
|
+
const footer = document.createElement('div');
|
|
251
|
+
footer.innerHTML = '<a href="/details">View details</a><span>Static note</span>';
|
|
252
|
+
container.appendChild(footer);
|
|
253
|
+
|
|
254
|
+
const svgMarkup = await captureExportedMarkup(container);
|
|
255
|
+
expect(svgMarkup).not.toContain('<a ');
|
|
256
|
+
expect(svgMarkup).not.toContain('View details');
|
|
257
|
+
expect(svgMarkup).toContain('Static note');
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
// Risk section counter-check: MUI's own chart legend can itself be interactive -- this must
|
|
261
|
+
// remove OPERATION, not CONTENT. A legend built from non-interactive markup (as every affected
|
|
262
|
+
// hram panel's hand-built legend is, per the WO) must survive completely intact.
|
|
263
|
+
it('keeps a non-interactive legend fully intact -- removes operation, not content', async () => {
|
|
264
|
+
const container = buildContainer();
|
|
265
|
+
// the fixture's own `.legend` div ("Legend HTML") already carries no button/input/role --
|
|
266
|
+
// confirm it survives the same export that strips the footer's button above.
|
|
267
|
+
const svgMarkup = await captureExportedMarkup(container);
|
|
268
|
+
expect(svgMarkup).toContain('Legend HTML');
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
// reviewer + ui_reviewer finding, independently: MUI's own chart legend, when interactive
|
|
272
|
+
// (`onItemClick`/`toggleVisibilityOnClick`), renders each item as `<button role="button"
|
|
273
|
+
// class="...MuiChartsLegend-series...">` wrapping the swatch+label -- that markup IS the
|
|
274
|
+
// legend's content. A blanket removal would delete it along with the click handler; this is the
|
|
275
|
+
// WO's own risk section made concrete, and it must be UNWRAPPED (content kept, button-ness
|
|
276
|
+
// dropped), not removed.
|
|
277
|
+
it('unwraps an interactive MUI legend item instead of removing it -- keeps the swatch and label', async () => {
|
|
278
|
+
const container = buildContainer();
|
|
279
|
+
const legendUl = document.createElement('ul');
|
|
280
|
+
legendUl.className = 'MuiChartsLegend-root';
|
|
281
|
+
legendUl.innerHTML = `
|
|
282
|
+
<li data-series="opened">
|
|
283
|
+
<button type="button" role="button" class="MuiChartsLegend-series">
|
|
284
|
+
<span class="MuiChartsLegend-mark" style="background-color: rgb(25, 118, 210);"></span>
|
|
285
|
+
<span class="MuiChartsLegend-label">Opened</span>
|
|
286
|
+
</button>
|
|
287
|
+
</li>
|
|
288
|
+
`;
|
|
289
|
+
container.appendChild(legendUl);
|
|
290
|
+
|
|
291
|
+
const svgMarkup = await captureExportedMarkup(container);
|
|
292
|
+
expect(svgMarkup).not.toContain('<button');
|
|
293
|
+
expect(svgMarkup).toContain('MuiChartsLegend-series');
|
|
294
|
+
expect(svgMarkup).toContain('MuiChartsLegend-mark');
|
|
295
|
+
expect(svgMarkup).toContain('Opened');
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it('rejects when the container fails to rasterize', async () => {
|
|
299
|
+
global.Image = class FailingImage {
|
|
300
|
+
set src(value) {
|
|
301
|
+
this._src = value;
|
|
302
|
+
queueMicrotask(() => this.onerror?.());
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// eslint-disable-next-line class-methods-use-this
|
|
306
|
+
get src() {
|
|
307
|
+
return this._src;
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
const container = buildContainer();
|
|
311
|
+
await expect(exportChartPng(container)).rejects.toThrow(/rasterize/);
|
|
312
|
+
});
|
|
313
|
+
});
|