diff --git a/ts/docx/document/index.ts b/ts/docx/document/index.ts index fc96b34a53..6bac5313cb 100644 --- a/ts/docx/document/index.ts +++ b/ts/docx/document/index.ts @@ -5,11 +5,6 @@ import {Paragraph} from "../paragraph"; export class Document extends XmlComponent { private body: Body; - - xmlKeys = { - document: "w:document", - body: "w:body" - }; constructor() { super("w:document"); @@ -36,7 +31,13 @@ export class Document extends XmlComponent { this.root.push(this.body); } - addParagraph(paragraph: Paragraph) { + addParagraph(paragraph: Paragraph): void { this.body.push(paragraph); } + + clearVariables(): void { + console.log("clearing"); + this.body.clearVariables(); + delete this.body; + } } \ No newline at end of file diff --git a/ts/docx/xml-components/index.ts b/ts/docx/xml-components/index.ts index 42c15ed5de..bac715b390 100644 --- a/ts/docx/xml-components/index.ts +++ b/ts/docx/xml-components/index.ts @@ -8,6 +8,8 @@ export abstract class BaseXmlComponent { } abstract replaceKey(): void; + clearVariables(): void { + }; } export abstract class XmlComponent extends BaseXmlComponent { @@ -23,7 +25,9 @@ export abstract class XmlComponent extends BaseXmlComponent { //console.log(this.root); if (this.root !== undefined) { this.root.forEach(root => { - root.replaceKey(); + if (root && root instanceof BaseXmlComponent) { + root.replaceKey(); + } }); this[this.rootKey] = this.root; delete this.root; diff --git a/ts/export/formatter.ts b/ts/export/formatter.ts index 2d736008fd..83492da2d7 100644 --- a/ts/export/formatter.ts +++ b/ts/export/formatter.ts @@ -5,8 +5,9 @@ export class Formatter { format(input: any): Object { this.replaceKeys(input); + input.clearVariables(); var newJson = this.clense(input); - + console.log(JSON.stringify(newJson, null, " ")); return newJson; } @@ -26,7 +27,7 @@ export class Formatter { //console.log(value); //parent.replaceKey(); }); - + return input; } diff --git a/ts/export/packer/packer.ts b/ts/export/packer/packer.ts index c8c4c2a845..d57ba72776 100644 --- a/ts/export/packer/packer.ts +++ b/ts/export/packer/packer.ts @@ -46,6 +46,7 @@ export abstract class Packer { prefix: "root" });*/ var xmlDocument = xml(this.formatter.format(this.document)); + console.log(xmlDocument); var xmlStyle = xml(this.style); var xmlProperties = xml(this.formatter.format(this.properties), { declaration: { standalone: 'yes', encoding: 'UTF-8' } }); diff --git a/ts/properties/components.ts b/ts/properties/components.ts index 1aebf5c3ab..bc8cf457ac 100644 --- a/ts/properties/components.ts +++ b/ts/properties/components.ts @@ -2,13 +2,13 @@ import {XmlComponent} from "../docx/xml-components"; import {DocumentAttributes} from "../docx/xml-components/document-attributes"; abstract class Component extends XmlComponent { - protected createNullBlockOrValue(value: string): XmlComponent { - /*if (value === undefined) { + protected createNullBlockOrValue(value: string): any { + if (value === undefined) { return [{}]; } else { return value; - }*/ - return null; + } + //return null; } } export class Title extends Component { diff --git a/ts/tests/formatterTest.ts b/ts/tests/formatterTest.ts index 8b8494cf82..7165c44c16 100644 --- a/ts/tests/formatterTest.ts +++ b/ts/tests/formatterTest.ts @@ -36,12 +36,11 @@ describe("Formatter", () => { assert(stringifiedJson.indexOf("xmlKeys") < 0); }); - it.only("should format simple paragraph with bold text", () => { + it("should format simple paragraph with bold text", () => { var paragraph = new docx.Paragraph(); paragraph.addText(new docx.TextRun("test").bold()); var newJson = formatter.format(paragraph); newJson = jsonify(newJson); - console.log(JSON.stringify(newJson, null, " ")); assert.isDefined(newJson["w:p"][1]["w:r"][0]["w:rPr"][0]["w:b"][0]["_attr"]["w:val"]); }); diff --git a/ts/tests/localPackerTest.ts b/ts/tests/localPackerTest.ts index b8d1b55265..b9eb1f3dea 100644 --- a/ts/tests/localPackerTest.ts +++ b/ts/tests/localPackerTest.ts @@ -25,7 +25,7 @@ describe("Packer", () => { packer = new LocalPacker(document, DefaultStyle(), properties, "build/tests/test.docx"); }); - describe('#pack()', () => { + describe.only('#pack()', () => { it("should create a standard docx file", (done) => { packer.pack(); diff --git a/ts/tests/paragraphStyleTest.ts b/ts/tests/paragraphStyleTest.ts index 8f1be7b26d..b19299b8eb 100644 --- a/ts/tests/paragraphStyleTest.ts +++ b/ts/tests/paragraphStyleTest.ts @@ -15,13 +15,14 @@ describe("ParagraphStyle", () => { it("should create a style with given value", () => { style = new Style("test"); var newJson = jsonify(style); - assert(newJson.pStyle[0]._attr.val === "test"); + console.log(newJson.root[0].root.val); + assert.equal(newJson.root[0].root.val, "test"); }); it("should create a style with blank val", () => { style = new Style(""); var newJson = jsonify(style); - assert(newJson.pStyle[0]._attr.val === ""); + assert.equal(newJson.root[0].root.val, ""); }); }); diff --git a/ts/tests/paragraphTest.ts b/ts/tests/paragraphTest.ts index 38eedf8887..2881e28c7c 100644 --- a/ts/tests/paragraphTest.ts +++ b/ts/tests/paragraphTest.ts @@ -34,8 +34,7 @@ describe("Paragraph", () => { it("should add heading style to JSON", () => { paragraph.heading1(); var newJson = jsonify(paragraph); - - assert(newJson.root[1].root[1].root[0]._attr.val === "Heading1"); + assert.equal(newJson.root[0].root[1].root[0].root.val, "Heading1"); }); }); @@ -44,7 +43,7 @@ describe("Paragraph", () => { paragraph.heading2(); var newJson = jsonify(paragraph); - assert(newJson.p[1].pPr[1].pStyle[0]._attr.val === "Heading2"); + assert.equal(newJson.root[0].root[1].root[0].root.val, "Heading2"); }); }); @@ -53,7 +52,7 @@ describe("Paragraph", () => { paragraph.heading3(); var newJson = jsonify(paragraph); - assert(newJson.p[1].pPr[1].pStyle[0]._attr.val === "Heading3"); + assert.equal(newJson.root[0].root[1].root[0].root.val, "Heading3"); }); }); @@ -62,7 +61,7 @@ describe("Paragraph", () => { paragraph.title(); var newJson = jsonify(paragraph); - assert(newJson.p[1].pPr[1].pStyle[0]._attr.val === "Title"); + assert.equal(newJson.root[0].root[1].root[0].root.val, "Title"); }); }); @@ -71,7 +70,7 @@ describe("Paragraph", () => { paragraph.center(); var newJson = jsonify(paragraph); - assert(newJson.p[1].pPr[1].jc[0]._attr.val === "center"); + assert.equal(newJson.root[0].root[1].root[0].root.val, "center"); }); }); @@ -79,8 +78,7 @@ describe("Paragraph", () => { it("should add thematic break to JSON", () => { paragraph.thematicBreak(); var newJson = jsonify(paragraph); - - assert.isDefined(newJson.p[1].pPr[1].pBdr); + assert.equal(newJson.root[0].root[1].rootKey, "w:pBdr"); }); }); @@ -88,13 +86,13 @@ describe("Paragraph", () => { it("should add page break to JSON", () => { paragraph.pageBreak(); var newJson = jsonify(paragraph); - assert.isDefined(newJson.p[1].pPr[1].r[1].br); + assert.equal(newJson.root[0].root[1].root[1].rootKey, "w:br"); }); it("should add page break with 'page' type", () => { paragraph.pageBreak(); var newJson = jsonify(paragraph); - assert(newJson.p[1].pPr[1].r[1].br[0]._attr.type === "page"); + assert.equal(newJson.root[0].root[1].root[1].root[0].root.type, "page"); }); }); @@ -102,13 +100,13 @@ describe("Paragraph", () => { it("should add list paragraph style to JSON", () => { paragraph.bullet(); var newJson = jsonify(paragraph); - assert(newJson.p[1].pPr[1].pStyle[0]._attr.val === "ListParagraph"); + assert.equal(newJson.root[0].root[1].root[0].root.val, "ListParagraph"); }); it("it should add numbered properties", () => { paragraph.bullet(); var newJson = jsonify(paragraph); - assert.isDefined(newJson.p[1].pPr[2].numPr); + assert.isDefined(newJson.root[0].root[2]); }); }); }); \ No newline at end of file