xmlapi-libxmljs
Version:
libxmljs implementation of xmlapi
152 lines (151 loc) • 5.01 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const libxmljs_document_1 = require("./libxmljs_document");
const libxmljs_element_1 = require("./libxmljs_element");
const libxmljs_attribute_1 = require("./libxmljs_attribute");
const utils_1 = require("./utils");
const xmlapi_1 = require("xmlapi");
const utils_2 = require("xmlapi/utils");
const wu = require('wu');
class LibxmljsNode extends xmlapi_1.AbstractNode {
constructor(wrapee) {
super();
this.wrapee = wrapee;
}
native() {
return this.wrapee;
}
document() {
return utils_2.wrappedOrNull(libxmljs_document_1.LibxmljsDocument, this.wrapee.doc());
}
parent() {
if (this.isRoot()) {
return null;
}
return new libxmljs_element_1.LibxmljsElement(this.wrapee.parent());
}
previousSibling() {
return utils_1.nodeOrElementOrNull(this.wrapee.prevSibling());
}
nextSibling() {
return utils_1.nodeOrElementOrNull(this.wrapee.nextSibling());
}
previousElementSibling() {
return utils_2.wrappedOrNull(libxmljs_element_1.LibxmljsElement, this.wrapee.prevElement());
}
nextElementSibling() {
return utils_2.wrappedOrNull(libxmljs_element_1.LibxmljsElement, this.wrapee.nextElement());
}
text(value) {
if (value !== undefined) {
this.wrapee.text(value);
}
else {
return this.wrapee.text();
}
}
type() {
return this.wrapee.type();
}
isSame(other) {
return !utils_2.isOddball(other) && other.wrapee === this.wrapee;
}
getPath() {
return this.wrapee.path();
}
remove() {
this.wrapee.remove();
return this;
}
replace(replacement) {
this.wrapee.replace(replacement.wrapee);
}
insertBefore(newNode) {
this.wrapee.addPrevSibling(newNode.wrapee);
}
insertAfter(newNode) {
this.wrapee.addNextSibling(newNode.wrapee);
}
evaluate(xpath, nsMap) {
let result = this.wrapee.find(xpath, nsMap);
if (Array.isArray(result)) {
return wu((function* () {
for (let node of result) {
yield utils_1.nodeOrElementOrAttribute(node);
}
})());
}
if (typeof result === 'boolean' || typeof result === 'number' || typeof result === 'string') {
return result;
}
throw new Error('Unexpected XPath result');
}
evaluateNode(xpath, nsMap) {
let result = this.wrapee.get(xpath, nsMap);
if (!result) {
return null;
}
if (typeof result !== 'object' || !utils_1.isNode(result)) {
throw new Error('XPath result is not a node');
}
return utils_1.nodeOrElement(result);
}
evaluateElement(xpath, nsMap) {
let result = this.wrapee.get(xpath, nsMap);
if (!result) {
return null;
}
if (typeof result !== 'object' || result.type() !== 'element') {
throw new Error('XPath result is not an element');
}
return new libxmljs_element_1.LibxmljsElement(result);
}
evaluateAttribute(xpath, nsMap) {
let result = this.wrapee.get(xpath, nsMap);
if (!result) {
return null;
}
if (typeof result !== 'object' || result.type() !== 'attribute') {
throw new Error('XPath result is not an attribute');
}
return new libxmljs_attribute_1.LibxmljsAttribute(result);
}
evaluateNodes(xpath, nsMap) {
return this._evaluateMany(xpath, nsMap).map(x => {
if (!utils_1.isNode(x)) {
throw new Error('XPath result is not a list of nodes');
}
return utils_1.nodeOrElement(x);
}); // todo: why not inferred?
}
evaluateElements(xpath, nsMap) {
return this._evaluateMany(xpath, nsMap).map(x => {
if (x.type() !== 'element') {
throw new Error('XPath result is not a list of elements');
}
return new libxmljs_element_1.LibxmljsElement(x);
}); // todo: why not inferred?
}
evaluateAttributes(xpath, nsMap) {
return this._evaluateMany(xpath, nsMap).map(x => {
if (x.type() !== 'attribute') {
throw new Error('XPath result is not a list of attributes');
}
return new libxmljs_attribute_1.LibxmljsAttribute(x);
}); // todo: why not inferred?
}
clone() {
return utils_1.nodeOrElement(this.wrapee.clone());
}
serialize() {
return this.wrapee.toString(true);
}
_evaluateMany(xpath, nsMap) {
let result = this.wrapee.find(xpath, nsMap) || [];
if (!Array.isArray(result)) {
throw new Error('XPath result is not a list');
}
return wu(result);
}
}
exports.LibxmljsNode = LibxmljsNode;