UNPKG

docxml

Version:

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

240 lines (234 loc) 11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.paragraphPropertiesToNode = exports.paragraphPropertiesFromNode = exports.getTwipOrNull = void 0; 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 section_properties_js_1 = require("./section-properties.js"); const text_properties_js_1 = require("./text-properties.js"); function getTwipOrNull(length) { return length && length.twip !== undefined && length.twip !== null ? length.twip : null; } exports.getTwipOrNull = getTwipOrNull; function paragraphPropertiesFromNode(node) { const data = node ? // deno-lint-ignore no-explicit-any (0, xquery_js_1.evaluateXPathToMap)(`map { "alignment": ${namespaces_js_1.QNS.w}jc/@${namespaces_js_1.QNS.w}val/string(), "outlineLvl": ${namespaces_js_1.QNS.w}outlineLvl/@${namespaces_js_1.QNS.w}val/number(), "style": ${namespaces_js_1.QNS.w}pStyle/@${namespaces_js_1.QNS.w}val/string(), "spacing": ${namespaces_js_1.QNS.w}spacing/map { "before": docxml:length(@${namespaces_js_1.QNS.w}before, 'twip'), "after": docxml:length(@${namespaces_js_1.QNS.w}after, 'twip'), "line": docxml:length(@${namespaces_js_1.QNS.w}line, 'twip'), "lineRule": @${namespaces_js_1.QNS.w}lineRule/string(), "afterAutoSpacing": docxml:st-on-off(@${namespaces_js_1.QNS.w}afterAutoSpacing), "beforeAutoSpacing": docxml:st-on-off(@${namespaces_js_1.QNS.w}beforeAutoSpacing) }, "indentation": ${namespaces_js_1.QNS.w}ind/map { "start": docxml:length((@${namespaces_js_1.QNS.w}start|@${namespaces_js_1.QNS.w}left)[1], 'twip'), "startChars": (@${namespaces_js_1.QNS.w}startChars|@${namespaces_js_1.QNS.w}leftChars)[1]/number(), "end": docxml:length((@${namespaces_js_1.QNS.w}end|@${namespaces_js_1.QNS.w}right)[1], 'twip'), "endChars": (@${namespaces_js_1.QNS.w}endChars|@${namespaces_js_1.QNS.w}rightChars)/number(), "hanging": docxml:length(@${namespaces_js_1.QNS.w}hanging, 'twip'), "hangingChars": @${namespaces_js_1.QNS.w}hangingChars/number(), "firstLine": docxml:length(@${namespaces_js_1.QNS.w}firstLine, 'twip'), "firstLineChars": @${namespaces_js_1.QNS.w}firstLineChars/number() }, "shading": ./${namespaces_js_1.QNS.w}shd/docxml:ct-shd(.), "borders": ./${namespaces_js_1.QNS.w}pBdr/map { "top": docxml:ct-border(${namespaces_js_1.QNS.w}top), "left": docxml:ct-border(${namespaces_js_1.QNS.w}left), "bottom": docxml:ct-border(${namespaces_js_1.QNS.w}bottom), "right": docxml:ct-border(${namespaces_js_1.QNS.w}right), "between": docxml:ct-border(${namespaces_js_1.QNS.w}between) }, "listItem": ./${namespaces_js_1.QNS.w}numPr/map { "numbering": ./${namespaces_js_1.QNS.w}numId/@${namespaces_js_1.QNS.w}val/number(), "depth": ./${namespaces_js_1.QNS.w}ilvl/@${namespaces_js_1.QNS.w}val/number() }, "change": ${namespaces_js_1.QNS.w}pPrChange/map { "id": @${namespaces_js_1.QNS.w}id/string(), "author": @${namespaces_js_1.QNS.w}author/string(), "date": @${namespaces_js_1.QNS.w}date/string(), "_node": ./${namespaces_js_1.QNS.w}pPr }, "tabs": ./${namespaces_js_1.QNS.w}tabs/array {${namespaces_js_1.QNS.w}tab/map { "type": @${namespaces_js_1.QNS.w}val/string(), "leader": @${namespaces_js_1.QNS.w}leader/string(), "position": docxml:length(@${namespaces_js_1.QNS.w}pos, 'twip') }} }`, node) || {} : {}; const rpr = node && (0, xquery_js_1.evaluateXPathToFirstNode)(`./${namespaces_js_1.QNS.w}rPr`, node); if (rpr) { data.pilcrow = (0, text_properties_js_1.textPropertiesFromNode)(rpr); } if (data.change) { data.change = { ...data.change, date: new Date(data.change.date), ...paragraphPropertiesFromNode(data.change._node), _node: undefined, }; } else { delete data.change; } return data; } exports.paragraphPropertiesFromNode = paragraphPropertiesFromNode; function paragraphPropertiesToNode(data = {}, sectionProperties = null) { if (!Object.keys(data).length && !sectionProperties) { return null; } return (0, dom_js_1.create)(` element ${namespaces_js_1.QNS.w}pPr { if (exists($style)) then element ${namespaces_js_1.QNS.w}pStyle { attribute ${namespaces_js_1.QNS.w}val { $style } } else (), if (exists($listItem)) then element ${namespaces_js_1.QNS.w}numPr { if (exists($listItem('numbering'))) then element ${namespaces_js_1.QNS.w}numId { attribute ${namespaces_js_1.QNS.w}val { $listItem('numbering') } } else (), if (exists($listItem('depth'))) then element ${namespaces_js_1.QNS.w}ilvl { attribute ${namespaces_js_1.QNS.w}val { $listItem('depth') } } else () } else (), if (exists($alignment)) then element ${namespaces_js_1.QNS.w}jc { attribute ${namespaces_js_1.QNS.w}val { $alignment } } else (), if (exists($outlineLvl)) then element ${namespaces_js_1.QNS.w}outlineLvl { attribute ${namespaces_js_1.QNS.w}val { $outlineLvl } } else (), docxml:ct-shd(fn:QName("${namespaces_js_1.NamespaceUri.w}", "shd"), $shading), if (exists($spacing)) then element ${namespaces_js_1.QNS.w}spacing { if (exists($spacing('before'))) then attribute ${namespaces_js_1.QNS.w}before { $spacing('before') } else (), if (exists($spacing('after'))) then attribute ${namespaces_js_1.QNS.w}after { $spacing('after') } else (), if (exists($spacing('line'))) then attribute ${namespaces_js_1.QNS.w}line { $spacing('line') } else (), if (exists($spacing('lineRule'))) then attribute ${namespaces_js_1.QNS.w}lineRule { $spacing('lineRule') } else (), if (exists($spacing('afterAutoSpacing'))) then attribute ${namespaces_js_1.QNS.w}afterAutoSpacing { $spacing('afterAutoSpacing') } else (), if (exists($spacing('beforeAutoSpacing'))) then attribute ${namespaces_js_1.QNS.w}beforeAutoSpacing { $spacing('beforeAutoSpacing') } else () } else (), if (exists($indentation)) then element ${namespaces_js_1.QNS.w}ind { if (exists($indentation('start'))) then attribute ${namespaces_js_1.QNS.w}start { $indentation('start') } else (), if (exists($indentation('startChars'))) then attribute ${namespaces_js_1.QNS.w}startChars { $indentation('startChars') } else (), if (exists($indentation('end'))) then attribute ${namespaces_js_1.QNS.w}end { $indentation('end') } else (), if (exists($indentation('endChars'))) then attribute ${namespaces_js_1.QNS.w}endChars { $indentation('endChars') } else (), if (exists($indentation('hanging'))) then attribute ${namespaces_js_1.QNS.w}hanging { $indentation('hanging') } else (), if (exists($indentation('hangingChars'))) then attribute ${namespaces_js_1.QNS.w}hangingChars { $indentation('hangingChars') } else (), if (exists($indentation('firstLine'))) then attribute ${namespaces_js_1.QNS.w}firstLine { $indentation('firstLine') } else (), if (exists($indentation('firstLineChars'))) then attribute ${namespaces_js_1.QNS.w}firstLineChars { $indentation('firstLineChars') } else () } else (), if (exists($borders)) then element ${namespaces_js_1.QNS.w}pBdr { (: In sequence order: :) docxml:ct-border(fn:QName("${namespaces_js_1.NamespaceUri.w}", "top"), $borders('top')), docxml:ct-border(fn:QName("${namespaces_js_1.NamespaceUri.w}", "left"), $borders('left')), docxml:ct-border(fn:QName("${namespaces_js_1.NamespaceUri.w}", "bottom"), $borders('bottom')), docxml:ct-border(fn:QName("${namespaces_js_1.NamespaceUri.w}", "right"), $borders('right')), docxml:ct-border(fn:QName("${namespaces_js_1.NamespaceUri.w}", "between"), $borders('between')) } else (), $rpr, $sectpr, if (exists($change)) then element ${namespaces_js_1.QNS.w}pPrChange { attribute ${namespaces_js_1.QNS.w}id { $change('id') }, attribute ${namespaces_js_1.QNS.w}author { $change('author') }, attribute ${namespaces_js_1.QNS.w}date { $change('date') }, $change('node') } else (), if (exists($tabs)) then element ${namespaces_js_1.QNS.w}tabs { for $tab in array:flatten($tabs) return element ${namespaces_js_1.QNS.w}tab { if (exists($tab('type'))) then attribute ${namespaces_js_1.QNS.w}val { $tab('type') } else (), if (exists($tab('leader'))) then attribute ${namespaces_js_1.QNS.w}leader { $tab('leader') } else (), if (exists($tab('position'))) then attribute ${namespaces_js_1.QNS.w}pos { $tab('position') } else () } } else () } `, { style: data.style || null, alignment: data.alignment || null, outlineLvl: data.outlineLvl === null || data.outlineLvl === undefined ? null : data.outlineLvl, indentation: data.indentation ? { ...data.indentation, hanging: getTwipOrNull(data.indentation.hanging), firstLine: getTwipOrNull(data.indentation.firstLine), start: getTwipOrNull(data.indentation.start), end: getTwipOrNull(data.indentation.end), } : null, shading: data.shading || null, spacing: data.spacing ? { ...data.spacing, before: getTwipOrNull(data.spacing.before), after: getTwipOrNull(data.spacing.after), line: getTwipOrNull(data.spacing.line), lineRule: data.spacing.lineRule || null, } : null, borders: data.borders ? { top: null, left: null, bottom: null, right: null, between: null, ...data.borders, } : null, listItem: data.listItem || null, change: data.change ? { id: data.change.id, author: data.change.author, date: data.change.date.toISOString(), node: paragraphPropertiesToNode(data.change), } : null, rpr: (0, text_properties_js_1.textPropertiesToNode)(data.pilcrow || undefined), sectpr: sectionProperties && (0, section_properties_js_1.sectionPropertiesToNode)(sectionProperties), tabs: data.tabs?.length ? data.tabs?.map(tab => ({ type: tab.type, leader: tab.leader, position: getTwipOrNull(tab.position), })) : null }); } exports.paragraphPropertiesToNode = paragraphPropertiesToNode;