mirror of
https://github.com/google/blockly.git
synced 2026-05-09 21:50:12 +02:00
b0475b0c68
* fix: Remove spurious blank lines Remove extraneous blank lines introduced by deletion of 'use strict'; pragmas. Also fix the location of the goog.declareModuleId call in core/utils/array.ts. * fix: Add missing double-blank-line before body of modules Our convention is to have two blank lines between the imports (or module ID, if there are no imports) and the beginning of the body of the module. Enforce this. * fix: one addition format error for PR #6243
98 lines
2.6 KiB
TypeScript
98 lines
2.6 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2018 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* @fileoverview XML element manipulation.
|
|
* These methods are not specific to Blockly, and could be factored out into
|
|
* a JavaScript framework such as Closure.
|
|
*/
|
|
|
|
/**
|
|
* XML element manipulation.
|
|
* These methods are not specific to Blockly, and could be factored out into
|
|
* a JavaScript framework such as Closure.
|
|
* @namespace Blockly.utils.xml
|
|
*/
|
|
import * as goog from '../../closure/goog/goog.js';
|
|
goog.declareModuleId('Blockly.utils.xml');
|
|
|
|
|
|
/**
|
|
* Namespace for Blockly's XML.
|
|
* @alias Blockly.utils.xml.NAME_SPACE
|
|
*/
|
|
export const NAME_SPACE = 'https://developers.google.com/blockly/xml';
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
let xmlDocument: Document = globalThis['document'];
|
|
|
|
/**
|
|
* Get the document object to use for XML serialization.
|
|
* @return The document object.
|
|
* @alias Blockly.utils.xml.getDocument
|
|
*/
|
|
export function getDocument(): Document {
|
|
return xmlDocument;
|
|
}
|
|
|
|
/**
|
|
* Get the document object to use for XML serialization.
|
|
* @param document The document object to use.
|
|
* @alias Blockly.utils.xml.setDocument
|
|
*/
|
|
export function setDocument(document: Document) {
|
|
xmlDocument = document;
|
|
}
|
|
|
|
/**
|
|
* Create DOM element for XML.
|
|
* @param tagName Name of DOM element.
|
|
* @return New DOM element.
|
|
* @alias Blockly.utils.xml.createElement
|
|
*/
|
|
export function createElement(tagName: string): Element {
|
|
return xmlDocument.createElementNS(NAME_SPACE, tagName);
|
|
}
|
|
|
|
/**
|
|
* Create text element for XML.
|
|
* @param text Text content.
|
|
* @return New DOM text node.
|
|
* @alias Blockly.utils.xml.createTextNode
|
|
*/
|
|
export function createTextNode(text: string): Text {
|
|
return xmlDocument.createTextNode(text);
|
|
}
|
|
|
|
/**
|
|
* Converts an XML string into a DOM tree.
|
|
* @param text XML string.
|
|
* @return The DOM document.
|
|
* @throws if XML doesn't parse.
|
|
* @alias Blockly.utils.xml.textToDomDocument
|
|
*/
|
|
export function textToDomDocument(text: string): Document {
|
|
const 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 dom A tree of XML nodes.
|
|
* @return Text representation.
|
|
* @alias Blockly.utils.xml.domToText
|
|
*/
|
|
export function domToText(dom: Node): string {
|
|
const oSerializer = new XMLSerializer();
|
|
return oSerializer.serializeToString(dom);
|
|
}
|