UNPKG

@wordpress/blocks

Version:
189 lines (187 loc) 6.68 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/parser/get-block-attributes.js var get_block_attributes_exports = {}; __export(get_block_attributes_exports, { getBlockAttribute: () => getBlockAttribute, getBlockAttributes: () => getBlockAttributes, isOfType: () => isOfType, isOfTypes: () => isOfTypes, isValidByEnum: () => isValidByEnum, isValidByType: () => isValidByType, matcherFromSource: () => matcherFromSource, parseWithAttributeSchema: () => parseWithAttributeSchema, toBooleanAttributeMatcher: () => toBooleanAttributeMatcher }); module.exports = __toCommonJS(get_block_attributes_exports); var import_hpq = require("hpq"); var import_memize = __toESM(require("memize")); var import_hooks = require("@wordpress/hooks"); var import_rich_text = require("@wordpress/rich-text"); var import_matchers = require("../matchers"); var import_utils = require("../utils"); var toBooleanAttributeMatcher = (matcher) => (value) => matcher(value) !== void 0; function isOfType(value, type) { switch (type) { case "rich-text": return value instanceof import_rich_text.RichTextData; case "string": return typeof value === "string"; case "boolean": return typeof value === "boolean"; case "object": return !!value && value.constructor === Object; case "null": return value === null; case "array": return Array.isArray(value); case "integer": case "number": return typeof value === "number"; } return true; } function isOfTypes(value, types) { return types.some((type) => isOfType(value, type)); } function getBlockAttribute(attributeKey, attributeSchema, innerDOM, commentAttributes, innerHTML) { let value; switch (attributeSchema.source) { // An undefined source means that it's an attribute serialized to the // block's "comment". case void 0: value = commentAttributes ? commentAttributes[attributeKey] : void 0; break; // raw source means that it's the original raw block content. case "raw": value = innerHTML; break; case "attribute": case "property": case "html": case "text": case "rich-text": case "children": case "node": case "query": case "tag": value = parseWithAttributeSchema(innerDOM, attributeSchema); break; } if (!isValidByType(value, attributeSchema.type) || !isValidByEnum(value, attributeSchema.enum)) { value = void 0; } if (value === void 0) { value = (0, import_utils.getDefault)(attributeSchema); } return value; } function isValidByType(value, type) { return type === void 0 || isOfTypes(value, Array.isArray(type) ? type : [type]); } function isValidByEnum(value, enumSet) { return !Array.isArray(enumSet) || enumSet.includes(value); } var matcherFromSource = (0, import_memize.default)((sourceConfig) => { switch (sourceConfig.source) { case "attribute": { let matcher = (0, import_matchers.attr)(sourceConfig.selector, sourceConfig.attribute); if (sourceConfig.type === "boolean") { matcher = toBooleanAttributeMatcher(matcher); } return matcher; } case "html": return (0, import_matchers.html)(sourceConfig.selector, sourceConfig.multiline); case "text": return (0, import_matchers.text)(sourceConfig.selector); case "rich-text": return (0, import_matchers.richText)( sourceConfig.selector, sourceConfig.__unstablePreserveWhiteSpace ); case "children": return (0, import_matchers.children)(sourceConfig.selector); case "node": return (0, import_matchers.node)(sourceConfig.selector); case "query": const subMatchers = Object.fromEntries( Object.entries(sourceConfig.query).map( ([key, subSourceConfig]) => [ key, matcherFromSource(subSourceConfig) ] ) ); return (0, import_matchers.query)(sourceConfig.selector, subMatchers); case "tag": { const matcher = (0, import_matchers.prop)(sourceConfig.selector, "nodeName"); return (domNode) => matcher(domNode)?.toLowerCase(); } default: console.error(`Unknown source type "${sourceConfig.source}"`); } }); function parseHtml(innerHTML) { return (0, import_hpq.parse)(innerHTML, (h) => h); } function parseWithAttributeSchema(innerHTML, attributeSchema) { return matcherFromSource(attributeSchema)(parseHtml(innerHTML)); } function getBlockAttributes(blockTypeOrName, innerHTML, attributes = {}) { const doc = parseHtml(innerHTML); const blockType = (0, import_utils.normalizeBlockType)(blockTypeOrName); const blockAttributes = Object.fromEntries( Object.entries(blockType.attributes ?? {}).map( ([key, schema]) => [ key, getBlockAttribute(key, schema, doc, attributes, innerHTML) ] ) ); return (0, import_hooks.applyFilters)( "blocks.getBlockAttributes", blockAttributes, blockType, innerHTML, attributes ); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { getBlockAttribute, getBlockAttributes, isOfType, isOfTypes, isValidByEnum, isValidByType, matcherFromSource, parseWithAttributeSchema, toBooleanAttributeMatcher }); //# sourceMappingURL=get-block-attributes.js.map