diff --git a/core/xml.js b/core/xml.js index 6ce6e3da4..25fa36a2a 100644 --- a/core/xml.js +++ b/core/xml.js @@ -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 . 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 documents. if (!doc || !doc.documentElement || doc.documentElement.nodeName.toLowerCase() != 'xml') { diff --git a/core/xml_utils.js b/core/xml_utils.js index 9281b087e..fa5efdc28 100644 --- a/core/xml_utils.js +++ b/core/xml_utils.js @@ -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); +}; diff --git a/gulpfile.js b/gulpfile.js index 02ea08107..fdbb9b367 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -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; };