UNPKG

docxml

Version:

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

64 lines (63 loc) 1.78 kB
import { Component } from '../classes/Component.js'; import { DocumentXml } from '../files/DocumentXml.js'; import { registerComponent } from '../utilities/components.js'; import { create } from '../utilities/dom.js'; import { QNS } from '../utilities/namespaces.js'; import { evaluateXPathToMap } from '../utilities/xquery.js'; /** * The start of a range associated with a comment. */ export class Comment extends Component { /** * Creates an XML DOM node for this component instance. */ toNode(ancestry) { const doc = ancestry.find((ancestor) => ancestor instanceof DocumentXml); if (!doc || !doc.comments.has(this.props.id)) { throw new Error(`Comment "${this.props.id}" does not exist`); } return create(` element ${QNS.w}r { element ${QNS.w}rPr { element ${QNS.w}rStyle { attribute ${QNS.w}val { "CommentReference" } } }, element ${QNS.w}commentReference { attribute ${QNS.w}id { $id } } } `, { id: this.props.id, }); } /** * Asserts whether or not a given XML node correlates with this component. */ static matchesNode(node) { return node.nodeName === 'w:commentRangeStart'; } /** * Instantiate this component from the XML in an existing DOCX file. */ static fromNode(node) { return new Comment(evaluateXPathToMap(` map { "id": ./@${QNS.w}id/number() } `, node)); } } Object.defineProperty(Comment, "children", { enumerable: true, configurable: true, writable: true, value: [] }); Object.defineProperty(Comment, "mixed", { enumerable: true, configurable: true, writable: true, value: false }); registerComponent(Comment);