docxml
Version:
TypeScript (component) library for building and parsing a DOCX file
62 lines (61 loc) • 2.16 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.update = exports.create = exports.parse = exports.serialize = void 0;
const fontoxpath_1 = __importDefault(require("fontoxpath"));
const slimdom_1 = require("slimdom");
const xquery_js_1 = require("./xquery.js");
/**
* Serialize an XML node to string using Slimdom's own serializer function, but with the "standard"
* typing that Deno has for Node and Document.
*/
function serialize(node) {
return (0, slimdom_1.serializeToWellFormedString)(node);
}
exports.serialize = serialize;
/**
* Parse an XML string to DOM using Slimdom's own parser function, but with the "standard"
* typing that Deno has for Node and Document -- so that type matching is not complicated further
* down the line.
*/
function parse(xml) {
return (0, slimdom_1.parseXmlDocument)(xml);
}
exports.parse = parse;
/**
* Create a new XML DOM node using XQuery.
*
* For example:
* const el = create(`<derp>{$nerf}</derp>`, { nerf: 'skeet' });
* // Element <derp>skeet</derp>
*/
function create(query, variables = {}, asDocument = false) {
const node = (0, xquery_js_1.evaluateXPathToFirstNode)(query, null, null, variables, {
language: xquery_js_1.XQUERY_3_1_LANGUAGE,
nodesFactory: new slimdom_1.Document(),
});
if (!node) {
throw new Error('Query did not result in a node');
}
if (asDocument) {
const doc = new slimdom_1.Document();
doc.appendChild(node);
return doc;
}
return node;
}
exports.create = create;
/**
* Run an XQuery Update Facility expression, maybe even repeatedly, which can change an existing DOM.
*
* Updates by references, returns an empty promise.
*/
function update(dom, expression, times = 1) {
while (times-- > 0) {
fontoxpath_1.default.executePendingUpdateList(fontoxpath_1.default.evaluateUpdatingExpressionSync(expression, dom, null, {}, { debug: true })
.pendingUpdateList);
}
}
exports.update = update;