docxml
Version:
TypeScript (component) library for building and parsing a DOCX file
55 lines (54 loc) • 1.7 kB
JavaScript
import { Component } from '../classes/Component.js';
import { 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 line break, page break or section break in a DOCX document. Place
* this in one of the `<Text>`, `<TextAddition>` or `<TextDeletion>` components.
*/
export class Break extends Component {
/**
* Creates an XML DOM node for this component instance.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
toNode(_ancestry) {
return create(`
element ${QNS.w}br {
if (exists($type)) then attribute ${QNS.w}type { $type } else (),
if (exists($clear)) then attribute ${QNS.w}clear { $clear } else ()
}
`, {
type: this.props.type || null,
clear: this.props.clear || null,
});
}
/**
* Asserts whether or not a given XML node correlates with this component.
*/
static matchesNode(node) {
return node.nodeName === 'w:br';
}
/**
* Instantiate this component from the XML in an existing DOCX file.
*/
static fromNode(node) {
return new Break(evaluateXPathToMap(`map {
"type": ./@${QNS.w}type/string(),
"clear": ./@${QNS.w}clear/string()
}`, node));
}
}
Object.defineProperty(Break, "children", {
enumerable: true,
configurable: true,
writable: true,
value: []
});
Object.defineProperty(Break, "mixed", {
enumerable: true,
configurable: true,
writable: true,
value: false
});
registerComponent(Break);