@wordpress/blocks
Version:
Block API for WordPress.
50 lines (49 loc) • 1.65 kB
JavaScript
// packages/blocks/src/api/raw-handling/ms-list-converter.js
import { deepFilterHTML } from "./utils";
import msListIgnore from "./ms-list-ignore";
function isList(node) {
return node.nodeName === "OL" || node.nodeName === "UL";
}
function msListConverter(node, doc) {
if (node.nodeName !== "P") {
return;
}
const style = node.getAttribute("style");
if (!style || !style.includes("mso-list")) {
return;
}
const prevNode = node.previousElementSibling;
if (!prevNode || !isList(prevNode)) {
const type = node.textContent.trim().slice(0, 1);
const isNumeric = /[1iIaA]/.test(type);
const newListNode = doc.createElement(isNumeric ? "ol" : "ul");
if (isNumeric) {
newListNode.setAttribute("type", type);
}
node.parentNode.insertBefore(newListNode, node);
}
const listNode = node.previousElementSibling;
const listType = listNode.nodeName;
const listItem = doc.createElement("li");
let receivingNode = listNode;
listItem.innerHTML = deepFilterHTML(node.innerHTML, [msListIgnore]);
const matches = /mso-list\s*:[^;]+level([0-9]+)/i.exec(style);
let level = matches ? parseInt(matches[1], 10) - 1 || 0 : 0;
while (level--) {
receivingNode = receivingNode.lastChild || receivingNode;
if (isList(receivingNode)) {
receivingNode = receivingNode.lastChild || receivingNode;
}
}
if (!isList(receivingNode)) {
receivingNode = receivingNode.appendChild(
doc.createElement(listType)
);
}
receivingNode.appendChild(listItem);
node.parentNode.removeChild(node);
}
export {
msListConverter as default
};
//# sourceMappingURL=ms-list-converter.js.map