From bcfde92afb7b11d2a37b148ef9af0c3f7b6cf0d1 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Mon, 6 Apr 2020 09:59:30 -0700 Subject: [PATCH] Fix setValue not handling block disposal (#3766) --- core/field.js | 10 +++++++--- tests/blocks/test_blocks.js | 16 ++++++++++++++++ tests/playground.html | 3 +++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/core/field.js b/core/field.js index 2c6328fc8..274bebf86 100644 --- a/core/field.js +++ b/core/field.js @@ -858,16 +858,20 @@ Blockly.Field.prototype.setValue = function(newValue) { return; } } + var source = this.sourceBlock_; + if (source && source.disposed) { + doLogging && console.log('source disposed, return'); + return; + } var oldValue = this.getValue(); if (oldValue === newValue) { doLogging && console.log('same, return'); - // No change. return; } - if (this.sourceBlock_ && Blockly.Events.isEnabled()) { + if (source && Blockly.Events.isEnabled()) { Blockly.Events.fire(new Blockly.Events.BlockChange( - this.sourceBlock_, 'field', this.name || null, oldValue, newValue)); + source, 'field', this.name || null, oldValue, newValue)); } this.doValueUpdate_(newValue); if (this.isDirty_) { diff --git a/tests/blocks/test_blocks.js b/tests/blocks/test_blocks.js index 47b44317d..0bb9036b6 100644 --- a/tests/blocks/test_blocks.js +++ b/tests/blocks/test_blocks.js @@ -1343,6 +1343,22 @@ Blockly.Blocks['test_images_clickhandler'] = { } }; +Blockly.Blocks['test_validators_dispose_block'] = { + init: function() { + this.appendDummyInput() + .appendField("dispose block") + .appendField(new Blockly.FieldTextInput("default", this.validate), "INPUT"); + this.setColour(230); + this.setCommentText('Any changes to the text cause the block to be disposed'); + }, + + validate: function(newValue) { + if (newValue != "default") { + this.getSourceBlock().dispose(true); + } + } +}; + Blockly.Blocks['test_validators_text_null'] = { init: function() { this.appendDummyInput() diff --git a/tests/playground.html b/tests/playground.html index 9f3d5cfd9..bba66449a 100644 --- a/tests/playground.html +++ b/tests/playground.html @@ -1677,6 +1677,9 @@ var spaghettiXml = [ + + +