@wordpress/blocks
Version:
Block API for WordPress.
142 lines (140 loc) • 4.62 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/blocks/src/api/raw-handling/utils.js
var utils_exports = {};
__export(utils_exports, {
deepFilterHTML: () => deepFilterHTML,
deepFilterNodeList: () => deepFilterNodeList,
getBlockContentSchema: () => getBlockContentSchema,
getBlockContentSchemaFromTransforms: () => getBlockContentSchemaFromTransforms,
getSibling: () => getSibling,
isPlain: () => isPlain
});
module.exports = __toCommonJS(utils_exports);
var import_dom = require("@wordpress/dom");
var import__ = require("..");
var import_get_raw_transforms = require("./get-raw-transforms");
function getBlockContentSchemaFromTransforms(transforms, context) {
const phrasingContentSchema = (0, import_dom.getPhrasingContentSchema)(context);
const schemaArgs = { phrasingContentSchema, isPaste: context === "paste" };
const schemas = transforms.map(({ isMatch, blockName, schema }) => {
const hasAnchorSupport = (0, import__.hasBlockSupport)(blockName, "anchor");
schema = typeof schema === "function" ? schema(schemaArgs) : schema;
if (!hasAnchorSupport && !isMatch) {
return schema;
}
if (!schema) {
return {};
}
return Object.fromEntries(
Object.entries(schema).map(([key, value]) => {
let attributes = value.attributes || [];
if (hasAnchorSupport) {
attributes = [...attributes, "id"];
}
return [
key,
{
...value,
attributes,
isMatch: isMatch ? isMatch : void 0
}
];
})
);
});
function mergeTagNameSchemaProperties(objValue, srcValue, key) {
switch (key) {
case "children": {
if (objValue === "*" || srcValue === "*") {
return "*";
}
return { ...objValue, ...srcValue };
}
case "attributes":
case "require": {
return [...objValue || [], ...srcValue || []];
}
case "isMatch": {
if (!objValue || !srcValue) {
return void 0;
}
return (...args) => {
return objValue(...args) || srcValue(...args);
};
}
}
}
function mergeTagNameSchemas(a, b) {
for (const key in b) {
a[key] = a[key] ? mergeTagNameSchemaProperties(a[key], b[key], key) : { ...b[key] };
}
return a;
}
function mergeSchemas(a, b) {
for (const key in b) {
a[key] = a[key] ? mergeTagNameSchemas(a[key], b[key]) : { ...b[key] };
}
return a;
}
return schemas.reduce(mergeSchemas, {});
}
function getBlockContentSchema(context) {
return getBlockContentSchemaFromTransforms((0, import_get_raw_transforms.getRawTransforms)(), context);
}
function isPlain(HTML) {
return !/<(?!br[ />])/i.test(HTML);
}
function deepFilterNodeList(nodeList, filters, doc, schema) {
Array.from(nodeList).forEach((node) => {
deepFilterNodeList(node.childNodes, filters, doc, schema);
filters.forEach((item) => {
if (!doc.contains(node)) {
return;
}
item(node, doc, schema);
});
});
}
function deepFilterHTML(HTML, filters = [], schema) {
const doc = document.implementation.createHTMLDocument("");
doc.body.innerHTML = HTML;
deepFilterNodeList(doc.body.childNodes, filters, doc, schema);
return doc.body.innerHTML;
}
function getSibling(node, which) {
const sibling = node[`${which}Sibling`];
if (sibling && (0, import_dom.isPhrasingContent)(sibling)) {
return sibling;
}
const { parentNode } = node;
if (!parentNode || !(0, import_dom.isPhrasingContent)(parentNode)) {
return;
}
return getSibling(parentNode, which);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
deepFilterHTML,
deepFilterNodeList,
getBlockContentSchema,
getBlockContentSchemaFromTransforms,
getSibling,
isPlain
});
//# sourceMappingURL=utils.js.map