Added back tests and method
This commit is contained in:
@ -1,95 +1,81 @@
|
|||||||
// import { expect } from "chai";
|
import { expect } from "chai";
|
||||||
// import { convertToXmlComponent, ImportedXmlComponent } from "./";
|
import { Element, xml2js } from "xml-js";
|
||||||
|
|
||||||
// const xmlString = `
|
import { ImportedXmlComponent } from "./";
|
||||||
// <w:p w:one="value 1" w:two="value 2">
|
import { convertToXmlComponent } from "./imported-xml-component";
|
||||||
// <w:rPr>
|
|
||||||
// <w:noProof>some value</w:noProof>
|
|
||||||
// </w:rPr>
|
|
||||||
// <w:r active="true">
|
|
||||||
// <w:t>Text 1</w:t>
|
|
||||||
// </w:r>
|
|
||||||
// <w:r active="true">
|
|
||||||
// <w:t>Text 2</w:t>
|
|
||||||
// </w:r>
|
|
||||||
// </w:p>
|
|
||||||
// `;
|
|
||||||
|
|
||||||
// // tslint:disable:object-literal-key-quotes
|
const xmlString = `
|
||||||
// const importedXmlElement = {
|
<w:p w:one="value 1" w:two="value 2">
|
||||||
// "w:p": {
|
<w:rPr>
|
||||||
// _attr: { "w:one": "value 1", "w:two": "value 2" },
|
<w:noProof>some value</w:noProof>
|
||||||
// "w:rPr": { "w:noProof": "some value" },
|
</w:rPr>
|
||||||
// "w:r": [{ _attr: { active: "true" }, "w:t": "Text 1" }, { _attr: { active: "true" }, "w:t": "Text 2" }],
|
<w:r active="true">
|
||||||
// },
|
<w:t>Text 1</w:t>
|
||||||
// };
|
</w:r>
|
||||||
// // tslint:enable:object-literal-key-quotes
|
<w:r active="true">
|
||||||
|
<w:t>Text 2</w:t>
|
||||||
|
</w:r>
|
||||||
|
</w:p>
|
||||||
|
`;
|
||||||
|
|
||||||
// const convertedXmlElement = {
|
const convertedXmlElement = {
|
||||||
// deleted: false,
|
deleted: false,
|
||||||
// rootKey: "w:p",
|
root: [
|
||||||
// root: [
|
{
|
||||||
// {
|
deleted: false,
|
||||||
// deleted: false,
|
rootKey: "w:p",
|
||||||
// rootKey: "w:rPr",
|
root: [
|
||||||
// root: [{ deleted: false, rootKey: "w:noProof", root: ["some value"] }],
|
{ deleted: false, rootKey: "w:rPr", root: [{ deleted: false, rootKey: "w:noProof", root: ["some value"] }] },
|
||||||
// },
|
{ deleted: false, rootKey: "w:r", root: [{ deleted: false, rootKey: "w:t", root: ["Text 1"] }], _attr: { active: "true" } },
|
||||||
// {
|
{ deleted: false, rootKey: "w:r", root: [{ deleted: false, rootKey: "w:t", root: ["Text 2"] }], _attr: { active: "true" } },
|
||||||
// deleted: false,
|
],
|
||||||
// rootKey: "w:r",
|
_attr: { "w:one": "value 1", "w:two": "value 2" },
|
||||||
// root: [{ deleted: false, rootKey: "w:t", root: ["Text 1"] }],
|
},
|
||||||
// _attr: { active: "true" },
|
],
|
||||||
// },
|
rootKey: undefined,
|
||||||
// {
|
};
|
||||||
// deleted: false,
|
|
||||||
// rootKey: "w:r",
|
|
||||||
// root: [{ deleted: false, rootKey: "w:t", root: ["Text 2"] }],
|
|
||||||
// _attr: { active: "true" },
|
|
||||||
// },
|
|
||||||
// ],
|
|
||||||
// _attr: { "w:one": "value 1", "w:two": "value 2" },
|
|
||||||
// };
|
|
||||||
|
|
||||||
// describe("ImportedXmlComponent", () => {
|
describe("ImportedXmlComponent", () => {
|
||||||
// let importedXmlComponent: ImportedXmlComponent;
|
let importedXmlComponent: ImportedXmlComponent;
|
||||||
|
|
||||||
// beforeEach(() => {
|
beforeEach(() => {
|
||||||
// const attributes = {
|
const attributes = {
|
||||||
// someAttr: "1",
|
someAttr: "1",
|
||||||
// otherAttr: "2",
|
otherAttr: "2",
|
||||||
// };
|
};
|
||||||
// importedXmlComponent = new ImportedXmlComponent("w:test", attributes);
|
importedXmlComponent = new ImportedXmlComponent("w:test", attributes);
|
||||||
// importedXmlComponent.push(new ImportedXmlComponent("w:child"));
|
importedXmlComponent.push(new ImportedXmlComponent("w:child"));
|
||||||
// });
|
});
|
||||||
|
|
||||||
// describe("#prepForXml()", () => {
|
describe("#prepForXml()", () => {
|
||||||
// it("should transform for xml", () => {
|
it("should transform for xml", () => {
|
||||||
// const converted = importedXmlComponent.prepForXml();
|
const converted = importedXmlComponent.prepForXml();
|
||||||
// expect(converted).to.eql({
|
expect(converted).to.eql({
|
||||||
// "w:test": [
|
"w:test": [
|
||||||
// {
|
{
|
||||||
// _attr: {
|
_attr: {
|
||||||
// someAttr: "1",
|
someAttr: "1",
|
||||||
// otherAttr: "2",
|
otherAttr: "2",
|
||||||
// },
|
},
|
||||||
// },
|
},
|
||||||
// {
|
{
|
||||||
// "w:child": [],
|
"w:child": [],
|
||||||
// },
|
},
|
||||||
// ],
|
],
|
||||||
// });
|
});
|
||||||
// });
|
});
|
||||||
// });
|
});
|
||||||
|
|
||||||
// it("should create XmlComponent from xml string", () => {
|
it("should create XmlComponent from xml string", () => {
|
||||||
// const converted = ImportedXmlComponent.fromXmlString(xmlString);
|
const converted = ImportedXmlComponent.fromXmlString(xmlString);
|
||||||
// expect(converted).to.eql(convertedXmlElement);
|
expect(converted).to.eql(convertedXmlElement);
|
||||||
// });
|
});
|
||||||
|
|
||||||
// describe("convertToXmlComponent", () => {
|
describe("convertToXmlComponent", () => {
|
||||||
// it("should convert to xml component", () => {
|
it("should convert to xml component", () => {
|
||||||
// const converted = convertToXmlComponent("w:p", importedXmlElement["w:p"]);
|
const xmlObj = xml2js(xmlString, { compact: false }) as Element;
|
||||||
// expect(converted).to.eql(convertedXmlElement);
|
const converted = convertToXmlComponent(xmlObj);
|
||||||
// });
|
expect(converted).to.eql(convertedXmlElement);
|
||||||
// });
|
});
|
||||||
// });
|
});
|
||||||
|
});
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// tslint:disable:no-any
|
// tslint:disable:no-any
|
||||||
import { Element as XmlElement } from "xml-js";
|
import { Element as XmlElement, xml2js } from "xml-js";
|
||||||
import { IXmlableObject, XmlComponent } from ".";
|
import { IXmlableObject, XmlComponent } from ".";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -9,6 +9,7 @@ import { IXmlableObject, XmlComponent } from ".";
|
|||||||
|
|
||||||
export function convertToXmlComponent(element: XmlElement): ImportedXmlComponent | string | undefined {
|
export function convertToXmlComponent(element: XmlElement): ImportedXmlComponent | string | undefined {
|
||||||
switch (element.type) {
|
switch (element.type) {
|
||||||
|
case undefined:
|
||||||
case "element":
|
case "element":
|
||||||
const xmlComponent = new ImportedXmlComponent(element.name as string, element.attributes);
|
const xmlComponent = new ImportedXmlComponent(element.name as string, element.attributes);
|
||||||
const childElments = element.elements || [];
|
const childElments = element.elements || [];
|
||||||
@ -30,6 +31,15 @@ export function convertToXmlComponent(element: XmlElement): ImportedXmlComponent
|
|||||||
* Represents imported xml component from xml file.
|
* Represents imported xml component from xml file.
|
||||||
*/
|
*/
|
||||||
export class ImportedXmlComponent extends XmlComponent {
|
export class ImportedXmlComponent extends XmlComponent {
|
||||||
|
/**
|
||||||
|
* 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.
|
* Converts the xml string to a XmlComponent tree.
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user