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:
@ -1,6 +1,6 @@
|
||||
// tslint:disable:no-any
|
||||
import { Element as XmlElement, xml2js } from "xml-js";
|
||||
import { IXmlableObject, XmlComponent } from ".";
|
||||
import { IXmlableObject, XmlAttributeComponent, XmlComponent } from ".";
|
||||
|
||||
/**
|
||||
* Converts the given xml element (in json format) into XmlComponent.
|
||||
@ -27,6 +27,10 @@ export function convertToXmlComponent(element: XmlElement): ImportedXmlComponent
|
||||
}
|
||||
}
|
||||
|
||||
class ImportedXmlComponentAttributes extends XmlAttributeComponent<any> {
|
||||
// noop
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents imported xml component from xml file.
|
||||
*/
|
||||
@ -46,58 +50,14 @@ export class ImportedXmlComponent extends XmlComponent {
|
||||
* @param importedContent xml content of the imported component
|
||||
*/
|
||||
|
||||
// tslint:disable-next-line:variable-name
|
||||
private readonly _attr: any;
|
||||
|
||||
// tslint:disable-next-line:variable-name
|
||||
constructor(rootKey: string, _attr?: any) {
|
||||
super(rootKey);
|
||||
if (_attr) {
|
||||
this._attr = _attr;
|
||||
this.root.push(new ImportedXmlComponentAttributes(_attr));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms the object so it can be converted to xml. Example:
|
||||
* <w:someKey someAttr="1" otherAttr="11">
|
||||
* <w:child childAttr="2">
|
||||
* </w:child>
|
||||
* </w:someKey>
|
||||
* {
|
||||
* 'w:someKey': [
|
||||
* {
|
||||
* _attr: {
|
||||
* someAttr: "1",
|
||||
* otherAttr: "11"
|
||||
* }
|
||||
* },
|
||||
* {
|
||||
* 'w:child': [
|
||||
* {
|
||||
* _attr: {
|
||||
* childAttr: "2"
|
||||
* }
|
||||
* }
|
||||
* ]
|
||||
* }
|
||||
* ]
|
||||
* }
|
||||
*/
|
||||
public prepForXml(): IXmlableObject | undefined {
|
||||
const result = super.prepForXml();
|
||||
if (!result) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (!!this._attr) {
|
||||
if (!Array.isArray(result[this.rootKey])) {
|
||||
result[this.rootKey] = [result[this.rootKey]];
|
||||
}
|
||||
result[this.rootKey].unshift({ _attr: this._attr });
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public push(xmlComponent: XmlComponent | string): void {
|
||||
this.root.push(xmlComponent);
|
||||
}
|
||||
|
Reference in New Issue
Block a user