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