UNPKG

docxml

Version:

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

74 lines (73 loc) 2.55 kB
/** * @file * Note this file is 99% the same as RowDeletion. Please maintain both accordingly. */ 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 { evaluateXPathToBoolean, evaluateXPathToFirstNode } from '../utilities/xquery.js'; import { createNodeFromRow, parsePropsAndChildNodes, Row } from './Row.js'; /** * A component that represents a change-tracked table row that was deleted. Works the same way as * a normal row, but requires some props describing the change. */ export class RowDeletion extends Component { /** * Creates an XML DOM node for this component instance. */ async toNode(ancestry) { const node = await createNodeFromRow(this, ancestry); let trPr = evaluateXPathToFirstNode(`./${QNS.w}trPr`, node); if (!trPr) { trPr = create(`element ${QNS.w}trPr {}`); node.insertBefore(trPr, node.firstChild); } trPr.insertBefore(create(` element ${QNS.w}del { attribute ${QNS.w}id { $id }, attribute ${QNS.w}author { $author }, attribute ${QNS.w}date { $date } } `, { ...this.props, date: this.props.date.toISOString(), }), null); return node; } /** * Asserts whether or not a given XML node correlates with this component. */ static matchesNode(node) { return evaluateXPathToBoolean(` self::${QNS.w}tr and ./${QNS.w}trPr/${QNS.w}del and not(./${QNS.w}trPr/${QNS.w}ins) `, node); } /** * Instantiate this component from the XML in an existing DOCX file. */ static fromNode(node, context) { const { children, ...rowProps } = parsePropsAndChildNodes(node); const changeProps = getChangeInformation(evaluateXPathToFirstNode(`./${QNS.w}trPr`, node)); return new RowDeletion({ ...rowProps, ...changeProps, }, ...createChildComponentsFromNodes(this.children, children, context)); } } Object.defineProperty(RowDeletion, "children", { enumerable: true, configurable: true, writable: true, value: Row.children }); Object.defineProperty(RowDeletion, "mixed", { enumerable: true, configurable: true, writable: true, value: Row.mixed }); registerComponent(RowDeletion);