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
* in non-browser environments.
* 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 {DOMImplementation} domImplementation A DOMImplementation.
* @param {Document} document A Document.
* @api
*/
export function registerDOMImplementation(domImplementation) {
domImplementation_ = domImplementation;
export function registerDocument(document) {
document_ = 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);
if (document_ === undefined && typeof document !== 'undefined') {
document_ = document.implementation.createDocument('', '', null);
}
return document_;
}

View File

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

View File

@@ -486,3 +486,27 @@ 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;
}
/**
* 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_;
}