docxml
Version:
TypeScript (component) library for building and parsing a DOCX file
155 lines (154 loc) • 6.66 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.textPropertiesToNode = exports.textPropertiesFromNode = 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 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,
};
}
function textPropertiesFromNode(node) {
if (!node) {
return {};
}
return (0, xquery_js_1.evaluateXPathToMap)(`
map {
"style": ./${namespaces_js_1.QNS.w}rStyle/@${namespaces_js_1.QNS.w}val/string(),
"color": ./${namespaces_js_1.QNS.w}color/@${namespaces_js_1.QNS.w}val/string(),
"shading": ./${namespaces_js_1.QNS.w}shd/docxml:ct-shd(.),
"isUnderlined": ./${namespaces_js_1.QNS.w}u/@${namespaces_js_1.QNS.w}val/string(),
"isBold": map {
"simple": docxml:ct-on-off(./${namespaces_js_1.QNS.w}b),
"complex": docxml:ct-on-off(./${namespaces_js_1.QNS.w}bCs)
},
"isItalic": map {
"simple": docxml:ct-on-off(./${namespaces_js_1.QNS.w}i),
"complex": docxml:ct-on-off(./${namespaces_js_1.QNS.w}iCs)
},
"isSmallCaps": docxml:ct-on-off(./${namespaces_js_1.QNS.w}smallCaps),
"isCaps": docxml:ct-on-off(./${namespaces_js_1.QNS.w}caps),
"verticalAlign": ./${namespaces_js_1.QNS.w}vertAlign/@${namespaces_js_1.QNS.w}val/string(),
"language": ./${namespaces_js_1.QNS.w}lang/@${namespaces_js_1.QNS.w}val/string(),
"fontSize": map {
"simple": docxml:length(${namespaces_js_1.QNS.w}sz/@${namespaces_js_1.QNS.w}val, "hpt"),
"complex": docxml:length(${namespaces_js_1.QNS.w}szCs/@${namespaces_js_1.QNS.w}val, "hpt")
},
"minimumKerningFontSize": docxml:length(${namespaces_js_1.QNS.w}kern/@${namespaces_js_1.QNS.w}val, "hpt"),
"isStrike": docxml:ct-on-off(./${namespaces_js_1.QNS.w}strike),
"spacing": docxml:length(${namespaces_js_1.QNS.w}spacing/@${namespaces_js_1.QNS.w}val, 'twip'),
"font": ./${namespaces_js_1.QNS.w}rFonts/map {
"cs": @${namespaces_js_1.QNS.w}cs/string(),
"ascii": @${namespaces_js_1.QNS.w}ascii/string(),
"hAnsi": @${namespaces_js_1.QNS.w}hAnsi/string()
}
}
`, node);
}
exports.textPropertiesFromNode = textPropertiesFromNode;
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 (0, dom_js_1.create)(`element ${namespaces_js_1.QNS.w}rPr {
if ($style) then element ${namespaces_js_1.QNS.w}rStyle {
attribute ${namespaces_js_1.QNS.w}val { $style }
} else (),
if ($color) then element ${namespaces_js_1.QNS.w}color {
attribute ${namespaces_js_1.QNS.w}val { $color }
} else (),
if ($isUnderlined) then element ${namespaces_js_1.QNS.w}u {
attribute ${namespaces_js_1.QNS.w}val { $isUnderlined }
} else (),
if ($isBold('simple')) then element ${namespaces_js_1.QNS.w}b {} else (),
if ($isBold('complex')) then element ${namespaces_js_1.QNS.w}bCs {} else (),
if ($isItalic('simple')) then element ${namespaces_js_1.QNS.w}i {} else (),
if ($isItalic('complex')) then element ${namespaces_js_1.QNS.w}iCs {} else (),
if ($isSmallCaps) then element ${namespaces_js_1.QNS.w}smallCaps {} else (),
if ($isCaps) then element ${namespaces_js_1.QNS.w}caps {} else (),
docxml:ct-shd(fn:QName("${namespaces_js_1.NamespaceUri.w}", "shd"), $shading),
if ($verticalAlign) then element ${namespaces_js_1.QNS.w}vertAlign {
attribute ${namespaces_js_1.QNS.w}val { $verticalAlign }
} else (),
if ($language) then element ${namespaces_js_1.QNS.w}lang {
attribute ${namespaces_js_1.QNS.w}val { $language }
} else (),
if (exists($fontSize('simple'))) then element ${namespaces_js_1.QNS.w}sz {
attribute ${namespaces_js_1.QNS.w}val { $fontSize('simple')('hpt') }
} else (),
if (exists($fontSize('complex'))) then element ${namespaces_js_1.QNS.w}szCs {
attribute ${namespaces_js_1.QNS.w}val { $fontSize('complex')('hpt') }
} else (),
if ($minimumKerningFontSize) then element ${namespaces_js_1.QNS.w}kern {
attribute ${namespaces_js_1.QNS.w}val { $minimumKerningFontSize }
} else (),
if ($isStrike) then element ${namespaces_js_1.QNS.w}strike {} else (),
if ($spacing) then element ${namespaces_js_1.QNS.w}spacing {
attribute ${namespaces_js_1.QNS.w}val { $spacing }
} else (),
if (exists($font)) then element ${namespaces_js_1.QNS.w}rFonts {
if (exists($font('cs'))) then attribute ${namespaces_js_1.QNS.w}cs {
$font('cs')
} else (),
if (exists($font('ascii'))) then attribute ${namespaces_js_1.QNS.w}ascii {
$font('ascii')
} else (),
if (exists($font('hAnsi'))) then attribute ${namespaces_js_1.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,
});
}
exports.textPropertiesToNode = textPropertiesToNode;