UNPKG

@wordpress/blocks

Version:
59 lines (57 loc) 2.27 kB
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/html-formatting-remover.js var html_formatting_remover_exports = {}; __export(html_formatting_remover_exports, { default: () => htmlFormattingRemover }); module.exports = __toCommonJS(html_formatting_remover_exports); var import_utils = require("./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 = (0, import_utils.getSibling)(node, "previous"); if (!previousSibling || previousSibling.nodeName === "BR" || previousSibling.textContent.slice(-1) === " ") { newData = newData.slice(1); } } if (newData[newData.length - 1] === " ") { const nextSibling = (0, import_utils.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; } } //# sourceMappingURL=html-formatting-remover.js.map