docxml
Version:
TypeScript (component) library for building and parsing a DOCX file
109 lines (108 loc) • 4.21 kB
JavaScript
;
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _UnhandledXmlFile_xml;
Object.defineProperty(exports, "__esModule", { value: true });
exports.XmlFileWithContentTypes = exports.UnhandledXmlFile = exports.XmlFile = void 0;
const enums_js_1 = require("../enums.js");
const dom_js_1 = require("../utilities/dom.js");
class XmlFileBase {
constructor(location) {
Object.defineProperty(this, "location", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.location = location;
}
get contentType() {
return this.constructor.contentType;
}
/**
* Create a (slimdom) Document DOM for this XML file. This is useful for serializing it to string
* and writing to a ZIP/DOCX archive later.
*/
toNode() {
throw new Error(`${this.constructor.name}#toNode() is not implemented`);
}
/**
* @deprecated FOR TEST PURPOSES ONLY
*/
$$$toNode() {
return this.toNode();
}
/**
* Get all XmlFile instances related to this one, including self. This helps the system
* serialize itself back to DOCX fullly. Probably not useful for consumers of the library.
*
* By default only returns the instance itself but no other related instances.
*/
getRelated() {
return [this];
}
/**
* Let a file tell the system when it is effectively empty, so it can be omitted from the archive.
*/
isEmpty() {
return false;
}
/**
* Add all related files to the given archive.
*/
async addToArchive(archive) {
await Promise.all(this.getRelated().map(async (related) => {
if (related instanceof XmlFileBase) {
archive.addXmlFile(related.location, await related.toNode());
}
else {
related.addToArchive(archive);
}
}));
}
}
Object.defineProperty(XmlFileBase, "contentType", {
enumerable: true,
configurable: true,
writable: true,
value: enums_js_1.FileMime.xml
});
class XmlFile extends XmlFileBase {
/**
* Promise a new JS instance of this file based on the given archive.
*/
static fromArchive(_archive, location) {
return Promise.resolve(new XmlFile(location));
}
}
exports.XmlFile = XmlFile;
class UnhandledXmlFile extends XmlFile {
constructor(location, xml) {
super(location);
_UnhandledXmlFile_xml.set(this, void 0);
__classPrivateFieldSet(this, _UnhandledXmlFile_xml, xml, "f");
}
toNode() {
return (0, dom_js_1.parse)(__classPrivateFieldGet(this, _UnhandledXmlFile_xml, "f"));
}
static async fromArchive(archive, location) {
return new UnhandledXmlFile(location, await archive.readText(location));
}
}
exports.UnhandledXmlFile = UnhandledXmlFile;
_UnhandledXmlFile_xml = new WeakMap();
class XmlFileWithContentTypes extends XmlFileBase {
static fromArchive(_archive, _contentTypes, location) {
return Promise.resolve(new XmlFile(location));
}
}
exports.XmlFileWithContentTypes = XmlFileWithContentTypes;