grafana/public/app/core/components/SVG/SanitizedSVG.tsx
Nathan Marrs 8b574e22b5
SVG: Add dompurify preprocessor step (#62143)
Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
2023-01-25 10:37:29 -08:00

19 lines
514 B
TypeScript

import * as DOMPurify from 'dompurify';
import React from 'react';
import SVG, { Props } from 'react-inlinesvg';
export const SanitizedSVG = (props: Props) => {
return <SVG {...props} cacheRequests={true} preProcessor={getCleanSVG} />;
};
let cache = new Map<string, string>();
function getCleanSVG(code: string): string {
let clean = cache.get(code);
if (!clean) {
clean = DOMPurify.sanitize(code, { USE_PROFILES: { svg: true, svgFilters: true } });
cache.set(code, clean);
}
return clean;
}