Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Remove last remaining circular import in core/ #6818

Merged
merged 7 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions appengine/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ BlocklyStorage.restoreBlocks = function(opt_workspace) {
var url = window.location.href.split('#')[0];
if ('localStorage' in window && window.localStorage[url]) {
var workspace = opt_workspace || Blockly.getMainWorkspace();
var xml = Blockly.Xml.textToDom(window.localStorage[url]);
var xml = Blockly.utils.xml.textToDom(window.localStorage[url]);
Blockly.Xml.domToWorkspace(xml, workspace);
}
};
Expand Down Expand Up @@ -168,7 +168,7 @@ BlocklyStorage.monitorChanges_ = function(workspace) {
*/
BlocklyStorage.loadXml_ = function(xml, workspace) {
try {
xml = Blockly.Xml.textToDom(xml);
xml = Blockly.utils.xml.textToDom(xml);
} catch (e) {
BlocklyStorage.alert(BlocklyStorage.XML_ERROR + '\nXML: ' + xml);
return;
Expand Down
3 changes: 1 addition & 2 deletions blocks/lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
goog.module('Blockly.libraryBlocks.lists');

const xmlUtils = goog.require('Blockly.utils.xml');
const Xml = goog.require('Blockly.Xml');
const {Align} = goog.require('Blockly.Input');
/* eslint-disable-next-line no-unused-vars */
const {Block} = goog.requireType('Blockly.Block');
Expand Down Expand Up @@ -480,7 +479,7 @@ blocks['lists_getIndex'] = {
this.updateStatement_(true);
} else if (typeof state === 'string') {
// backward compatible for json serialised mutations
this.domToMutation(Xml.textToDom(state));
this.domToMutation(xmlUtils.textToDom(state));
}
},

Expand Down
6 changes: 4 additions & 2 deletions closure/goog/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -617,8 +617,10 @@ goog.declareModuleId = function(namespace) {
'within an ES6 module');
}
if (goog.moduleLoaderState_ && goog.moduleLoaderState_.moduleName) {
// throw new Error(
// 'goog.declareModuleId may only be called once per module.');
throw new Error(
'goog.declareModuleId may only be called once per module.' +
'This error can also be caused by circular imports, which ' +
'are not supported by debug module loader.');
}
if (namespace in goog.loadedModules_) {
throw new Error(
Expand Down
4 changes: 3 additions & 1 deletion core/events/events_block_change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {Block} from '../block.js';
import type {BlockSvg} from '../block_svg.js';
import * as deprecation from '../utils/deprecation.js';
import * as registry from '../registry.js';
import * as utilsXml from '../utils/xml.js';
import {Workspace} from '../workspace.js';
import * as Xml from '../xml.js';

Expand Down Expand Up @@ -184,7 +185,8 @@ export class BlockChange extends BlockBase {
if (block.loadExtraState) {
block.loadExtraState(JSON.parse(value as string || '{}'));
} else if (block.domToMutation) {
block.domToMutation(Xml.textToDom(value as string || '<mutation/>'));
block.domToMutation(
utilsXml.textToDom(value as string || '<mutation/>'));
}
eventUtils.fire(
new BlockChange(block, 'mutation', null, oldState, value));
Expand Down
5 changes: 3 additions & 2 deletions core/events/events_block_create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {Block} from '../block.js';
import * as deprecation from '../utils/deprecation.js';
import * as registry from '../registry.js';
import * as blocks from '../serialization/blocks.js';
import * as utilsXml from '../utils/xml.js';
import * as Xml from '../xml.js';

import {BlockBase, BlockBaseJson} from './events_block_base.js';
Expand Down Expand Up @@ -101,7 +102,7 @@ export class BlockCreate extends BlockBase {
'Blockly.Events.BlockCreate.prototype.fromJson', 'version 9',
'version 10', 'Blockly.Events.fromJson');
super.fromJson(json);
this.xml = Xml.textToDom(json['xml']);
this.xml = utilsXml.textToDom(json['xml']);
this.ids = json['ids'];
this.json = json['json'] as blocks.State;
if (json['recordUndo'] !== undefined) {
Expand All @@ -123,7 +124,7 @@ export class BlockCreate extends BlockBase {
const newEvent =
super.fromJson(json, workspace, event ?? new BlockCreate()) as
BlockCreate;
newEvent.xml = Xml.textToDom(json['xml']);
newEvent.xml = utilsXml.textToDom(json['xml']);
newEvent.ids = json['ids'];
newEvent.json = json['json'] as blocks.State;
if (json['recordUndo'] !== undefined) {
Expand Down
5 changes: 3 additions & 2 deletions core/events/events_block_delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {Block} from '../block.js';
import * as deprecation from '../utils/deprecation.js';
import * as registry from '../registry.js';
import * as blocks from '../serialization/blocks.js';
import * as utilsXml from '../utils/xml.js';
import * as Xml from '../xml.js';

import {BlockBase, BlockBaseJson} from './events_block_base.js';
Expand Down Expand Up @@ -114,7 +115,7 @@ export class BlockDelete extends BlockBase {
'Blockly.Events.BlockDelete.prototype.fromJson', 'version 9',
'version 10', 'Blockly.Events.fromJson');
super.fromJson(json);
this.oldXml = Xml.textToDom(json['oldXml']);
this.oldXml = utilsXml.textToDom(json['oldXml']);
this.ids = json['ids'];
this.wasShadow =
json['wasShadow'] || this.oldXml.tagName.toLowerCase() === 'shadow';
Expand All @@ -138,7 +139,7 @@ export class BlockDelete extends BlockBase {
const newEvent =
super.fromJson(json, workspace, event ?? new BlockDelete()) as
BlockDelete;
newEvent.oldXml = Xml.textToDom(json['oldXml']);
newEvent.oldXml = utilsXml.textToDom(json['oldXml']);
newEvent.ids = json['ids'];
newEvent.wasShadow =
json['wasShadow'] || newEvent.oldXml.tagName.toLowerCase() === 'shadow';
Expand Down
5 changes: 3 additions & 2 deletions core/events/events_comment_create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ goog.declareModuleId('Blockly.Events.CommentCreate');
import * as deprecation from '../utils/deprecation.js';
import * as registry from '../registry.js';
import type {WorkspaceComment} from '../workspace_comment.js';
import * as utilsXml from '../utils/xml.js';
import * as Xml from '../xml.js';

import {CommentBase, CommentBaseJson} from './events_comment_base.js';
Expand Down Expand Up @@ -74,7 +75,7 @@ export class CommentCreate extends CommentBase {
'Blockly.Events.CommentCreate.prototype.fromJson', 'version 9',
'version 10', 'Blockly.Events.fromJson');
super.fromJson(json);
this.xml = Xml.textToDom(json['xml']);
this.xml = utilsXml.textToDom(json['xml']);
}

/**
Expand All @@ -91,7 +92,7 @@ export class CommentCreate extends CommentBase {
const newEvent =
super.fromJson(json, workspace, event ?? new CommentCreate()) as
CommentCreate;
newEvent.xml = Xml.textToDom(json['xml']);
newEvent.xml = utilsXml.textToDom(json['xml']);
return newEvent;
}

Expand Down
3 changes: 2 additions & 1 deletion core/events/events_comment_delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type {WorkspaceComment} from '../workspace_comment.js';

import {CommentBase, CommentBaseJson} from './events_comment_base.js';
import * as eventUtils from './utils.js';
import * as utilsXml from '../utils/xml.js';
import * as Xml from '../xml.js';
import type {Workspace} from '../workspace.js';

Expand Down Expand Up @@ -85,7 +86,7 @@ export class CommentDelete extends CommentBase {
const newEvent =
super.fromJson(json, workspace, event ?? new CommentDelete()) as
CommentDelete;
newEvent.xml = Xml.textToDom(json['xml']);
newEvent.xml = utilsXml.textToDom(json['xml']);
return newEvent;
}
}
Expand Down
5 changes: 2 additions & 3 deletions core/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import * as userAgent from './utils/useragent.js';
import * as utilsXml from './utils/xml.js';
import * as WidgetDiv from './widgetdiv.js';
import type {WorkspaceSvg} from './workspace_svg.js';
import * as Xml from './xml.js';

/**
* A function that is called to validate changes to the field's value before
Expand Down Expand Up @@ -455,7 +454,7 @@ export abstract class Field<T = any> implements IASTNodeLocationSvg,
callingClass.prototype.toXml !== this.toXml) {
const elem = utilsXml.createElement('field');
elem.setAttribute('name', this.name || '');
const text = Xml.domToText(this.toXml(elem));
const text = utilsXml.domToText(this.toXml(elem));
return text.replace(
' xmlns="https://developers.google.com/blockly/xml"', '');
}
Expand All @@ -477,7 +476,7 @@ export abstract class Field<T = any> implements IASTNodeLocationSvg,
boolean {
if (callingClass.prototype.loadState === this.loadState &&
callingClass.prototype.fromXml !== this.fromXml) {
this.fromXml(Xml.textToDom(state as string));
this.fromXml(utilsXml.textToDom(state as string));
return true;
}
// Either they called this on purpose from their loadState, or they have
Expand Down
5 changes: 3 additions & 2 deletions core/flyout_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {Svg} from './utils/svg.js';
import * as toolbox from './utils/toolbox.js';
import * as Variables from './variables.js';
import {WorkspaceSvg} from './workspace_svg.js';
import * as utilsXml from './utils/xml.js';
import * as Xml from './xml.js';


Expand Down Expand Up @@ -744,7 +745,7 @@ export abstract class Flyout extends DeleteArea implements IFlyout {
let block;
if (blockInfo['blockxml']) {
const xml = (typeof blockInfo['blockxml'] === 'string' ?
Xml.textToDom(blockInfo['blockxml']) :
utilsXml.textToDom(blockInfo['blockxml']) :
blockInfo['blockxml']) as Element;
block = this.getRecycledBlock_(xml.getAttribute('type')!);
if (!block) {
Expand Down Expand Up @@ -803,7 +804,7 @@ export abstract class Flyout extends DeleteArea implements IFlyout {
gap = parseInt(blockInfo['gap'].toString());
} else if (blockInfo['blockxml']) {
const xml = (typeof blockInfo['blockxml'] === 'string' ?
Xml.textToDom(blockInfo['blockxml']) :
utilsXml.textToDom(blockInfo['blockxml']) :
blockInfo['blockxml']) as Element;
gap = parseInt(xml.getAttribute('gap')!);
}
Expand Down
4 changes: 2 additions & 2 deletions core/procedures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,12 @@ export function mutateCallers(defBlock: Block) {
const callers = getCallers(name, defBlock.workspace);
for (let i = 0, caller; caller = callers[i]; i++) {
const oldMutationDom = caller.mutationToDom!();
const oldMutation = oldMutationDom && Xml.domToText(oldMutationDom);
const oldMutation = oldMutationDom && utilsXml.domToText(oldMutationDom);
if (caller.domToMutation) {
caller.domToMutation(xmlElement);
}
const newMutationDom = caller.mutationToDom!();
const newMutation = newMutationDom && Xml.domToText(newMutationDom);
const newMutation = newMutationDom && utilsXml.domToText(newMutationDom);
if (oldMutation !== newMutation) {
// Fire a mutation on every caller block. But don't record this as an
// undo action since it is deterministically tied to the procedure's
Expand Down
3 changes: 2 additions & 1 deletion core/serialization/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import * as eventUtils from '../events/utils.js';
import {inputTypes} from '../input_types.js';
import type {ISerializer} from '../interfaces/i_serializer.js';
import {Size} from '../utils/size.js';
import * as utilsXml from '../utils/xml.js';
import type {Workspace} from '../workspace.js';
import * as Xml from '../xml.js';

Expand Down Expand Up @@ -468,7 +469,7 @@ function loadExtraState(block: Block, state: State) {
if (block.loadExtraState) {
block.loadExtraState(state['extraState']);
} else if (block.domToMutation) {
block.domToMutation(Xml.textToDom(state['extraState']));
block.domToMutation(utilsXml.textToDom(state['extraState']));
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/utils/toolbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ goog.declareModuleId('Blockly.utils.toolbox');
import type {ConnectionState} from '../serialization/blocks.js';
import type {CssConfig as CategoryCssConfig} from '../toolbox/category.js';
import type {CssConfig as SeparatorCssConfig} from '../toolbox/separator.js';
import * as Xml from '../xml.js';
import * as utilsXml from './xml.js';


/**
Expand Down Expand Up @@ -422,7 +422,7 @@ export function parseToolboxTree(toolboxDef: Element|null|string): Element|
let parsedToolboxDef: Element|null = null;
if (toolboxDef) {
if (typeof toolboxDef === 'string') {
parsedToolboxDef = Xml.textToDom(toolboxDef);
parsedToolboxDef = utilsXml.textToDom(toolboxDef);
if (parsedToolboxDef.nodeName.toLowerCase() !== 'xml') {
throw TypeError('Toolbox should be an <xml> document.');
}
Expand Down
18 changes: 18 additions & 0 deletions core/utils/xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ export function createTextNode(text: string): Text {
return document.createTextNode(text);
}

/**
* Converts an XML string into a DOM structure.
*
* @param text An XML string.
* @returns A DOM object representing the singular child of the document
* element.
* @throws if the text doesn't parse.
* @alias Blockly.utils.xml.textToDom
cpcallen marked this conversation as resolved.
Show resolved Hide resolved
*/
export function textToDom(text: string): Element {
const doc = textToDomDocument(text);
if (!doc || !doc.documentElement ||
doc.getElementsByTagName('parsererror').length) {
throw Error('textToDom was unable to parse: ' + text);
}
return doc.documentElement;
}

/**
* Converts an XML string into a DOM tree.
*
Expand Down
3 changes: 1 addition & 2 deletions core/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import * as utilsXml from './utils/xml.js';
import {VariableModel} from './variable_model.js';
import type {Workspace} from './workspace.js';
import type {WorkspaceSvg} from './workspace_svg.js';
import * as Xml from './xml.js';


/**
Expand Down Expand Up @@ -142,7 +141,7 @@ export function flyoutCategoryBlocks(workspace: Workspace): Element[] {
block.setAttribute('type', 'math_change');
block.setAttribute('gap', Blocks['variables_get'] ? '20' : '8');
block.appendChild(generateVariableFieldDom(mostRecentVariable));
const value = Xml.textToDom(
const value = utilsXml.textToDom(
'<value name="DELTA">' +
'<shadow type="math_number">' +
'<field name="NUM">1</field>' +
Expand Down
12 changes: 6 additions & 6 deletions core/xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ goog.declareModuleId('Blockly.Xml');
import type {Block} from './block.js';
import type {BlockSvg} from './block_svg.js';
import type {Connection} from './connection.js';
import * as deprecation from './utils/deprecation.js';
import * as eventUtils from './events/utils.js';
import type {Field} from './field.js';
import {inputTypes} from './input_types.js';
Expand Down Expand Up @@ -386,15 +387,14 @@ export function domToPrettyText(dom: Node): string {
* @returns A DOM object representing the singular child of the document
* element.
* @throws if the text doesn't parse.
* @deprecated Moved to core/utils/xml.js.
* @alias Blockly.Xml.textToDom
*/
export function textToDom(text: string): Element {
const doc = utilsXml.textToDomDocument(text);
if (!doc || !doc.documentElement ||
doc.getElementsByTagName('parsererror').length) {
throw Error('textToDom was unable to parse: ' + text);
}
return doc.documentElement;
deprecation.warn(
'Blockly.Xml.textToDom', 'version 9', 'version 10',
'Use Blockly.utils.xml.textToDom instead');
return utilsXml.textToDom(text);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions demos/blockfactory/app_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ AppController.prototype.formatBlockLibraryForExport_ = function(blockXmlMap) {

// Append each block node to XML DOM.
for (var blockType in blockXmlMap) {
var blockXmlDom = Blockly.Xml.textToDom(blockXmlMap[blockType]);
var blockXmlDom = Blockly.utils.xml.textToDom(blockXmlMap[blockType]);
var blockNode = blockXmlDom.firstElementChild;
xmlDom.appendChild(blockNode);
}
Expand All @@ -155,7 +155,7 @@ AppController.prototype.formatBlockLibraryForExport_ = function(blockXmlMap) {
* @private
*/
AppController.prototype.formatBlockLibraryForImport_ = function(xmlText) {
var inputXml = Blockly.Xml.textToDom(xmlText);
var inputXml = Blockly.utils.xml.textToDom(xmlText);
// Convert the live HTMLCollection of child Elements into a static array,
// since the addition to editorWorkspaceXml below removes it from inputXml.
var inputChildren = Array.from(inputXml.children);
Expand Down Expand Up @@ -192,7 +192,7 @@ AppController.prototype.formatBlockLibraryForImport_ = function(xmlText) {
* @private
*/
AppController.prototype.getBlockTypeFromXml_ = function(xmlText) {
var xmlDom = Blockly.Xml.textToDom(xmlText);
var xmlDom = Blockly.utils.xml.textToDom(xmlText);
// Find factory base block.
var factoryBaseBlockXml = xmlDom.getElementsByTagName('block')[0];
// Get field elements from factory base.
Expand Down
2 changes: 1 addition & 1 deletion demos/blockfactory/block_library_storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ BlockLibraryStorage.prototype.removeBlock = function(blockType) {
BlockLibraryStorage.prototype.getBlockXml = function(blockType) {
var xml = this.blocks[blockType] || null;
if (xml) {
var xml = Blockly.Xml.textToDom(xml);
var xml = Blockly.utils.xml.textToDom(xml);
}
return xml;
};
Expand Down
2 changes: 1 addition & 1 deletion demos/blockfactory/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ BlockFactory.disableEnableLink = function() {
*/
BlockFactory.showStarterBlock = function() {
BlockFactory.mainWorkspace.clear();
var xml = Blockly.Xml.textToDom(BlockFactory.STARTER_BLOCK_XML_TEXT);
var xml = Blockly.utils.xml.textToDom(BlockFactory.STARTER_BLOCK_XML_TEXT);
Blockly.Xml.domToWorkspace(xml, BlockFactory.mainWorkspace);
};

Expand Down
Loading