docxml
Version:
TypeScript (component) library for building and parsing a DOCX file
150 lines (149 loc) • 5.41 kB
JavaScript
import { create } from '../utilities/dom.js';
import { NamespaceUri, QNS } from '../utilities/namespaces.js';
import { evaluateXPathToMap } from '../utilities/xquery.js';
function explodeSimpleOrComplex(value) {
if (value === null) {
return { simple: null, complex: null };
}
if (value.simple === undefined &&
value.complex === undefined) {
return {
simple: value || null,
complex: value || null,
};
}
return {
simple: value.simple || null,
complex: value.complex || null,
};
}
export function textPropertiesFromNode(node) {
if (!node) {
return {};
}
return evaluateXPathToMap(`
map {
"style": ./${QNS.w}rStyle/@${QNS.w}val/string(),
"color": ./${QNS.w}color/@${QNS.w}val/string(),
"shading": ./${QNS.w}shd/docxml:ct-shd(.),
"isUnderlined": ./${QNS.w}u/@${QNS.w}val/string(),
"isBold": map {
"simple": docxml:ct-on-off(./${QNS.w}b),
"complex": docxml:ct-on-off(./${QNS.w}bCs)
},
"isItalic": map {
"simple": docxml:ct-on-off(./${QNS.w}i),
"complex": docxml:ct-on-off(./${QNS.w}iCs)
},
"isSmallCaps": docxml:ct-on-off(./${QNS.w}smallCaps),
"isCaps": docxml:ct-on-off(./${QNS.w}caps),
"verticalAlign": ./${QNS.w}vertAlign/@${QNS.w}val/string(),
"language": ./${QNS.w}lang/@${QNS.w}val/string(),
"fontSize": map {
"simple": docxml:length(${QNS.w}sz/@${QNS.w}val, "hpt"),
"complex": docxml:length(${QNS.w}szCs/@${QNS.w}val, "hpt")
},
"minimumKerningFontSize": docxml:length(${QNS.w}kern/@${QNS.w}val, "hpt"),
"isStrike": docxml:ct-on-off(./${QNS.w}strike),
"spacing": docxml:length(${QNS.w}spacing/@${QNS.w}val, 'twip'),
"font": ./${QNS.w}rFonts/map {
"cs": @${QNS.w}cs/string(),
"ascii": @${QNS.w}ascii/string(),
"hAnsi": @${QNS.w}hAnsi/string()
}
}
`, node);
}
export function textPropertiesToNode(data = {}) {
if (!data.style &&
!data.color &&
!data.isUnderlined &&
!data.language &&
!data.isBold &&
!data.verticalAlign &&
!data.isItalic &&
!data.isSmallCaps &&
!data.isCaps &&
!data.fontSize &&
!data.isStrike &&
!data.shading &&
!data.font) {
return null;
}
return create(`element ${QNS.w}rPr {
if ($style) then element ${QNS.w}rStyle {
attribute ${QNS.w}val { $style }
} else (),
if ($color) then element ${QNS.w}color {
attribute ${QNS.w}val { $color }
} else (),
if ($isUnderlined) then element ${QNS.w}u {
attribute ${QNS.w}val { $isUnderlined }
} else (),
if ($isBold('simple')) then element ${QNS.w}b {} else (),
if ($isBold('complex')) then element ${QNS.w}bCs {} else (),
if ($isItalic('simple')) then element ${QNS.w}i {} else (),
if ($isItalic('complex')) then element ${QNS.w}iCs {} else (),
if ($isSmallCaps) then element ${QNS.w}smallCaps {} else (),
if ($isCaps) then element ${QNS.w}caps {} else (),
docxml:ct-shd(fn:QName("${NamespaceUri.w}", "shd"), $shading),
if ($verticalAlign) then element ${QNS.w}vertAlign {
attribute ${QNS.w}val { $verticalAlign }
} else (),
if ($language) then element ${QNS.w}lang {
attribute ${QNS.w}val { $language }
} else (),
if (exists($fontSize('simple'))) then element ${QNS.w}sz {
attribute ${QNS.w}val { $fontSize('simple')('hpt') }
} else (),
if (exists($fontSize('complex'))) then element ${QNS.w}szCs {
attribute ${QNS.w}val { $fontSize('complex')('hpt') }
} else (),
if ($minimumKerningFontSize) then element ${QNS.w}kern {
attribute ${QNS.w}val { $minimumKerningFontSize }
} else (),
if ($isStrike) then element ${QNS.w}strike {} else (),
if ($spacing) then element ${QNS.w}spacing {
attribute ${QNS.w}val { $spacing }
} else (),
if (exists($font)) then element ${QNS.w}rFonts {
if (exists($font('cs'))) then attribute ${QNS.w}cs {
$font('cs')
} else (),
if (exists($font('ascii'))) then attribute ${QNS.w}ascii {
$font('ascii')
} else (),
if (exists($font('hAnsi'))) then attribute ${QNS.w}hAnsi {
$font('hAnsi')
} else ()
} else ()
}`, {
style: data.style || null,
color: data.color || null,
isUnderlined: data.isUnderlined === true ? 'single' : data.isUnderlined || null,
language: data.language || null,
shading: data.shading || null,
isBold: explodeSimpleOrComplex(data.isBold || false),
verticalAlign: data.verticalAlign || null,
isItalic: explodeSimpleOrComplex(data.isItalic || false),
isSmallCaps: data.isSmallCaps || false,
isCaps: data.isCaps || false,
fontSize: explodeSimpleOrComplex(data.fontSize || null),
minimumKerningFontSize: data.minimumKerningFontSize ? data.minimumKerningFontSize.hpt : null,
isStrike: data.isStrike || false,
spacing: data.spacing ? data.spacing.twip : null,
font: typeof data.font === 'string'
? {
cs: data.font,
ascii: data.font,
hAnsi: data.font,
}
: data.font
? {
cs: data.font.cs || null,
ascii: data.font.ascii || null,
hAnsi: data.font.hAnsi || null,
}
: null,
});
}