docxml
Version:
TypeScript (component) library for building and parsing a DOCX file
60 lines (59 loc) • 2.01 kB
JavaScript
var _a;
// Import without assignment ensures Deno does not tree-shake this component. To avoid circular
// definitions, components register themselves in a side-effect of their module.
import './Text.js';
import { Component, } from '../classes/Component.js';
import { getChangeInformation } from '../utilities/changes.js';
import { createChildComponentsFromNodes, registerComponent } from '../utilities/components.js';
import { create } from '../utilities/dom.js';
import { QNS } from '../utilities/namespaces.js';
import { evaluateXPathToNodes } from '../utilities/xquery.js';
/**
* A component that represents a change-tracked text that was inserted.
*/
export class TextAddition extends Component {
/**
* Creates an XML DOM node for this component instance.
*/
async toNode(ancestry) {
return create(`
element ${QNS.w}ins {
attribute ${QNS.w}id { $id },
attribute ${QNS.w}author { $author },
attribute ${QNS.w}date { $date },
$children
}
`, {
...this.props,
date: this.props.date.toISOString(),
children: await this.childrenToNode(ancestry),
});
}
/**
* Asserts whether or not a given XML node correlates with this component.
*/
static matchesNode(node) {
return node.nodeName === 'w:ins';
}
/**
* Instantiate this component from the XML in an existing DOCX file.
*/
static fromNode(node, context) {
const props = getChangeInformation(node);
return new TextAddition(props, ...createChildComponentsFromNodes(this.children, evaluateXPathToNodes(`./${QNS.w}r`, node), context));
}
}
_a = TextAddition;
Object.defineProperty(TextAddition, "children", {
enumerable: true,
configurable: true,
writable: true,
value: ['Text', _a.name, 'TextDeletion']
});
Object.defineProperty(TextAddition, "mixed", {
enumerable: true,
configurable: true,
writable: true,
value: false
});
registerComponent(TextAddition);