From 449c4477cde7500cd3a8ff86bc75b647731868ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Wed, 14 May 2025 12:56:23 +0200 Subject: [PATCH] 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 --- sao/src/common.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/sao/src/common.js b/sao/src/common.js index 1891dfef2d..d0ddae321a 100644 --- a/sao/src/common.js +++ b/sao/src/common.js @@ -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);