Don't monkey-patch Blocky.utils.xml.document (#5461)

Use Blockly.utils.xml.setDocument instead.
This commit is contained in:
Christopher Allen
2021-09-14 19:55:31 +01:00
committed by GitHub
parent f4430e9e47
commit d202ae0201
4 changed files with 42 additions and 22 deletions

View File

@@ -19,6 +19,8 @@
goog.module('Blockly.utils.xml'); goog.module('Blockly.utils.xml');
goog.module.declareLegacyNamespace(); goog.module.declareLegacyNamespace();
const {globalThis} = goog.require('Blockly.utils.global');
/** /**
* Namespace for Blockly's XML. * Namespace for Blockly's XML.
@@ -27,19 +29,31 @@ const NAME_SPACE = 'https://developers.google.com/blockly/xml';
exports.NAME_SPACE = NAME_SPACE; exports.NAME_SPACE = NAME_SPACE;
/** /**
* Get the document object. This method is overridden in the Node.js build of * The Document object to use. By default this is just document, but
* Blockly. See gulpfile.js, package-blockly-node task. * the Node.js build of Blockly (see scripts/package/node/core.js)
* * calls setDocument to supply a Document implementation from the
* Note that this function is named getDocument so as to not shadow the * jsdom package instead.
* global of the same name, but (for now) exported as .document to not * @type {!Document}
* break existing importers. */
* let xmlDocument = globalThis.document;
/**
* Get the document object to use for XML serialization.
* @return {!Document} The document object. * @return {!Document} The document object.
*/ */
const getDocument = function() { const getDocument = function() {
return document; return xmlDocument;
}; };
exports.document = getDocument; exports.getDocument = getDocument;
/**
* Get the document object to use for XML serialization.
* @param {!Document} document The document object to use.
*/
const setDocument = function(document) {
xmlDocument = document;
};
exports.setDocument = setDocument;
/** /**
* Create DOM element for XML. * Create DOM element for XML.
@@ -47,7 +61,7 @@ exports.document = getDocument;
* @return {!Element} New DOM element. * @return {!Element} New DOM element.
*/ */
const createElement = function(tagName) { const createElement = function(tagName) {
return exports.document().createElementNS(NAME_SPACE, tagName); return xmlDocument.createElementNS(NAME_SPACE, tagName);
}; };
exports.createElement = createElement; exports.createElement = createElement;
@@ -57,7 +71,7 @@ exports.createElement = createElement;
* @return {!Text} New DOM text node. * @return {!Text} New DOM text node.
*/ */
const createTextNode = function(text) { const createTextNode = function(text) {
return exports.document().createTextNode(text); return xmlDocument.createTextNode(text);
}; };
exports.createTextNode = createTextNode; exports.createTextNode = createTextNode;

View File

@@ -32,7 +32,7 @@ const renamings = {
export: 'conditionalBind', export: 'conditionalBind',
}, },
}, },
} },
}, },
'6.20210701.0': { '6.20210701.0': {
'Blockly': { 'Blockly': {
@@ -70,14 +70,21 @@ const renamings = {
'Blockly.utils': { 'Blockly.utils': {
exports: { exports: {
genUid: {module: 'Blockly.utils.idGenerator'}, genUid: {module: 'Blockly.utils.idGenerator'},
} },
}, },
'Blockly.utils.global': { 'Blockly.utils.global': {
export: 'globalThis', // Previous default export now named. export: 'globalThis', // Previous default export now named.
}, },
'Blockly.utils.IdGenerator': { 'Blockly.utils.IdGenerator': {
module: 'Blockly.utils.idGenerator', module: 'Blockly.utils.idGenerator',
} },
'Blockly.utils.xml': {
exports: {
// document was a function before, too - not a static property
// or get accessor.
document: {export: 'getDocument'},
},
},
}, },
}; };

View File

@@ -24,11 +24,10 @@ Blockly.setLocale = function (locale) {
// XMLSerializer. // XMLSerializer.
const globalThis = Blockly.utils.global.globalThis; const globalThis = Blockly.utils.global.globalThis;
if (typeof globalThis.document !== 'object') { if (typeof globalThis.document !== 'object') {
globalThis.DOMParser = require('jsdom/lib/jsdom/living').DOMParser; const jsdom = require('jsdom/lib/jsdom/living');
globalThis.XMLSerializer = require('jsdom/lib/jsdom/living').XMLSerializer; globalThis.DOMParser = jsdom.DOMParser;
var doc = Blockly.utils.xml.textToDomDocument( globalThis.XMLSerializer = jsdom.XMLSerializer;
'<xml xmlns="https://developers.google.com/blockly/xml"></xml>'); const xmlDocument = Blockly.utils.xml.textToDomDocument(
Blockly.utils.xml.document = function() { `<xml xmlns="${Blockly.utils.xml.NAME_SPACE}"></xml>`);
return doc; Blockly.utils.xml.setDocument(xmlDocument);
};
} }

View File

@@ -231,7 +231,7 @@ goog.addDependency('../../core/utils/svg.js', ['Blockly.utils.Svg'], [], {'lang'
goog.addDependency('../../core/utils/svg_paths.js', ['Blockly.utils.svgPaths'], [], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/utils/svg_paths.js', ['Blockly.utils.svgPaths'], [], {'lang': 'es6', 'module': 'goog'});
goog.addDependency('../../core/utils/toolbox.js', ['Blockly.utils.toolbox'], ['Blockly.Xml', 'Blockly.utils.userAgent'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/utils/toolbox.js', ['Blockly.utils.toolbox'], ['Blockly.Xml', 'Blockly.utils.userAgent'], {'lang': 'es6', 'module': 'goog'});
goog.addDependency('../../core/utils/useragent.js', ['Blockly.utils.userAgent'], ['Blockly.utils.global'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/utils/useragent.js', ['Blockly.utils.userAgent'], ['Blockly.utils.global'], {'lang': 'es6', 'module': 'goog'});
goog.addDependency('../../core/utils/xml.js', ['Blockly.utils.xml'], [], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/utils/xml.js', ['Blockly.utils.xml'], ['Blockly.utils.global'], {'lang': 'es6', 'module': 'goog'});
goog.addDependency('../../core/variable_map.js', ['Blockly.VariableMap'], ['Blockly.Events', 'Blockly.Events.VarDelete', 'Blockly.Events.VarRename', 'Blockly.Msg', 'Blockly.Names', 'Blockly.VariableModel', 'Blockly.dialog', 'Blockly.utils.idGenerator', 'Blockly.utils.object'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/variable_map.js', ['Blockly.VariableMap'], ['Blockly.Events', 'Blockly.Events.VarDelete', 'Blockly.Events.VarRename', 'Blockly.Msg', 'Blockly.Names', 'Blockly.VariableModel', 'Blockly.dialog', 'Blockly.utils.idGenerator', 'Blockly.utils.object'], {'lang': 'es6', 'module': 'goog'});
goog.addDependency('../../core/variable_model.js', ['Blockly.VariableModel'], ['Blockly.Events', 'Blockly.Events.VarCreate', 'Blockly.utils.idGenerator'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/variable_model.js', ['Blockly.VariableModel'], ['Blockly.Events', 'Blockly.Events.VarCreate', 'Blockly.utils.idGenerator'], {'lang': 'es6', 'module': 'goog'});
goog.addDependency('../../core/variables.js', ['Blockly.Variables'], ['Blockly.Blocks', 'Blockly.Msg', 'Blockly.VariableModel', 'Blockly.Xml', 'Blockly.dialog', 'Blockly.utils.xml'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/variables.js', ['Blockly.Variables'], ['Blockly.Blocks', 'Blockly.Msg', 'Blockly.VariableModel', 'Blockly.Xml', 'Blockly.dialog', 'Blockly.utils.xml'], {'lang': 'es6', 'module': 'goog'});