Skip to content

Commit

Permalink
Merge pull request #144 from TurboWarp/xml-blocktype
Browse files Browse the repository at this point in the history
BlockType.XML
  • Loading branch information
GarboMuffin authored Aug 7, 2023
2 parents a1d2cd1 + c32c666 commit 461ef26
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/engine/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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();
Expand Down
7 changes: 6 additions & 1 deletion src/extension-support/block-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
6 changes: 6 additions & 0 deletions src/extension-support/extension-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
38 changes: 38 additions & 0 deletions test/integration/tw_xml_block.js
Original file line number Diff line number Diff line change
@@ -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: '<test it="works">!</test>'
}
]
})
});

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();
});

0 comments on commit 461ef26

Please sign in to comment.