docxml
Version:
TypeScript (component) library for building and parsing a DOCX file
80 lines (79 loc) • 2.46 kB
JavaScript
import './Text.js';
import { Component } from '../classes/Component.js';
import { createChildComponentsFromNodes, registerComponent } from '../utilities/components.js';
import { create } from '../utilities/dom.js';
import { QNS } from '../utilities/namespaces.js';
import { evaluateXPathToMap } from '../utilities/xquery.js';
/**
* A component that represents a (simple) instruction field.
*/
export class Field extends Component {
/**
* Creates an XML DOM node for this component instance.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async toNode(ancestry) {
return create(`
element ${QNS.w}fldSimple {
attribute ${QNS.w}instr { $instruction },
if ($isDirty) then attribute ${QNS.w}dirty { "1" } else (),
if ($isLocked) then attribute ${QNS.w}fldLock { "1" } else (),
$children
}
`, {
instruction: this.props.instruction,
isDirty: !!this.props.isDirty,
isLocked: !!this.props.isLocked,
children: await this.childrenToNode(ancestry),
});
}
/**
* Asserts whether or not a given XML node correlates with this component.
*/
static matchesNode(node) {
return node.nodeName === 'w:fldSimple';
}
/**
* Instantiate this component from the XML in an existing DOCX file.
*/
static fromNode(node, context) {
const { children, ...props } = evaluateXPathToMap(`map {
"instruction": ./@${QNS.w}instruction/string(),
"isDirty": docxml:st-on-off(@${QNS.w}dirty),
"isLocked": docxml:st-on-off(@${QNS.w}fldLock),
"children": array{ ./(
${QNS.w}r |
${QNS.w}hyperlink |
${QNS.w}del |
${QNS.w}ins |
${QNS.w}commentRangeStart |
${QNS.w}commentRangeEnd |
${QNS.w}bookmarkRangeStart |
${QNS.w}bookmarkRangeEnd
) }
}`, node);
return new Field(props, ...createChildComponentsFromNodes(this.children, children, context));
}
}
Object.defineProperty(Field, "children", {
enumerable: true,
configurable: true,
writable: true,
value: [
'BookmarkRangeStart',
'BookmarkRangeEnd',
'CommentRangeStart',
'CommentRangeEnd',
'TextDeletion',
'TextAddition',
'Text',
'Hyperlink',
]
});
Object.defineProperty(Field, "mixed", {
enumerable: true,
configurable: true,
writable: true,
value: false
});
registerComponent(Field);