#1699 Allow images to work with Hyperlink
Add stack to IContext for prepForXml
This commit is contained in:
@ -5,6 +5,8 @@ import { IXmlableObject } from "./xmlable-object";
|
||||
export interface IContext {
|
||||
readonly file: File;
|
||||
readonly viewWrapper: IViewWrapper;
|
||||
// eslint-disable-next-line functional/prefer-readonly-type
|
||||
readonly stack: IXmlableObject[];
|
||||
}
|
||||
|
||||
export abstract class BaseXmlComponent {
|
||||
|
@ -34,7 +34,7 @@ export class NextAttributeComponent<T extends AttributeData> extends BaseXmlComp
|
||||
|
||||
public prepForXml(_: IContext): IXmlableObject {
|
||||
const attrs = Object.values<{ readonly key: string; readonly value: string | boolean | number }>(this.root)
|
||||
.filter(({ value }) => !!value)
|
||||
.filter(({ value }) => value !== undefined)
|
||||
.reduce((acc, { key, value }) => ({ ...acc, [key]: value }), {} as IXmlAttribute);
|
||||
return { _attr: attrs };
|
||||
}
|
||||
|
@ -62,7 +62,7 @@ describe("ImportedXmlComponent", () => {
|
||||
describe("#prepForXml()", () => {
|
||||
it("should transform for xml", () => {
|
||||
// tslint:disable-next-line: no-object-literal-type-assertion
|
||||
const converted = importedXmlComponent.prepForXml({} as IContext);
|
||||
const converted = importedXmlComponent.prepForXml({ stack: [] } as unknown as IContext);
|
||||
expect(converted).to.deep.equal({
|
||||
"w:test": [
|
||||
{
|
||||
|
@ -12,7 +12,15 @@ export abstract class XmlComponent extends BaseXmlComponent {
|
||||
this.root = new Array<BaseXmlComponent | string>();
|
||||
}
|
||||
|
||||
// This method is called by the formatter to get the XML representation of this component.
|
||||
// It is called recursively for all child components.
|
||||
// It is a serializer to be used in the xml library.
|
||||
// https://www.npmjs.com/package/xml
|
||||
// Child components can override this method to customize the XML representation, or execute side effects.
|
||||
public prepForXml(context: IContext): IXmlableObject | undefined {
|
||||
// Mutating the stack is required for performance reasons
|
||||
// eslint-disable-next-line functional/immutable-data
|
||||
context.stack.push(this);
|
||||
const children = this.root
|
||||
.map((comp) => {
|
||||
if (comp instanceof BaseXmlComponent) {
|
||||
@ -21,6 +29,9 @@ export abstract class XmlComponent extends BaseXmlComponent {
|
||||
return comp;
|
||||
})
|
||||
.filter((comp) => comp !== undefined); // Exclude undefined
|
||||
|
||||
// eslint-disable-next-line functional/immutable-data
|
||||
context.stack.pop();
|
||||
// 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.
|
||||
|
Reference in New Issue
Block a user