From 5c2c4d8400e672636d33f910117683137a667a6b Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Fri, 15 Jul 2016 20:59:20 -0700 Subject: [PATCH] Refactor validators to survive aggressive Closure compression. --- core/events.js | 3 +-- core/field.js | 41 +++++++++++++++++++++++------------------ core/field_angle.js | 5 ++--- core/field_number.js | 3 +-- core/field_variable.js | 3 +-- 5 files changed, 28 insertions(+), 27 deletions(-) diff --git a/core/events.js b/core/events.js index ac78aa3a3..1d1e2b70e 100644 --- a/core/events.js +++ b/core/events.js @@ -554,8 +554,7 @@ Blockly.Events.Change.prototype.run = function(forward) { if (field) { // Run the validator for any side-effects it may have. // The validator's opinion on validity is ignored. - var validator = field.getValidator(); - validator && validator.call(field, value); + field.callValidator(value); field.setValue(value); } else { console.warn("Can't set non-existant field: " + this.name); diff --git a/core/field.js b/core/field.js index b6cff255b..00a717c27 100644 --- a/core/field.js +++ b/core/field.js @@ -236,6 +236,15 @@ Blockly.Field.prototype.getValidator = function() { return this.validator_; }; +/** + * Validates a change. Does nothing. Subclasses may override this. + * @param {string} text The user's text. + * @return {string} No change needed. + */ +Blockly.Field.prototype.classValidator = function(text) { + return text; +}; + /** * Calls the validation function for this field, as well as all the validation * function for the field's class and its parents. @@ -243,25 +252,21 @@ Blockly.Field.prototype.getValidator = function() { * @return {?string} Revised text, or null if invalid. */ Blockly.Field.prototype.callValidator = function(text) { - // Collect a list of validators, from Field, through to the subclass, ending - // with the user's validator. - var validators = [this.getValidator()]; - var fieldClass = this.constructor; - while (fieldClass) { - validators.unshift(fieldClass.classValidator); - fieldClass = fieldClass.superClass_; + var classResult = this.classValidator(text); + if (classResult === null) { + // Class validator rejects value. Game over. + return null; + } else if (classResult !== undefined) { + text = classResult; } - // Call each validator in turn, allowing each to rewrite or reject. - for (var i = 0; i < validators.length; i++) { - var validator = validators[i]; - if (validator) { - var result = validator.call(this, text); - if (result === null) { - // Validator rejects value. Game over. - return null; - } else if (result !== undefined) { - text = result; - } + var userValidator = this.getValidator(); + if (userValidator) { + var userResult = userValidator(text); + if (userResult === null) { + // User validator rejects value. Game over. + return null; + } else if (userResult !== undefined) { + text = userResult; } } return text; diff --git a/core/field_angle.js b/core/field_angle.js index 422191730..a294948e9 100644 --- a/core/field_angle.js +++ b/core/field_angle.js @@ -205,7 +205,7 @@ Blockly.FieldAngle.prototype.onMouseMove = function(e) { angle = Math.round(angle / Blockly.FieldAngle.ROUND) * Blockly.FieldAngle.ROUND; } - angle = Blockly.FieldAngle.classValidator(angle); + angle = this.callValidator(angle); Blockly.FieldTextInput.htmlInput_.value = angle; this.setValue(angle); this.validate_(); @@ -274,9 +274,8 @@ Blockly.FieldAngle.prototype.updateGraph_ = function() { * Ensure that only an angle may be entered. * @param {string} text The user's text. * @return {?string} A string representing a valid angle, or null if invalid. - * @this {!Blockly.FieldAngle} */ -Blockly.FieldAngle.classValidator = function(text) { +Blockly.FieldAngle.prototype.classValidator = function(text) { if (text === null) { return null; } diff --git a/core/field_number.js b/core/field_number.js index 94cda5108..3eb99d398 100644 --- a/core/field_number.js +++ b/core/field_number.js @@ -75,9 +75,8 @@ Blockly.FieldNumber.prototype.setConstraints = function(min, max, precision) { * Ensure that only a number in the correct range may be entered. * @param {string} text The user's text. * @return {?string} A string representing a valid number, or null if invalid. - * @this {!Blockly.FieldNumber} */ -Blockly.FieldNumber.classValidator = function(text) { +Blockly.FieldNumber.prototype.classValidator = function(text) { if (text === null) { return null; } diff --git a/core/field_variable.js b/core/field_variable.js index 3c6491794..f6cfd4ecb 100644 --- a/core/field_variable.js +++ b/core/field_variable.js @@ -127,9 +127,8 @@ Blockly.FieldVariable.dropdownCreate = function() { * @return {null|undefined|string} An acceptable new variable name, or null if * change is to be either aborted (cancel button) or has been already * handled (rename), or undefined if an existing variable was chosen. - * @this {!Blockly.FieldVariable} */ -Blockly.FieldVariable.classValidator = function(text) { +Blockly.FieldVariable.prototype.classValidator = function(text) { function promptName(promptText, defaultText) { Blockly.hideChaff(); var newVar = window.prompt(promptText, defaultText);