Avoid parsing image content as XML is they do not look like XML

This prevent the browser to log an error in the console.
Also we test for the presence of svg tag to be sure it is an svg image.

Closes #14019
This commit is contained in:
Cédric Krier
2025-05-14 12:56:23 +02:00
parent 0ad9570070
commit 10c273d576
+11 -8
View File
@@ -4389,16 +4389,19 @@
'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs%3D');
}
var type = '';
try {
var xml = data;
if (xml instanceof Uint8Array) {
xml = new TextDecoder().decode(data);
}
if (jQuery.parseXML(xml)) {
var xml = data;
if (xml instanceof Uint8Array) {
xml = new TextDecoder().decode(data);
}
// simple test to avoid logging of parsing error
if (/^\s*<[\s\S]+>\s*$/.test(xml.trim())) {
let parser = new DOMParser();
let doc = parser.parseFromString(xml, 'image/svg+xml');
if (!doc.querySelector('parsererror')
&& (doc.documentElement.tagName.toLowerCase() === 'svg' ||
doc.getElementsByTagName('svg').length > 0)) {
type = 'image/svg+xml';
}
} catch (e) {
// continue
}
var blob = new Blob([data], {type: type});
return window.URL.createObjectURL(blob);