UNPKG

@wordpress/blocks

Version:
39 lines (38 loc) 1.3 kB
// packages/blocks/src/api/raw-handling/html-formatting-remover.js import { getSibling } from "./utils"; function isFormattingSpace(character) { return character === " " || character === "\r" || character === "\n" || character === " "; } function htmlFormattingRemover(node) { if (node.nodeType !== node.TEXT_NODE) { return; } let parent = node; while (parent = parent.parentNode) { if (parent.nodeType === parent.ELEMENT_NODE && parent.nodeName === "PRE") { return; } } let newData = node.data.replace(/[ \r\n\t]+/g, " "); if (newData[0] === " ") { const previousSibling = getSibling(node, "previous"); if (!previousSibling || previousSibling.nodeName === "BR" || previousSibling.textContent.slice(-1) === " ") { newData = newData.slice(1); } } if (newData[newData.length - 1] === " ") { const nextSibling = getSibling(node, "next"); if (!nextSibling || nextSibling.nodeName === "BR" || nextSibling.nodeType === nextSibling.TEXT_NODE && isFormattingSpace(nextSibling.textContent[0])) { newData = newData.slice(0, -1); } } if (!newData) { node.parentNode.removeChild(node); } else { node.data = newData; } } export { htmlFormattingRemover as default }; //# sourceMappingURL=html-formatting-remover.js.map