* Decode an XML block tag and create a block (and possibly sub blocks) on the * workspace. * * @param xmlBlock XML block element. * @param workspace The workspace. * @param parentConnection The parent connection to connect this block to * after instantiating. * @param connectedToParentNex
( xmlBlock: Element, workspace: Workspace, parentConnection?: Connection, connectedToParentNext?: boolean, )
| 979 | * @returns The root block created. |
| 980 | */ |
| 981 | function domToBlockHeadless( |
| 982 | xmlBlock: Element, |
| 983 | workspace: Workspace, |
| 984 | parentConnection?: Connection, |
| 985 | connectedToParentNext?: boolean, |
| 986 | ): Block { |
| 987 | let block = null; |
| 988 | const prototypeName = xmlBlock.getAttribute('type'); |
| 989 | if (!prototypeName) { |
| 990 | throw TypeError('Block type unspecified: ' + xmlBlock.outerHTML); |
| 991 | } |
| 992 | const id = xmlBlock.getAttribute('id') ?? undefined; |
| 993 | block = workspace.newBlock(prototypeName, id); |
| 994 | |
| 995 | // Preprocess childNodes so tags can be processed in a consistent order. |
| 996 | const xmlChildNameMap = mapSupportedXmlTags(xmlBlock); |
| 997 | |
| 998 | const shouldCallInitSvg = applyMutationTagNodes( |
| 999 | xmlChildNameMap.mutation, |
| 1000 | block, |
| 1001 | ); |
| 1002 | applyCommentTagNodes(xmlChildNameMap.comment, block); |
| 1003 | applyDataTagNodes(xmlChildNameMap.data, block); |
| 1004 | |
| 1005 | // Connect parent after processing mutation and before setting fields. |
| 1006 | if (parentConnection) { |
| 1007 | if (connectedToParentNext) { |
| 1008 | if (block.previousConnection) { |
| 1009 | parentConnection.connect(block.previousConnection); |
| 1010 | } else { |
| 1011 | throw TypeError('Next block does not have previous statement.'); |
| 1012 | } |
| 1013 | } else { |
| 1014 | if (block.outputConnection) { |
| 1015 | parentConnection.connect(block.outputConnection); |
| 1016 | } else if (block.previousConnection) { |
| 1017 | parentConnection.connect(block.previousConnection); |
| 1018 | } else { |
| 1019 | throw TypeError( |
| 1020 | 'Child block does not have output or previous statement.', |
| 1021 | ); |
| 1022 | } |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | applyFieldTagNodes(xmlChildNameMap.field, block); |
| 1027 | applyInputTagNodes(xmlChildNameMap.input, workspace, block, prototypeName); |
| 1028 | applyNextTagNodes(xmlChildNameMap.next, workspace, block); |
| 1029 | |
| 1030 | if (shouldCallInitSvg) { |
| 1031 | // This shouldn't even be called here |
| 1032 | // (ref: https://github.com/google/blockly/pull/4296#issuecomment-884226021 |
| 1033 | // But the XML serializer/deserializer is iceboxed so I'm not going to fix |
| 1034 | // it. |
| 1035 | (block as BlockSvg).initSvg(); |
| 1036 | } |
| 1037 | |
| 1038 | const inline = xmlBlock.getAttribute('inline'); |
no test coverage detected