UNPKG

docxml

Version:

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

93 lines (92 loc) 3.22 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 './Cell.js'; import { Component, } from '../classes/Component.js'; import { tableRowPropertiesFromNode, tableRowPropertiesToNode, } from '../properties/table-row-properties.js'; import { createChildComponentsFromNodes, registerComponent } from '../utilities/components.js'; import { create } from '../utilities/dom.js'; import { QNS } from '../utilities/namespaces.js'; import { evaluateXPathToBoolean, evaluateXPathToFirstNode, evaluateXPathToNodes, } from '../utilities/xquery.js'; import { Table } from './Table.js'; /** * For drying some logic between {@link Row}, {@link RowAddition} and {@link RowDeletion} * * Parses the children (and no props yet) from an existing XML node. */ export function parsePropsAndChildNodes(node) { return { ...tableRowPropertiesFromNode(evaluateXPathToFirstNode(`./${QNS.w}trPr`, node)), children: evaluateXPathToNodes(`./${QNS.w}tc[ not(./${QNS.w}tcPr/${QNS.w}vMerge/@${QNS.w}val = "continue") ]`, node), }; } /** * For drying some logic between {@link Row}, {@link RowAddition} and {@link RowDeletion} * * Creates an XML node for a given row. */ export async function createNodeFromRow(row, ancestry) { const table = ancestry.find((ancestor) => ancestor instanceof Table); if (!table) { throw new Error('A row cannot be rendered outside the context of a table'); } const y = ancestry[0].children.indexOf(row); const anc = [row, ...ancestry]; return create(` element ${QNS.w}tr { $trPr, $children } `, { trPr: tableRowPropertiesToNode(row.props), children: await Promise.all(table.model.getCellsInRow(y).map((cell, x) => { const info = table.model.getCellInfo(cell); return info.column === x && info.row === y ? cell.toNode(anc) : cell.toRepeatingNode(anc, x, y); })), }); } /** * A component that represents a table row. */ export class Row extends Component { /** * Creates an XML DOM node for this component instance. */ async toNode(ancestry) { const node = await createNodeFromRow(this, ancestry); return node; } /** * Asserts whether or not a given XML node correlates with this component. */ static matchesNode(node) { return evaluateXPathToBoolean(` self::${QNS.w}tr and not(./${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, ...props } = parsePropsAndChildNodes(node); return new Row(props, ...createChildComponentsFromNodes(this.children, children, context)); } } Object.defineProperty(Row, "children", { enumerable: true, configurable: true, writable: true, value: ['Cell'] }); Object.defineProperty(Row, "mixed", { enumerable: true, configurable: true, writable: true, value: false }); registerComponent(Row);