Files
docx-js/src/file/xml-components/xml-component.ts

41 lines
1.1 KiB
TypeScript
Raw Normal View History

2017-09-19 15:46:20 +01:00
import { BaseXmlComponent } from "./base";
import { IXmlableObject } from "./xmlable-object";
export { BaseXmlComponent };
export abstract class XmlComponent extends BaseXmlComponent {
protected root: Array<BaseXmlComponent | string>;
constructor(rootKey: string) {
super(rootKey);
this.root = new Array<BaseXmlComponent>();
}
public prepForXml(): IXmlableObject {
2018-01-23 01:33:12 +00:00
const children = this.root
.filter(c => {
if (c instanceof BaseXmlComponent) {
return !c.isDeleted;
}
return true;
})
2018-01-23 01:33:12 +00:00
.map((comp) => {
if (comp instanceof BaseXmlComponent) {
return comp.prepForXml();
}
return comp;
})
.filter((comp) => comp); // Exclude null, undefined, and empty strings
2017-09-19 15:46:20 +01:00
return {
[this.rootKey]: children,
};
}
2018-04-23 11:49:57 +02:00
public addChildElement(child: XmlComponent | string) {
this.root.push(child);
}
public delete() {
this.deleted = true;
}
2017-09-19 15:46:20 +01:00
}