xmlapi-libxmljs
Version:
libxmljs implementation of xmlapi
130 lines (129 loc) • 4.3 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
const libxmljs_node_1 = require("./libxmljs_node");
const libxmljs_attribute_1 = require("./libxmljs_attribute");
const utils_1 = require("./utils");
const xmlapi_1 = require("xmlapi");
const utils_2 = require("xmlapi/utils");
let LibxmljsElement = LibxmljsElement_1 = class LibxmljsElement extends libxmljs_node_1.LibxmljsNode {
constructor(wrapee) {
super(wrapee);
}
native() {
return this.wrapee;
}
namespaceUri() {
let ns = this.wrapee.namespace();
if (ns) {
return ns.href();
}
return null;
}
namespacePrefix() {
let ns = this.wrapee.namespace();
if (ns) {
return ns.prefix();
}
return null;
}
localName() {
return this.wrapee.name();
}
prefixedName() {
let ns = this.wrapee.namespace();
if (ns && ns.prefix()) {
return ns.prefix() + ':' + this.localName();
}
return this.localName();
}
firstChild() {
return this.child(0);
}
child(index) {
return utils_1.nodeOrElementOrNull(this.wrapee.child(index));
}
// todo: make it constant-time in libxmljs!!
// see https://github.com/libxmljs/libxmljs/issues/420
lastChild() {
let children = this.wrapee.childNodes();
return utils_1.nodeOrElementOrNull(children[children.length - 1]);
}
lastElementChild() {
let children = this.wrapee.childNodes();
for (let i = children.length - 1; i >= 0; --i) {
if (children[i].type() === 'element') {
return new LibxmljsElement_1(children[i]);
}
}
}
hasAttributes() {
return !!this.wrapee.attrs().length; // todo: try constant time
}
attributes() {
return this.wrapee.attrs().map(x => new libxmljs_attribute_1.LibxmljsAttribute(x));
}
attribute(name) {
let attr = this.wrapee.attr(name);
return attr === null ? null : attr.value();
}
attributeNs(nsUri, localName) {
let attr = this.wrapee.attrs().find(x => x.namespace() && x.namespace().href() === nsUri && x.name() === localName);
if (attr) {
return attr.value();
}
return null;
}
setAttribute(name, value) {
if (value === undefined) {
this.removeAttribute(name);
}
else {
this.wrapee.attr({ [name]: value.toString() });
}
return this; // todo
}
renameAttributeIfExists(nameOld, nameNew) {
let attr = this.wrapee.attr(nameOld);
if (attr) {
this.wrapee.attr({ [nameNew]: attr.value() });
attr.remove();
}
}
removeAttribute(name) {
let attr = this.wrapee.attr(name);
if (attr) {
attr.remove();
}
}
appendChild(child) {
this.wrapee.addChild(child.native()); // see http://stackoverflow.com/a/13723325/5271870
return child;
}
buildNsMap() {
let ret = {};
// for (let el of merge(this, this.walkAncestors())) {
// let wrapee = (el as LibxmlElement).wrapee;
// for (let ns of wrapee.namespaces()) {
// let prefix = ns.prefix() || '';
// if (!(prefix in ret)) {
// ret[prefix] = ns.href();
// }
// }
// }
return ret;
}
clone() {
return super.clone();
}
};
LibxmljsElement = LibxmljsElement_1 = __decorate([
utils_2.mixin(xmlapi_1.AbstractElement)
], LibxmljsElement);
exports.LibxmljsElement = LibxmljsElement;
var LibxmljsElement_1;