diff --git a/src/ol/format/XMLFeature.js b/src/ol/format/XMLFeature.js index b0bed3b402..0f413e21c3 100644 --- a/src/ol/format/XMLFeature.js +++ b/src/ol/format/XMLFeature.js @@ -5,7 +5,7 @@ import {abstract} from '../util.js'; import {extend} from '../array.js'; import FeatureFormat from '../format/Feature.js'; import FormatType from '../format/FormatType.js'; -import {isDocument, parse} from '../xml.js'; +import {isDocument, parse, getXMLSerializer} from '../xml.js'; /** * @classdesc @@ -23,7 +23,7 @@ class XMLFeature extends FeatureFormat { * @type {XMLSerializer} * @private */ - this.xmlSerializer_ = new XMLSerializer(); + this.xmlSerializer_ = getXMLSerializer(); } /** diff --git a/src/ol/format/xsd.js b/src/ol/format/xsd.js index 5e7f8436e9..271b590679 100644 --- a/src/ol/format/xsd.js +++ b/src/ol/format/xsd.js @@ -1,7 +1,7 @@ /** * @module ol/format/xsd */ -import {getAllTextContent, DOCUMENT} from '../xml.js'; +import {getAllTextContent, getDocument} from '../xml.js'; import {padNumber} from '../string.js'; @@ -112,7 +112,7 @@ export function writeBooleanTextNode(node, bool) { * @param {string} string String. */ export function writeCDATASection(node, string) { - node.appendChild(DOCUMENT.createCDATASection(string)); + node.appendChild(getDocument().createCDATASection(string)); } @@ -128,7 +128,7 @@ export function writeDateTimeTextNode(node, dateTime) { padNumber(date.getUTCHours(), 2) + ':' + padNumber(date.getUTCMinutes(), 2) + ':' + padNumber(date.getUTCSeconds(), 2) + 'Z'; - node.appendChild(DOCUMENT.createTextNode(string)); + node.appendChild(getDocument().createTextNode(string)); } @@ -138,7 +138,7 @@ export function writeDateTimeTextNode(node, dateTime) { */ export function writeDecimalTextNode(node, decimal) { const string = decimal.toPrecision(); - node.appendChild(DOCUMENT.createTextNode(string)); + node.appendChild(getDocument().createTextNode(string)); } @@ -148,7 +148,7 @@ export function writeDecimalTextNode(node, decimal) { */ export function writeNonNegativeIntegerTextNode(node, nonNegativeInteger) { const string = nonNegativeInteger.toString(); - node.appendChild(DOCUMENT.createTextNode(string)); + node.appendChild(getDocument().createTextNode(string)); } @@ -157,5 +157,5 @@ export function writeNonNegativeIntegerTextNode(node, nonNegativeInteger) { * @param {string} string String. */ export function writeStringTextNode(node, string) { - node.appendChild(DOCUMENT.createTextNode(string)); + node.appendChild(getDocument().createTextNode(string)); } diff --git a/src/ol/xml.js b/src/ol/xml.js index 0f47f57a4a..276a9c4482 100644 --- a/src/ol/xml.js +++ b/src/ol/xml.js @@ -23,15 +23,6 @@ import {extend} from './array.js'; */ -/** - * This document should be used when creating nodes for XML serializations. This - * document is also used by {@link module:ol/xml~createElementNS} - * @const - * @type {Document} - */ -export const DOCUMENT = document.implementation.createDocument('', '', null); - - /** * @type {string} */ @@ -44,7 +35,7 @@ export const XML_SCHEMA_INSTANCE_URI = 'http://www.w3.org/2001/XMLSchema-instanc * @return {Element} Node. */ export function createElementNS(namespaceURI, qualifiedName) { - return DOCUMENT.createElementNS(namespaceURI, qualifiedName); + return getDocument().createElementNS(namespaceURI, qualifiedName); } @@ -494,3 +485,51 @@ export function pushSerializeAndPop(object, serializersNS, nodeFactory, values, serialize(serializersNS, nodeFactory, values, objectStack, opt_keys, opt_this); return /** @type {O|undefined} */ (objectStack.pop()); } + +let xmlSerializer_ = undefined; + +/** + * Register a XMLSerializer. Can be used to inject a XMLSerializer + * where there is no globally available implementation. + * + * @param {XMLSerializer} xmlSerializer A XMLSerializer. + * @api + */ +export function registerXMLSerializer(xmlSerializer) { + xmlSerializer_ = xmlSerializer; +} + +/** + * @return {XMLSerializer} The XMLSerializer. + */ +export function getXMLSerializer() { + if (xmlSerializer_ === undefined && typeof XMLSerializer !== 'undefined') { + xmlSerializer_ = new XMLSerializer(); + } + return xmlSerializer_; +} + + +let document_ = undefined; + +/** + * Register a Document to use when creating nodes for XML serializations. Can be used + * to inject a Document where there is no globally available implementation. + * + * @param {Document} document A Document. + * @api + */ +export function registerDocument(document) { + document_ = document; +} + +/** + * Get a document that should be used when creating nodes for XML serializations. + * @return {Document} The document. + */ +export function getDocument() { + if (document_ === undefined && typeof document !== 'undefined') { + document_ = document.implementation.createDocument('', '', null); + } + return document_; +}