Skip to content
This repository was archived by the owner on Jan 27, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions core/ui/fields/field_variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,9 @@ Blockly.FieldVariable.prototype.dropdownChange = function(text) {
Blockly.Msg.CONFIRM_CREATE_VARIABLE,
'',
function(newVar) {
this.setText(newVar);
// Since variables are case-insensitive, ensure that if the new variable
// matches with an existing variable, the new case prevails throughout.
Blockly.Variables.renameVariable(newVar, newVar, this.sourceBlock_.blockSpace);
if (newVar) {
this.setText(newVar);
}
}.bind(this));
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion core/utils/variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ Blockly.Variables.getVars = function (opt_category) {
* @param {Blockly.BlockSpace} blockSpace BlockSpace to rename child blocks of
*/
Blockly.Variables.renameVariable = function(oldName, newName, blockSpace) {
if (newName === oldName) {
if (newName === oldName || !newName) {
return;
}
var blocks = blockSpace.getAllBlocks({shareMainModal: false});
Expand Down
14 changes: 14 additions & 0 deletions tests/variables_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,17 @@ function test_allVariablesOtherCategory() {

goog.dom.removeNode(container);
}

function test_cannotRenameVariablesToEmpty() {
var container = Blockly.Test.initializeBlockSpaceEditor();

var blockXML = '<xml><block type="variables_get"><title name="VAR">i</title></block></xml>';
Blockly.Xml.domToBlockSpace(Blockly.mainBlockSpace, Blockly.Xml.textToDom(blockXML));

Blockly.Variables.renameVariable("i", "z", Blockly.mainBlockSpace);
assertEquals("Variable renamed to 'z'", 'z', Blockly.Variables.allVariables()[0]);
Blockly.Variables.renameVariable("z", "", Blockly.mainBlockSpace);
assertEquals("Cannot rename var to empty", 'z', Blockly.Variables.allVariables()[0]);

goog.dom.removeNode(container);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay tests!