From b77a30450eaa708427909007fd53a582e4fc3e5d Mon Sep 17 00:00:00 2001 From: alschmiedt Date: Fri, 5 Jun 2020 09:22:52 -0700 Subject: [PATCH] Fix performance (#3931) * Speed up performance for converting the xml to json --- core/flyout_base.js | 23 ++++++++++++++--------- core/utils/toolbox.js | 14 ++++++++------ 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/core/flyout_base.js b/core/flyout_base.js index f60a0108b..e860c9c97 100644 --- a/core/flyout_base.js +++ b/core/flyout_base.js @@ -612,18 +612,23 @@ Blockly.Flyout.prototype.createBlock_ = function(blockXml) { * @private */ Blockly.Flyout.prototype.getBlockXml_ = function(blockInfo) { - var blockXml = null; - // All blockInfo will have type, so check for blockxml first. - if (blockInfo['blockxml']) { - blockXml = Blockly.Xml.textToDom(blockInfo['blockxml']); + var blockElement = null; + var blockXml = blockInfo['blockxml']; + + if (blockXml && typeof blockXml != 'string') { + blockElement = blockXml; + } else if (blockXml && typeof blockXml == 'string') { + blockElement = Blockly.Xml.textToDom(blockXml); } else if (blockInfo['type']) { - blockXml = Blockly.utils.xml.createElement('xml'); - blockXml.setAttribute('type', blockInfo['type']); - blockXml.setAttribute('disabled', blockInfo['disabled']); - } else { + blockElement = Blockly.utils.xml.createElement('xml'); + blockElement.setAttribute('type', blockInfo['type']); + blockElement.setAttribute('disabled', blockInfo['disabled']); + } + + if (!blockElement) { throw Error('Error: Invalid block definition. Block definition must have blockxml or type.'); } - return blockXml; + return blockElement; }; /** diff --git a/core/utils/toolbox.js b/core/utils/toolbox.js index 458da19e5..e9e7b7ee0 100644 --- a/core/utils/toolbox.js +++ b/core/utils/toolbox.js @@ -126,15 +126,17 @@ Blockly.utils.toolbox.toolboxXmlToJson_ = function(toolboxDef) { continue; } var obj = {}; - obj['kind'] = child.tagName.toUpperCase(); + var tagName = child.tagName.toUpperCase(); + obj['kind'] = tagName; + // Store the xml for a block - if (child.tagName.toUpperCase() == 'BLOCK') { - obj['blockxml'] = Blockly.utils.xml.domToText(child); - } - // Get the contents for a category. - if (child.tagName.toUpperCase() == 'CATEGORY') { + if (tagName == 'BLOCK') { + obj['blockxml'] = child; + } else if (tagName == 'CATEGORY') { + // Get the contents of a category obj['contents'] = Blockly.utils.toolbox.toolboxXmlToJson_(child); } + // Add xml attributes to object for (var j = 0; j < child.attributes.length; j++) { var attr = child.attributes[j];