diff --git a/core/block.js b/core/block.js index 162d87368..a00cc911d 100644 --- a/core/block.js +++ b/core/block.js @@ -1635,25 +1635,21 @@ Blockly.Block.prototype.interpolate_ = function(message, args, lastDummyAlign, // An array of [field, fieldName] tuples. var fieldStack = []; for (var i = 0, element; (element = elements[i]); i++) { - switch (element['type']) { - case 'input_value': - case 'input_statement': - case 'input_dummy': - var input = this.inputFromJson_(element, warningPrefix); - // Should never be null, but just in case. - if (input) { - for (var j = 0, tuple; (tuple = fieldStack[j]); j++) { - input.appendField(tuple[0], tuple[1]); - } - fieldStack.length = 0; + if (this.isInputKeyword_(element['type'])) { + var input = this.inputFromJson_(element, warningPrefix); + // Should never be null, but just in case. + if (input) { + for (var j = 0, tuple; (tuple = fieldStack[j]); j++) { + input.appendField(tuple[0], tuple[1]); } - break; + fieldStack.length = 0; + } + } else { // All other types, including ones starting with 'input_' get routed here. - default: - var field = this.fieldFromJson_(element); - if (field) { - fieldStack.push([field, element['name']]); - } + var field = this.fieldFromJson_(element); + if (field) { + fieldStack.push([field, element['name']]); + } } } }; @@ -1722,10 +1718,7 @@ Blockly.Block.prototype.interpolateArguments_ = } var length = elements.length; - var startsWith = Blockly.utils.string.startsWith; - // TODO: This matches the old behavior, but it doesn't work for fields - // that don't start with 'field_'. - if (length && startsWith(elements[length - 1]['type'], 'field_')) { + if (length && !this.isInputKeyword_(elements[length - 1]['type'])) { var dummyInput = {'type': 'input_dummy'}; if (lastDummyAlign) { dummyInput['align'] = lastDummyAlign; @@ -1808,6 +1801,19 @@ Blockly.Block.prototype.inputFromJson_ = function(element, warningPrefix) { return input; }; +/** + * Returns true if the given string matches one of the input keywords. + * @param {string} str The string to check. + * @return {boolean} True if the given string matches one of the input keywords, + * false otherwise. + * @private + */ +Blockly.Block.prototype.isInputKeyword_ = function(str) { + return str == 'input_value' || + str == 'input_statement' || + str == 'input_dummy'; +}; + /** * Turns a string into the JSON definition of a label field. If the string * becomes an empty string when trimmed, this returns null. diff --git a/core/input.js b/core/input.js index 253160a64..9a165226e 100644 --- a/core/input.js +++ b/core/input.js @@ -14,7 +14,7 @@ goog.provide('Blockly.Input'); goog.require('Blockly.Connection'); goog.require('Blockly.constants'); -goog.require('Blockly.FieldLabel'); +goog.require('Blockly.fieldRegistry'); /** @@ -100,7 +100,10 @@ Blockly.Input.prototype.insertFieldAt = function(index, field, opt_name) { // Generate a FieldLabel when given a plain text field. if (typeof field == 'string') { - field = new Blockly.FieldLabel(/** @type {string} */ (field)); + field = /** @type {!Blockly.Field} **/ (Blockly.fieldRegistry.fromJson({ + 'type': 'field_label', + 'text': field, + })); } field.setSourceBlock(this.sourceBlock_); diff --git a/tests/mocha/block_json_test.js b/tests/mocha/block_json_test.js index ed5301116..6b93339fd 100644 --- a/tests/mocha/block_json_test.js +++ b/tests/mocha/block_json_test.js @@ -69,6 +69,7 @@ suite('Block JSON initialization', function() { type: 'test', interpolateArguments_: Blockly.Block.prototype.interpolateArguments_, stringToFieldJson_: Blockly.Block.prototype.stringToFieldJson_, + isInputKeyword_: Blockly.Block.prototype.isInputKeyword_, }; chai.assert.deepEqual( block.interpolateArguments_(tokens, args, lastAlign), @@ -217,7 +218,7 @@ suite('Block JSON initialization', function() { ]); }); - test.skip('Add last dummy for no_field_prefix_field', function() { + test('Add last dummy for no_field_prefix_field', function() { this.assertInterpolation( [ { @@ -236,6 +237,25 @@ suite('Block JSON initialization', function() { ]); }); + test('Add last dummy for input_prefix_field', function() { + this.assertInterpolation( + [ + { + 'type': 'input_prefix_field', + } + ], + [], + undefined, + [ + { + 'type': 'input_prefix_field', + }, + { + 'type': 'input_dummy', + } + ]); + }); + test('Set last dummy alignment', function() { this.assertInterpolation( ['test1', 'test2', 'test3'], diff --git a/tests/mocha/input_test.js b/tests/mocha/input_test.js index c135bfe97..e88a84062 100644 --- a/tests/mocha/input_test.js +++ b/tests/mocha/input_test.js @@ -57,6 +57,16 @@ suite('Inputs', function() { this.dummy.insertFieldAt(0, 'field'); chai.assert.instanceOf(this.dummy.fieldRow[0], Blockly.FieldLabel); }); + test('String w/ field_label overwritten', function() { + Blockly.fieldRegistry.unregister('field_label'); + Blockly.fieldRegistry.register('field_label', Blockly.FieldNumber); + + this.dummy.insertFieldAt(0, '1'); + chai.assert.instanceOf(this.dummy.fieldRow[0], Blockly.FieldNumber); + + Blockly.fieldRegistry.unregister('field_label'); + Blockly.fieldRegistry.register('field_label', Blockly.FieldLabel); + }); test('Empty String', function() { this.dummy.insertFieldAt(0, ''); chai.assert.isEmpty(this.dummy.fieldRow);