UNPKG

docxml

Version:

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

99 lines (98 loc) 3.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Row = exports.createNodeFromRow = exports.parsePropsAndChildNodes = void 0; // 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. require("./Cell.js"); const Component_js_1 = require("../classes/Component.js"); const table_row_properties_js_1 = require("../properties/table-row-properties.js"); const components_js_1 = require("../utilities/components.js"); const dom_js_1 = require("../utilities/dom.js"); const namespaces_js_1 = require("../utilities/namespaces.js"); const xquery_js_1 = require("../utilities/xquery.js"); const Table_js_1 = require("./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. */ function parsePropsAndChildNodes(node) { return { ...(0, table_row_properties_js_1.tableRowPropertiesFromNode)((0, xquery_js_1.evaluateXPathToFirstNode)(`./${namespaces_js_1.QNS.w}trPr`, node)), children: (0, xquery_js_1.evaluateXPathToNodes)(`./${namespaces_js_1.QNS.w}tc[ not(./${namespaces_js_1.QNS.w}tcPr/${namespaces_js_1.QNS.w}vMerge/@${namespaces_js_1.QNS.w}val = "continue") ]`, node), }; } exports.parsePropsAndChildNodes = parsePropsAndChildNodes; /** * For drying some logic between {@link Row}, {@link RowAddition} and {@link RowDeletion} * * Creates an XML node for a given row. */ async function createNodeFromRow(row, ancestry) { const table = ancestry.find((ancestor) => ancestor instanceof Table_js_1.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 (0, dom_js_1.create)(` element ${namespaces_js_1.QNS.w}tr { $trPr, $children } `, { trPr: (0, table_row_properties_js_1.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); })), }); } exports.createNodeFromRow = createNodeFromRow; /** * A component that represents a table row. */ class Row extends Component_js_1.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 (0, xquery_js_1.evaluateXPathToBoolean)(` self::${namespaces_js_1.QNS.w}tr and not(./${namespaces_js_1.QNS.w}trPr/${namespaces_js_1.QNS.w}ins) and not(./${namespaces_js_1.QNS.w}trPr/${namespaces_js_1.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, ...(0, components_js_1.createChildComponentsFromNodes)(this.children, children, context)); } } exports.Row = Row; Object.defineProperty(Row, "children", { enumerable: true, configurable: true, writable: true, value: ['Cell'] }); Object.defineProperty(Row, "mixed", { enumerable: true, configurable: true, writable: true, value: false }); (0, components_js_1.registerComponent)(Row);