diff --git a/core/field_variable.js b/core/field_variable.js index a68a6882a..42fba9ebe 100644 --- a/core/field_variable.js +++ b/core/field_variable.js @@ -156,7 +156,9 @@ Blockly.FieldVariable.prototype.toXml = function(fieldElement) { fieldElement.id = this.variable_.getId(); fieldElement.textContent = this.variable_.name; - fieldElement.setAttribute('variabletype', this.variable_.type); + if (this.variable_.type) { + fieldElement.setAttribute('variabletype', this.variable_.type); + } return fieldElement; }; diff --git a/core/variable_map.js b/core/variable_map.js index 0b75c6098..670191e1a 100644 --- a/core/variable_map.js +++ b/core/variable_map.js @@ -166,10 +166,10 @@ Blockly.VariableMap.prototype.renameVariableWithConflict_ = function(variable, * Create a variable with a given name, optional type, and optional ID. * @param {string} name The name of the variable. This must be unique across * variables and procedures. - * @param {string=} opt_type The type of the variable like 'int' or 'string'. + * @param {?string=} opt_type The type of the variable like 'int' or 'string'. * Does not need to be unique. Field_variable can filter variables based on * their type. This will default to '' which is a specific type. - * @param {string=} opt_id The unique ID of the variable. This will default to + * @param {?string=} opt_id The unique ID of the variable. This will default to * a UUID. * @return {Blockly.VariableModel} The newly created variable. */ diff --git a/core/workspace.js b/core/workspace.js index 5c304bc46..117804fe9 100644 --- a/core/workspace.js +++ b/core/workspace.js @@ -396,10 +396,10 @@ Blockly.Workspace.prototype.renameVariableById = function(id, newName) { * Create a variable with a given name, optional type, and optional ID. * @param {string} name The name of the variable. This must be unique across * variables and procedures. - * @param {string=} opt_type The type of the variable like 'int' or 'string'. + * @param {?string=} opt_type The type of the variable like 'int' or 'string'. * Does not need to be unique. Field_variable can filter variables based on * their type. This will default to '' which is a specific type. - * @param {string=} opt_id The unique ID of the variable. This will default to + * @param {?string=} opt_id The unique ID of the variable. This will default to * a UUID. * @return {Blockly.VariableModel} The newly created variable. */ diff --git a/core/xml.js b/core/xml.js index cd13f009b..f5d2abd41 100644 --- a/core/xml.js +++ b/core/xml.js @@ -74,7 +74,9 @@ Blockly.Xml.variablesToDom = function(variableList) { for (var i = 0, variable; variable = variableList[i]; i++) { var element = Blockly.utils.xml.createElement('variable'); element.appendChild(Blockly.utils.xml.createTextNode(variable.name)); - element.setAttribute('type', variable.type); + if (variable.type) { + element.setAttribute('type', variable.type); + } element.id = variable.getId(); variables.appendChild(element); } @@ -575,9 +577,6 @@ Blockly.Xml.domToVariables = function(xmlVariables, workspace) { var id = xmlChild.getAttribute('id'); var name = xmlChild.textContent; - if (type == null) { - throw Error('Variable with id, ' + id + ' is without a type'); - } workspace.createVariable(name, type, id); } }; diff --git a/tests/jsunit/xml_test.js b/tests/jsunit/xml_test.js index 2c358eedc..56c34d682 100644 --- a/tests/jsunit/xml_test.js +++ b/tests/jsunit/xml_test.js @@ -82,8 +82,8 @@ function xmlTest_tearDownWithMockBlocks() { } /** - * Check the values of the non variable field dom. - * @param {!Element} fieldDom The xml dom of the non variable field. + * Check the values of the non variable field DOM. + * @param {!Element} fieldDom The XML DOM of the non variable field. * @param {!string} name The expected name of the variable. * @param {!string} text The expected text of the variable. */ @@ -96,10 +96,10 @@ function xmlTest_checkNonVariableField(fieldDom, name, text) { /** * Check the values of the variable field DOM. - * @param {!Element} fieldDom The xml dom of the variable field. + * @param {!Element} fieldDom The XML DOM of the variable field. * @param {!string} name The expected name of the variable. * @param {!string} type The expected type of the variable. - * @param {!string} id The expected id of the variable. + * @param {!string} id The expected ID of the variable. * @param {!string} text The expected text of the variable. */ function xmlTest_checkVariableFieldDomValues(fieldDom, name, type, id, text) { @@ -111,9 +111,9 @@ function xmlTest_checkVariableFieldDomValues(fieldDom, name, type, id, text) { /** * Check the values of the variable DOM. - * @param {!Element} variableDom The xml dom of the variable. + * @param {!Element} variableDom The XML DOM of the variable. * @param {!string} type The expected type of the variable. - * @param {!string} id The expected id of the variable. + * @param {!string} id The expected ID of the variable. * @param {!string} text The expected text of the variable. */ function xmlTest_checkVariableDomValues(variableDom, type, id, text) { @@ -291,8 +291,8 @@ function test_blockToDom_fieldToDom_trivial() { var block = new Blockly.Block(workspace, 'field_variable_test_block'); block.inputList[0].fieldRow[0].setValue('id1'); var resultFieldDom = Blockly.Xml.blockToDom(block).childNodes[0]; - xmlTest_checkVariableFieldDomValues(resultFieldDom, 'VAR', '', 'id1', - 'name1'); + xmlTest_checkVariableFieldDomValues(resultFieldDom, 'VAR', null, 'id1', + 'name1'); xmlTest_tearDownWithMockBlocks(); } @@ -308,8 +308,9 @@ function test_blockToDom_fieldToDom_defaultCase() { Blockly.Events.enable(); var resultFieldDom = Blockly.Xml.blockToDom(block).childNodes[0]; - // Expect type is '' and id is '1' since we don't specify type and id. - xmlTest_checkVariableFieldDomValues(resultFieldDom, 'VAR', '', '1', 'name1'); + // Expect type is null and ID is '1' since we don't specify type and ID. + xmlTest_checkVariableFieldDomValues(resultFieldDom, 'VAR', null, '1', + 'name1'); } finally { xmlTest_tearDownWithMockBlocks(); } @@ -344,7 +345,7 @@ function test_variablesToDom_oneVariable() { assertEquals(1, resultDom.children.length); var resultVariableDom = resultDom.children[0]; assertEquals('name1', resultVariableDom.textContent); - assertEquals('', resultVariableDom.getAttribute('type')); + assertEquals(null, resultVariableDom.getAttribute('type')); assertEquals('1', resultVariableDom.getAttribute('id')); xmlTest_tearDown(); } @@ -363,7 +364,7 @@ function test_variablesToDom_twoVariables_oneBlock() { var resultDom = Blockly.Xml.variablesToDom(workspace.getAllVariables()); assertEquals(2, resultDom.children.length); - xmlTest_checkVariableDomValues(resultDom.children[0], '', 'id1', + xmlTest_checkVariableDomValues(resultDom.children[0], null, 'id1', 'name1'); xmlTest_checkVariableDomValues(resultDom.children[1], 'type2', 'id2', 'name2'); diff --git a/tests/mocha/xml_test.js b/tests/mocha/xml_test.js index 82bd0c73c..51dfa7f6d 100644 --- a/tests/mocha/xml_test.js +++ b/tests/mocha/xml_test.js @@ -259,7 +259,7 @@ suite('XML', function() { 'field_variable_test_block'); block.inputList[0].fieldRow[0].setValue('id1'); var resultFieldDom = Blockly.Xml.blockToDom(block).childNodes[0]; - assertVariableField(resultFieldDom, 'VAR', '', 'id1', 'name1'); + assertVariableField(resultFieldDom, 'VAR', null, 'id1', 'name1'); }); test('Variable Default Case', function() { var cacheGenUid = Blockly.utils.genUid; @@ -277,8 +277,8 @@ suite('XML', function() { Blockly.Events.enable(); var resultFieldDom = Blockly.Xml.blockToDom(block).childNodes[0]; - // Expect type is '' and id is '1' since we don't specify type and id. - assertVariableField(resultFieldDom, 'VAR', '', '1', 'name1'); + // Expect type is null and ID is '1' since we don't specify type and ID. + assertVariableField(resultFieldDom, 'VAR', null, '1', 'name1'); } finally { Blockly.utils.genUid = cacheGenUid; } diff --git a/tests/xml/blockly.xsd b/tests/xml/blockly.xsd index 4ab48e443..67562c4c0 100644 --- a/tests/xml/blockly.xsd +++ b/tests/xml/blockly.xsd @@ -69,7 +69,7 @@ - +