docxml
Version:
TypeScript (component) library for building and parsing a DOCX file
108 lines (107 loc) • 4.88 kB
JavaScript
import { create } from '../utilities/dom.js';
import { QNS } from '../utilities/namespaces.js';
import { evaluateXPathToMap } from '../utilities/xquery.js';
export function sectionPropertiesFromNode(node) {
if (!node) {
return {};
}
const data = evaluateXPathToMap(`map {
"headers": map {
"first": ./${QNS.w}headerReference[@${QNS.w}type = 'first']/@${QNS.r}id/string(),
"even": ./${QNS.w}headerReference[@${QNS.w}type = 'even']/@${QNS.r}id/string(),
"odd": ./${QNS.w}headerReference[@${QNS.w}type = 'default']/@${QNS.r}id/string()
},
"footers": map {
"first": ./${QNS.w}footerReference[@${QNS.w}type = 'first']/@${QNS.r}id/string(),
"even": ./${QNS.w}footerReference[@${QNS.w}type = 'even']/@${QNS.r}id/string(),
"odd": ./${QNS.w}footerReference[@${QNS.w}type = 'default']/@${QNS.r}id/string()
},
"pageWidth": docxml:length(${QNS.w}pgSz/@${QNS.w}w, 'twip'),
"pageHeight": docxml:length(${QNS.w}pgSz/@${QNS.w}h, 'twip'),
"pageOrientation": ./${QNS.w}pgSz/@${QNS.w}orient/string(),
"pageMargin": map {
"top": docxml:length(./${QNS.w}pgMar/@${QNS.w}top, 'twip'),
"right": docxml:length(./${QNS.w}pgMar/@${QNS.w}right, 'twip'),
"bottom": docxml:length(./${QNS.w}pgMar/@${QNS.w}bottom, 'twip'),
"left": docxml:length(./${QNS.w}pgMar/@${QNS.w}left, 'twip'),
"header": docxml:length(./${QNS.w}pgMar/@${QNS.w}header, 'twip'),
"footer": docxml:length(./${QNS.w}pgMar/@${QNS.w}footer, 'twip'),
"gutter": docxml:length(./${QNS.w}pgMar/@${QNS.w}gutter, 'twip')
},
"isTitlePage": exists(./${QNS.w}titlePg) and (not(./${QNS.w}titlePg/@${QNS.w}val) or docxml:st-on-off(./${QNS.w}titlePg/@${QNS.w}val))
}`, node);
return data;
}
export function sectionPropertiesToNode(data = {}) {
return create(`element ${QNS.w}sectPr {
if (exists($headers('first'))) then element ${QNS.w}headerReference {
attribute ${QNS.r}id { $headers('first') },
attribute ${QNS.w}type { 'first' }
} else (),
if (exists($headers('even'))) then element ${QNS.w}headerReference {
attribute ${QNS.r}id { $headers('even') },
attribute ${QNS.w}type { 'even' }
} else (),
if (exists($headers('odd'))) then element ${QNS.w}headerReference {
attribute ${QNS.r}id { $headers('odd') },
attribute ${QNS.w}type { 'default' }
} else (),
if (exists($footers('first'))) then element ${QNS.w}footerReference {
attribute ${QNS.r}id { $footers('first') },
attribute ${QNS.w}type { 'first' }
} else (),
if (exists($footers('even'))) then element ${QNS.w}footerReference {
attribute ${QNS.r}id { $footers('even') },
attribute ${QNS.w}type { 'even' }
} else (),
if (exists($footers('odd'))) then element ${QNS.w}footerReference {
attribute ${QNS.r}id { $footers('odd') },
attribute ${QNS.w}type { 'default' }
} else (),
if (exists($pageWidth) or exists($pageHeight) or $pageOrientation) then element ${QNS.w}pgSz {
if (exists($pageWidth)) then attribute ${QNS.w}w {
round($pageWidth('twip'))
} else (),
if (exists($pageHeight)) then attribute ${QNS.w}h {
round($pageHeight('twip'))
} else (),
if ($pageOrientation) then attribute ${QNS.w}orient { $pageOrientation } else ()
} else (),
if (exists($pageMargin)) then element ${QNS.w}pgMar {
if (exists($pageMargin('top'))) then attribute ${QNS.w}top {
round($pageMargin('top')('twip'))
} else (),
if (exists($pageMargin('right'))) then attribute ${QNS.w}right {
round($pageMargin('right')('twip'))
} else (),
if (exists($pageMargin('bottom'))) then attribute ${QNS.w}bottom {
round($pageMargin('bottom')('twip'))
} else (),
if (exists($pageMargin('left'))) then attribute ${QNS.w}left {
round($pageMargin('left')('twip'))
} else (),
if (exists($pageMargin('header'))) then attribute ${QNS.w}header {
round($pageMargin('header')('twip'))
} else (),
if (exists($pageMargin('footer'))) then attribute ${QNS.w}footer {
round($pageMargin('footer')('twip'))
} else (),
if (exists($pageMargin('gutter'))) then attribute ${QNS.w}gutter {
round($pageMargin('gutter')('twip'))
} else ()
} else (),
if(exists($isTitlePage)) then element ${QNS.w}titlePg { attribute ${QNS.w}val { "1" } } else ()
}`, {
headers: typeof data.headers === 'string'
? { first: data.headers, even: data.headers, odd: data.headers }
: data.headers || {},
footers: typeof data.footers === 'string'
? { first: data.footers, even: data.footers, odd: data.footers }
: data.footers || {},
pageWidth: data.pageWidth || null,
pageHeight: data.pageHeight || null,
pageMargin: data.pageMargin || null,
pageOrientation: data.pageOrientation || null,
isTitlePage: data.isTitlePage || null,
});
}