@wordpress/blocks
Version:
Block API for WordPress.
8 lines (7 loc) • 7.32 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/api/children.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { renderToString } from '@wordpress/element';\nimport deprecated from '@wordpress/deprecated';\n\n/**\n * Internal dependencies\n */\nimport * as node from './node';\n\n/**\n * A representation of a block's rich text value.\n *\n * @typedef {WPBlockNode[]} WPBlockChildren\n */\n\n/**\n * Given block children, returns a serialize-capable WordPress element.\n *\n * @param {WPBlockChildren} children Block children object to convert.\n *\n * @return {Element} A serialize-capable element.\n */\nexport function getSerializeCapableElement( children ) {\n\t// The fact that block children are compatible with the element serializer is\n\t// merely an implementation detail that currently serves to be true, but\n\t// should not be mistaken as being a guarantee on the external API. The\n\t// public API only offers guarantees to work with strings (toHTML) and DOM\n\t// elements (fromDOM), and should provide utilities to manipulate the value\n\t// rather than expect consumers to inspect or construct its shape (concat).\n\treturn children;\n}\n\n/**\n * Given block children, returns an array of block nodes.\n *\n * @param {WPBlockChildren} children Block children object to convert.\n *\n * @return {Array<WPBlockNode>} An array of individual block nodes.\n */\nfunction getChildrenArray( children ) {\n\tdeprecated( 'wp.blocks.children.getChildrenArray', {\n\t\tsince: '6.1',\n\t\tversion: '6.3',\n\t\tlink: 'https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/introducing-attributes-and-editable-fields/',\n\t} );\n\n\t// The fact that block children are compatible with the element serializer\n\t// is merely an implementation detail that currently serves to be true, but\n\t// should not be mistaken as being a guarantee on the external API.\n\treturn children;\n}\n\n/**\n * Given two or more block nodes, returns a new block node representing a\n * concatenation of its values.\n *\n * @param {...WPBlockChildren} blockNodes Block nodes to concatenate.\n *\n * @return {WPBlockChildren} Concatenated block node.\n */\nexport function concat( ...blockNodes ) {\n\tdeprecated( 'wp.blocks.children.concat', {\n\t\tsince: '6.1',\n\t\tversion: '6.3',\n\t\talternative: 'wp.richText.concat',\n\t\tlink: 'https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/introducing-attributes-and-editable-fields/',\n\t} );\n\n\tconst result = [];\n\tfor ( let i = 0; i < blockNodes.length; i++ ) {\n\t\tconst blockNode = Array.isArray( blockNodes[ i ] )\n\t\t\t? blockNodes[ i ]\n\t\t\t: [ blockNodes[ i ] ];\n\t\tfor ( let j = 0; j < blockNode.length; j++ ) {\n\t\t\tconst child = blockNode[ j ];\n\t\t\tconst canConcatToPreviousString =\n\t\t\t\ttypeof child === 'string' &&\n\t\t\t\ttypeof result[ result.length - 1 ] === 'string';\n\n\t\t\tif ( canConcatToPreviousString ) {\n\t\t\t\tresult[ result.length - 1 ] += child;\n\t\t\t} else {\n\t\t\t\tresult.push( child );\n\t\t\t}\n\t\t}\n\t}\n\n\treturn result;\n}\n\n/**\n * Given an iterable set of DOM nodes, returns equivalent block children.\n * Ignores any non-element/text nodes included in set.\n *\n * @param {Iterable.<Node>} domNodes Iterable set of DOM nodes to convert.\n *\n * @return {WPBlockChildren} Block children equivalent to DOM nodes.\n */\nexport function fromDOM( domNodes ) {\n\tdeprecated( 'wp.blocks.children.fromDOM', {\n\t\tsince: '6.1',\n\t\tversion: '6.3',\n\t\talternative: 'wp.richText.create',\n\t\tlink: 'https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/introducing-attributes-and-editable-fields/',\n\t} );\n\n\tconst result = [];\n\tfor ( let i = 0; i < domNodes.length; i++ ) {\n\t\ttry {\n\t\t\tresult.push( node.fromDOM( domNodes[ i ] ) );\n\t\t} catch ( error ) {\n\t\t\t// Simply ignore if DOM node could not be converted.\n\t\t}\n\t}\n\n\treturn result;\n}\n\n/**\n * Given a block node, returns its HTML string representation.\n *\n * @param {WPBlockChildren} children Block node(s) to convert to string.\n *\n * @return {string} String HTML representation of block node.\n */\nexport function toHTML( children ) {\n\tdeprecated( 'wp.blocks.children.toHTML', {\n\t\tsince: '6.1',\n\t\tversion: '6.3',\n\t\talternative: 'wp.richText.toHTMLString',\n\t\tlink: 'https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/introducing-attributes-and-editable-fields/',\n\t} );\n\n\tconst element = getSerializeCapableElement( children );\n\n\treturn renderToString( element );\n}\n\n/**\n * Given a selector, returns an hpq matcher generating a WPBlockChildren value\n * matching the selector result.\n *\n * @param {string} selector DOM selector.\n *\n * @return {Function} hpq matcher.\n */\nexport function matcher( selector ) {\n\tdeprecated( 'wp.blocks.children.matcher', {\n\t\tsince: '6.1',\n\t\tversion: '6.3',\n\t\talternative: 'html source',\n\t\tlink: 'https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/introducing-attributes-and-editable-fields/',\n\t} );\n\n\treturn ( domNode ) => {\n\t\tlet match = domNode;\n\n\t\tif ( selector ) {\n\t\t\tmatch = domNode.querySelector( selector );\n\t\t}\n\n\t\tif ( match ) {\n\t\t\treturn fromDOM( match.childNodes );\n\t\t}\n\n\t\treturn [];\n\t};\n}\n\n/**\n * Object of utility functions used in managing block attribute values of\n * source `children`.\n *\n * @see https://github.com/WordPress/gutenberg/pull/10439\n *\n * @deprecated since 4.0. The `children` source should not be used, and can be\n * replaced by the `html` source.\n *\n * @private\n */\nexport default {\n\tconcat,\n\tgetChildrenArray,\n\tfromDOM,\n\ttoHTML,\n\tmatcher,\n};\n"],
"mappings": ";AAGA,SAAS,sBAAsB;AAC/B,OAAO,gBAAgB;AAKvB,YAAY,UAAU;AAef,SAAS,2BAA4B,UAAW;AAOtD,SAAO;AACR;AASA,SAAS,iBAAkB,UAAW;AACrC,aAAY,uCAAuC;AAAA,IAClD,OAAO;AAAA,IACP,SAAS;AAAA,IACT,MAAM;AAAA,EACP,CAAE;AAKF,SAAO;AACR;AAUO,SAAS,UAAW,YAAa;AACvC,aAAY,6BAA6B;AAAA,IACxC,OAAO;AAAA,IACP,SAAS;AAAA,IACT,aAAa;AAAA,IACb,MAAM;AAAA,EACP,CAAE;AAEF,QAAM,SAAS,CAAC;AAChB,WAAU,IAAI,GAAG,IAAI,WAAW,QAAQ,KAAM;AAC7C,UAAM,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