docxml
Version:
TypeScript (component) library for building and parsing a DOCX file
35 lines (34 loc) • 1.3 kB
JavaScript
const componentByName = new Map();
/**
* Register a component in such a way that it can be found by its name later. Uses the class
* name as unique key.
*
* @TODO test that this works well in minified JS
*
* This helps avoid circular dependencies in components that can be a descendant of themselves.
* For example, Table --> Row --> Cell --> Table
*/
export function registerComponent(component) {
componentByName.set(component.name, component);
return component;
}
/**
* Transforms a list of (OOXML) XML nodes to component instances based on a list of possible component
* names. The component names must have been registered before using {@link registerComponent}.
*
* This is useful for instantiating the children of a component based on an existing DOCX file.
*/
export function createChildComponentsFromNodes(names, nodes, context) {
const children = names.map((name) => {
const component = componentByName.get(name);
if (!component) {
throw new Error(`Unknown component "${name}"`);
}
return component;
});
return nodes
.map((node) => (node.nodeType === 3
? node.nodeValue
: children.find((Child) => Child.matchesNode(node))?.fromNode(node, context)))
.filter((child) => !!child);
}