epubavocado
Version:
I am an EPUB object model aspiring to be standards compliant.
41 lines (40 loc) • 1.13 kB
JavaScript
import { select, selectAll } from '../../xpath.js';
export class Entity {
constructor(node, context) {
this._node = node;
this._context = context || this;
}
_select(expression) {
return select(expression, this._node);
}
_selectAll(expression) {
return selectAll(expression, this._node);
}
_resolve(expression, constructor) {
const node = this._select(expression);
if (!node) {
return null;
}
if (constructor) {
return new constructor(node, this._context);
}
const attrType = node;
const nodeType = node;
if (attrType.value) {
return attrType.value;
}
if (nodeType.nodeValue) {
return nodeType.nodeValue;
}
}
_resolveAll(expression, constructor) {
const nodes = this._selectAll(expression);
if (!nodes) {
return [];
}
if (constructor) {
return nodes.map((node) => new constructor(node, this._context));
}
return nodes.map((node) => node.nodeValue);
}
}