@wordpress/blocks
Version:
Block API for WordPress.
54 lines (49 loc) • 1.27 kB
JavaScript
/* wp:polyfill */
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = imageCorrector;
var _blob = require("@wordpress/blob");
/**
* WordPress dependencies
*/
function imageCorrector(node) {
if (node.nodeName !== 'IMG') {
return;
}
if (node.src.indexOf('file:') === 0) {
node.src = '';
}
// This piece cannot be tested outside a browser env.
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;
// Can throw DOMException!
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 = (0, _blob.createBlobURL)(file);
}
// Remove trackers and hardly visible images.
if (node.height === 1 || node.width === 1) {
node.parentNode.removeChild(node);
}
}
//# sourceMappingURL=image-corrector.js.map