Merge pull request #6303 from JMoises-XCode/fix/svg-export-css-variables

fix: resolve CSS variables in SVG export
This commit is contained in:
mononaut 2026-02-18 15:46:12 +09:00 committed by GitHub
commit 2d320b1a49
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 35 additions and 2 deletions

View file

@ -0,0 +1,3 @@
I hereby accept the terms of the Contributor License Agreement in the CONTRIBUTING.md file of the mempool/mempool git repository as of February 13, 2026.
Signed: JMoises-XCode

View file

@ -79,10 +79,40 @@ export const formatterXAxisTimeCategory = (
}
};
export const download = (href, name) => {
function resolveCssVars(svg: string): string {
return svg.replace(/var\(--[\w-]+\)/g, (match) => {
try {
const varName = match.slice(4, -1);
return getComputedStyle(document.documentElement)
.getPropertyValue(varName)
.trim() || match;
} catch {
return match;
}
});
}
function resolveCssVarsInSvg(dataUrl: string): string {
try {
const utf8Prefix = 'data:image/svg+xml;charset=UTF-8,';
if (dataUrl.startsWith(utf8Prefix)) {
const svg = decodeURIComponent(dataUrl.slice(utf8Prefix.length));
return utf8Prefix + encodeURIComponent(resolveCssVars(svg));
}
const b64Prefix = 'data:image/svg+xml;base64,';
if (dataUrl.startsWith(b64Prefix)) {
const svg = atob(dataUrl.slice(b64Prefix.length));
return b64Prefix + btoa(resolveCssVars(svg));
}
} catch { }
return dataUrl;
}
export const download = (href: string, name: string, skipCssVarResolve = false): void => {
const a = document.createElement('a');
a.download = name;
a.href = href;
a.href = (!skipCssVarResolve && href.startsWith('data:image/svg+xml')) ? resolveCssVarsInSvg(href) : href;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);