@wordpress/blocks
Version:
Block API for WordPress.
40 lines (39 loc) • 1.05 kB
JavaScript
// packages/blocks/src/api/raw-handling/image-corrector.js
import { createBlobURL } from "@wordpress/blob";
function imageCorrector(node) {
if (node.nodeName !== "IMG") {
return;
}
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 (e) {
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.js.map