From 7115abb56d2d49f51121d2ffa6d67a530ba2f61e Mon Sep 17 00:00:00 2001 From: GarboMuffin Date: Thu, 29 Jun 2023 22:09:50 -0500 Subject: [PATCH 1/2] BlockType.XML --- src/engine/runtime.js | 11 +++++++++++ src/extension-support/block-type.js | 7 ++++++- src/extension-support/extension-manager.js | 6 ++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/engine/runtime.js b/src/engine/runtime.js index 210149e8b68..383c2cfe292 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -1252,6 +1252,10 @@ class Runtime extends EventEmitter { return this._convertButtonForScratchBlocks(blockInfo, categoryInfo); } + if (blockInfo.blockType === BlockType.XML) { + return this._convertXmlForScratchBlocks(blockInfo); + } + return this._convertBlockForScratchBlocks(blockInfo, categoryInfo); } @@ -1463,6 +1467,13 @@ class Runtime extends EventEmitter { }; } + _convertXmlForScratchBlocks (xmlInfo) { + return { + info: xmlInfo, + xml: xmlInfo.xml + }; + } + handleExtensionButtonPress (buttonData) { const callback = this.extensionButtons.get(buttonData); callback(); diff --git a/src/extension-support/block-type.js b/src/extension-support/block-type.js index 26776f68600..ecaf70754dc 100644 --- a/src/extension-support/block-type.js +++ b/src/extension-support/block-type.js @@ -49,7 +49,12 @@ const BlockType = { /** * General reporter with numeric or string value */ - REPORTER: 'reporter' + REPORTER: 'reporter', + + /** + * Arbitrary scratch-blocks XML. + */ + XML: 'xml' }; module.exports = BlockType; diff --git a/src/extension-support/extension-manager.js b/src/extension-support/extension-manager.js index c79d66d5b7d..c7a5a098200 100644 --- a/src/extension-support/extension-manager.js +++ b/src/extension-support/extension-manager.js @@ -493,6 +493,12 @@ class ExtensionManager { * @private */ _prepareBlockInfo (serviceName, blockInfo) { + if (blockInfo.blockType === BlockType.XML) { + blockInfo = Object.assign({}, blockInfo); + blockInfo.xml = String(blockInfo.xml) || ''; + return blockInfo; + } + blockInfo = Object.assign({}, { blockType: BlockType.COMMAND, terminal: false, From c32c66679fb6c17b0e003d905325cde5ca02864e Mon Sep 17 00:00:00 2001 From: Muffin Date: Sun, 6 Aug 2023 21:48:18 -0500 Subject: [PATCH 2/2] Add test --- test/integration/tw_xml_block.js | 38 ++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 test/integration/tw_xml_block.js diff --git a/test/integration/tw_xml_block.js b/test/integration/tw_xml_block.js new file mode 100644 index 00000000000..4b5ecfa8b16 --- /dev/null +++ b/test/integration/tw_xml_block.js @@ -0,0 +1,38 @@ +const htmlparser = require('htmlparser2'); +const {test} = require('tap'); +const VirtualMachine = require('../../src/virtual-machine'); +const BlockType = require('../../src/extension-support/block-type'); + +test('XML blocks', t => { + const vm = new VirtualMachine(); + vm.extensionManager._registerInternalExtension({ + getInfo: () => ({ + id: 'testxml', + name: 'XML Test', + blocks: [ + { + blockType: BlockType.XML, + xml: '!' + } + ] + }) + }); + + const xmlList = vm.runtime.getBlocksXML(); + t.equal(xmlList.length, 1); + + const parsedXML = htmlparser.parseDOM(xmlList[0].xml); + t.equal(parsedXML.length, 1); + + const category = parsedXML[0]; + t.equal(category.name, 'category'); + t.equal(category.children.length, 1); + + const label = category.children[0]; + t.equal(label.name, 'test'); + t.equal(label.attribs.it, 'works'); + t.equal(label.children.length, 1); + t.equal(label.children[0].data, '!'); + + t.end(); +});