Optimize XML output by properly constructing objects to send to the xml library so that it can produce proper empty elements.

Rework the way attributes are stored in ImportedXmlComponent to match elsewhere (required allowing for a null xmlKeys in the XmlAttributeComponent interface).
Rework the way paragraphs get added to the end of table cells if needed.
The goal in both reworks is to not mess around with the objects output from `prepForXml` if we can avoid it.
Made the output of RunProperties, ParagraphProperties, TableCellProperties, TableRowProperties, and TableProperties all optional based on whether they contain any attributes or children.  Changed code in PageBorders, TableCellMargin, and TableCellBorders that implemented this same thing by overriding `prepForXml` so that it uses the new XmlComponent subclass instead.
Removed commented out code that attempted to fix-up XML output and make proper empty elements.
Fixed all affected tests.
Turn off `no-null-keyword` in the linter as we need to use null to signal to the `xml` library to create an empty element with no attributes (`undefined` will not work in its place).

Fixes #306
This commit is contained in:
Bruce Duncan
2019-04-09 05:27:18 -04:00
parent 920bd3c175
commit 816cb54b14
63 changed files with 1210 additions and 1605 deletions

View File

@ -17,7 +17,7 @@ export abstract class XmlComponent extends BaseXmlComponent {
if (c instanceof BaseXmlComponent) {
return !c.IsDeleted;
}
return true;
return c !== undefined;
})
.map((comp) => {
if (comp instanceof BaseXmlComponent) {
@ -26,8 +26,15 @@ export abstract class XmlComponent extends BaseXmlComponent {
return comp;
})
.filter((comp) => comp !== undefined); // Exclude undefined
// If we only have a single IXmlableObject in our children array and it
// represents our attributes, use the object itself as our children to
// avoid an unneeded XML close element. (Note: We have to use this
// function to get typescript to allow our check.)
// Additionally, if the array is empty, use null as our children to
// get an empty XML element generated.
const onlyAttrs = (c) => typeof c === "object" && c._attr;
return {
[this.rootKey]: children,
[this.rootKey]: children.length ? (children.length === 1 && onlyAttrs(children[0]) ? children[0] : children) : null,
};
}
@ -41,3 +48,12 @@ export abstract class XmlComponent extends BaseXmlComponent {
this.deleted = true;
}
}
export abstract class IgnoreIfEmptyXmlComponent extends XmlComponent {
public prepForXml(): IXmlableObject | undefined {
const result = super.prepForXml();
if (result && result[this.rootKey]) {
return result;
}
}
}