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.
This commit is contained in:
Andrew n marshall
2018-04-18 13:35:30 -07:00
committed by GitHub
parent 46da00dbd3
commit 2bfff4a335

View File

@@ -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