@wordpress/blocks
Version:
Block API for WordPress.
98 lines (97 loc) • 2.93 kB
JavaScript
// packages/blocks/src/api/children.js
import { renderToString } from "@wordpress/element";
import deprecated from "@wordpress/deprecated";
import * as node from "./node";
function getSerializeCapableElement(children) {
return children;
}
function getChildrenArray(children) {
deprecated("wp.blocks.children.getChildrenArray", {
since: "6.1",
version: "6.3",
link: "https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/introducing-attributes-and-editable-fields/"
});
return children;
}
function concat(...blockNodes) {
deprecated("wp.blocks.children.concat", {
since: "6.1",
version: "6.3",
alternative: "wp.richText.concat",
link: "https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/introducing-attributes-and-editable-fields/"
});
const result = [];
for (let i = 0; i < blockNodes.length; i++) {
const blockNode = Array.isArray(blockNodes[i]) ? blockNodes[i] : [blockNodes[i]];
for (let j = 0; j < blockNode.length; j++) {
const child = blockNode[j];
const canConcatToPreviousString = typeof child === "string" && typeof result[result.length - 1] === "string";
if (canConcatToPreviousString) {
result[result.length - 1] += child;
} else {
result.push(child);
}
}
}
return result;
}
function fromDOM2(domNodes) {
deprecated("wp.blocks.children.fromDOM", {
since: "6.1",
version: "6.3",
alternative: "wp.richText.create",
link: "https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/introducing-attributes-and-editable-fields/"
});
const result = [];
for (let i = 0; i < domNodes.length; i++) {
try {
result.push(node.fromDOM(domNodes[i]));
} catch (error) {
}
}
return result;
}
function toHTML(children) {
deprecated("wp.blocks.children.toHTML", {
since: "6.1",
version: "6.3",
alternative: "wp.richText.toHTMLString",
link: "https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/introducing-attributes-and-editable-fields/"
});
const element = getSerializeCapableElement(children);
return renderToString(element);
}
function matcher(selector) {
deprecated("wp.blocks.children.matcher", {
since: "6.1",
version: "6.3",
alternative: "html source",
link: "https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/introducing-attributes-and-editable-fields/"
});
return (domNode) => {
let match = domNode;
if (selector) {
match = domNode.querySelector(selector);
}
if (match) {
return fromDOM2(match.childNodes);
}
return [];
};
}
var children_default = {
concat,
getChildrenArray,
fromDOM: fromDOM2,
toHTML,
matcher
};
export {
concat,
children_default as default,
fromDOM2 as fromDOM,
getSerializeCapableElement,
matcher,
toHTML
};
//# sourceMappingURL=children.js.map