fix: SVG regex prefers </svg> terminator, add self-closing children test
Use greedy <svg>...</svg> match preferring closing tag over inner /> to prevent truncation at first self-closing child element. Self-closing root <svg .../> handled as separate alternative. Added test with <path/> + <circle/> children asserting well-formed output.
This commit is contained in:
2 files changed
+11
-1
No files matched your search
@@ -86,7 +86,7 @@ export const sanitizeSvg = (svgText: string) => {
|
||||
// linkedom's DOMParser omits html/body for fragment-only SVG; DOMPurify needs a body root.
|
||||
const wrapped = `<html><body>${svgText}</body></html>`;
|
||||
const sanitized = purify.sanitize(wrapped, svgSanitizeOptions);
|
||||
const svgMatch = sanitized.match(/<svg[\s\S]*?(?:<\/svg>|\/>)/i);
|
||||
const svgMatch = sanitized.match(/<svg[^>]*\/>|<svg[\s\S]*<\/svg>/i);
|
||||
return svgMatch ? svgMatch[0] : "";
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user