@wordpress/blocks
Version:
Block API for WordPress.
41 lines (40 loc) • 1.07 kB
JavaScript
// packages/blocks/src/api/raw-handling/image-corrector.ts
import { createBlobURL } from "@wordpress/blob";
function imageCorrector(img) {
if (img.nodeName !== "IMG") {
return;
}
const node = img;
if (node.src.indexOf("file:") === 0) {
node.src = "";
}
if (node.src.indexOf("data:") === 0) {
const [properties, data] = node.src.split(",");
const [type] = properties.slice(5).split(";");
if (!data || !type) {
node.src = "";
return;
}
let decoded;
try {
decoded = atob(data);
} catch {
node.src = "";
return;
}
const uint8Array = new Uint8Array(decoded.length);
for (let i = 0; i < uint8Array.length; i++) {
uint8Array[i] = decoded.charCodeAt(i);
}
const name = type.replace("/", ".");
const file = new window.File([uint8Array], name, { type });
node.src = createBlobURL(file);
}
if (node.height === 1 || node.width === 1) {
node.parentNode.removeChild(node);
}
}
export {
imageCorrector as default
};
//# sourceMappingURL=image-corrector.mjs.map