diff --git a/public/app/core/components/SVG/SanitizedSVG.test.tsx b/public/app/core/components/SVG/SanitizedSVG.test.tsx
new file mode 100644
index 00000000000..16f2af2cd99
--- /dev/null
+++ b/public/app/core/components/SVG/SanitizedSVG.test.tsx
@@ -0,0 +1,58 @@
+import { getSvgId, getSvgStyle, svgStyleCleanup } from './utils';
+
+const ID = 'TEST_ID';
+
+const svgNoId =
+ '';
+
+const svgWithId = ``;
+
+const svgWithWrongIdInStyle =
+ '';
+
+describe('SanitizedSVG', () => {
+ it('should cleanup the style and generate an ID', () => {
+ const cleanStyle = svgStyleCleanup(svgNoId);
+ const updatedStyle = getSvgStyle(cleanStyle);
+ const svgId = getSvgId(cleanStyle);
+
+ expect(cleanStyle.indexOf('id="')).toBeGreaterThan(-1);
+ expect(svgId).toBeDefined();
+ expect(svgId?.startsWith('x')).toBeTruthy();
+ expect(updatedStyle?.indexOf(`#${svgId}`)).toBeGreaterThan(-1);
+
+ expect(cleanStyle).toEqual(
+ ``
+ );
+ });
+
+ it('should cleanup the style and use the existing ID', () => {
+ const cleanStyle = svgStyleCleanup(svgWithId);
+ const updatedStyle = getSvgStyle(cleanStyle);
+ const svgId = getSvgId(cleanStyle);
+
+ expect(cleanStyle.indexOf('id="')).toBeGreaterThan(-1);
+ expect(svgId).toBeDefined();
+ expect(svgId).toEqual(ID);
+ expect(updatedStyle?.indexOf(`#${svgId}`)).toBeGreaterThan(-1);
+
+ expect(cleanStyle).toEqual(
+ ``
+ );
+ });
+
+ it('should cleanup the style and replace the wrong ID', () => {
+ const cleanStyle = svgStyleCleanup(svgWithWrongIdInStyle);
+ const updatedStyle = getSvgStyle(cleanStyle);
+ const svgId = getSvgId(cleanStyle);
+
+ expect(cleanStyle.indexOf('id="')).toBeGreaterThan(-1);
+ expect(svgId).toBeDefined();
+ expect(svgId?.startsWith('x')).toBeTruthy();
+ expect(updatedStyle?.indexOf(`#${svgId}`)).toBeGreaterThan(-1);
+
+ expect(cleanStyle).toEqual(
+ ``
+ );
+ });
+});
diff --git a/public/app/core/components/SVG/SanitizedSVG.tsx b/public/app/core/components/SVG/SanitizedSVG.tsx
index 5f4f0a6f5aa..b53bdccf5a1 100644
--- a/public/app/core/components/SVG/SanitizedSVG.tsx
+++ b/public/app/core/components/SVG/SanitizedSVG.tsx
@@ -3,8 +3,13 @@ import SVG, { Props } from 'react-inlinesvg';
import { textUtil } from '@grafana/data';
-export const SanitizedSVG = (props: Props) => {
- return ;
+import { svgStyleCleanup } from './utils';
+
+type SanitizedSVGProps = Props & { cleanStyle?: boolean };
+
+export const SanitizedSVG = (props: SanitizedSVGProps) => {
+ const { cleanStyle, ...inlineSvgProps } = props;
+ return ;
};
let cache = new Map();
@@ -15,5 +20,21 @@ function getCleanSVG(code: string): string {
clean = textUtil.sanitizeSVGContent(code);
cache.set(code, clean);
}
+
+ return clean;
+}
+
+function getCleanSVGAndStyle(code: string): string {
+ let clean = cache.get(code);
+ if (!clean) {
+ clean = textUtil.sanitizeSVGContent(code);
+
+ if (clean.indexOf('