From 2bfff4a3351026c897e20ccb2676aab2e476a7a1 Mon Sep 17 00:00:00 2001 From: Andrew n marshall Date: Wed, 18 Apr 2018 13:35:30 -0700 Subject: [PATCH] Warn if jsonInit() receives a colour attribute without a value. (#1795) * Warn if jsonInit() receives a colour attribute without a value. * Extract colour init code into function. * Adding block type name to prior warnings. --- core/block.js | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/core/block.js b/core/block.js index 55536627e..50762ea05 100644 --- a/core/block.js +++ b/core/block.js @@ -1086,23 +1086,15 @@ Blockly.Block.prototype.appendDummyInput = function(opt_name) { * @param {!Object} json Structured data describing the block. */ Blockly.Block.prototype.jsonInit = function(json) { - var blockTypeName = json['type']; + var warningPrefix = json['type'] ? 'Block "' + json['type'] + '": ' : ''; // Validate inputs. goog.asserts.assert( json['output'] == undefined || json['previousStatement'] == undefined, - 'Must not have both an output and a previousStatement.'); + warningPrefix + 'Must not have both an output and a previousStatement.'); // Set basic properties of block. - if (json['colour'] !== undefined) { - var rawValue = json['colour']; - try { - this.setColour(rawValue); - } catch (colorError) { - console.warn( - 'Block "' + blockTypeName + '": Illegal color value: ', rawValue); - } - } + this.jsonInitColour_(json, warningPrefix); // Interpolate the message blocks. var i = 0; @@ -1140,8 +1132,10 @@ Blockly.Block.prototype.jsonInit = function(json) { this.setHelpUrl(localizedValue); } if (goog.isString(json['extensions'])) { - console.warn('JSON attribute \'extensions\' should be an array of ' + - 'strings. Found raw string in JSON for \'' + json['type'] + '\' block.'); + console.warn( + warningPrefix + 'JSON attribute \'extensions\' should be an array of' + + ' strings. Found raw string in JSON for \'' + json['type'] + + '\' block.'); json['extensions'] = [json['extensions']]; // Correct and continue. } @@ -1159,6 +1153,27 @@ Blockly.Block.prototype.jsonInit = function(json) { } }; +/** + * Initialize the colour of this block from the JSON description. + * @param {!Object} json Structured data describing the block. + * @param {string} warningPrefix Warning prefix string identifying block. + * @private + */ +Blockly.Block.prototype.jsonInitColour_ = function(json, warningPrefix) { + if ('colour' in json) { + if (json['colour'] === undefined) { + console.warn(warningPrefix + 'Undefined color value.'); + } else { + var rawValue = json['colour']; + try { + this.setColour(rawValue); + } catch (colorError) { + console.warn(warningPrefix + 'Illegal color value: ', rawValue); + } + } + } +}; + /** * Add key/values from mixinObj to this block object. By default, this method * will check that the keys in mixinObj will not overwrite existing values in