UNPKG

epubavocado

Version:

I am an EPUB object model aspiring to be standards compliant.

587 lines (586 loc) 24.7 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Package = exports.Metadata = exports.Link = exports.BelongsToCollection = exports.Type = exports.Subject = exports.Source = exports.Rights = exports.Relation = exports.Publisher = exports.Format = exports.Description = exports.Date = exports.Creator = exports.Coverage = exports.Contributor = exports.Language = exports.Title = exports.Identifier = exports.Meta = exports.Spine = exports.SpineItem = exports.Manifest = exports.ManifestItem = void 0; var entity_js_1 = require("./mixins/entity.js"); var i18n_js_1 = require("./package/mixins/i18n.js"); var id_js_1 = require("./package/mixins/id.js"); var properties_js_1 = require("./package/mixins/properties.js"); var resource_js_1 = require("./mixins/resource.js"); var value_js_1 = require("./package/mixins/value.js"); var util_js_1 = require("../util.js"); var xpath_js_1 = require("../xpath.js"); var manifest_item_js_1 = require("./package/manifest-item.js"); Object.defineProperty(exports, "ManifestItem", { enumerable: true, get: function () { return manifest_item_js_1.ManifestItem; } }); var manifest_js_1 = require("./package/manifest.js"); Object.defineProperty(exports, "Manifest", { enumerable: true, get: function () { return manifest_js_1.Manifest; } }); var spine_item_js_1 = require("./package/spine-item.js"); Object.defineProperty(exports, "SpineItem", { enumerable: true, get: function () { return spine_item_js_1.SpineItem; } }); var spine_js_1 = require("./package/spine.js"); Object.defineProperty(exports, "Spine", { enumerable: true, get: function () { return spine_js_1.Spine; } }); var util_js_2 = require("./package/util.js"); var nodeTypeMap = function () { return ({ 'opf:package': Package, 'opf:metadata': Metadata, 'opf:manifest': manifest_js_1.Manifest, 'opf:spine': spine_js_1.Spine, 'opf:meta': Meta, 'opf:item': manifest_item_js_1.ManifestItem, 'opf:itemref': spine_item_js_1.SpineItem, 'opf:link': Link, // 'opf:collection': Collection, 'dc:identifier': Identifier, 'dc:title': Title, 'dc:language': Language, 'dc:contributor': Contributor, 'dc:coverage': Coverage, 'dc:creator': Creator, 'dc:date': Date, 'dc:description': Description, 'dc:format': Format, 'dc:publisher': Publisher, 'dc:relation': Relation, 'dc:rights': Rights, 'dc:source': Source, 'dc:subject': Subject, 'dc:type': Type, }); }; function MetaProperties(Base) { return /** @class */ (function (_super) { __extends(MetaProperties, _super); function MetaProperties() { return _super !== null && _super.apply(this, arguments) || this; } MetaProperties.prototype._resolveMetaProperty = function (property, constructor) { if (constructor === void 0) { constructor = Meta; } return util_js_1.toArray(this._resolveMetaPropertyList(property, constructor))[0]; }; MetaProperties.prototype._resolveMetaPropertyList = function (property, constructor) { var _this = this; var _a; if (constructor === void 0) { constructor = Meta; } var id = this.id(); if (!id) { return []; } var propertyMap = (_a = this._context.metadata()) === null || _a === void 0 ? void 0 : _a._metaPropertyMap[id]; if (!propertyMap) { return []; } var metaNodes = propertyMap[property]; if (!metaNodes) { return []; } return metaNodes.map(function (node) { return new constructor(node, _this._context); }); }; MetaProperties.prototype.alternateScript = function () { return this.alternateScripts()[0]; }; MetaProperties.prototype.alternateScripts = function () { return this._resolveMetaPropertyList('alternate-script'); }; MetaProperties.prototype.displaySeq = function () { return this._resolveMetaProperty('display-seq'); }; MetaProperties.prototype.fileAs = function () { return this._resolveMetaProperty('file-as'); }; MetaProperties.prototype.groupPosition = function () { return this._resolveMetaProperty('group-position'); }; MetaProperties.prototype.metaAuth = function () { return this._resolveMetaProperty('meta-auth'); }; return MetaProperties; }(Base)); } function MetaAttributes(Base) { return /** @class */ (function (_super) { __extends(MetaAttributes, _super); function MetaAttributes() { return _super !== null && _super.apply(this, arguments) || this; } MetaAttributes.prototype.property = function () { return this._resolve('./@property'); }; MetaAttributes.prototype.scheme = function () { return this._resolve('./@scheme'); }; return MetaAttributes; }(Base)); } function Refines(Base) { return /** @class */ (function (_super) { __extends(Refines, _super); function Refines() { return _super !== null && _super.apply(this, arguments) || this; } Refines.prototype.refines = function () { var refines = this._resolve('./@refines'); if (!refines) { return null; } // drop the # prefix var idRefined = refines[0] === '#' ? refines.substr(1) : refines; var node = this._context._select("//*[@id='" + idRefined + "']"); if (!node) { return null; } var name = node.localName; var namespace = node.namespaceURI; if (!namespace) { return null; } var prefix = xpath_js_1.prefixMap[namespace]; var typeConstructor = nodeTypeMap()[prefix + ":" + name]; if (!typeConstructor) { return null; } return new typeConstructor(node, this._context); }; return Refines; }(Base)); } var Meta = /** @class */ (function (_super) { __extends(Meta, _super); function Meta() { return _super !== null && _super.apply(this, arguments) || this; } return Meta; }(MetaProperties(MetaAttributes(Refines(i18n_js_1.I18n(value_js_1.Value(id_js_1.ID(entity_js_1.Entity)))))))); exports.Meta = Meta; var Identifier = /** @class */ (function (_super) { __extends(Identifier, _super); function Identifier() { return _super !== null && _super.apply(this, arguments) || this; } Identifier.prototype.identifierType = function () { return this._resolveMetaProperty('identifier-type'); }; return Identifier; }(value_js_1.Value(MetaProperties(id_js_1.ID(entity_js_1.Entity))))); exports.Identifier = Identifier; var Title = /** @class */ (function (_super) { __extends(Title, _super); function Title() { return _super !== null && _super.apply(this, arguments) || this; } Title.prototype.titleType = function () { return this._resolveMetaProperty('title-type'); }; return Title; }(value_js_1.Value(i18n_js_1.I18n(MetaProperties(id_js_1.ID(entity_js_1.Entity)))))); exports.Title = Title; var Language = /** @class */ (function (_super) { __extends(Language, _super); function Language() { return _super !== null && _super.apply(this, arguments) || this; } return Language; }(value_js_1.Value(MetaProperties(id_js_1.ID(entity_js_1.Entity))))); exports.Language = Language; var Contributor = /** @class */ (function (_super) { __extends(Contributor, _super); function Contributor() { return _super !== null && _super.apply(this, arguments) || this; } Contributor.prototype.role = function () { return this._resolveMetaProperty('role'); }; return Contributor; }(value_js_1.Value(i18n_js_1.I18n(MetaProperties(id_js_1.ID(entity_js_1.Entity)))))); exports.Contributor = Contributor; var Coverage = /** @class */ (function (_super) { __extends(Coverage, _super); function Coverage() { return _super !== null && _super.apply(this, arguments) || this; } return Coverage; }(value_js_1.Value(i18n_js_1.I18n(MetaProperties(id_js_1.ID(entity_js_1.Entity)))))); exports.Coverage = Coverage; var Creator = /** @class */ (function (_super) { __extends(Creator, _super); function Creator() { return _super !== null && _super.apply(this, arguments) || this; } return Creator; }(Contributor)); exports.Creator = Creator; var Date = /** @class */ (function (_super) { __extends(Date, _super); function Date() { return _super !== null && _super.apply(this, arguments) || this; } return Date; }(value_js_1.Value(MetaProperties(id_js_1.ID(entity_js_1.Entity))))); exports.Date = Date; var Description = /** @class */ (function (_super) { __extends(Description, _super); function Description() { return _super !== null && _super.apply(this, arguments) || this; } return Description; }(value_js_1.Value(i18n_js_1.I18n(MetaProperties(id_js_1.ID(entity_js_1.Entity)))))); exports.Description = Description; var Format = /** @class */ (function (_super) { __extends(Format, _super); function Format() { return _super !== null && _super.apply(this, arguments) || this; } return Format; }(value_js_1.Value(MetaProperties(id_js_1.ID(entity_js_1.Entity))))); exports.Format = Format; var Publisher = /** @class */ (function (_super) { __extends(Publisher, _super); function Publisher() { return _super !== null && _super.apply(this, arguments) || this; } return Publisher; }(value_js_1.Value(i18n_js_1.I18n(MetaProperties(id_js_1.ID(entity_js_1.Entity)))))); exports.Publisher = Publisher; var Relation = /** @class */ (function (_super) { __extends(Relation, _super); function Relation() { return _super !== null && _super.apply(this, arguments) || this; } return Relation; }(value_js_1.Value(i18n_js_1.I18n(MetaProperties(id_js_1.ID(entity_js_1.Entity)))))); exports.Relation = Relation; var Rights = /** @class */ (function (_super) { __extends(Rights, _super); function Rights() { return _super !== null && _super.apply(this, arguments) || this; } return Rights; }(value_js_1.Value(i18n_js_1.I18n(MetaProperties(id_js_1.ID(entity_js_1.Entity)))))); exports.Rights = Rights; var Source = /** @class */ (function (_super) { __extends(Source, _super); function Source() { return _super !== null && _super.apply(this, arguments) || this; } Source.prototype.sourceOf = function () { return this._resolveMetaProperty('source-of'); }; return Source; }(Identifier)); exports.Source = Source; var Subject = /** @class */ (function (_super) { __extends(Subject, _super); function Subject() { return _super !== null && _super.apply(this, arguments) || this; } Subject.prototype.authority = function () { return this._resolveMetaProperty('authority'); }; Subject.prototype.term = function () { return this._resolveMetaProperty('term'); }; return Subject; }(value_js_1.Value(i18n_js_1.I18n(MetaProperties(id_js_1.ID(entity_js_1.Entity)))))); exports.Subject = Subject; var Type = /** @class */ (function (_super) { __extends(Type, _super); function Type() { return _super !== null && _super.apply(this, arguments) || this; } return Type; }(value_js_1.Value(MetaProperties(id_js_1.ID(entity_js_1.Entity))))); exports.Type = Type; var BelongsToCollection = /** @class */ (function (_super) { __extends(BelongsToCollection, _super); function BelongsToCollection() { return _super !== null && _super.apply(this, arguments) || this; } BelongsToCollection.prototype.identifier = function () { return this._resolveMetaProperty('dcterms:identifier'); }; BelongsToCollection.prototype.collectionType = function () { return this._resolveMetaProperty('collection-type'); }; BelongsToCollection.prototype.belongsToCollection = function () { return this.belongsToCollections()[0]; }; BelongsToCollection.prototype.belongsToCollections = function () { return this._resolveMetaPropertyList('belongs-to-collection', BelongsToCollection); }; return BelongsToCollection; }(value_js_1.Value(i18n_js_1.I18n(Refines(MetaAttributes(MetaProperties(id_js_1.ID(entity_js_1.Entity)))))))); exports.BelongsToCollection = BelongsToCollection; var Link = /** @class */ (function (_super) { __extends(Link, _super); function Link() { return _super !== null && _super.apply(this, arguments) || this; } Link.prototype.rel = function () { return this.rels()[0]; }; Link.prototype.rels = function () { var rel = this._resolve('./@rel'); if (rel) { return util_js_1.splitRelAttribute(rel); } return []; }; return Link; }(resource_js_1.Resource(properties_js_1.Properties(Refines(id_js_1.ID(entity_js_1.Entity)))))); exports.Link = Link; var Metadata = /** @class */ (function (_super) { __extends(Metadata, _super); function Metadata(node, context) { var _this = _super.call(this, node, context) || this; _this._metaPropertyMap = {}; var metaRefiningSelected = util_js_1.toArray(_this._selectAll('./opf:meta[@refines and @property]')); metaRefiningSelected.forEach(function (selectedValue) { var node = selectedValue; if (!node) { return; } var refinesAttr = xpath_js_1.select('./@refines', node); if (!refinesAttr) { return; } var refinesValue = refinesAttr.value; // drop the # prefix var idRefined = refinesValue[0] === '#' ? refinesValue.substr(1) : refinesValue; var propertyAttr = xpath_js_1.select('./@property', node); if (!propertyAttr) { return; } var property = propertyAttr.value; if (!_this._metaPropertyMap[idRefined]) { _this._metaPropertyMap[idRefined] = {}; } if (!_this._metaPropertyMap[idRefined][property]) { _this._metaPropertyMap[idRefined][property] = []; } _this._metaPropertyMap[idRefined][property].push(node); }); return _this; } Metadata.prototype.identifier = function (_a) { var id = _a.id; return this._resolve("./dc:identifier" + util_js_2.idFilter(id), Identifier); }; Metadata.prototype.modified = function () { var node = this._select("./opf:meta[@property='dcterms:modified' and not(@refines)]"); if (node) { return new Meta(node, this._context); } }; Metadata.prototype.title = function (_a) { var id = _a.id; return this.titles({ ids: id ? [id] : [] })[0]; }; Metadata.prototype.titles = function (_a) { var ids = _a.ids; return this._resolveAll("./dc:title" + util_js_2.idFilter(ids), Title); }; Metadata.prototype.language = function (_a) { var id = _a.id; return this.languages({ ids: id ? [id] : [] })[0]; }; Metadata.prototype.languages = function (_a) { var ids = _a.ids; return this._resolveAll("./dc:language" + util_js_2.idFilter(ids), Language); }; Metadata.prototype.contributor = function (_a) { var id = _a.id; return this.contributors({ ids: id ? [id] : [] })[0]; }; Metadata.prototype.contributors = function (_a) { var ids = _a.ids; return this._resolveAll("./dc:contributor" + util_js_2.idFilter(ids), Contributor); }; Metadata.prototype.coverage = function (_a) { var id = _a.id; return this.coverages({ ids: id ? [id] : [] })[0]; }; Metadata.prototype.coverages = function (_a) { var ids = _a.ids; return this._resolveAll("./dc:coverage" + util_js_2.idFilter(ids), Coverage); }; Metadata.prototype.creator = function (_a) { var id = _a.id; return this.creators({ ids: id ? [id] : [] })[0]; }; Metadata.prototype.creators = function (_a) { var ids = _a.ids; return this._resolveAll("./dc:creator" + util_js_2.idFilter(ids), Creator); }; Metadata.prototype.date = function (_a) { var id = _a.id; return this._resolve("./dc:date" + util_js_2.idFilter(id), Date); }; Metadata.prototype.description = function (_a) { var id = _a.id; return this.descriptions({ ids: id ? [id] : [] })[0]; }; Metadata.prototype.descriptions = function (_a) { var ids = _a.ids; return this._resolveAll("./dc:description" + util_js_2.idFilter(ids), Description); }; Metadata.prototype.format = function (_a) { var id = _a.id; return this.formats({ ids: id ? [id] : [] })[0]; }; Metadata.prototype.formats = function (_a) { var ids = _a.ids; return this._resolveAll("./dc:format" + util_js_2.idFilter(ids), Format); }; Metadata.prototype.publisher = function (_a) { var id = _a.id; return this.publishers({ ids: id ? [id] : [] })[0]; }; Metadata.prototype.publishers = function (_a) { var ids = _a.ids; return this._resolveAll("./dc:publisher" + util_js_2.idFilter(ids), Publisher); }; Metadata.prototype.relation = function (_a) { var id = _a.id; return this.relations({ ids: id ? [id] : [] })[0]; }; Metadata.prototype.relations = function (_a) { var ids = _a.ids; return this._resolveAll("./dc:relation" + util_js_2.idFilter(ids), Relation); }; Metadata.prototype.rights = function (_a) { var id = _a.id; return this.rightses({ ids: id ? [id] : [] })[0]; }; Metadata.prototype.rightses = function (_a) { var ids = _a.ids; return this._resolveAll("./dc:rights" + util_js_2.idFilter(ids), Rights); }; Metadata.prototype.source = function (_a) { var id = _a.id; return this.sources({ ids: id ? [id] : [] })[0]; }; Metadata.prototype.sources = function (_a) { var ids = _a.ids; return this._resolveAll("./dc:source" + util_js_2.idFilter(ids), Source); }; Metadata.prototype.subject = function (_a) { var id = _a.id; return this.subjects({ ids: id ? [id] : [] })[0]; }; Metadata.prototype.subjects = function (_a) { var ids = _a.ids; return this._resolveAll("./dc:subject" + util_js_2.idFilter(ids), Subject); }; Metadata.prototype.type = function (_a) { var id = _a.id; return this.types({ ids: id ? [id] : [] })[0]; }; Metadata.prototype.types = function (_a) { var ids = _a.ids; return this._resolveAll("./dc:type" + util_js_2.idFilter(ids), Type); }; Metadata.prototype.belongsToCollection = function (_a) { var id = _a.id; return this.belongsToCollections({ ids: id ? [id] : [] })[0]; }; Metadata.prototype.belongsToCollections = function (_a) { var _b = _a === void 0 ? {} : _a, ids = _b.ids; return this._resolveAll("./opf:meta" + util_js_2.idFilter(ids) + "[@property='belongs-to-collection' and not(@refines)]", BelongsToCollection); }; Metadata.prototype.meta = function (_a) { var _b = _a === void 0 ? {} : _a, id = _b.id, property = _b.property, refines = _b.refines; return this.metas({ ids: id ? [id] : [], property: property, refines: refines, })[0]; }; Metadata.prototype.metas = function (_a) { var _b = _a === void 0 ? {} : _a, ids = _b.ids, property = _b.property, refines = _b.refines; return this._resolveAll("./opf:meta" + util_js_2.idFilter(ids) + util_js_2.attributeFilter('@property', property, 'and') + util_js_2.attributeFilter('@refines', refines ? "#" + refines : undefined, 'or'), Meta); }; Metadata.prototype.link = function (_a) { var _b = _a === void 0 ? {} : _a, id = _b.id, href = _b.href, anyProperties = _b.anyProperties, allProperties = _b.allProperties, onlyProperties = _b.onlyProperties, anyRel = _b.anyRel, allRel = _b.allRel, onlyRel = _b.onlyRel; return this.links({ ids: id ? [id] : [], href: href, anyProperties: anyProperties, allProperties: allProperties, onlyProperties: onlyProperties, anyRel: anyRel, allRel: allRel, onlyRel: onlyRel, })[0]; }; Metadata.prototype.links = function (args) { if (args === void 0) { args = {}; } var ids = args.ids, href = args.href, anyProperties = args.anyProperties, allProperties = args.allProperties, onlyProperties = args.onlyProperties, anyRel = args.anyRel, allRel = args.allRel, onlyRel = args.onlyRel; if (onlyProperties) { return this.links(__assign(__assign({}, args), { allProperties: onlyProperties, onlyProperties: undefined })).filter(function (item) { return util_js_1.toArray(item.properties()).length === onlyProperties.length; }); } if (onlyRel) { return this.links(__assign(__assign({}, args), { allRel: onlyRel, onlyRel: undefined })).filter(function (item) { return util_js_1.toArray(item.rels()).length === onlyRel.length; }); } var expression = "./opf:link" + util_js_2.idFilter(ids) + util_js_2.attributeFilter('@href', href) + util_js_2.anyPropertiesFilter(anyProperties) + util_js_2.allPropertiesFilter(allProperties) + util_js_2.anyRelFilter(anyRel) + util_js_2.allRelFilter(allRel); return this._resolveAll(expression, Link); }; return Metadata; }(id_js_1.ID(entity_js_1.Entity))); exports.Metadata = Metadata; var Package = /** @class */ (function (_super) { __extends(Package, _super); function Package(doc) { return _super.call(this, xpath_js_1.select('/opf:package', doc)) || this; } Package.prototype.version = function () { return this._resolve('./@version'); }; Package.prototype.uniqueIdentifier = function () { var _a; var uniqueIdentifierIDRef = this._resolve('./@unique-identifier'); if (uniqueIdentifierIDRef) { return (_a = this.metadata()) === null || _a === void 0 ? void 0 : _a.identifier({ id: uniqueIdentifierIDRef }); } }; Package.prototype.releaseIdentifier = function () { var _a; var uniqueIdentifier = this.uniqueIdentifier(); var modified = (_a = this.metadata()) === null || _a === void 0 ? void 0 : _a.modified(); if (uniqueIdentifier && modified) { return uniqueIdentifier.value() + "@" + modified.value(); } }; Package.prototype.metadata = function () { return (this._metadata = this._metadata || this._resolve('./opf:metadata', Metadata)); }; Package.prototype.spine = function () { return (this._spine = this._spine || this._resolve('./opf:spine', spine_js_1.Spine)); }; Package.prototype.manifest = function () { return (this._manifest = this._manifest || this._resolve('./opf:manifest', manifest_js_1.Manifest)); }; return Package; }(i18n_js_1.I18n(id_js_1.ID(entity_js_1.Entity)))); exports.Package = Package;