diff --git a/ts/docx/document/body/columns.ts b/ts/docx/document/body/columns.ts index d656fafeaf..12a0f0ae80 100644 --- a/ts/docx/document/body/columns.ts +++ b/ts/docx/document/body/columns.ts @@ -1,7 +1,11 @@ import {XmlComponent, Attributes} from "../../xml-components"; -export class Columns { +export class Columns implements XmlComponent { private cols: Array; + + xmlKeys = { + cols: 'w:cols' + } constructor() { this.cols = new Array(); diff --git a/ts/docx/document/body/doc-grid.ts b/ts/docx/document/body/doc-grid.ts index 3bc9bf2cb2..66104790c7 100644 --- a/ts/docx/document/body/doc-grid.ts +++ b/ts/docx/document/body/doc-grid.ts @@ -1,7 +1,11 @@ import {XmlComponent, Attributes} from "../../xml-components"; -export class DocumentGrid { +export class DocumentGrid implements XmlComponent { private docGrid: Array; + + xmlKeys = { + docGrid: 'w:docGrid' + } constructor() { this.docGrid = new Array(); diff --git a/ts/docx/document/body/index.ts b/ts/docx/document/body/index.ts index 0ca5a94dbc..7956fa2e5a 100644 --- a/ts/docx/document/body/index.ts +++ b/ts/docx/document/body/index.ts @@ -5,9 +5,13 @@ import {PageMargin} from "./page-margin"; import {Columns} from "./columns"; import {DocumentGrid} from "./doc-grid"; -export class Body { +export class Body implements XmlComponent { private body: Array; - + + xmlKeys = { + body: 'w:body' + } + constructor() { this.body = new Array(); this.body.push(new SectionProperties()); @@ -16,7 +20,7 @@ export class Body { this.body.push(new Columns()); this.body.push(new DocumentGrid()); } - + push(component: XmlComponent) { this.body.push(component); } diff --git a/ts/docx/document/body/page-margin.ts b/ts/docx/document/body/page-margin.ts index 4bd9861a99..891c6b037b 100644 --- a/ts/docx/document/body/page-margin.ts +++ b/ts/docx/document/body/page-margin.ts @@ -1,7 +1,11 @@ import {XmlComponent, Attributes} from "../../xml-components"; -export class PageMargin { +export class PageMargin implements XmlComponent { private pgMar: Array; + + xmlKeys = { + pgMar: 'w:pgMar' + } constructor() { this.pgMar = new Array(); diff --git a/ts/docx/document/body/page-size.ts b/ts/docx/document/body/page-size.ts index bc8b6da2b9..fa824f1b9a 100644 --- a/ts/docx/document/body/page-size.ts +++ b/ts/docx/document/body/page-size.ts @@ -1,7 +1,11 @@ import {XmlComponent, Attributes} from "../../xml-components"; -export class PageSize { +export class PageSize implements XmlComponent { private pgSz: Array; + + xmlKeys = { + pgSz: 'w:pgSz' + } constructor() { this.pgSz = new Array(); diff --git a/ts/docx/document/body/section-properties.ts b/ts/docx/document/body/section-properties.ts index 28a2494aa3..8e25e78bac 100644 --- a/ts/docx/document/body/section-properties.ts +++ b/ts/docx/document/body/section-properties.ts @@ -1,8 +1,12 @@ import {XmlComponent, Attributes} from "../../xml-components"; -export class SectionProperties { +export class SectionProperties implements XmlComponent { private sectPr: Array; + xmlKeys = { + sectPr: 'sectPr' + } + constructor() { this.sectPr = new Array(); this.sectPr.push(new Attributes({ diff --git a/ts/docx/paragraph/border.ts b/ts/docx/paragraph/border.ts index 272b20f747..6b3c1b1fb6 100644 --- a/ts/docx/paragraph/border.ts +++ b/ts/docx/paragraph/border.ts @@ -3,6 +3,10 @@ import {XmlComponent, Attributes} from "../xml-components"; class Border implements XmlComponent { private bottom: Array; + xmlKeys = { + bottom: 'w:bottom' + } + constructor() { this.bottom = new Array(); this.bottom.push(new Attributes({ @@ -14,9 +18,13 @@ class Border implements XmlComponent { } } -export class ThematicBreak { +export class ThematicBreak implements XmlComponent { private pBdr: Array; + xmlKeys = { + pBdr: 'w:pBdr' + } + constructor() { this.pBdr = new Array(); this.pBdr.push(new Border()); diff --git a/ts/docx/paragraph/index.ts b/ts/docx/paragraph/index.ts index 2395ad1fda..79b70a2d6b 100644 --- a/ts/docx/paragraph/index.ts +++ b/ts/docx/paragraph/index.ts @@ -7,8 +7,12 @@ import {TabStop} from "../tab-stop"; import {Style} from "./style"; import {NumberProperties} from "./unordered-list"; -class Alignment { +class Alignment implements XmlComponent { private jc: Array; + + xmlKeys = { + jc: 'w:jc' + } constructor(type: string) { this.jc = new Array(); @@ -18,9 +22,13 @@ class Alignment { } } -export class Paragraph { +export class Paragraph implements XmlComponent { private p: Array; private properties: ParagraphProperties; + + xmlKeys = { + p: 'w:p' + } constructor(text?: string) { this.p = new Array(); diff --git a/ts/docx/paragraph/page-break.ts b/ts/docx/paragraph/page-break.ts index 2d13cd7b5f..a40e5a8ee8 100644 --- a/ts/docx/paragraph/page-break.ts +++ b/ts/docx/paragraph/page-break.ts @@ -3,6 +3,11 @@ import {Run} from "../run"; class Break implements XmlComponent { private br: Array; + + xmlKeys = { + br: 'w:br' + } + constructor() { this.br = new Array(); this.br.push(new Attributes({ diff --git a/ts/docx/paragraph/properties.ts b/ts/docx/paragraph/properties.ts index a0adf57ccc..a47e1f550c 100644 --- a/ts/docx/paragraph/properties.ts +++ b/ts/docx/paragraph/properties.ts @@ -2,6 +2,10 @@ import {XmlComponent, Attributes} from "../xml-components"; export class ParagraphProperties implements XmlComponent { private pPr: Array; + + xmlKeys = { + pPr: 'w:rPr' + } constructor() { this.pPr = new Array(); diff --git a/ts/docx/paragraph/style.ts b/ts/docx/paragraph/style.ts index fa1770c941..fe41f6ee5d 100644 --- a/ts/docx/paragraph/style.ts +++ b/ts/docx/paragraph/style.ts @@ -2,6 +2,10 @@ import {XmlComponent, Attributes} from "../xml-components"; export class Style implements XmlComponent { private pStyle: Array; + + xmlKeys = { + pStyle: 'w:pStyle' + } constructor(type: string) { this.pStyle = new Array(); diff --git a/ts/docx/paragraph/unordered-list.ts b/ts/docx/paragraph/unordered-list.ts index c2f52a5028..c29421c8aa 100644 --- a/ts/docx/paragraph/unordered-list.ts +++ b/ts/docx/paragraph/unordered-list.ts @@ -4,6 +4,10 @@ import {Style} from "./style"; export class NumberProperties implements XmlComponent { private numPr: Array; + xmlKeys = { + numPr: 'w:numPr' + } + constructor() { this.numPr = new Array(); this.numPr.push(new IndentLevel(0)); @@ -13,7 +17,11 @@ export class NumberProperties implements XmlComponent { export class IndentLevel implements XmlComponent { private ilvl: Array; - + + xmlKeys = { + ilvl: 'w:ilvl' + } + constructor(level: number) { this.ilvl = new Array(); this.ilvl.push(new Attributes({ @@ -24,7 +32,11 @@ export class IndentLevel implements XmlComponent { export class NumberId implements XmlComponent { private ilvl: Array; - + + xmlKeys = { + ilvl: 'w:ilvl' + } + constructor(id: number) { this.ilvl = new Array(); this.ilvl.push(new Attributes({ diff --git a/ts/docx/run/emphasis.ts b/ts/docx/run/emphasis.ts index 540777c5eb..f5478404e3 100644 --- a/ts/docx/run/emphasis.ts +++ b/ts/docx/run/emphasis.ts @@ -1,8 +1,12 @@ import {XmlComponent, Attributes} from "../xml-components"; -export class Bold { +export class Bold implements XmlComponent { private b: Array; - + + xmlKeys = { + b: 'w:b' + } + constructor() { this.b = new Array(); this.b.push(new Attributes({ @@ -13,7 +17,11 @@ export class Bold { export class Italics { private i: Array; - + + xmlKeys = { + i: 'w:i' + } + constructor() { this.i = new Array(); this.i.push(new Attributes({ @@ -24,7 +32,11 @@ export class Italics { export class Underline { private u: Array; - + + xmlKeys = { + u: 'w:u' + } + constructor() { this.u = new Array(); this.u.push(new Attributes({ diff --git a/ts/docx/run/index.ts b/ts/docx/run/index.ts index 69582fb940..6080dbd483 100644 --- a/ts/docx/run/index.ts +++ b/ts/docx/run/index.ts @@ -5,6 +5,10 @@ import {Bold, Italics, Underline} from "./emphasis"; export class Run implements XmlComponent { protected r: Array; private properties: RunProperties; + + xmlKeys = { + r: 'w:r' + } constructor() { this.r = new Array(); diff --git a/ts/docx/run/properties.ts b/ts/docx/run/properties.ts index 0453113a8b..35567b81e4 100644 --- a/ts/docx/run/properties.ts +++ b/ts/docx/run/properties.ts @@ -1,7 +1,11 @@ import {XmlComponent, Attributes} from "../xml-components"; -export class RunProperties { +export class RunProperties implements XmlComponent { private rPr: Array; + + xmlKeys = { + rPr: 'w:rPr' + } constructor() { this.rPr = new Array(); diff --git a/ts/docx/tab-stop.ts b/ts/docx/tab-stop.ts index 7b7e5de64d..d258672621 100644 --- a/ts/docx/tab-stop.ts +++ b/ts/docx/tab-stop.ts @@ -1,3 +1,5 @@ -export class TabStop { +import {XmlComponent, Attributes} from "./xml-components"; +export class TabStop implements XmlComponent{ + xmlKeys = {} } diff --git a/ts/docx/xml-components/index.ts b/ts/docx/xml-components/index.ts index b415ac616a..a04f48a579 100644 --- a/ts/docx/xml-components/index.ts +++ b/ts/docx/xml-components/index.ts @@ -1,5 +1,5 @@ export interface XmlComponent { - + xmlKeys: Object; } interface AttributesProperties { @@ -26,6 +26,8 @@ interface AttributesProperties { export class Attributes implements XmlComponent { private _attrs: Object; + xmlKeys = {}; + constructor(properties?: AttributesProperties) { this._attrs = properties @@ -38,6 +40,10 @@ export class Attributes implements XmlComponent { export class Text implements XmlComponent { private t: string; + xmlKeys = { + t: 'w:t' + } + constructor(text: string) { this.t = text; } diff --git a/ts/export/formatter.ts b/ts/export/formatter.ts index d1fa335b74..e3ae7ca590 100644 --- a/ts/export/formatter.ts +++ b/ts/export/formatter.ts @@ -2,57 +2,36 @@ import * as _ from "lodash"; export class Formatter { - private xmlKeyDictionary = { - p: 'w:p', - t: 'w:t', - color: 'w:color', - space: 'w:space', - sz: 'w:sz', - val: 'w:val', - type: 'w:type', - ilvl: 'w:ilvl', - numId: 'w:numId', - pBdr: 'w:pBdr', - jc: 'w:jc', - r: 'w:r', - pPr: 'w:pPr', - pStyle: 'w:pStyle', - numPr: 'w:numPr', - b: 'w:b', - i: 'w:i', - u: 'w:u', - rPr: 'w:rPr' - }; - format(input: any): Object { - var newJson = this.jsonify(input); - - this.deepTraverseJson(newJson, (parent, value, key) => { + this.deepTraverseJson(input, (parent, value, key) => { if (isNaN(key)) { - var newKey = this.getReplacementKey(key); - parent[newKey] = parent[key]; + var newKey = this.getReplacementKey(parent, key); if (newKey !== key) { + parent[newKey] = parent[key]; delete parent[key]; } else { - console.error("Key is not in dictionary:" + key); + //console.error("Key is not in dictionary:" + key); } } }); - - newJson = this.clenseProperties(newJson); + + var newJson = this.clense(input); return newJson; } - private clenseProperties(input: any): Object { + private clense(input: any): Object { var newJson = this.jsonify(input); this.deepTraverseJson(newJson, (parent, value, key) => { if (key === "properties") { delete parent[key]; } + if (key === "xmlKeys") { + delete parent[key]; + } }); - + return newJson } @@ -63,15 +42,15 @@ export class Formatter { private deepTraverseJson(json: Object, lambda: (json: any, value: any, key: any) => void): void { _.forOwn(json, (value, key) => { - if (_.isObject(value)) { + if (_.isObject(value) && key !== "xmlKeys") { this.deepTraverseJson(value, lambda); } lambda(json, value, key); }); } - private getReplacementKey(key: string): string { - var newKey = this.xmlKeyDictionary[key]; + private getReplacementKey(input: any, key: string): string { + var newKey = input.xmlKeys[key]; if (newKey !== undefined) { return newKey; diff --git a/ts/export/packer/express.ts b/ts/export/packer/express.ts index bee0d53462..906d32cae5 100644 --- a/ts/export/packer/express.ts +++ b/ts/export/packer/express.ts @@ -7,7 +7,7 @@ export class ExpressPacker extends Packer { private res: express.Response; constructor(document: Document, res: express.Response) { - super(document); + super(document, null, null); this.res = res; this.res.on('close', () => { diff --git a/ts/export/packer/local.ts b/ts/export/packer/local.ts index a7d66c966f..07b2b830b6 100644 --- a/ts/export/packer/local.ts +++ b/ts/export/packer/local.ts @@ -6,7 +6,7 @@ export class LocalPacker extends Packer { private stream: fs.WriteStream constructor(document: Document, path: string) { - super(document); + super(document, null, null); this.stream = fs.createWriteStream(path); } diff --git a/ts/properties/index.ts b/ts/properties/index.ts index e69de29bb2..ef40cc5b5f 100644 --- a/ts/properties/index.ts +++ b/ts/properties/index.ts @@ -0,0 +1,3 @@ +export class Properties { + +} \ No newline at end of file diff --git a/ts/style/index.ts b/ts/style/index.ts index e69de29bb2..2e89774120 100644 --- a/ts/style/index.ts +++ b/ts/style/index.ts @@ -0,0 +1,3 @@ +export class Style { + +} \ No newline at end of file diff --git a/ts/tests/bodyTest.ts b/ts/tests/bodyTest.ts index b19eee512c..1eb0179bdd 100644 --- a/ts/tests/bodyTest.ts +++ b/ts/tests/bodyTest.ts @@ -8,7 +8,7 @@ function jsonify(obj: Object) { return JSON.parse(stringifiedJson); } -describe.only('Body', () => { +describe('Body', () => { var body: Body; beforeEach(() => { diff --git a/ts/tests/formatterTest.ts b/ts/tests/formatterTest.ts index 829bf2fe8b..ba3da3b9e4 100644 --- a/ts/tests/formatterTest.ts +++ b/ts/tests/formatterTest.ts @@ -19,7 +19,7 @@ describe('Formatter', () => { }); describe('#format()', () => { - it("should format simple paragraph", () => { + it.only("should format simple paragraph", () => { var paragraph = new docx.Paragraph(); var newJson = formatter.format(paragraph); newJson = jsonify(newJson);