docxml
Version:
TypeScript (component) library for building and parsing a DOCX file
109 lines (104 loc) • 4.92 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.tableCellPropertiesToNode = exports.tableCellPropertiesFromNode = 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");
function tableCellPropertiesFromNode(node) {
return node
? (0, xquery_js_1.evaluateXPathToMap)(`
let $colStart := docxml:cell-column(.)
let $rowStart := count(../../preceding-sibling::${namespaces_js_1.QNS.w}tr)
(: The first next row that contains a new cell in this column :)
let $firstNextRow := ../../following-sibling::${namespaces_js_1.QNS.w}tr[
child::${namespaces_js_1.QNS.w}tc[
docxml:cell-column(.) = $colStart and
not(
./${namespaces_js_1.QNS.w}tcPr/${namespaces_js_1.QNS.w}vMerge[
@${namespaces_js_1.QNS.w}val = "continue" or
not(./@${namespaces_js_1.QNS.w}val)
]
)
]
][1]
let $rowEnd := if ($firstNextRow)
then count($firstNextRow/preceding-sibling::${namespaces_js_1.QNS.w}tr)
else count(../../../${namespaces_js_1.QNS.w}tr)
return map {
"colSpan": if (./${namespaces_js_1.QNS.w}gridSpan)
then ./${namespaces_js_1.QNS.w}gridSpan/@${namespaces_js_1.QNS.w}val/number()
else 1,
"rowSpan": if ($rowEnd != $rowStart)
then $rowEnd - $rowStart
else 1,
"shading": ./${namespaces_js_1.QNS.w}shd/docxml:ct-shd(.),
"borders": ./${namespaces_js_1.QNS.w}tcBorders/map {
"top": docxml:ct-border(${namespaces_js_1.QNS.w}top),
"start": docxml:ct-border((${namespaces_js_1.QNS.w}start|${namespaces_js_1.QNS.w}left)[1]),
"bottom": docxml:ct-border(${namespaces_js_1.QNS.w}bottom),
"end": docxml:ct-border((${namespaces_js_1.QNS.w}end|${namespaces_js_1.QNS.w}right)[1]),
"tl2br": docxml:ct-border(${namespaces_js_1.QNS.w}tl2br),
"tr2bl": docxml:ct-border(${namespaces_js_1.QNS.w}tr2bl),
"insideH": docxml:ct-border(${namespaces_js_1.QNS.w}insideH),
"insideV": docxml:ct-border(${namespaces_js_1.QNS.w}insideV)
},
"verticalAlignment": ./${namespaces_js_1.QNS.w}vAlign/@${namespaces_js_1.QNS.w}val/string()
}
`, node)
: {};
}
exports.tableCellPropertiesFromNode = tableCellPropertiesFromNode;
function tableCellPropertiesToNode(tcpr = {}, asRepeatingNode) {
return (0, dom_js_1.create)(`element ${namespaces_js_1.QNS.w}tcPr {
if ($width) then element ${namespaces_js_1.QNS.w}tcW {
attribute ${namespaces_js_1.QNS.w}w { $width },
attribute ${namespaces_js_1.QNS.w}type { "dxa" }
} else (),
if ($colSpan > 1) then element ${namespaces_js_1.QNS.w}gridSpan {
attribute ${namespaces_js_1.QNS.w}val { $colSpan }
} else (),
if ($asRepeatingNode) then element ${namespaces_js_1.QNS.w}vMerge {
attribute ${namespaces_js_1.QNS.w}val { "continue" }
} else (
if ($rowSpan > 1) then element ${namespaces_js_1.QNS.w}vMerge {
attribute ${namespaces_js_1.QNS.w}val { "restart" }
} else ()
),
docxml:ct-shd(fn:QName("${namespaces_js_1.NamespaceUri.w}", "shd"), $shading),
if (exists($borders)) then element ${namespaces_js_1.QNS.w}tcBorders {
(: 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}", "start"), $borders('start')),
docxml:ct-border(fn:QName("${namespaces_js_1.NamespaceUri.w}", "bottom"), $borders('bottom')),
docxml:ct-border(fn:QName("${namespaces_js_1.NamespaceUri.w}", "end"), $borders('end')),
docxml:ct-border(fn:QName("${namespaces_js_1.NamespaceUri.w}", "tl2br"), $borders('tl2br')),
docxml:ct-border(fn:QName("${namespaces_js_1.NamespaceUri.w}", "tr2bl"), $borders('tr2bl')),
docxml:ct-border(fn:QName("${namespaces_js_1.NamespaceUri.w}", "insideH"), $borders('insideH')),
docxml:ct-border(fn:QName("${namespaces_js_1.NamespaceUri.w}", "insideV"), $borders('insideV'))
} else (),
if (exists($verticalAlignment)) then element ${namespaces_js_1.QNS.w}vAlign {
attribute ${namespaces_js_1.QNS.w}val { $verticalAlignment }
} else ()
}`, {
asRepeatingNode: !!asRepeatingNode,
colSpan: tcpr.colSpan || 1,
rowSpan: tcpr.rowSpan || 1,
width: tcpr.width ? Math.round(tcpr.width.twip) : null,
shading: tcpr.shading || null,
borders: tcpr.borders
? {
top: null,
left: null,
bottom: null,
right: null,
insideH: null,
insideV: null,
tl2br: null,
tr2bl: null,
...tcpr.borders,
}
: null,
verticalAlignment: tcpr.verticalAlignment || null,
});
}
exports.tableCellPropertiesToNode = tableCellPropertiesToNode;