Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions core/utils/xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
goog.module('Blockly.utils.xml');
goog.module.declareLegacyNamespace();

const {globalThis} = goog.require('Blockly.utils.global');


/**
* Namespace for Blockly's XML.
Expand All @@ -27,27 +29,39 @@ const NAME_SPACE = 'https://developers.google.com/blockly/xml';
exports.NAME_SPACE = NAME_SPACE;

/**
* Get the document object. This method is overridden in the Node.js build of
* Blockly. See gulpfile.js, package-blockly-node task.
*
* Note that this function is named getDocument so as to not shadow the
* global of the same name, but (for now) exported as .document to not
* break existing importers.
*
* The Document object to use. By default this is just document, but
* the Node.js build of Blockly (see scripts/package/node/core.js)
* calls setDocument to supply a Document implementation from the
* jsdom package instead.
* @type {!Document}
*/
let xmlDocument = globalThis.document;

/**
* Get the document object to use for XML serialization.
* @return {!Document} The document object.
*/
const getDocument = function() {
return document;
return xmlDocument;
};
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.document = getDocument;
exports.setDocument = setDocument;

/**
* Create DOM element for XML.
* @param {string} tagName Name of DOM element.
* @return {!Element} New DOM element.
*/
const createElement = function(tagName) {
return exports.document().createElementNS(NAME_SPACE, tagName);
return xmlDocument.createElementNS(NAME_SPACE, tagName);
};
exports.createElement = createElement;

Expand All @@ -57,7 +71,7 @@ exports.createElement = createElement;
* @return {!Text} New DOM text node.
*/
const createTextNode = function(text) {
return exports.document().createTextNode(text);
return xmlDocument.createTextNode(text);
};
exports.createTextNode = createTextNode;

Expand Down
13 changes: 10 additions & 3 deletions scripts/migration/renamings.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const renamings = {
export: 'conditionalBind',
},
},
}
},
},
'6.20210701.0': {
'Blockly': {
Expand Down Expand Up @@ -70,14 +70,21 @@ const renamings = {
'Blockly.utils': {
exports: {
genUid: {module: 'Blockly.utils.idGenerator'},
}
},
},
'Blockly.utils.global': {
export: 'globalThis', // Previous default export now named.
},
'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'},
},
},
},
};

Expand Down
13 changes: 6 additions & 7 deletions scripts/package/node/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ Blockly.setLocale = function (locale) {
// XMLSerializer.
const globalThis = Blockly.utils.global.globalThis;
if (typeof globalThis.document !== 'object') {
globalThis.DOMParser = require('jsdom/lib/jsdom/living').DOMParser;
globalThis.XMLSerializer = require('jsdom/lib/jsdom/living').XMLSerializer;
var doc = Blockly.utils.xml.textToDomDocument(
'<xml xmlns="https://developers.google.com/blockly/xml"></xml>');
Blockly.utils.xml.document = function() {
return doc;
};
const jsdom = require('jsdom/lib/jsdom/living');
globalThis.DOMParser = jsdom.DOMParser;
globalThis.XMLSerializer = jsdom.XMLSerializer;
const xmlDocument = Blockly.utils.xml.textToDomDocument(
`<xml xmlns="${Blockly.utils.xml.NAME_SPACE}"></xml>`);
Blockly.utils.xml.setDocument(xmlDocument);
}
2 changes: 1 addition & 1 deletion tests/deps.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.