UNPKG

@wordpress/e2e-test-utils-playwright

Version:
8 lines (7 loc) 3.3 kB
{ "version": 3, "sources": ["../../src/editor/get-blocks.ts"], "sourcesContent": ["/**\n * Internal dependencies\n */\nimport type { Editor } from './index';\n\ntype Block = {\n\tname: string;\n\tattributes: Record< string, unknown >;\n\tinnerBlocks: Block[];\n};\n\n/**\n * Returns the edited blocks.\n *\n * @param this\n * @param options\n * @param options.clientId Limit the results to be only under a partial tree of the specified clientId.\n * @param options.full Whether to return the full block data or just the name and attributes.\n *\n * @return The blocks.\n */\nexport async function getBlocks(\n\tthis: Editor,\n\t{ clientId, full = false }: { clientId?: string; full?: boolean } = {}\n) {\n\tawait this.page.waitForFunction(\n\t\t() => window?.wp?.blocks && window?.wp?.data\n\t);\n\n\treturn await this.page.evaluate(\n\t\t( [ _full, _clientId ] ) => {\n\t\t\t// Serialize serializable attributes of blocks.\n\t\t\tfunction serializeAttributes(\n\t\t\t\tattributes: Record< string, unknown >\n\t\t\t) {\n\t\t\t\treturn Object.fromEntries(\n\t\t\t\t\tObject.entries( attributes ).map( ( [ key, value ] ) => {\n\t\t\t\t\t\t// Serialize RichTextData to string.\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\tvalue instanceof window.wp.richText.RichTextData\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\treturn [ key, ( value as string ).toString() ];\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn [ key, value ];\n\t\t\t\t\t} )\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// Remove other unpredictable properties like clientId from blocks for testing purposes.\n\t\t\tfunction recursivelyTransformBlocks( blocks: Block[] ): Block[] {\n\t\t\t\treturn blocks.map( ( block ) => ( {\n\t\t\t\t\tname: block.name,\n\t\t\t\t\tattributes: serializeAttributes( block.attributes ),\n\t\t\t\t\tinnerBlocks: recursivelyTransformBlocks(\n\t\t\t\t\t\tblock.innerBlocks\n\t\t\t\t\t),\n\t\t\t\t} ) );\n\t\t\t}\n\n\t\t\tconst blocks = window.wp.data\n\t\t\t\t.select( 'core/block-editor' )\n\t\t\t\t.getBlocks( _clientId ) as Block[];\n\n\t\t\t// The editor might still contain an unmodified empty block even when it's technically \"empty\".\n\t\t\tif (\n\t\t\t\tblocks.length === 1 &&\n\t\t\t\twindow.wp.blocks.isUnmodifiedDefaultBlock( blocks[ 0 ] )\n\t\t\t) {\n\t\t\t\treturn [];\n\t\t\t}\n\n\t\t\treturn _full ? blocks : recursivelyTransformBlocks( blocks );\n\t\t},\n\t\t[ full, clientId ]\n\t);\n}\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAqBA,eAAsB,UAErB,EAAE,UAAU,OAAO,MAAM,IAA2C,CAAC,GACpE;AACD,QAAM,KAAK,KAAK;AAAA,IACf,MAAM,QAAQ,IAAI,UAAU,QAAQ,IAAI;AAAA,EACzC;AAEA,SAAO,MAAM,KAAK,KAAK;AAAA,IACtB,CAAE,CAAE,OAAO,SAAU,MAAO;AAE3B,eAAS,oBACR,YACC;AACD,eAAO,OAAO;AAAA,UACb,OAAO,QAAS,UAAW,EAAE,IAAK,CAAE,CAAE,KAAK,KAAM,MAAO;AAEvD,gBACC,iBAAiB,OAAO,GAAG,SAAS,cACnC;AACD,qBAAO,CAAE,KAAO,MAAkB,SAAS,CAAE;AAAA,YAC9C;AACA,mBAAO,CAAE,KAAK,KAAM;AAAA,UACrB,CAAE;AAAA,QACH;AAAA,MACD;AAGA,eAAS,2BAA4BA,SAA2B;AAC/D,eAAOA,QAAO,IAAK,CAAE,WAAa;AAAA,UACjC,MAAM,MAAM;AAAA,UACZ,YAAY,oBAAqB,MAAM,UAAW;AAAA,UAClD,aAAa;AAAA,YACZ,MAAM;AAAA,UACP;AAAA,QACD,EAAI;AAAA,MACL;AAEA,YAAM,SAAS,OAAO,GAAG,KACvB,OAAQ,mBAAoB,EAC5B,UAAW,SAAU;AAGvB,UACC,OAAO,WAAW,KAClB,OAAO,GAAG,OAAO,yBAA0B,OAAQ,CAAE,CAAE,GACtD;AACD,eAAO,CAAC;AAAA,MACT;AAEA,aAAO,QAAQ,SAAS,2BAA4B,MAAO;AAAA,IAC5D;AAAA,IACA,CAAE,MAAM,QAAS;AAAA,EAClB;AACD;", "names": ["blocks"] }