Make type and variabletype attributes optional

If a variable doesn’t have a type, then don’t add these empty attributes in the XML.
This commit is contained in:
Neil Fraser
2019-07-12 15:04:36 -07:00
committed by Neil Fraser
parent 8abba6ae10
commit 829dcc207c
7 changed files with 27 additions and 25 deletions

View File

@@ -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;
};

View File

@@ -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.
*/

View File

@@ -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.
*/

View File

@@ -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);
}
};

View File

@@ -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');

View File

@@ -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;
}

View File

@@ -69,7 +69,7 @@
<xs:simpleContent>
<xs:extension base="xs:NCName">
<xs:attribute name="id"/>
<xs:attribute name="type" use="required"/>
<xs:attribute name="type"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>