UNPKG

docxml

Version:

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

63 lines (62 loc) 2.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Symbol = void 0; const Component_js_1 = require("../classes/Component.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"); /** * A component that represents a special character from a different font. */ class Symbol extends Component_js_1.Component { /** * Creates an XML DOM node for this component instance. */ // eslint-disable-next-line @typescript-eslint/no-unused-vars toNode(_ancestry) { return (0, dom_js_1.create)(` element ${namespaces_js_1.QNS.w}sym { if (exists($font)) then attribute ${namespaces_js_1.QNS.w}font { $font } else (), if (exists($code)) then attribute ${namespaces_js_1.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 } = (0, xquery_js_1.evaluateXPathToMap)(`map { "font": ./@${namespaces_js_1.QNS.w}font/string(), "char": ./@${namespaces_js_1.QNS.w}char/string() }`, node); return new Symbol({ font: font || 'Symbol', code: parseInt(char, 16) || 0, }); } } exports.Symbol = Symbol; Object.defineProperty(Symbol, "children", { enumerable: true, configurable: true, writable: true, value: [] }); Object.defineProperty(Symbol, "mixed", { enumerable: true, configurable: true, writable: true, value: false }); (0, components_js_1.registerComponent)(Symbol);