From 10c273d5767efb1d447391a0b23fe86be5832e97 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 --- sao/src/common.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/sao/src/common.js b/sao/src/common.js index e353a67de3..fcc84cb833 100644 --- a/sao/src/common.js +++ b/sao/src/common.js @@ -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);