xmlapi-web
Version:
browser implementation of xmlapi
58 lines (57 loc) • 2.29 kB
JavaScript
;
const webapi_element_1 = require('./webapi_element');
const webapi_node_1 = require('./webapi_node');
const xmlapi_1 = require('xmlapi');
const utils_1 = require('xmlapi/utils');
class WebapiDocument extends xmlapi_1.AbstractDocument {
constructor(wrapee) {
super();
this.wrapee = wrapee;
}
static parse(xmlString) {
let nativeDocument = WebapiDocument.getParser().parseFromString(xmlString, 'application/xml');
if (considerIsParseErrorDocument(nativeDocument)) {
throw new Error('XML Parse error\n\n'
+ WebapiDocument.getSerializer().serializeToString(nativeDocument));
}
return new WebapiDocument(nativeDocument);
}
static getParser() {
return WebapiDocument.parser || (WebapiDocument.parser = new DOMParser());
}
static getSerializer() {
return WebapiDocument.serializer || (WebapiDocument.serializer = new XMLSerializer());
}
get native() {
return this.wrapee;
}
get root() {
return utils_1.wrappedOrNull(webapi_element_1.WebapiElement, this.wrapee.documentElement);
}
equals(other) {
return !utils_1.isOddball(other) && other.wrapee === this.wrapee;
}
createElement(name) {
let [, prefix] = name.split(':').reverse();
let uri = this.wrapee.lookupNamespaceURI(prefix || null);
let elem = this.wrapee.createElementNS(uri, name);
return new webapi_element_1.WebapiElement(elem);
}
createTextNode(value) {
return new webapi_node_1.WebapiNode(this.wrapee.createTextNode(value));
}
serialize() {
return WebapiDocument.getSerializer().serializeToString(this.wrapee);
}
}
exports.WebapiDocument = WebapiDocument;
//------------------------------------------------------------------------------
function considerIsParseErrorDocument(document) {
const NS_XHTML = 'http://www.w3.org/1999/xhtml';
const NS_MOZILLA_ERROR = 'http://www.mozilla.org/newlayout/xml/parsererror.xml';
if (document.getElementsByTagNameNS(NS_XHTML, 'parsererror').length
|| document.getElementsByTagNameNS(NS_MOZILLA_ERROR, 'parsererror').length) {
return true;
}
}
exports.considerIsParseErrorDocument = considerIsParseErrorDocument;