Fix #698 by adjusting the regex to not have \. Still not 100% sure w… (#700)

* Fix #698 by adjusting the regex to not have \.  Still not 100% sure why that was there.
Also replaces bad names on input.  There are probably more invalid names but this is
a start.
This commit is contained in:
picklesrus
2016-12-09 17:19:17 -08:00
committed by GitHub
parent ccbaa4664e
commit bdc5a02922
3 changed files with 19 additions and 4 deletions

View File

@@ -199,7 +199,9 @@ AppController.prototype.formatBlockLibraryForImport_ = function(xmlText) {
xmlText = Blockly.Xml.domToText(xmlDom);
// All block types should be lowercase.
var blockType = this.getBlockTypeFromXml_(xmlText).toLowerCase();
// Some names are invalid so fix them up.
blockType = FactoryUtils.cleanBlockType(blockType);
blockXmlTextMap[blockType] = xmlText;
}

View File

@@ -64,8 +64,8 @@ BlockLibraryController = function(blockLibraryName, opt_blockLibraryStorage) {
BlockLibraryController.prototype.getCurrentBlockType = function() {
var rootBlock = FactoryUtils.getRootBlock(BlockFactory.mainWorkspace);
var blockType = rootBlock.getFieldValue('NAME').trim().toLowerCase();
// Replace white space with underscores
return blockType.replace(/\W/g, '_').replace(/^(\d)/, '_\\1');
// Replace invalid characters.
return FactoryUtils.cleanBlockType(blockType);
};
/**

View File

@@ -44,7 +44,7 @@ goog.provide('FactoryUtils');
* @return {string} Block definition.
*/
FactoryUtils.getBlockDefinition = function(blockType, rootBlock, format, workspace) {
blockType = blockType.replace(/\W/g, '_').replace(/^(\d)/, '_\\1');
blockType = FactoryUtils.cleanBlockType(blockType);
switch (format) {
case 'JSON':
var code = FactoryUtils.formatJson_(blockType, rootBlock);
@@ -56,6 +56,19 @@ FactoryUtils.getBlockDefinition = function(blockType, rootBlock, format, workspa
return code;
};
/**
* Convert invalid block name to a valid one. Replaces whitespace
* and prepend names that start with a digit with an '_'.
* @param {string} blockType Type of block.
* @return {string} Cleaned up block type.
*/
FactoryUtils.cleanBlockType = function(blockType) {
if (!blockType) {
return '';
}
return blockType.replace(/\W/g, '_').replace(/^(\d)/, '_$1');
};
/**
* Get the generator code for a given block.
* @param {!Blockly.Block} block Rendered block in preview workspace.