diff --git a/src/ol/dom.js b/src/ol/dom.js index 900fa6d559..74e3f45f6c 100644 --- a/src/ol/dom.js +++ b/src/ol/dom.js @@ -122,3 +122,32 @@ export function replaceChildren(node, children) { node.insertBefore(newChild, oldChild); } } + +let domImplementation_ = undefined; + +/** + * Register an external DOMImplementation. Can be used to supply a DOMImplementation + * in non-browser environments. + * + * @param {DOMImplementation} domImplementation A DOMImplementation. + * @api + */ +export function registerDOMImplementation(domImplementation) { + domImplementation_ = document; +} + +let document_ = undefined; + +/** + * Get a document that should be used when creating nodes for XML serializations. + * @return {Document} The document. + */ +export function getDocument() { + if (document_ === undefined) { + if (!domImplementation_ && typeof document !== 'undefined') { + domImplementation_ = document.implementation; + } + document_ = domImplementation_.createDocument('', '', null); + } + return document_; +} diff --git a/src/ol/format/xsd.js b/src/ol/format/xsd.js index 5e7f8436e9..0aeb2c5c01 100644 --- a/src/ol/format/xsd.js +++ b/src/ol/format/xsd.js @@ -1,7 +1,8 @@ /** * @module ol/format/xsd */ -import {getAllTextContent, DOCUMENT} from '../xml.js'; +import {getDocument} from '../dom.js'; +import {getAllTextContent} from '../xml.js'; import {padNumber} from '../string.js'; @@ -112,7 +113,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 +129,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 +139,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 +149,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 +158,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..cee71100eb 100644 --- a/src/ol/xml.js +++ b/src/ol/xml.js @@ -2,6 +2,7 @@ * @module ol/xml */ import {extend} from './array.js'; +import {getDocument} from './dom.js'; /** @@ -23,15 +24,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 +36,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); }