@wordpress/blocks
Version:
Block API for WordPress.
44 lines (42 loc) • 1.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isInlineContent;
var _dom = require("@wordpress/dom");
/**
* WordPress dependencies
*/
/**
* Checks if the given node should be considered inline content, optionally
* depending on a context tag.
*
* @param {Node} node Node name.
* @param {string} contextTag Tag name.
*
* @return {boolean} True if the node is inline content, false if nohe.
*/
function isInline(node, contextTag) {
if ((0, _dom.isTextContent)(node)) {
return true;
}
if (!contextTag) {
return false;
}
const tag = node.nodeName.toLowerCase();
const inlineAllowedTagGroups = [['ul', 'li', 'ol'], ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']];
return inlineAllowedTagGroups.some(tagGroup => [tag, contextTag].filter(t => !tagGroup.includes(t)).length === 0);
}
function deepCheck(nodes, contextTag) {
return nodes.every(node => isInline(node, contextTag) && deepCheck(Array.from(node.children), contextTag));
}
function isDoubleBR(node) {
return node.nodeName === 'BR' && node.previousSibling && node.previousSibling.nodeName === 'BR';
}
function isInlineContent(HTML, contextTag) {
const doc = document.implementation.createHTMLDocument('');
doc.body.innerHTML = HTML;
const nodes = Array.from(doc.body.children);
return !nodes.some(isDoubleBR) && deepCheck(nodes, contextTag);
}
//# sourceMappingURL=is-inline-content.js.map