Rework to register Document and add XMLSerializer

This commit is contained in:
Björn Harrtell
2019-11-13 22:54:56 +01:00
parent fc46166821
commit bdc20e0293
3 changed files with 34 additions and 15 deletions

View File

@@ -123,31 +123,26 @@ export function replaceChildren(node, children) {
} }
} }
let domImplementation_ = undefined; let document_ = undefined;
/** /**
* Register an external DOMImplementation. Can be used to supply a DOMImplementation * Register a Document to use when creating nodes for XML serializations. Can be used
* in non-browser environments. * to inject a Document where there is no globally available implementation.
* *
* @param {DOMImplementation} domImplementation A DOMImplementation. * @param {Document} document A Document.
* @api * @api
*/ */
export function registerDOMImplementation(domImplementation) { export function registerDocument(document) {
domImplementation_ = domImplementation; document_ = document;
} }
let document_ = undefined;
/** /**
* Get a document that should be used when creating nodes for XML serializations. * Get a document that should be used when creating nodes for XML serializations.
* @return {Document} The document. * @return {Document} The document.
*/ */
export function getDocument() { export function getDocument() {
if (document_ === undefined) { if (document_ === undefined && typeof document !== 'undefined') {
if (!domImplementation_ && typeof document !== 'undefined') { document_ = document.implementation.createDocument('', '', null);
domImplementation_ = document.implementation;
}
document_ = domImplementation_.createDocument('', '', null);
} }
return document_; return document_;
} }

View File

@@ -5,7 +5,7 @@ import {abstract} from '../util.js';
import {extend} from '../array.js'; import {extend} from '../array.js';
import FeatureFormat from '../format/Feature.js'; import FeatureFormat from '../format/Feature.js';
import FormatType from '../format/FormatType.js'; import FormatType from '../format/FormatType.js';
import {isDocument, parse} from '../xml.js'; import {isDocument, parse, getXMLSerializer} from '../xml.js';
/** /**
* @classdesc * @classdesc
@@ -23,7 +23,7 @@ class XMLFeature extends FeatureFormat {
* @type {XMLSerializer} * @type {XMLSerializer}
* @private * @private
*/ */
this.xmlSerializer_ = new XMLSerializer(); this.xmlSerializer_ = getXMLSerializer();
} }
/** /**

View File

@@ -486,3 +486,27 @@ export function pushSerializeAndPop(object, serializersNS, nodeFactory, values,
serialize(serializersNS, nodeFactory, values, objectStack, opt_keys, opt_this); serialize(serializersNS, nodeFactory, values, objectStack, opt_keys, opt_this);
return /** @type {O|undefined} */ (objectStack.pop()); 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;
}
/**
* Get a document that should be used when creating nodes for XML serializations.
* @return {XMLSerializer} The XMLSerializer.
*/
export function getXMLSerializer() {
if (xmlSerializer_ === undefined && typeof XMLSerializer !== 'undefined') {
xmlSerializer_ = new XMLSerializer();
}
return xmlSerializer_;
}