docxml
Version:
TypeScript (component) library for building and parsing a DOCX file
111 lines (97 loc) • 4.33 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DOCXML_NS_URI = void 0;
const fontoxpath_1 = __importDefault(require("fontoxpath"));
const length_js_1 = require("./length.js");
const namespaces_js_1 = require("./namespaces.js");
exports.DOCXML_NS_URI = 'https://github.com/wvbe/docxml';
fontoxpath_1.default.registerCustomXPathFunction({ namespaceURI: exports.DOCXML_NS_URI, localName: 'length' }, ['xs:float?', 'xs:string'], 'map(*)?', (_facade, value, unit) => (value === null ? null : (0, length_js_1.convert)(value, unit)));
fontoxpath_1.default.registerXQueryModule(`
module namespace docxml = "${exports.DOCXML_NS_URI}";
declare %public function docxml:cell-column($cell) as xs:double {
sum(
$cell/preceding-sibling::${namespaces_js_1.QNS.w}tc/(
if (./${namespaces_js_1.QNS.w}tcPr/${namespaces_js_1.QNS.w}gridSpan)
then number(./${namespaces_js_1.QNS.w}tcPr/${namespaces_js_1.QNS.w}gridSpan/@${namespaces_js_1.QNS.w}val)
else 1
)
)
};
declare %public function docxml:spans-cell-column($cell, $column) as xs:boolean {
let $start := docxml:cell-column($cell)
let $gridSpan := if ($cell/${namespaces_js_1.QNS.w}tcPr/${namespaces_js_1.QNS.w}gridSpan)
then number($cell/${namespaces_js_1.QNS.w}tcPr/${namespaces_js_1.QNS.w}gridSpan/@${namespaces_js_1.QNS.w}val)
else 1
return boolean(($column >= $start) and ($column < ($start + $gridSpan)))
};
(:
Correlates with the ST_OnOff simple type. In short,
- If $val is "on", "true" or "1", value is TRUE
- Any other value means FALSE
:)
declare %public function docxml:st-on-off($val) as xs:boolean {
$val = ("on", "true", "1")
};
(:
Correlates with the CT_OnOff complex type. In short;
- If the element does not exist, value is FALSE
- If the element exists but has no @val, defaults to value TRUE
- If the element @val is set to "on", "true" or "1", value is TRUE
- Any other value of @val means FALSE
Also works with CT_BooleanProperty.
:)
declare %public function docxml:ct-on-off($val) as xs:boolean {
if (not(exists($val)))
then false()
else if (not($val/@${namespaces_js_1.QNS.w}val))
then true()
else docxml:st-on-off($val/@${namespaces_js_1.QNS.w}val)
};
(:
Correlates with the CT_Shd complex type. Returns a map of TypeScript shape "Shading".
:)
declare %public function docxml:ct-shd($val) as map(*)? {
$val/map {
"foreground": ./@${namespaces_js_1.QNS.w}color/string(),
"background": ./@${namespaces_js_1.QNS.w}fill/string(),
"pattern": ./@${namespaces_js_1.QNS.w}val/string()
}
};
(: @TODO Test this function :)
declare %public function docxml:ct-shd($name as xs:QName, $data as map(*)?) {
if (exists($data)) then element {$name} {
if (exists($data('foreground'))) then attribute ${namespaces_js_1.QNS.w}color { $data('foreground') } else (),
if (exists($data('background'))) then attribute ${namespaces_js_1.QNS.w}fill { $data('background') } else (),
if (exists($data('pattern'))) then attribute ${namespaces_js_1.QNS.w}val { $data('pattern') } else ()
} else ()
};
(:
Correlates with the CT_Border complex type.
@TODO Test this function
:)
declare %public function docxml:ct-border($val) as map(*)? {
$val/map {
"type": ./@${namespaces_js_1.QNS.w}val/string(),
"width": docxml:length(@${namespaces_js_1.QNS.w}sz, 'opt'),
"spacing": if (exists(./@${namespaces_js_1.QNS.w}space))
then ./@${namespaces_js_1.QNS.w}space/number()
else (),
"color": ./@${namespaces_js_1.QNS.w}color/string()
}
};
(:
Creates a new element of the CT_Border complex type.
@TODO Test this function
:)
declare %public function docxml:ct-border($name as xs:QName, $data as map(*)?) {
if (exists($data)) then element {$name} {
if ($data('type')) then attribute ${namespaces_js_1.QNS.w}val { $data('type') } else (),
if (exists($data('width'))) then attribute ${namespaces_js_1.QNS.w}sz { $data('width')('opt') } else (),
if (exists($data('spacing'))) then attribute ${namespaces_js_1.QNS.w}space { $data('spacing') } else (),
if ($data('color')) then attribute ${namespaces_js_1.QNS.w}color { $data('color') } else ()
} else ()
};
`);