mirror of
https://github.com/google/blockly.git
synced 2026-01-10 18:37:09 +01:00
Move DOMParser and XMLSerializer to Xml.utils
This commit is contained in:
18
core/xml.js
18
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 <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') {
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user