UNPKG

@wordpress/blocks

Version:
51 lines (50 loc) 1.6 kB
// packages/blocks/src/api/raw-handling/phrasing-content-reducer.ts import { wrap, replaceTag } from "@wordpress/dom"; function phrasingContentReducer(node, doc) { if (node.nodeName === "SPAN" && node.style) { const { fontWeight, fontStyle, textDecorationLine, textDecoration, verticalAlign } = node.style; const element = node; if (fontWeight === "bold" || fontWeight === "700") { wrap(doc.createElement("strong"), element); } if (fontStyle === "italic") { wrap(doc.createElement("em"), element); } if (textDecorationLine === "line-through" || textDecoration.includes("line-through")) { wrap(doc.createElement("s"), element); } if (verticalAlign === "super") { wrap(doc.createElement("sup"), element); } else if (verticalAlign === "sub") { wrap(doc.createElement("sub"), element); } } else if (node.nodeName === "B") { replaceTag(node, "strong"); } else if (node.nodeName === "I") { replaceTag(node, "em"); } else if (node.nodeName === "A") { const anchor = node; if (anchor.target && anchor.target.toLowerCase() === "_blank") { anchor.rel = "noopener"; } else { anchor.removeAttribute("target"); anchor.removeAttribute("rel"); } if (anchor.name && !anchor.id) { anchor.id = anchor.name; } if (anchor.id && !anchor.ownerDocument.querySelector(`[href="#${anchor.id}"]`)) { anchor.removeAttribute("id"); } } } export { phrasingContentReducer as default }; //# sourceMappingURL=phrasing-content-reducer.mjs.map