Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace XMLHttpRequest usage with the Fetch API in inlineImages (in test/driver.js) #14651

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 46 additions & 46 deletions test/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,29 +78,52 @@ function writeSVG(svgElement, ctx) {
});
}

function inlineImages(images) {
const imagePromises = [];
for (let i = 0, ii = images.length; i < ii; i++) {
imagePromises.push(
new Promise(function (resolve, reject) {
const xhr = new XMLHttpRequest();
xhr.responseType = "blob";
xhr.onload = function () {
const reader = new FileReader();
reader.onloadend = function () {
resolve(reader.result);
};
reader.readAsDataURL(xhr.response);
};
xhr.onerror = function (e) {
reject(new Error("Error fetching inline image " + e));
};
xhr.open("GET", images[i].src);
xhr.send();
})
async function inlineImages(node, silentErrors = false) {
const promises = [];

for (const image of node.getElementsByTagName("img")) {
const url = image.src;

promises.push(
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();
reader.onload = () => {
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) {
Expand All @@ -125,29 +148,6 @@ async function convertCanvasesToImages(annotationCanvasMap, outputScale) {
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 {
/**
* For the reference tests, the full content of the various layers must be
Expand Down Expand Up @@ -236,7 +236,7 @@ class Rasterize {
AnnotationLayer.render(parameters);

// Inline SVG images from text annotations.
await resolveImages(div);
await inlineImages(div);
foreignObject.appendChild(div);
svg.appendChild(foreignObject);

Expand Down Expand Up @@ -300,7 +300,7 @@ class Rasterize {
});

// Some unsupported type of images (e.g. tiff) lead to errors.
await resolveImages(div, /* silentErrors = */ true);
await inlineImages(div, /* silentErrors = */ true);
svg.appendChild(foreignObject);

await writeSVG(svg, ctx);
Expand Down