diff --git a/core/field_textinput.js b/core/field_textinput.js index 336a2da24..159a30ef5 100644 --- a/core/field_textinput.js +++ b/core/field_textinput.js @@ -54,6 +54,13 @@ goog.require('Blockly.utils.userAgent'); * @constructor */ Blockly.FieldTextInput = function(opt_value, opt_validator, opt_config) { + /** + * Allow browser to spellcheck this field. + * @type {boolean} + * @private + */ + this.spellcheck_ = true; + if (opt_value == null) { opt_value = ''; } @@ -73,11 +80,7 @@ Blockly.utils.object.inherits(Blockly.FieldTextInput, Blockly.Field); */ Blockly.FieldTextInput.fromJson = function(options) { var text = Blockly.utils.replaceMessageReferences(options['text']); - var field = new Blockly.FieldTextInput(text); - if (typeof options['spellcheck'] === 'boolean') { - field.setSpellcheck(options['spellcheck']); - } - return field; + return new Blockly.FieldTextInput(text, null, options); }; /** @@ -105,10 +108,14 @@ Blockly.FieldTextInput.BORDERRADIUS = 4; Blockly.FieldTextInput.prototype.CURSOR = 'text'; /** - * Allow browser to spellcheck this field. - * @protected + * @override */ -Blockly.FieldTextInput.prototype.spellcheck_ = true; +Blockly.FieldTextInput.prototype.configure_ = function(config) { + Blockly.FieldTextInput.superClass_.configure_.call(this, config); + if (typeof config['spellcheck'] == 'boolean') { + this.spellcheck_ = config['spellcheck']; + } +}; /** * Ensure that the input value casts to a valid string. @@ -194,7 +201,13 @@ Blockly.FieldTextInput.prototype.render_ = function() { * @param {boolean} check True if checked. */ Blockly.FieldTextInput.prototype.setSpellcheck = function(check) { + if (check == this.spellcheck_) { + return; + } this.spellcheck_ = check; + if (this.htmlInput_) { + this.htmlInput_.setAttribute('spellcheck', this.spellcheck_); + } }; /** diff --git a/tests/mocha/field_textinput_test.js b/tests/mocha/field_textinput_test.js index 4177330a5..06ad18a0d 100644 --- a/tests/mocha/field_textinput_test.js +++ b/tests/mocha/field_textinput_test.js @@ -222,4 +222,60 @@ suite('Text Input Fields', function() { }); }); }); + suite('Customization', function() { + suite('Spellcheck', function() { + setup(function() { + this.prepField = function(field) { + field.sourceBlock_ = { + workspace: { + scale: 1 + } + }; + Blockly.WidgetDiv.DIV = document.createElement('div'); + this.stub = sinon.stub(field, 'resizeEditor_'); + }; + + this.assertSpellcheck = function(field, value) { + this.prepField(field); + field.showEditor_(); + chai.assert.equal(field.htmlInput_.getAttribute('spellcheck'), + value.toString()); + }; + }); + teardown(function() { + if (this.stub) { + this.stub.restore(); + } + }); + test('Default', function() { + var field = new Blockly.FieldTextInput('test'); + this.assertSpellcheck(field, true); + }); + test('JS Constructor', function() { + var field = new Blockly.FieldTextInput('test', null, { + spellcheck: false + }); + this.assertSpellcheck(field, false); + }); + test('JSON Definition', function() { + var field = Blockly.FieldTextInput.fromJson({ + text: 'test', + spellcheck: false + }); + this.assertSpellcheck(field, false); + }); + test('setSpellcheck Editor Hidden', function() { + var field = new Blockly.FieldTextInput('test'); + field.setSpellcheck(false); + this.assertSpellcheck(field, false); + }); + test('setSpellcheck Editor Shown', function() { + var field = new Blockly.FieldTextInput('test'); + this.prepField(field); + field.showEditor_(); + field.setSpellcheck(false); + chai.assert.equal(field.htmlInput_.getAttribute('spellcheck'), 'false'); + }); + }); + }); });