Files
blockly/core/utils/xml.ts
Maribeth Bottorff 037eb59b89 chore: Lint TsDoc. (#6353)
* chore: add linting for tsdoc

* chore: don't require types on return

* chore: remove redundant fileoverview from ts

* chore: change return to returns and add some newlines

* chore: remove license tag

* chore: don't require params/return docs

* chore: remove spurious struct tags

* Revert "chore: change return to returns and add some newlines"

This reverts commit d6d8656a45.

* chore: don't auto-add param names

* chore: disable require-param bc it breaks on this

* return to returns and add line breaks

* chore: configure additional jsdoc rules

* chore: run format

* Revert "chore: remove license tag"

This reverts commit 173455588a.

* chore: allow license tag format

* chore: only require jsdoc on exported items

* chore: add missing jsdoc or silence where needed

* chore: run format

* chore: lint fixes
2022-08-23 14:27:22 -07:00

100 lines
2.4 KiB
TypeScript

/**
* @license
* Copyright 2018 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* 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.
*
* @returns 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.
* @returns 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.
* @returns 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.
* @returns 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.
* @returns Text representation.
* @alias Blockly.utils.xml.domToText
*/
export function domToText(dom: Node): string {
const oSerializer = new XMLSerializer();
return oSerializer.serializeToString(dom);
}