Merge pull request #14651 from Snuffleupagus/Driver-inlineImages-fetch

Replace XMLHttpRequest usage with the Fetch API in `inlineImages` (in `test/driver.js`)
This commit is contained in:
Tim van der Meij 2022-03-09 20:47:38 +01:00 committed by GitHub
commit ee39499a5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -79,29 +79,52 @@ function writeSVG(svgElement, ctx) {
}); });
} }
function inlineImages(images) { async function inlineImages(node, silentErrors = false) {
const imagePromises = []; const promises = [];
for (let i = 0, ii = images.length; i < ii; i++) {
imagePromises.push( for (const image of node.getElementsByTagName("img")) {
new Promise(function (resolve, reject) { const url = image.src;
const xhr = new XMLHttpRequest();
xhr.responseType = "blob"; promises.push(
xhr.onload = function () { fetch(url)
const reader = new FileReader(); .then(response => {
reader.onloadend = function () { if (!response.ok) {
resolve(reader.result); throw new Error(response.statusText);
}; }
reader.readAsDataURL(xhr.response); return response.blob();
}; })
xhr.onerror = function (e) { .then(blob => {
reject(new Error("Error fetching inline image " + e)); return new Promise((resolve, reject) => {
}; const reader = new FileReader();
xhr.open("GET", images[i].src); reader.onload = () => {
xhr.send(); resolve(reader.result);
}) };
reader.onerror = reject;
reader.readAsDataURL(blob);
});
})
.then(dataUrl => {
return new Promise((resolve, reject) => {
image.onload = resolve;
image.onerror = evt => {
if (silentErrors) {
resolve();
return;
}
reject(evt);
};
image.src = dataUrl;
});
})
.catch(reason => {
throw new Error(`Error inlining image (${url}): ${reason}`);
})
); );
} }
return Promise.all(imagePromises);
await Promise.all(promises);
} }
async function convertCanvasesToImages(annotationCanvasMap, outputScale) { async function convertCanvasesToImages(annotationCanvasMap, outputScale) {
@ -126,29 +149,6 @@ async function convertCanvasesToImages(annotationCanvasMap, outputScale) {
return results; return results;
} }
async function resolveImages(node, silentErrors = false) {
const images = node.getElementsByTagName("img");
const data = await inlineImages(images);
const loadedPromises = [];
for (let i = 0, ii = data.length; i < ii; i++) {
loadedPromises.push(
new Promise(function (resolveImage, rejectImage) {
images[i].onload = resolveImage;
images[i].onerror = function (e) {
if (silentErrors) {
resolveImage();
} else {
rejectImage(new Error("Error loading image " + e));
}
};
images[i].src = data[i];
})
);
}
await Promise.all(loadedPromises);
}
class Rasterize { class Rasterize {
/** /**
* For the reference tests, the full content of the various layers must be * For the reference tests, the full content of the various layers must be
@ -237,7 +237,7 @@ class Rasterize {
AnnotationLayer.render(parameters); AnnotationLayer.render(parameters);
// Inline SVG images from text annotations. // Inline SVG images from text annotations.
await resolveImages(div); await inlineImages(div);
foreignObject.appendChild(div); foreignObject.appendChild(div);
svg.appendChild(foreignObject); svg.appendChild(foreignObject);
@ -301,7 +301,7 @@ class Rasterize {
}); });
// Some unsupported type of images (e.g. tiff) lead to errors. // Some unsupported type of images (e.g. tiff) lead to errors.
await resolveImages(div, /* silentErrors = */ true); await inlineImages(div, /* silentErrors = */ true);
svg.appendChild(foreignObject); svg.appendChild(foreignObject);
await writeSVG(svg, ctx); await writeSVG(svg, ctx);