mirror of
https://github.com/google/blockly.git
synced 2026-01-04 15:40:08 +01:00
* 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:
@@ -199,7 +199,9 @@ AppController.prototype.formatBlockLibraryForImport_ = function(xmlText) {
|
|||||||
xmlText = Blockly.Xml.domToText(xmlDom);
|
xmlText = Blockly.Xml.domToText(xmlDom);
|
||||||
// All block types should be lowercase.
|
// All block types should be lowercase.
|
||||||
var blockType = this.getBlockTypeFromXml_(xmlText).toLowerCase();
|
var blockType = this.getBlockTypeFromXml_(xmlText).toLowerCase();
|
||||||
|
// Some names are invalid so fix them up.
|
||||||
|
blockType = FactoryUtils.cleanBlockType(blockType);
|
||||||
|
|
||||||
blockXmlTextMap[blockType] = xmlText;
|
blockXmlTextMap[blockType] = xmlText;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -64,8 +64,8 @@ BlockLibraryController = function(blockLibraryName, opt_blockLibraryStorage) {
|
|||||||
BlockLibraryController.prototype.getCurrentBlockType = function() {
|
BlockLibraryController.prototype.getCurrentBlockType = function() {
|
||||||
var rootBlock = FactoryUtils.getRootBlock(BlockFactory.mainWorkspace);
|
var rootBlock = FactoryUtils.getRootBlock(BlockFactory.mainWorkspace);
|
||||||
var blockType = rootBlock.getFieldValue('NAME').trim().toLowerCase();
|
var blockType = rootBlock.getFieldValue('NAME').trim().toLowerCase();
|
||||||
// Replace white space with underscores
|
// Replace invalid characters.
|
||||||
return blockType.replace(/\W/g, '_').replace(/^(\d)/, '_\\1');
|
return FactoryUtils.cleanBlockType(blockType);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ goog.provide('FactoryUtils');
|
|||||||
* @return {string} Block definition.
|
* @return {string} Block definition.
|
||||||
*/
|
*/
|
||||||
FactoryUtils.getBlockDefinition = function(blockType, rootBlock, format, workspace) {
|
FactoryUtils.getBlockDefinition = function(blockType, rootBlock, format, workspace) {
|
||||||
blockType = blockType.replace(/\W/g, '_').replace(/^(\d)/, '_\\1');
|
blockType = FactoryUtils.cleanBlockType(blockType);
|
||||||
switch (format) {
|
switch (format) {
|
||||||
case 'JSON':
|
case 'JSON':
|
||||||
var code = FactoryUtils.formatJson_(blockType, rootBlock);
|
var code = FactoryUtils.formatJson_(blockType, rootBlock);
|
||||||
@@ -56,6 +56,19 @@ FactoryUtils.getBlockDefinition = function(blockType, rootBlock, format, workspa
|
|||||||
return code;
|
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.
|
* Get the generator code for a given block.
|
||||||
* @param {!Blockly.Block} block Rendered block in preview workspace.
|
* @param {!Blockly.Block} block Rendered block in preview workspace.
|
||||||
|
|||||||
Reference in New Issue
Block a user