docxml
Version:
TypeScript (component) library for building and parsing a DOCX file
139 lines (138 loc) • 8.38 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.tablePropertiesToNode = exports.tablePropertiesFromNode = 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 tablePropertiesFromNode(node) {
const properties = node
? (0, xquery_js_1.evaluateXPathToMap)(`map {
"style": ./${namespaces_js_1.QNS.w}tblStyle/@${namespaces_js_1.QNS.w}val/string(),
"activeConditions": ./${namespaces_js_1.QNS.w}tblLook/map {
"firstColumn": docxml:st-on-off(@${namespaces_js_1.QNS.w}firstColumn),
"lastColumn": docxml:st-on-off(@${namespaces_js_1.QNS.w}lastColumn),
"firstRow": docxml:st-on-off(@${namespaces_js_1.QNS.w}firstRow),
"lastRow": docxml:st-on-off(@${namespaces_js_1.QNS.w}lastRow),
"noHBand": docxml:st-on-off(@${namespaces_js_1.QNS.w}noHBand),
"noVBand": docxml:st-on-off(@${namespaces_js_1.QNS.w}noVBand)
},
"indentation": docxml:length(${namespaces_js_1.QNS.w}tblInd[not(@${namespaces_js_1.QNS.w}type = 'nil')]/@${namespaces_js_1.QNS.w}w, 'twip'),
"cellSpacing": docxml:length(${namespaces_js_1.QNS.w}tblCellSpacing[not(@${namespaces_js_1.QNS.w}type = 'nil')]/@${namespaces_js_1.QNS.w}w, 'twip'),
"cellPadding": ./${namespaces_js_1.QNS.w}tblCellMar/map {
"top": docxml:length(${namespaces_js_1.QNS.w}top[not(@${namespaces_js_1.QNS.w}type = 'nil')]/@${namespaces_js_1.QNS.w}w, 'twip'),
"start": docxml:length((${namespaces_js_1.QNS.w}start|${namespaces_js_1.QNS.w}left)[not(@${namespaces_js_1.QNS.w}type = 'nil')][1]/@${namespaces_js_1.QNS.w}w, 'twip'),
"bottom": docxml:length(${namespaces_js_1.QNS.w}bottom[not(@${namespaces_js_1.QNS.w}type = 'nil')]/@${namespaces_js_1.QNS.w}w, 'twip'),
"end": docxml:length((${namespaces_js_1.QNS.w}end|${namespaces_js_1.QNS.w}right)[not(@${namespaces_js_1.QNS.w}type = 'nil')][1]/@${namespaces_js_1.QNS.w}w, 'twip')
},
"columnBandingSize": ./${namespaces_js_1.QNS.w}tblStyleColBandSize/@${namespaces_js_1.QNS.w}val/number(),
"rowBandingSize": ./${namespaces_js_1.QNS.w}tblStyleRowBandSize/@${namespaces_js_1.QNS.w}val/number(),
"borders": ./${namespaces_js_1.QNS.w}tblBorders/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]),
"insideH": docxml:ct-border(${namespaces_js_1.QNS.w}insideH),
"insideV": docxml:ct-border(${namespaces_js_1.QNS.w}insideV)
},
"width": ./${namespaces_js_1.QNS.w}tblW/map {
"length": ./@${namespaces_js_1.QNS.w}w/string(),
"unit": ./@${namespaces_js_1.QNS.w}type/string()
},
"strictColumnWidths": boolean(./${namespaces_js_1.QNS.w}tblLayout/@${namespaces_js_1.QNS.w}type = "fixed")
}`, node)
: {};
return properties;
}
exports.tablePropertiesFromNode = tablePropertiesFromNode;
function tablePropertiesToNode(tblpr = {}) {
return (0, dom_js_1.create)(`element ${namespaces_js_1.QNS.w}tblPr {
if ($style) then element ${namespaces_js_1.QNS.w}tblStyle {
attribute ${namespaces_js_1.QNS.w}val { $style }
} else (),
if (exists($width)) then element ${namespaces_js_1.QNS.w}tblW {
attribute ${namespaces_js_1.QNS.w}w { $width('length') },
attribute ${namespaces_js_1.QNS.w}type { $width('unit') }
} else (),
if (exists($activeConditions)) then element ${namespaces_js_1.QNS.w}tblLook {
if ($activeConditions('firstColumn')) then attribute ${namespaces_js_1.QNS.w}firstColumn { "1" } else attribute ${namespaces_js_1.QNS.w}firstColumn { "0" },
if ($activeConditions('firstRow')) then attribute ${namespaces_js_1.QNS.w}firstRow { "1" } else attribute ${namespaces_js_1.QNS.w}firstRow { "0" },
if ($activeConditions('lastColumn')) then attribute ${namespaces_js_1.QNS.w}lastColumn { "1" } else attribute ${namespaces_js_1.QNS.w}lastColumn { "0" } ,
if ($activeConditions('lastRow')) then attribute ${namespaces_js_1.QNS.w}lastRow { "1" } else attribute ${namespaces_js_1.QNS.w}lastRow { "0" },
if ($activeConditions('noHBand')) then attribute ${namespaces_js_1.QNS.w}noHBand { "1" } else attribute ${namespaces_js_1.QNS.w}noHBand { "0" },
if ($activeConditions('noVBand')) then attribute ${namespaces_js_1.QNS.w}noVBand { "1" } else attribute ${namespaces_js_1.QNS.w}noVBand { "0" }
} else (),
if (exists($cellPadding)) then element ${namespaces_js_1.QNS.w}tblCellMar {
if (exists($cellPadding('top'))) then element ${namespaces_js_1.QNS.w}top {
attribute ${namespaces_js_1.QNS.w}w { round($cellPadding('top')('twip')) },
attribute ${namespaces_js_1.QNS.w}type { "dxa" }
} else (),
if (exists($cellPadding('bottom'))) then element ${namespaces_js_1.QNS.w}bottom {
attribute ${namespaces_js_1.QNS.w}w { round($cellPadding('bottom')('twip')) },
attribute ${namespaces_js_1.QNS.w}type { "dxa" }
} else (),
if (exists($cellPadding('start'))) then element ${namespaces_js_1.QNS.w}start {
attribute ${namespaces_js_1.QNS.w}w { round($cellPadding('start')('twip')) },
attribute ${namespaces_js_1.QNS.w}type { "dxa" }
} else (),
if (exists($cellPadding('end'))) then element ${namespaces_js_1.QNS.w}end {
attribute ${namespaces_js_1.QNS.w}w { round($cellPadding('end')('twip')) },
attribute ${namespaces_js_1.QNS.w}type { "dxa" }
} else ()
} else (),
if (exists($borders)) then element ${namespaces_js_1.QNS.w}tblBorders {
(: 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}", "insideH"), $borders('insideH')),
docxml:ct-border(fn:QName("${namespaces_js_1.NamespaceUri.w}", "insideV"), $borders('insideV'))
} else (),
if (exists($indentation)) then element ${namespaces_js_1.QNS.w}tblInd {
attribute ${namespaces_js_1.QNS.w}w { round($indentation('twip')) },
attribute ${namespaces_js_1.QNS.w}type { "dxa" }
} else (),
if (exists($cellSpacing)) then element ${namespaces_js_1.QNS.w}tblCellSpacing {
attribute ${namespaces_js_1.QNS.w}w { round($cellSpacing('twip')) },
attribute ${namespaces_js_1.QNS.w}type { "dxa" }
} else (),
if (exists($columnBandingSize)) then element ${namespaces_js_1.QNS.w}tblStyleColBandSize {
attribute ${namespaces_js_1.QNS.w}val { $columnBandingSize }
} else (),
if (exists($rowBandingSize)) then element ${namespaces_js_1.QNS.w}tblStyleRowBandSize {
attribute ${namespaces_js_1.QNS.w}val { $rowBandingSize }
} else (),
if ($strictColumnWidths) then element ${namespaces_js_1.QNS.w}tblLayout {
attribute ${namespaces_js_1.QNS.w}type { "fixed" }
} else ()
}`, {
style: tblpr.style || null,
activeConditions: tblpr.activeConditions || null,
width: typeof tblpr.width === 'string' && tblpr.width.endsWith('%')
? { length: tblpr.width, unit: 'pct' }
: typeof tblpr.width === 'number'
? {
length: tblpr.width,
unit: 'dxa',
}
: tblpr.width || null,
cellPadding: tblpr.cellPadding || null,
borders: tblpr.borders
? {
top: null,
start: null,
bottom: null,
end: null,
insideH: null,
insideV: null,
...tblpr.borders,
}
: null,
indentation: tblpr.indentation || null,
cellSpacing: tblpr.cellSpacing || null,
columnBandingSize: tblpr.columnBandingSize || null,
rowBandingSize: tblpr.rowBandingSize || null,
strictColumnWidths: tblpr.strictColumnWidths || false,
});
}
exports.tablePropertiesToNode = tablePropertiesToNode;