From 7cd062ec684981f381e8266b61f7788edee75a27 Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Wed, 19 Jul 2023 20:14:18 +0200 Subject: [PATCH] Draw correctly background images in ref tests It's a workaround for bug https://bugzilla.mozilla.org/show_bug.cgi?id=1844414. It should be reverted (in order to avoid a perf penalty) once the bug is fixed in Firefox. --- test/driver.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/test/driver.js b/test/driver.js index 65028017a..1df6232de 100644 --- a/test/driver.js +++ b/test/driver.js @@ -61,16 +61,12 @@ function loadStyles(styles) { return Promise.all(promises); } -function writeSVG(svgElement, ctx) { - // We need to have UTF-8 encoded XML. - const svg_xml = unescape( - encodeURIComponent(new XMLSerializer().serializeToString(svgElement)) - ); +function loadImage(svg_xml, ctx) { return new Promise((resolve, reject) => { const img = new Image(); img.src = "data:image/svg+xml;base64," + btoa(svg_xml); img.onload = function () { - ctx.drawImage(img, 0, 0); + ctx?.drawImage(img, 0, 0); resolve(); }; img.onerror = function (e) { @@ -79,6 +75,19 @@ function writeSVG(svgElement, ctx) { }); } +async function writeSVG(svgElement, ctx) { + // We need to have UTF-8 encoded XML. + const svg_xml = unescape( + encodeURIComponent(new XMLSerializer().serializeToString(svgElement)) + ); + if (svg_xml.includes("background-image: url("data:image")) { + // Workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=1844414 + // we load the image two times. + await loadImage(svg_xml, null); + } + return loadImage(svg_xml, ctx); +} + async function inlineImages(node, silentErrors = false) { const promises = [];