UNPKG

@wordpress/blocks

Version:
8 lines (7 loc) 6.05 kB
{ "version": 3, "sources": ["../../src/api/node.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport deprecated from '@wordpress/deprecated';\n\n/**\n * Internal dependencies\n */\nimport * as children from './children';\n\n/**\n * A representation of a single node within a block's rich text value. If\n * representing a text node, the value is simply a string of the node value.\n * As representing an element node, it is an object of:\n *\n * 1. `type` (string): Tag name.\n * 2. `props` (object): Attributes and children array of WPBlockNode.\n *\n * @typedef {string|Object} WPBlockNode\n */\n\n/**\n * Given a single node and a node type (e.g. `'br'`), returns true if the node\n * corresponds to that type, false otherwise.\n *\n * @param {WPBlockNode} node Block node to test\n * @param {string} type Node to type to test against.\n *\n * @return {boolean} Whether node is of intended type.\n */\nfunction isNodeOfType( node, type ) {\n\tdeprecated( 'wp.blocks.node.isNodeOfType', {\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\treturn node && node.type === type;\n}\n\n/**\n * Given an object implementing the NamedNodeMap interface, returns a plain\n * object equivalent value of name, value key-value pairs.\n *\n * @see https://dom.spec.whatwg.org/#interface-namednodemap\n *\n * @param {NamedNodeMap} nodeMap NamedNodeMap to convert to object.\n *\n * @return {Object} Object equivalent value of NamedNodeMap.\n */\nexport function getNamedNodeMapAsObject( nodeMap ) {\n\tconst result = {};\n\tfor ( let i = 0; i < nodeMap.length; i++ ) {\n\t\tconst { name, value } = nodeMap[ i ];\n\t\tresult[ name ] = value;\n\t}\n\n\treturn result;\n}\n\n/**\n * Given a DOM Element or Text node, returns an equivalent block node. Throws\n * if passed any node type other than element or text.\n *\n * @throws {TypeError} If non-element/text node is passed.\n *\n * @param {Node} domNode DOM node to convert.\n *\n * @return {WPBlockNode} Block node equivalent to DOM node.\n */\nexport function fromDOM( domNode ) {\n\tdeprecated( 'wp.blocks.node.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\tif ( domNode.nodeType === domNode.TEXT_NODE ) {\n\t\treturn domNode.nodeValue;\n\t}\n\n\tif ( domNode.nodeType !== domNode.ELEMENT_NODE ) {\n\t\tthrow new TypeError(\n\t\t\t'A block node can only be created from a node of type text or ' +\n\t\t\t\t'element.'\n\t\t);\n\t}\n\n\treturn {\n\t\ttype: domNode.nodeName.toLowerCase(),\n\t\tprops: {\n\t\t\t...getNamedNodeMapAsObject( domNode.attributes ),\n\t\t\tchildren: children.fromDOM( domNode.childNodes ),\n\t\t},\n\t};\n}\n\n/**\n * Given a block node, returns its HTML string representation.\n *\n * @param {WPBlockNode} node Block node to convert to string.\n *\n * @return {string} String HTML representation of block node.\n */\nexport function toHTML( node ) {\n\tdeprecated( 'wp.blocks.node.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\treturn children.toHTML( [ node ] );\n}\n\n/**\n * Given a selector, returns an hpq matcher generating a WPBlockNode 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.node.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\ttry {\n\t\t\treturn fromDOM( match );\n\t\t} catch ( error ) {\n\t\t\treturn null;\n\t\t}\n\t};\n}\n\n/**\n * Object of utility functions used in managing block attribute values of\n * source `node`.\n *\n * @see https://github.com/WordPress/gutenberg/pull/10439\n *\n * @deprecated since 4.0. The `node` source should not be used, and can be\n * replaced by the `html` source.\n *\n * @private\n */\nexport default {\n\tisNodeOfType,\n\tfromDOM,\n\ttoHTML,\n\tmatcher,\n};\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA,iBAAAA;AAAA,EAAA;AAAA;AAAA,gBAAAC;AAAA;AAAA;AAGA,wBAAuB;AAKvB,eAA0B;AAsB1B,SAAS,aAAc,MAAM,MAAO;AACnC,wBAAAC,SAAY,+BAA+B;AAAA,IAC1C,OAAO;AAAA,IACP,SAAS;AAAA,IACT,MAAM;AAAA,EACP,CAAE;AAEF,SAAO,QAAQ,KAAK,SAAS;AAC9B;AAYO,SAAS,wBAAyB,SAAU;AAClD,QAAM,SAAS,CAAC;AAChB,WAAU,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAM;AAC1C,UAAM,EAAE,MAAM,MAAM,IAAI,QAAS,CAAE;AACnC,WAAQ,IAAK,IAAI;AAAA,EAClB;AAEA,SAAO;AACR;AAYO,SAASF,SAAS,SAAU;AAClC,wBAAAE,SAAY,0BAA0B;AAAA,IACrC,OAAO;AAAA,IACP,SAAS;AAAA,IACT,aAAa;AAAA,IACb,MAAM;AAAA,EACP,CAAE;AAEF,MAAK,QAAQ,aAAa,QAAQ,WAAY;AAC7C,WAAO,QAAQ;AAAA,EAChB;AAEA,MAAK,QAAQ,aAAa,QAAQ,cAAe;AAChD,UAAM,IAAI;AAAA,MACT;AAAA,IAED;AAAA,EACD;AAEA,SAAO;AAAA,IACN,MAAM,QAAQ,SAAS,YAAY;AAAA,IACnC,OAAO;AAAA,MACN,GAAG,wBAAyB,QAAQ,UAAW;AAAA,MAC/C,UAAmB,iBAAS,QAAQ,UAAW;AAAA,IAChD;AAAA,EACD;AACD;AASO,SAASD,QAAQ,MAAO;AAC9B,wBA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