UNPKG

@wordpress/blocks

Version:
55 lines (54 loc) 1.84 kB
// packages/blocks/src/api/raw-handling/normalise-blocks.js import { isEmpty, isPhrasingContent } from "@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 (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 (isEmpty(node) && !options.raw) { decu.removeChild(node); } else { accu.appendChild(node); } } else if (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; } export { normaliseBlocks as default }; //# sourceMappingURL=normalise-blocks.js.map