{
  "version": 3,
  "sources": ["../../../src/api/raw-handling/utils.ts"],
  "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { isPhrasingContent, getPhrasingContentSchema } from '@wordpress/dom';\n\n/**\n * Internal dependencies\n */\nimport { hasBlockSupport } from '..';\nimport { getRawTransforms } from './get-raw-transforms';\nimport type { NodeFilterFunction } from './types';\n\nexport function getBlockContentSchemaFromTransforms(\n\ttransforms: any[],\n\tcontext?: string\n) {\n\tconst phrasingContentSchema = getPhrasingContentSchema( context );\n\tconst schemaArgs = { phrasingContentSchema, isPaste: context === 'paste' };\n\tconst schemas = transforms.map( ( { isMatch, blockName, schema } ) => {\n\t\tconst hasAnchorSupport = hasBlockSupport( blockName, 'anchor' );\n\n\t\tschema = typeof schema === 'function' ? schema( schemaArgs ) : schema;\n\n\t\t// If the block does not has anchor support and the transform does not\n\t\t// provides an isMatch we can return the schema right away.\n\t\tif ( ! hasAnchorSupport && ! isMatch ) {\n\t\t\treturn schema;\n\t\t}\n\n\t\tif ( ! schema ) {\n\t\t\treturn {};\n\t\t}\n\n\t\treturn Object.fromEntries(\n\t\t\tObject.entries( schema ).map( ( [ key, value ]: any[] ) => {\n\t\t\t\tlet attributes = ( value as any ).attributes || [];\n\t\t\t\t// If the block supports the \"anchor\" functionality, it needs to keep its ID attribute.\n\t\t\t\tif ( hasAnchorSupport ) {\n\t\t\t\t\tattributes = [ ...attributes, 'id' ];\n\t\t\t\t}\n\t\t\t\treturn [\n\t\t\t\t\tkey,\n\t\t\t\t\t{\n\t\t\t\t\t\t...value,\n\t\t\t\t\t\tattributes,\n\t\t\t\t\t\tisMatch: isMatch ? isMatch : undefined,\n\t\t\t\t\t},\n\t\t\t\t];\n\t\t\t} )\n\t\t);\n\t} );\n\n\tfunction mergeTagNameSchemaProperties(\n\t\tobjValue: any,\n\t\tsrcValue: any,\n\t\tkey: string\n\t) {\n\t\tswitch ( key ) {\n\t\t\tcase 'children': {\n\t\t\t\tif ( objValue === '*' || srcValue === '*' ) {\n\t\t\t\t\treturn '*';\n\t\t\t\t}\n\n\t\t\t\treturn { ...objValue, ...srcValue };\n\t\t\t}\n\t\t\tcase 'attributes':\n\t\t\tcase 'require': {\n\t\t\t\treturn [ ...( objValue || [] ), ...( srcValue || [] ) ];\n\t\t\t}\n\t\t\tcase 'isMatch': {\n\t\t\t\t// If one of the values being merge is undefined (matches everything),\n\t\t\t\t// the result of the merge will be undefined.\n\t\t\t\tif ( ! objValue || ! srcValue ) {\n\t\t\t\t\treturn undefined;\n\t\t\t\t}\n\t\t\t\t// When merging two isMatch functions, the result is a new function\n\t\t\t\t// that returns if one of the source functions returns true.\n\t\t\t\treturn ( ...args: any[] ) => {\n\t\t\t\t\treturn objValue( ...args ) || srcValue( ...args );\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\t}\n\n\t// A tagName schema is an object with children, attributes, require, and\n\t// isMatch properties.\n\tfunction mergeTagNameSchemas( a: any, b: any ) {\n\t\tfor ( const key in b ) {\n\t\t\ta[ key ] = a[ key ]\n\t\t\t\t? mergeTagNameSchemaProperties( a[ key ], b[ key ], key )\n\t\t\t\t: { ...b[ key ] };\n\t\t}\n\t\treturn a;\n\t}\n\n\t// A schema is an object with tagName schemas by tag name.\n\tfunction mergeSchemas( a: any, b: any ) {\n\t\tfor ( const key in b ) {\n\t\t\ta[ key ] = a[ key ]\n\t\t\t\t? mergeTagNameSchemas( a[ key ], b[ key ] )\n\t\t\t\t: { ...b[ key ] };\n\t\t}\n\t\treturn a;\n\t}\n\n\treturn schemas.reduce( mergeSchemas, {} );\n}\n\n/**\n * Gets the block content schema, which is extracted and merged from all\n * registered blocks with raw transforms.\n *\n * @param context Set to \"paste\" when in paste context, where the\n *                schema is more strict.\n *\n * @return A complete block content schema.\n */\nexport function getBlockContentSchema( context?: string ) {\n\treturn getBlockContentSchemaFromTransforms( getRawTransforms(), context );\n}\n\n/**\n * Checks whether HTML can be considered plain text. That is, it does not contain\n * any elements that are not line breaks, or it only contains a single non-semantic\n * wrapper element (span) with no semantic child elements.\n *\n * @param HTML The HTML to check.\n *\n * @return Whether the HTML can be considered plain text.\n */\nexport function isPlain( HTML: string ) {\n\tif ( ! /<(?!br[ />])/i.test( HTML ) ) {\n\t\treturn true;\n\t}\n\n\tconst doc = document.implementation.createHTMLDocument( '' );\n\tdoc.body.innerHTML = HTML;\n\n\tif ( doc.body.children.length !== 1 ) {\n\t\treturn false;\n\t}\n\n\tconst wrapper = doc.body.children.item( 0 )!;\n\n\tconst descendants = wrapper.getElementsByTagName( '*' );\n\tfor ( let i = 0; i < descendants.length; i++ ) {\n\t\tif ( descendants.item( i )!.tagName !== 'BR' ) {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\tif ( wrapper.tagName !== 'SPAN' ) {\n\t\treturn false;\n\t}\n\n\treturn true;\n}\n\nexport type { NodeFilterFunction } from './types';\n\n/**\n * Given node filters, deeply filters and mutates a NodeList.\n *\n * @param nodeList The nodeList to filter.\n * @param filters  An array of functions that can mutate with the provided node.\n * @param doc      The document of the nodeList.\n * @param schema   The schema to use.\n */\nexport function deepFilterNodeList(\n\tnodeList: NodeList,\n\tfilters: NodeFilterFunction[],\n\tdoc: Document,\n\tschema?: Record< string, unknown >\n) {\n\tArray.from( nodeList ).forEach( ( node ) => {\n\t\tdeepFilterNodeList( node.childNodes, filters, doc, schema );\n\n\t\tfilters.forEach( ( item ) => {\n\t\t\t// Make sure the node is still attached to the document.\n\t\t\tif ( ! doc.contains( node ) ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\titem( node, doc, schema );\n\t\t} );\n\t} );\n}\n\n/**\n * Given node filters, deeply filters HTML tags.\n * Filters from the deepest nodes to the top.\n *\n * @param HTML    The HTML to filter.\n * @param filters An array of functions that can mutate with the provided node.\n * @param schema  The schema to use.\n *\n * @return The filtered HTML.\n */\nexport function deepFilterHTML(\n\tHTML: string,\n\tfilters: NodeFilterFunction[] = [],\n\tschema?: Record< string, unknown >\n) {\n\tconst doc = document.implementation.createHTMLDocument( '' );\n\n\tdoc.body.innerHTML = HTML;\n\n\tdeepFilterNodeList( doc.body.childNodes, filters, doc, schema );\n\n\treturn doc.body.innerHTML;\n}\n\n/**\n * Gets a sibling within text-level context.\n *\n * @param node  The subject node.\n * @param which \"next\" or \"previous\".\n */\nexport function getSibling( node: Node, which: string ): Node | undefined {\n\tconst sibling = ( node as any )[ `${ which }Sibling` ] as Node | undefined;\n\n\tif ( sibling && isPhrasingContent( sibling ) ) {\n\t\treturn sibling;\n\t}\n\n\tconst { parentNode } = node;\n\n\tif ( ! parentNode || ! isPhrasingContent( parentNode ) ) {\n\t\treturn;\n\t}\n\n\treturn getSibling( parentNode, which );\n}\n"],
  "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,iBAA4D;AAK5D,eAAgC;AAChC,gCAAiC;AAG1B,SAAS,oCACf,YACA,SACC;AACD,QAAM,4BAAwB,qCAA0B,OAAQ;AAChE,QAAM,aAAa,EAAE,uBAAuB,SAAS,YAAY,QAAQ;AACzE,QAAM,UAAU,WAAW,IAAK,CAAE,EAAE,SAAS,WAAW,OAAO,MAAO;AACrE,UAAM,uBAAmB,0BAAiB,WAAW,QAAS;AAE9D,aAAS,OAAO,WAAW,aAAa,OAAQ,UAAW,IAAI;AAI/D,QAAK,CAAE,oBAAoB,CAAE,SAAU;AACtC,aAAO;AAAA,IACR;AAEA,QAAK,CAAE,QAAS;AACf,aAAO,CAAC;AAAA,IACT;AAEA,WAAO,OAAO;AAAA,MACb,OAAO,QAAS,MAAO,EAAE,IAAK,CAAE,CAAE,KAAK,KAAM,MAAc;AAC1D,YAAI,aAAe,MAAe,cAAc,CAAC;AAEjD,YAAK,kBAAmB;AACvB,uBAAa,CAAE,GAAG,YAAY,IAAK;AAAA,QACpC;AACA,eAAO;AAAA,UACN;AAAA,UACA;AAAA,YACC,GAAG;AAAA,YACH;AAAA,YACA,SAAS,UAAU,UAAU;AAAA,UA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