Rework to make DOMImplementation lazy and injectable

This commit is contained in:
Björn Harrtell
2019-11-06 22:24:05 +01:00
parent 6063021792
commit 90d61033e0
3 changed files with 38 additions and 16 deletions

View File

@@ -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_;
}

View File

@@ -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));
}

View File

@@ -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);
}