Validate newValue parameter in setValue (#2392)

* Validate newValue parameter in setValue

* bugfix

* alternate fix

* test

* cleanup
This commit is contained in:
asunwoo98
2019-04-22 19:00:37 -04:00
committed by RoboErikG
parent a84e86f097
commit aa4d885cba
2 changed files with 7 additions and 2 deletions

View File

@@ -621,6 +621,12 @@ Blockly.Field.prototype.setValue = function(newValue) {
// No change if null.
return;
}
// Validate input.
var validated = this.callValidator(newValue);
if (validated !== null) {
newValue = validated;
}
// Check for change.
var oldValue = this.getValue();
if (oldValue == newValue) {
return;

View File

@@ -45,10 +45,10 @@ goog.require('Blockly.FieldTextInput');
*/
Blockly.FieldNumber = function(opt_value, opt_min, opt_max, opt_precision,
opt_validator) {
this.setConstraints(opt_min, opt_max, opt_precision);
opt_value = (opt_value && !isNaN(opt_value)) ? String(opt_value) : '0';
Blockly.FieldNumber.superClass_.constructor.call(
this, opt_value, opt_validator);
this.setConstraints(opt_min, opt_max, opt_precision);
};
goog.inherits(Blockly.FieldNumber, Blockly.FieldTextInput);
@@ -95,7 +95,6 @@ Blockly.FieldNumber.prototype.setConstraints = function(min, max, precision) {
this.min_ = isNaN(min) ? -Infinity : min;
max = parseFloat(max);
this.max_ = isNaN(max) ? Infinity : max;
this.setValue(this.callValidator(this.getValue()));
};
/**