xmlapi-libxmljs
Version:
libxmljs implementation of xmlapi
57 lines (56 loc) • 1.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const xmlapi_1 = require("xmlapi");
const utils_1 = require("xmlapi/utils");
const libxmljs_element_1 = require("./libxmljs_element");
const libxmljs_node_1 = require("./libxmljs_node");
const libxmljs = require('libxmljs');
class LibxmljsDocument extends xmlapi_1.AbstractDocument {
constructor(wrapee) {
super();
this.wrapee = wrapee;
this.wrapee = wrapee || new libxmljs.Document();
}
static parse(xmlString) {
return new LibxmljsDocument(libxmljs.parseXmlString(xmlString));
}
native() {
return this.wrapee;
}
root() {
return new libxmljs_element_1.LibxmljsElement(this.wrapee.root());
}
equals(other) {
return !utils_1.isOddball(other) && other.wrapee === this.wrapee;
}
createElement(name, nsUri) {
let el;
if (nsUri) {
el = new libxmljs.Element(this.wrapee, name);
let ns = this.wrapee.root().namespaces().find(x => x.href() === nsUri); // todo
el.namespace(ns || nsUri);
}
else {
let [localName, prefix] = name.split(':').reverse();
el = new libxmljs.Element(this.wrapee, localName);
if (prefix) {
el.namespace(this.getNsByPrefix(prefix));
}
}
return new libxmljs_element_1.LibxmljsElement(el);
}
createTextNode(value) {
return new libxmljs_node_1.LibxmljsNode(new libxmljs.Text(this.wrapee, value));
}
serialize(pretty = false) {
let ret = this.wrapee.root().toString(pretty);
// if (pretty) {
// ret = prettify(ret);
// }
return ret;
}
getNsByPrefix(prefix) {
return this.wrapee.root().namespaces().find(x => x.prefix() === prefix);
}
}
exports.LibxmljsDocument = LibxmljsDocument;