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:
commit
ee39499a5a
@ -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)
|
||||||
|
.then(response => {
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(response.statusText);
|
||||||
|
}
|
||||||
|
return response.blob();
|
||||||
|
})
|
||||||
|
.then(blob => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
const reader = new FileReader();
|
const reader = new FileReader();
|
||||||
reader.onloadend = function () {
|
reader.onload = () => {
|
||||||
resolve(reader.result);
|
resolve(reader.result);
|
||||||
};
|
};
|
||||||
reader.readAsDataURL(xhr.response);
|
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);
|
||||||
};
|
};
|
||||||
xhr.onerror = function (e) {
|
|
||||||
reject(new Error("Error fetching inline image " + e));
|
image.src = dataUrl;
|
||||||
};
|
});
|
||||||
xhr.open("GET", images[i].src);
|
})
|
||||||
xhr.send();
|
.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);
|
||||||
|
Loading…
Reference in New Issue
Block a user