docxml
Version:
TypeScript (component) library for building and parsing a DOCX file
74 lines (73 loc) • 2.56 kB
JavaScript
/**
* @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 inserted. Works the same way as
* a normal row, but requires some props describing the change.
*/
export class RowAddition 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}ins {
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}ins and
not(./${QNS.w}trPr/${QNS.w}del)
`, 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 RowAddition({
...rowProps,
...changeProps,
}, ...createChildComponentsFromNodes(this.children, children, context));
}
}
Object.defineProperty(RowAddition, "children", {
enumerable: true,
configurable: true,
writable: true,
value: Row.children
});
Object.defineProperty(RowAddition, "mixed", {
enumerable: true,
configurable: true,
writable: true,
value: Row.mixed
});
registerComponent(RowAddition);