Removed textToDom Needing <xml> Documents (#2585)

* Removed textToDom needing <xml> documents.

* Fixed jsdoc.

* Followon changes resulting from lack of XML tag.

* Fix Mocha test.
This commit is contained in:
Beka Westberg
2019-06-24 15:50:06 -07:00
committed by RoboErikG
parent 27a6593d27
commit 5a4cc7ae12
7 changed files with 36 additions and 50 deletions

View File

@@ -197,9 +197,8 @@ Blockly.Events.Change.prototype.run = function(forward) {
oldMutation = oldMutationDom && Blockly.Xml.domToText(oldMutationDom);
}
if (block.domToMutation) {
value = value || '<mutation></mutation>';
var dom = Blockly.Xml.textToDom('<xml>' + value + '</xml>');
block.domToMutation(dom.firstChild);
var dom = Blockly.Xml.textToDom(value || '<mutation></mutation>');
block.domToMutation(dom);
}
Blockly.Events.fire(new Blockly.Events.Change(
block, 'mutation', null, oldMutation, value));
@@ -261,7 +260,7 @@ Blockly.Events.Create.prototype.toJson = function() {
*/
Blockly.Events.Create.prototype.fromJson = function(json) {
Blockly.Events.Create.superClass_.fromJson.call(this, json);
this.xml = Blockly.Xml.textToDom('<xml>' + json['xml'] + '</xml>').firstChild;
this.xml = Blockly.Xml.textToDom(json['xml']);
this.ids = json['ids'];
};

View File

@@ -26,8 +26,9 @@
goog.provide('Blockly.Options');
goog.require('Blockly.Xml');
goog.require('Blockly.Themes.Classic');
goog.require('Blockly.utils.userAgent');
goog.require('Blockly.Xml');
/**
@@ -48,7 +49,8 @@ Blockly.Options = function(options) {
var hasDisable = false;
var hasSounds = false;
} else {
var languageTree = Blockly.Options.parseToolboxTree(options['toolbox']);
var languageTree =
Blockly.Options.parseToolboxTree(options['toolbox'] || null);
var hasCategories = Boolean(languageTree &&
languageTree.getElementsByTagName('category').length);
var hasTrashcan = options['trashcan'];
@@ -263,11 +265,11 @@ Blockly.Options.parseGridOptions_ = function(options) {
Blockly.Options.parseToolboxTree = function(tree) {
if (tree) {
if (typeof tree != 'string') {
if (typeof XSLTProcessor == 'undefined' && tree.outerHTML) {
if (Blockly.utils.userAgent.IE && tree.outerHTML) {
// In this case the tree will not have been properly built by the
// browser. The HTML will be contained in the element, but it will
// not have the proper DOM structure since the browser doesn't support
// XSLTProcessor (XML -> HTML). This is the case in IE 9+.
// XSLTProcessor (XML -> HTML).
tree = tree.outerHTML;
} else if (!(tree instanceof Element)) {
tree = null;
@@ -275,6 +277,9 @@ Blockly.Options.parseToolboxTree = function(tree) {
}
if (typeof tree == 'string') {
tree = Blockly.Xml.textToDom(tree);
if (tree.nodeName.toLowerCase() != 'xml') {
throw TypeError('Toolbox should be an <xml> document.');
}
}
} else {
tree = null;

View File

@@ -431,7 +431,7 @@ Blockly.Trashcan.prototype.click = function() {
var xml = [];
for (var i = 0, text; text = this.contents_[i]; i++) {
xml[i] = Blockly.Xml.textToDom(text).firstChild;
xml[i] = Blockly.Xml.textToDom(text);
}
this.flyout_.show(xml);
};
@@ -531,5 +531,5 @@ Blockly.Trashcan.prototype.cleanBlockXML_ = function(xml) {
}
node = nextNode;
}
return '<xml>' + Blockly.Xml.domToText(xmlBlock) + '</xml>';
return Blockly.Xml.domToText(xmlBlock);
};

View File

@@ -175,39 +175,33 @@ Blockly.Variables.flyoutCategoryBlocks = function(workspace) {
variableModelList[variableModelList.length - 1]);
if (Blockly.Blocks['variables_set']) {
var gap = Blockly.Blocks['math_change'] ? 8 : 24;
var blockText = '<xml>' +
'<block type="variables_set" gap="' + gap + '">' +
var blockText = '<block type="variables_set" gap="' + gap + '">' +
mostRecentVariableFieldXmlString +
'</block>' +
'</xml>';
var block = Blockly.Xml.textToDom(blockText).firstChild;
'</block>';
var block = Blockly.Xml.textToDom(blockText);
xmlList.push(block);
}
if (Blockly.Blocks['math_change']) {
var gap = Blockly.Blocks['variables_get'] ? 20 : 8;
var blockText = '<xml>' +
'<block type="math_change" gap="' + gap + '">' +
var blockText = '<block type="math_change" gap="' + gap + '">' +
mostRecentVariableFieldXmlString +
'<value name="DELTA">' +
'<shadow type="math_number">' +
'<field name="NUM">1</field>' +
'</shadow>' +
'</value>' +
'</block>' +
'</xml>';
var block = Blockly.Xml.textToDom(blockText).firstChild;
'</block>';
var block = Blockly.Xml.textToDom(blockText);
xmlList.push(block);
}
if (Blockly.Blocks['variables_get']) {
variableModelList.sort(Blockly.VariableModel.compareByName);
for (var i = 0, variable; variable = variableModelList[i]; i++) {
var blockText = '<xml>' +
'<block type="variables_get" gap="8">' +
var blockText = '<block type="variables_get" gap="8">' +
Blockly.Variables.generateVariableFieldXmlString(variable) +
'</block>' +
'</xml>';
var block = Blockly.Xml.textToDom(blockText).firstChild;
'</block>';
var block = Blockly.Xml.textToDom(blockText);
xmlList.push(block);
}
}
@@ -481,10 +475,7 @@ Blockly.Variables.generateVariableFieldXmlString = function(variableModel) {
Blockly.Variables.generateVariableFieldDom = function(variableModel) {
var xmlFieldString =
Blockly.Variables.generateVariableFieldXmlString(variableModel);
var text = '<xml>' + xmlFieldString + '</xml>';
var dom = Blockly.Xml.textToDom(text);
var fieldDom = dom.firstChild;
return fieldDom;
return Blockly.Xml.textToDom(xmlFieldString);
};
/**

View File

@@ -93,23 +93,19 @@ Blockly.VariablesDynamic.flyoutCategoryBlocks = function(workspace) {
if (Blockly.Blocks['variables_set_dynamic']) {
var firstVariable = variableModelList[variableModelList.length - 1];
var gap = 24;
var blockText = '<xml>' +
'<block type="variables_set_dynamic" gap="' + gap + '">' +
var blockText = '<block type="variables_set_dynamic" gap="' + gap + '">' +
Blockly.Variables.generateVariableFieldXmlString(firstVariable) +
'</block>' +
'</xml>';
var block = Blockly.Xml.textToDom(blockText).firstChild;
'</block>';
var block = Blockly.Xml.textToDom(blockText);
xmlList.push(block);
}
if (Blockly.Blocks['variables_get_dynamic']) {
variableModelList.sort(Blockly.VariableModel.compareByName);
for (var i = 0, variable; variable = variableModelList[i]; i++) {
var blockText = '<xml>' +
'<block type="variables_get_dynamic" gap="8">' +
var blockText = '<block type="variables_get_dynamic" gap="8">' +
Blockly.Variables.generateVariableFieldXmlString(variable) +
'</block>' +
'</xml>';
var block = Blockly.Xml.textToDom(blockText).firstChild;
'</block>';
var block = Blockly.Xml.textToDom(blockText);
xmlList.push(block);
}
}

View File

@@ -317,20 +317,17 @@ Blockly.Xml.domToPrettyText = function(dom) {
};
/**
* Converts an XML string into a DOM structure. It requires the XML to have a
* root element of <xml>. Other XML string will result in throwing an error.
* Converts an XML string into a DOM structure.
* @param {string} text An XML string.
* @return {!Element} A DOM object representing the singular child of the
* document element.
* @throws if XML doesn't parse or is not the expected structure.
* @throws if the text doesn't parse.
*/
Blockly.Xml.textToDom = function(text) {
var doc = Blockly.Xml.utils.textToDomDocument(text);
// This function only accepts <xml> documents.
if (!doc || !doc.documentElement ||
doc.documentElement.nodeName.toLowerCase() != 'xml') {
// Whatever we got back from the parser is not the expected structure.
throw TypeError('Blockly.Xml.textToDom expected an <xml> document.');
doc.getElementsByTagName('parsererror').length) {
throw Error('textToDom was unable to parse: ' + text);
}
return doc.documentElement;
};