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
(grafted from 286d03999217e1ea39231bb286cfa926d2f47486)

--HG--
branch : 7.0
This commit is contained in:
Cédric Krier committed 2025-05-14 12:56:23 +02:00
1 parent 24e99bc45b
commit 449c4477cd
1 file changed
+11 -7
+11 -7
View File
@@ -4396,15 +4396,19 @@
return null;
}
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) {
}
var blob = new Blob([data], {type: type});
return window.URL.createObjectURL(blob);