docxml
Version:
TypeScript (component) library for building and parsing a DOCX file
106 lines (105 loc) • 3.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Text = void 0;
// Import without assignment ensures Deno does not tree-shake this component. To avoid circular
// definitions, components register themselves in a side-effect of their module.
require("./Break.js");
require("./FieldRangeEnd.js");
require("./FieldRangeInstruction.js");
require("./FieldRangeSeparator.js");
require("./FieldRangeStart.js");
require("./Image.js");
require("./NonBreakingHyphen.js");
require("./Symbol.js");
require("./Tab.js");
const Component_js_1 = require("../classes/Component.js");
const text_properties_js_1 = require("../properties/text-properties.js");
const components_js_1 = require("../utilities/components.js");
const dom_js_1 = require("../utilities/dom.js");
const namespaces_js_1 = require("../utilities/namespaces.js");
const xquery_js_1 = require("../utilities/xquery.js");
const TextDeletion_js_1 = require("./TextDeletion.js");
/**
* A component that represents text. All inline formatting options, such as bold/italic/underline,
* are in fact different props or styles on the `<Text>` component.
*/
class Text extends Component_js_1.Component {
/**
* Creates an XML DOM node for this component instance.
*/
async toNode(ancestry) {
const asTextDeletion = ancestry.some((ancestor) => ancestor instanceof TextDeletion_js_1.TextDeletion);
const anc = [this, ...ancestry];
return (0, dom_js_1.create)(`
element ${namespaces_js_1.QNS.w}r {
$rpr,
$children
}
`, {
rpr: (0, text_properties_js_1.textPropertiesToNode)(this.props),
children: await Promise.all(this.children.map((child) => {
if (typeof child === 'string') {
return (0, dom_js_1.create)(`element ${namespaces_js_1.QNS.w}${asTextDeletion ? 'delText' : 't'} {
attribute xml:space { "preserve" },
$text
}`, {
text: child,
});
}
return child.toNode(anc);
})),
});
}
/**
* Asserts whether or not a given XML node correlates with this component.
*/
static matchesNode(node) {
return node.nodeName === 'w:r';
}
/**
* Instantiate this component from the XML in an existing DOCX file.
*/
static fromNode(node, context) {
const { children, rpr } = (0, xquery_js_1.evaluateXPathToMap)(`
map {
"rpr": ./${namespaces_js_1.QNS.w}rPr,
"children": array{
./(
${namespaces_js_1.QNS.w}br,
${namespaces_js_1.QNS.w}tab,
${namespaces_js_1.QNS.w}drawing,
${namespaces_js_1.QNS.w}t/text(),
${namespaces_js_1.QNS.w}delText/text(),
${namespaces_js_1.QNS.w}fldChar,
${namespaces_js_1.QNS.w}instrText
)
}
}
`, node);
return new Text((0, text_properties_js_1.textPropertiesFromNode)(rpr), ...(0, components_js_1.createChildComponentsFromNodes)(this.children, children, context));
}
}
exports.Text = Text;
Object.defineProperty(Text, "children", {
enumerable: true,
configurable: true,
writable: true,
value: [
'Break',
'FieldRangeEnd',
'FieldRangeInstruction',
'FieldRangeSeparator',
'FieldRangeStart',
'Image',
'NonBreakingHyphen',
'Symbol',
'Tab',
]
});
Object.defineProperty(Text, "mixed", {
enumerable: true,
configurable: true,
writable: true,
value: true
});
(0, components_js_1.registerComponent)(Text);