Move DOMParser and XMLSerializer to Xml.utils

This commit is contained in:
Neil Fraser
2018-10-12 06:56:37 -07:00
committed by Neil Fraser
parent 8d70598b6b
commit f196816f62
3 changed files with 27 additions and 18 deletions

View File

@@ -316,8 +316,7 @@ Blockly.Xml.cloneShadow_ = function(shadow) {
* @return {string} Text representation.
*/
Blockly.Xml.domToText = function(dom) {
var oSerializer = new XMLSerializer();
return oSerializer.serializeToString(dom);
return Blockly.Xml.utils.domToText(dom);
};
/**
@@ -351,19 +350,6 @@ Blockly.Xml.domToPrettyText = function(dom) {
return text.replace(/^\n/, '');
};
/**
* Converts an XML string into a DOM tree. This method will be overridden in
* the Node.js build of Blockly. See gulpfile.js, blockly_javascript_en task.
* @param {string} text XML string.
* @return {!Element} The DOM document.
* @throws if XML doesn't parse.
* @private
*/
Blockly.Xml.textToDomDocument_ = function(text) {
var oParser = new DOMParser();
return oParser.parseFromString(text, 'text/xml');
};
/**
* 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.
@@ -372,7 +358,7 @@ Blockly.Xml.textToDomDocument_ = function(text) {
* @throws if XML doesn't parse or is not the expected structure.
*/
Blockly.Xml.textToDom = function(text) {
var doc = Blockly.Xml.textToDomDocument_(text);
var doc = Blockly.Xml.utils.textToDomDocument(text);
// This function only accepts <xml> documents.
if (!doc || !doc.documentElement ||
doc.documentElement.nodeName.toLowerCase() != 'xml') {

View File

@@ -52,3 +52,26 @@ Blockly.Xml.utils.createTextNode = function(text) {
return document.createTextNode(text);
};
/**
* Converts an XML string into a DOM tree. This method will be overridden in
* the Node.js build of Blockly. See gulpfile.js, blockly_javascript_en task.
* @param {string} text XML string.
* @return {!Element} The DOM document.
* @throws if XML doesn't parse.
*/
Blockly.Xml.utils.textToDomDocument = function(text) {
var oParser = new DOMParser();
return oParser.parseFromString(text, 'text/xml');
};
/**
* Converts a DOM structure into plain text.
* Currently the text format is fairly ugly: all one line with no whitespace.
* @param {!Element} dom A tree of XML elements.
* @return {string} Text representation.
*/
Blockly.Xml.utils.domToText = function(dom) {
// TODO: Support node.js.
var oSerializer = new XMLSerializer();
return oSerializer.serializeToString(dom);
};

View File

@@ -56,13 +56,13 @@ gulp.task('blockly_javascript_en', function() {
'msg/js/en.js'
];
// Concatenate the sources, appending the module export at the bottom.
// Override textToDomDocument_, providing Node alternative to DOMParser.
// Override textToDomDocument, providing Node alternative to DOMParser.
return gulp.src(srcs)
.pipe(gulp.concat('blockly_node_javascript_en.js'))
.pipe(insert.append(`
if (typeof DOMParser !== 'function') {
var JSDOM = require('jsdom').JSDOM;
Blockly.Xml.textToDomDocument_ = function(text) {
Blockly.Xml.utils.textToDomDocument = function(text) {
var jsdom = new JSDOM(text, { contentType: 'text/xml' });
return jsdom.window.document;
};