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

89 lines
2.8 KiB
TypeScript
Raw Normal View History

2018-10-31 21:57:10 +00:00
import { Element as XmlElement, xml2js } from "xml-js";
2022-08-31 08:59:27 +01:00
import { IXmlableObject, XmlAttributeComponent, XmlComponent } from "@file/xml-components";
2021-03-11 01:06:55 +00:00
import { IContext } from "./base";
2018-10-16 11:28:25 +03:00
/**
* Converts the given xml element (in json format) into XmlComponent.
*
2018-10-16 11:28:25 +03:00
* @param element the xml element in json presentation
*/
2018-10-17 09:15:32 +03:00
2022-08-31 08:59:27 +01:00
export const convertToXmlComponent = (element: XmlElement): ImportedXmlComponent | string | undefined => {
2018-10-17 09:15:32 +03:00
switch (element.type) {
2018-10-31 21:57:10 +00:00
case undefined:
2018-10-17 09:15:32 +03:00
case "element":
2022-09-19 20:48:50 +01:00
// eslint-disable-next-line no-case-declarations
2018-10-17 09:15:32 +03:00
const xmlComponent = new ImportedXmlComponent(element.name as string, element.attributes);
2022-09-19 20:48:50 +01:00
// eslint-disable-next-line no-case-declarations
2022-07-05 05:06:32 +01:00
const childElements = element.elements || [];
for (const childElm of childElements) {
2018-10-17 09:15:32 +03:00
const child = convertToXmlComponent(childElm);
if (child !== undefined) {
2023-11-01 22:52:41 +00:00
// eslint-disable-next-line functional/immutable-data
2018-10-17 09:15:32 +03:00
xmlComponent.push(child);
}
2018-10-16 11:28:25 +03:00
}
2018-10-17 09:15:32 +03:00
return xmlComponent;
case "text":
return element.text as string;
default:
return undefined;
2018-10-16 11:28:25 +03:00
}
2022-08-31 08:59:27 +01:00
};
2018-10-16 11:28:25 +03:00
2022-09-19 20:48:50 +01:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
class ImportedXmlComponentAttributes extends XmlAttributeComponent<any> {
// noop
}
/**
* Represents imported xml component from xml file.
*/
export class ImportedXmlComponent extends XmlComponent {
2018-10-31 21:57:10 +00:00
/**
* Converts the xml string to a XmlComponent tree.
*
* @param importedContent xml content of the imported component
*/
public static fromXmlString(importedContent: string): ImportedXmlComponent {
const xmlObj = xml2js(importedContent, { compact: false }) as XmlElement;
return convertToXmlComponent(xmlObj) as ImportedXmlComponent;
}
/**
* Converts the xml string to a XmlComponent tree.
*
* @param importedContent xml content of the imported component
*/
2022-09-19 20:48:50 +01:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2022-08-31 07:52:27 +01:00
public constructor(rootKey: string, _attr?: any) {
super(rootKey);
if (_attr) {
2023-11-01 22:52:41 +00:00
// eslint-disable-next-line functional/immutable-data
this.root.push(new ImportedXmlComponentAttributes(_attr));
}
}
2018-10-16 11:28:25 +03:00
public push(xmlComponent: XmlComponent | string): void {
this.root.push(xmlComponent);
}
}
/**
* Used for the attributes of root element that is being imported.
*/
export class ImportedRootElementAttributes extends XmlComponent {
2022-09-19 20:48:50 +01:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2022-08-31 07:52:27 +01:00
public constructor(private readonly _attr: any) {
super("");
}
2021-03-11 01:06:55 +00:00
public prepForXml(_: IContext): IXmlableObject {
return {
_attr: this._attr,
};
}
}