UNPKG

docxml

Version:

TypeScript (component) library for building and parsing a DOCX file

64 lines (63 loc) 2.19 kB
// 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 deleted. */ export class TextDeletion extends Component { /** * Creates an XML DOM node for this component instance. */ async toNode(ancestry) { return create(` element ${QNS.w}del { 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:del'; } /** * Instantiate this component from the XML in an existing DOCX file. */ static fromNode(node, context) { const props = getChangeInformation(node); return new TextDeletion(props, ...createChildComponentsFromNodes(this.children, evaluateXPathToNodes(`./${QNS.w}r`, node), context)); } } Object.defineProperty(TextDeletion, "children", { enumerable: true, configurable: true, writable: true, value: [ 'Text', 'TextAddition', // Sometimes deletions nested into themselves work well? At other times, they don't. // For safety, keep it disabled now (or put it behind a flag possibly) // 'TextDeletion', ] }); Object.defineProperty(TextDeletion, "mixed", { enumerable: true, configurable: true, writable: true, value: false }); registerComponent(TextDeletion);