UNPKG

@wordpress/blocks

Version:
234 lines (232 loc) 9.17 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/blocks/src/api/serializer.js var serializer_exports = {}; __export(serializer_exports, { __unstableSerializeAndClean: () => __unstableSerializeAndClean, default: () => serialize, getBlockDefaultClassName: () => getBlockDefaultClassName, getBlockInnerHTML: () => getBlockInnerHTML, getBlockMenuDefaultClassName: () => getBlockMenuDefaultClassName, getBlockProps: () => getBlockProps, getCommentAttributes: () => getCommentAttributes, getCommentDelimitedContent: () => getCommentDelimitedContent, getInnerBlocksProps: () => getInnerBlocksProps, getSaveContent: () => getSaveContent, getSaveElement: () => getSaveElement, serializeAttributes: () => serializeAttributes, serializeBlock: () => serializeBlock }); module.exports = __toCommonJS(serializer_exports); var import_element = require("@wordpress/element"); var import_hooks = require("@wordpress/hooks"); var import_is_shallow_equal = __toESM(require("@wordpress/is-shallow-equal")); var import_autop = require("@wordpress/autop"); var import_deprecated = __toESM(require("@wordpress/deprecated")); var import_registration = require("./registration"); var import_serialize_raw_block = require("./parser/serialize-raw-block"); var import_utils = require("./utils"); var import_jsx_runtime = require("react/jsx-runtime"); function getBlockDefaultClassName(blockName) { const className = "wp-block-" + blockName.replace(/\//, "-").replace(/^core-/, ""); return (0, import_hooks.applyFilters)( "blocks.getBlockDefaultClassName", className, blockName ); } function getBlockMenuDefaultClassName(blockName) { const className = "editor-block-list-item-" + blockName.replace(/\//, "-").replace(/^core-/, ""); return (0, import_hooks.applyFilters)( "blocks.getBlockMenuDefaultClassName", className, blockName ); } var blockPropsProvider = {}; var innerBlocksPropsProvider = {}; function getBlockProps(props = {}) { const { blockType, attributes } = blockPropsProvider; return getBlockProps.skipFilters ? props : (0, import_hooks.applyFilters)( "blocks.getSaveContent.extraProps", { ...props }, blockType, attributes ); } function getInnerBlocksProps(props = {}) { const { innerBlocks } = innerBlocksPropsProvider; if (!Array.isArray(innerBlocks)) { return { ...props, children: innerBlocks }; } const html = serialize(innerBlocks, { isInnerBlocks: true }); const children = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_element.RawHTML, { children: html }); return { ...props, children }; } function getSaveElement(blockTypeOrName, attributes, innerBlocks = []) { const blockType = (0, import_utils.normalizeBlockType)(blockTypeOrName); if (!blockType?.save) { return null; } let { save } = blockType; if (save.prototype instanceof import_element.Component) { const instance = new save({ attributes }); save = instance.render.bind(instance); } blockPropsProvider.blockType = blockType; blockPropsProvider.attributes = attributes; innerBlocksPropsProvider.innerBlocks = innerBlocks; let element = save({ attributes, innerBlocks }); if (element !== null && typeof element === "object" && (0, import_hooks.hasFilter)("blocks.getSaveContent.extraProps") && !(blockType.apiVersion > 1)) { const props = (0, import_hooks.applyFilters)( "blocks.getSaveContent.extraProps", { ...element.props }, blockType, attributes ); if (!(0, import_is_shallow_equal.default)(props, element.props)) { element = (0, import_element.cloneElement)(element, props); } } return (0, import_hooks.applyFilters)( "blocks.getSaveElement", element, blockType, attributes ); } function getSaveContent(blockTypeOrName, attributes, innerBlocks) { const blockType = (0, import_utils.normalizeBlockType)(blockTypeOrName); return (0, import_element.renderToString)( getSaveElement(blockType, attributes, innerBlocks) ); } function getCommentAttributes(blockType, attributes) { return Object.entries(blockType.attributes ?? {}).reduce( (accumulator, [key, attributeSchema]) => { const value = attributes[key]; if (void 0 === value) { return accumulator; } if (attributeSchema.source !== void 0) { return accumulator; } if (attributeSchema.role === "local") { return accumulator; } if (attributeSchema.__experimentalRole === "local") { (0, import_deprecated.default)("__experimentalRole attribute", { since: "6.7", version: "6.8", alternative: "role attribute", hint: `Check the block.json of the ${blockType?.name} block.` }); return accumulator; } if ("default" in attributeSchema && JSON.stringify(attributeSchema.default) === JSON.stringify(value)) { return accumulator; } accumulator[key] = value; return accumulator; }, {} ); } function serializeAttributes(attributes) { return JSON.stringify(attributes).replaceAll("\\\\", "\\u005c").replaceAll("--", "\\u002d\\u002d").replaceAll("<", "\\u003c").replaceAll(">", "\\u003e").replaceAll("&", "\\u0026").replaceAll('\\"', "\\u0022"); } function getBlockInnerHTML(block) { let saveContent = block.originalContent; if (block.isValid || block.innerBlocks.length) { try { saveContent = getSaveContent( block.name, block.attributes, block.innerBlocks ); } catch (error) { } } return saveContent; } function getCommentDelimitedContent(rawBlockName, attributes, content) { const serializedAttributes = attributes && Object.entries(attributes).length ? serializeAttributes(attributes) + " " : ""; const blockName = rawBlockName?.startsWith("core/") ? rawBlockName.slice(5) : rawBlockName; if (!content) { return `<!-- wp:${blockName} ${serializedAttributes}/-->`; } return `<!-- wp:${blockName} ${serializedAttributes}--> ` + content + ` <!-- /wp:${blockName} -->`; } function serializeBlock(block, { isInnerBlocks = false } = {}) { if (!block.isValid && block.__unstableBlockSource) { return (0, import_serialize_raw_block.serializeRawBlock)(block.__unstableBlockSource); } const blockName = block.name; const saveContent = getBlockInnerHTML(block); if (blockName === (0, import_registration.getUnregisteredTypeHandlerName)() || !isInnerBlocks && blockName === (0, import_registration.getFreeformContentHandlerName)()) { return saveContent; } const blockType = (0, import_registration.getBlockType)(blockName); if (!blockType) { return saveContent; } const saveAttributes = getCommentAttributes(blockType, block.attributes); return getCommentDelimitedContent(blockName, saveAttributes, saveContent); } function __unstableSerializeAndClean(blocks) { if (blocks.length === 1 && (0, import_utils.isUnmodifiedDefaultBlock)(blocks[0])) { blocks = []; } let content = serialize(blocks); if (blocks.length === 1 && blocks[0].name === (0, import_registration.getFreeformContentHandlerName)() && blocks[0].name === "core/freeform") { content = (0, import_autop.removep)(content); } return content; } function serialize(blocks, options) { const blocksArray = Array.isArray(blocks) ? blocks : [blocks]; return blocksArray.map((block) => serializeBlock(block, options)).join("\n\n"); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { __unstableSerializeAndClean, getBlockDefaultClassName, getBlockInnerHTML, getBlockMenuDefaultClassName, getBlockProps, getCommentAttributes, getCommentDelimitedContent, getInnerBlocksProps, getSaveContent, getSaveElement, serializeAttributes, serializeBlock }); //# sourceMappingURL=serializer.js.map