Skip to content

Commit edf9574

Browse files
authored
Extract core SVG element before rendering (#57)
1 parent b202b81 commit edf9574

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

svg-render.html

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,18 @@ <h2>Base64 Image Tag:</h2>
237237
downloadLink.style.display = 'none';
238238
fileSizeSpan.textContent = '';
239239

240-
// Find the <?xml tag and ignore everything before it
241-
const xmlIndex = svgContent.indexOf('<?xml');
242-
if (xmlIndex !== -1) {
243-
svgContent = svgContent.substring(xmlIndex);
240+
// Extract the SVG element, ignoring any surrounding content
241+
const lowerContent = svgContent.toLowerCase();
242+
const svgStart = lowerContent.indexOf('<svg');
243+
const svgEnd = lowerContent.lastIndexOf('</svg>');
244+
245+
if (svgStart === -1 || svgEnd === -1) {
246+
alert('Invalid SVG input');
247+
return;
244248
}
245249

250+
svgContent = svgContent.slice(svgStart, svgEnd + '</svg>'.length);
251+
246252
// Create a temporary SVG element
247253
const svgElement = new DOMParser().parseFromString(svgContent, 'image/svg+xml').documentElement;
248254

0 commit comments

Comments
 (0)