UNPKG

docxml

Version:

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

59 lines (58 loc) 1.81 kB
import { Component } from '../classes/Component.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'; /** * A component that represents a special character from a different font. */ export class Symbol extends Component { /** * Creates an XML DOM node for this component instance. */ // eslint-disable-next-line @typescript-eslint/no-unused-vars toNode(_ancestry) { return create(` element ${QNS.w}sym { if (exists($font)) then attribute ${QNS.w}font { $font } else (), if (exists($code)) then attribute ${QNS.w}char { $code } else () } `, { font: this.props.font, code: this.props.code.toString(16).padStart(4, '0').toUpperCase(), }); } /** * Asserts whether or not a given XML node correlates with this component. */ static matchesNode(node) { return node.nodeName === 'w:sym'; } /** * Instantiate this component from the XML in an existing DOCX file. */ // eslint-disable-next-line @typescript-eslint/ban-types static fromNode(node) { const { font, char } = evaluateXPathToMap(`map { "font": ./@${QNS.w}font/string(), "char": ./@${QNS.w}char/string() }`, node); return new Symbol({ font: font || 'Symbol', code: parseInt(char, 16) || 0, }); } } Object.defineProperty(Symbol, "children", { enumerable: true, configurable: true, writable: true, value: [] }); Object.defineProperty(Symbol, "mixed", { enumerable: true, configurable: true, writable: true, value: false }); registerComponent(Symbol);