{
  "version": 3,
  "sources": ["../../../src/api/raw-handling/shortcode-converter.ts"],
  "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { regexp, next } from '@wordpress/shortcode';\n\n/**\n * Internal dependencies\n */\nimport { createBlock, getBlockTransforms, findTransform } from '../factory';\nimport { getBlockType } from '../registration';\nimport { getBlockAttributes } from '../parser/get-block-attributes';\nimport { applyBuiltInValidationFixes } from '../parser/apply-built-in-validation-fixes';\nimport type { Block } from '../../types';\n\ninterface ShortcodeTransform {\n\ttype: string;\n\tblockName: string;\n\ttag: string | string[];\n\tisMatch?: ( attrs: unknown ) => boolean;\n\ttransform?: ( ...args: unknown[] ) => Block | Block[];\n\tattributes: Record<\n\t\tstring,\n\t\t{ shortcode?: ( ...args: unknown[] ) => unknown }\n\t>;\n}\n\nconst castArray = < T >( maybeArray: T | T[] ): T[] =>\n\tArray.isArray( maybeArray ) ? maybeArray : [ maybeArray ];\n\nconst beforeLineRegexp = /(\\n|<p>|<br\\s*\\/?>)\\s*$/;\nconst afterLineRegexp = /^\\s*(\\n|<\\/p>|<br\\s*\\/?>)/;\n\nfunction segmentHTMLToShortcodeBlock(\n\tHTML: string,\n\tlastIndex: number = 0,\n\texcludedBlockNames: string[] = []\n): Array< string | Block > {\n\t// Get all matches.\n\tconst transformsFrom = getBlockTransforms(\n\t\t'from'\n\t) as unknown as ShortcodeTransform[];\n\n\tconst transformation = findTransform(\n\t\ttransformsFrom as unknown as Parameters< typeof findTransform >[ 0 ],\n\t\t( ( transform: unknown ) => {\n\t\t\tconst t = transform as ShortcodeTransform;\n\t\t\treturn (\n\t\t\t\texcludedBlockNames.indexOf( t.blockName ) === -1 &&\n\t\t\t\tt.type === 'shortcode' &&\n\t\t\t\tcastArray( t.tag ).some( ( tag: string ) =>\n\t\t\t\t\tregexp( tag ).test( HTML )\n\t\t\t\t)\n\t\t\t);\n\t\t} ) as Parameters< typeof findTransform >[ 1 ]\n\t) as unknown as ShortcodeTransform | null;\n\n\tif ( ! transformation ) {\n\t\treturn [ HTML ];\n\t}\n\n\tconst transformTags = castArray( transformation.tag );\n\tconst transformTag = transformTags.find( ( tag ) =>\n\t\tregexp( tag ).test( HTML )\n\t);\n\n\tlet match: any;\n\tconst previousIndex = lastIndex;\n\n\tif ( ( match = next( transformTag!, HTML, lastIndex ) ) ) {\n\t\tlastIndex = match.index + match.content.length;\n\t\tconst beforeHTML = HTML.substr( 0, match.index );\n\t\tconst afterHTML = HTML.substr( lastIndex );\n\n\t\t// If the shortcode content does not contain HTML and the shortcode is\n\t\t// not on a new line (or in paragraph from Markdown converter),\n\t\t// consider the shortcode as inline text, and thus skip conversion for\n\t\t// this segment.\n\t\tif (\n\t\t\t! match.shortcode.content?.includes( '<' ) &&\n\t\t\t! (\n\t\t\t\tbeforeLineRegexp.test( beforeHTML ) &&\n\t\t\t\tafterLineRegexp.test( afterHTML )\n\t\t\t)\n\t\t) {\n\t\t\treturn segmentHTMLToShortcodeBlock( HTML, lastIndex );\n\t\t}\n\n\t\t// If a transformation's `isMatch` predicate fails for the inbound\n\t\t// shortcode, try again by excluding the current block type.\n\t\t//\n\t\t// This is the only call to `segmentHTMLToShortcodeBlock` that should\n\t\t// ever carry over `excludedBlockNames`. Other calls in the module\n\t\t// should skip that argument as a way to reset the exclusion state, so\n\t\t// that one `isMatch` fail in an HTML fragment doesn't prevent any\n\t\t// valid matches in subsequent fragments.\n\t\tif (\n\t\t\ttransformation.isMatch &&\n\t\t\t! transformation.isMatch( match.shortcode.attrs )\n\t\t) {\n\t\t\treturn segmentHTMLToShortcodeBlock( HTML, previousIndex, [\n\t\t\t\t...excludedBlockNames,\n\t\t\t\ttransformation.blockName!,\n\t\t\t] );\n\t\t}\n\n\t\tlet blocks: Block[] = [];\n\t\tif ( typeof transformation.transform === 'function' ) {\n\t\t\t// Passing all of `match` as second argument is intentionally broad\n\t\t\t// but shouldn't be too relied upon.\n\t\t\t//\n\t\t\t// See: https://github.com/WordPress/gutenberg/pull/3610#discussion_r152546926\n\t\t\tblocks = ( [] as Block[] ).concat(\n\t\t\t\ttransformation.transform( match.shortcode.attrs, match ) as\n\t\t\t\t\t| Block\n\t\t\t\t\t| Block[]\n\t\t\t);\n\n\t\t\t// Applying the built-in fixes can enhance the attributes with missing content like \"className\".\n\t\t\tblocks = blocks.map( ( block: Block ) => {\n\t\t\t\tblock.originalContent = match.shortcode.content;\n\t\t\t\treturn applyBuiltInValidationFixes(\n\t\t\t\t\tblock,\n\t\t\t\t\tgetBlockType( block.name )!\n\t\t\t\t);\n\t\t\t} );\n\t\t} else {\n\t\t\tconst attributes = Object.fromEntries(\n\t\t\t\tObject.entries( transformation.attributes )\n\t\t\t\t\t.filter( ( [ , schema ] ) => schema.shortcode )\n\t\t\t\t\t// Passing all of `match` as second argument is intentionally broad\n\t\t\t\t\t// but shouldn't be too relied upon.\n\t\t\t\t\t//\n\t\t\t\t\t// See: https://github.com/WordPress/gutenberg/pull/3610#discussion_r152546926\n\t\t\t\t\t.map( ( [ key, schema ] ) => [\n\t\t\t\t\t\tkey,\n\t\t\t\t\t\tschema.shortcode!( match.shortcode.attrs, match ),\n\t\t\t\t\t] )\n\t\t\t);\n\n\t\t\tconst blockType = getBlockType( transformation.blockName );\n\t\t\tif ( ! blockType ) {\n\t\t\t\treturn [ HTML ];\n\t\t\t}\n\n\t\t\tconst transformationBlockType = {\n\t\t\t\t...blockType,\n\t\t\t\tattributes: transformation.attributes,\n\t\t\t};\n\n\t\t\tlet block = createBlock(\n\t\t\t\ttransformation.blockName,\n\t\t\t\tgetBlockAttributes(\n\t\t\t\t\ttransformationBlockType as unknown as string,\n\t\t\t\t\tmatch.shortcode.content,\n\t\t\t\t\tattributes\n\t\t\t\t)\n\t\t\t);\n\n\t\t\t// Applying the built-in fixes can enhance the attributes with missing content like \"className\".\n\t\t\tblock.originalContent = match.shortcode.content;\n\t\t\tblock = applyBuiltInValidationFixes(\n\t\t\t\tblock,\n\t\t\t\ttransformationBlockType as unknown as Parameters<\n\t\t\t\t\ttypeof applyBuiltInValidationFixes\n\t\t\t\t>[ 1 ]\n\t\t\t);\n\n\t\t\tblocks = [ block ];\n\t\t}\n\n\t\treturn [\n\t\t\t...segmentHTMLToShortcodeBlock(\n\t\t\t\tbeforeHTML.replace( beforeLineRegexp, '' )\n\t\t\t),\n\t\t\t...blocks,\n\t\t\t...segmentHTMLToShortcodeBlock(\n\t\t\t\tafterHTML.replace( afterLineRegexp, '' )\n\t\t\t),\n\t\t];\n\t}\n\n\treturn [ HTML ];\n}\n\nexport default segmentHTMLToShortcodeBlock;\n"],
  "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,uBAA6B;AAK7B,qBAA+D;AAC/D,0BAA6B;AAC7B,kCAAmC;AACnC,6CAA4C;AAe5C,IAAM,YAAY,CAAO,eACxB,MAAM,QAAS,UAAW,IAAI,aAAa,CAAE,UAAW;AAEzD,IAAM,mBAAmB;AACzB,IAAM,kBAAkB;AAExB,SAAS,4BACR,MACA,YAAoB,GACpB,qBAA+B,CAAC,GACN;AAE1B,QAAM,qBAAiB;AAAA,IACtB;AAAA,EACD;AAEA,QAAM,qBAAiB;AAAA,IACtB;AAAA,KACE,CAAE,cAAwB;AAC3B,YAAM,IAAI;AACV,aACC,mBAAmB,QAAS,EAAE,SAAU,MAAM,MAC9C,EAAE,SAAS,eACX,UAAW,EAAE,GAAI,EAAE;AAAA,QAAM,CAAE,YAC1B,yBAAQ,GAAI,EAAE,KAAM,IAAK;AAAA,MAC1B;AAAA,IAEF;AAAA,EACD;AAEA,MAAK,CAAE,gBAAiB;AACvB,WAAO,CAAE,IAAK;AAAA,EACf;AAEA,QAAM,gBAAgB,UAAW,eAAe,GAAI;AACpD,QAAM,eAAe,cAAc;AAAA,IAAM,CAAE,YAC1C,yBAAQ,GAAI,EAAE,KAAM,IAAK;AAAA,EAC1B;AAEA,MAAI;AACJ,QAAM,gBAAgB;AAEtB,MAAO,YAAQ,uBAAM,cAAe,MAAM,SAAU,GAAM;AACz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