@wordpress/blocks
Version:
Block API for WordPress.
75 lines (73 loc) • 2.78 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/blocks/src/api/raw-handling/normalise-blocks.js
var normalise_blocks_exports = {};
__export(normalise_blocks_exports, {
default: () => normaliseBlocks
});
module.exports = __toCommonJS(normalise_blocks_exports);
var import_dom = require("@wordpress/dom");
function normaliseBlocks(HTML, options = {}) {
const decuDoc = document.implementation.createHTMLDocument("");
const accuDoc = document.implementation.createHTMLDocument("");
const decu = decuDoc.body;
const accu = accuDoc.body;
decu.innerHTML = HTML;
while (decu.firstChild) {
const node = decu.firstChild;
if (node.nodeType === node.TEXT_NODE) {
if ((0, import_dom.isEmpty)(node)) {
decu.removeChild(node);
} else {
if (!accu.lastChild || accu.lastChild.nodeName !== "P") {
accu.appendChild(accuDoc.createElement("P"));
}
accu.lastChild.appendChild(node);
}
} else if (node.nodeType === node.ELEMENT_NODE) {
if (node.nodeName === "BR") {
if (node.nextSibling && node.nextSibling.nodeName === "BR") {
accu.appendChild(accuDoc.createElement("P"));
decu.removeChild(node.nextSibling);
}
if (accu.lastChild && accu.lastChild.nodeName === "P" && accu.lastChild.hasChildNodes()) {
accu.lastChild.appendChild(node);
} else {
decu.removeChild(node);
}
} else if (node.nodeName === "P") {
if ((0, import_dom.isEmpty)(node) && !options.raw) {
decu.removeChild(node);
} else {
accu.appendChild(node);
}
} else if ((0, import_dom.isPhrasingContent)(node)) {
if (!accu.lastChild || accu.lastChild.nodeName !== "P") {
accu.appendChild(accuDoc.createElement("P"));
}
accu.lastChild.appendChild(node);
} else {
accu.appendChild(node);
}
} else {
decu.removeChild(node);
}
}
return accu.innerHTML;
}
//# sourceMappingURL=normalise-blocks.js.map