From f1fb0c4f2218b18bbc431afd52bfea99faba6516 Mon Sep 17 00:00:00 2001 From: felipe Date: Tue, 7 Mar 2017 19:22:10 +0100 Subject: [PATCH 01/12] initial attempt at Table --- ts/docx/document/index.ts | 6 +++ ts/docx/index.ts | 1 + ts/docx/table.ts | 3 -- ts/docx/table/grid.ts | 17 ++++++++ ts/docx/table/index.ts | 83 +++++++++++++++++++++++++++++++++++++ ts/docx/table/properties.ts | 24 +++++++++++ 6 files changed, 131 insertions(+), 3 deletions(-) delete mode 100644 ts/docx/table.ts create mode 100644 ts/docx/table/grid.ts create mode 100644 ts/docx/table/index.ts create mode 100644 ts/docx/table/properties.ts diff --git a/ts/docx/document/index.ts b/ts/docx/document/index.ts index 555ebf55bf..a6e8db42b4 100644 --- a/ts/docx/document/index.ts +++ b/ts/docx/document/index.ts @@ -1,7 +1,9 @@ import { Paragraph } from "../paragraph"; +import { Table } from "../table"; import { XmlComponent } from "../xml-components"; import { Body } from "./body"; import { DocumentAttributes } from "./document-attributes"; + export class Document extends XmlComponent { private body: Body; @@ -39,4 +41,8 @@ export class Document extends XmlComponent { this.addParagraph(para); return para; } + + public addTable(table: Table): void { + this.body.push(table); + } } diff --git a/ts/docx/index.ts b/ts/docx/index.ts index 3c7ccdd38a..ad1049df20 100644 --- a/ts/docx/index.ts +++ b/ts/docx/index.ts @@ -2,3 +2,4 @@ export { Document } from "./document"; export { Paragraph } from "./paragraph"; export { Run } from "./run"; export { TextRun } from "./run/text-run"; +export { Table } from './table'; diff --git a/ts/docx/table.ts b/ts/docx/table.ts deleted file mode 100644 index fa2be84ac5..0000000000 --- a/ts/docx/table.ts +++ /dev/null @@ -1,3 +0,0 @@ -export class Table { - -} diff --git a/ts/docx/table/grid.ts b/ts/docx/table/grid.ts new file mode 100644 index 0000000000..70404104c0 --- /dev/null +++ b/ts/docx/table/grid.ts @@ -0,0 +1,17 @@ +import {XmlComponent, Attributes} from "../xml-components"; + +export class TableGrid extends XmlComponent { + private cols: Array; + constructor(cols: Array) { + super('w:tblGrid'); + this.cols = cols; + cols.forEach(col => this.root.push(col)); + } +} + +export class GridCol extends XmlComponent { + constructor(width?: number) { + super('w:gridCol'); + this.root.push(new Attributes({w: width.toString()})) + } +} diff --git a/ts/docx/table/index.ts b/ts/docx/table/index.ts new file mode 100644 index 0000000000..757af155eb --- /dev/null +++ b/ts/docx/table/index.ts @@ -0,0 +1,83 @@ +import {XmlComponent, Attributes} from "../xml-components"; +import {Paragraph} from "../paragraph"; +import {TableProperties} from "./properties"; +import {TableGrid, GridCol} from './grid'; + +export class Table extends XmlComponent { + properties: TableProperties; + private rows: Array; + private grid: TableGrid; + + constructor(rows: number, cols: number) { + super('w:tbl'); + this.properties = new TableProperties(); + this.root.push(this.properties); + + const gridCols = []; + for (let i = 0; i++; i < cols) { + gridCols.push(new GridCol()); + } + this.grid = new TableGrid(gridCols); + this.root.push(this.grid); + + this.rows = []; + for (let i = 0; i < rows; i++) { + const cells = []; + for (let j = 0; j < cols; j++) { + cells.push(new TableCell()); + } + const row = new TableRow(cells); + this.rows.push(row); + this.root.push(row); + } + } + + getRow(ix: number): TableRow { + return this.rows[ix]; + } +} + +class TableRow extends XmlComponent { + private properties: TableRowProperties; + private cells: Array; + + constructor(cells: Array) { + super('w:tr'); + this.properties = new TableRowProperties(); + this.root.push(this.properties); + this.cells = cells; + cells.forEach(c => this.root.push(c)) + } + + getCell(ix: number): TableCell { + return this.cells[ix]; + } +} + +class TableRowProperties extends XmlComponent { + constructor() { + super('w:trPr'); + } +} + +class TableCell extends XmlComponent { + private properties: TableCellProperties; + content: any; + + constructor() { + super('w:tc'); + this.properties = new TableCellProperties(); + this.root.push(this.properties); + this.root.push() + // Table cells can have any block-level content, but for now + // we only allow a single paragraph: + this.content = new Paragraph(); + this.root.push(this.content); + } +} + +class TableCellProperties extends XmlComponent { + constructor() { + super('w:tcPr'); + } +} diff --git a/ts/docx/table/properties.ts b/ts/docx/table/properties.ts new file mode 100644 index 0000000000..d37fef0c1b --- /dev/null +++ b/ts/docx/table/properties.ts @@ -0,0 +1,24 @@ +import {XmlComponent, Attributes} from "../xml-components"; + +export class TableProperties extends XmlComponent { + private width: PreferredTableWidth; + + constructor() { + super('w:tblPr'); + } + + setWidth(type: string, w: string) { + this.width = new PreferredTableWidth(type, w); + this.root.push(this.width); + } +} + +class PreferredTableWidth extends XmlComponent { + constructor(type: string, w: string) { + super('w:tblW'); + this.root.push(new Attributes({ + type, + w, + })) + } +} From e7e5c61a90d18484353f66ee5b48b855f2e82268 Mon Sep 17 00:00:00 2001 From: felipe Date: Fri, 10 Mar 2017 16:51:19 +0100 Subject: [PATCH 02/12] clean up initial table attempt (linter, interfaces, etc.) --- ts/docx/index.ts | 2 +- ts/docx/table/grid.ts | 18 ++++++++------- ts/docx/table/index.ts | 44 ++++++++++++++++++++----------------- ts/docx/table/properties.ts | 32 ++++++++++++++++----------- 4 files changed, 54 insertions(+), 42 deletions(-) diff --git a/ts/docx/index.ts b/ts/docx/index.ts index ad1049df20..875821c29a 100644 --- a/ts/docx/index.ts +++ b/ts/docx/index.ts @@ -2,4 +2,4 @@ export { Document } from "./document"; export { Paragraph } from "./paragraph"; export { Run } from "./run"; export { TextRun } from "./run/text-run"; -export { Table } from './table'; +export { Table } from "./table"; diff --git a/ts/docx/table/grid.ts b/ts/docx/table/grid.ts index 70404104c0..91c38ddd21 100644 --- a/ts/docx/table/grid.ts +++ b/ts/docx/table/grid.ts @@ -1,17 +1,19 @@ -import {XmlComponent, Attributes} from "../xml-components"; +import { XmlAttributeComponent, XmlComponent } from "../xml-components"; export class TableGrid extends XmlComponent { - private cols: Array; - constructor(cols: Array) { - super('w:tblGrid'); - this.cols = cols; - cols.forEach(col => this.root.push(col)); + constructor(cols: number[]) { + super("w:tblGrid"); + cols.forEach((col) => this.root.push(new GridCol(col))); } } +class GridColAttributes extends XmlAttributeComponent<{w: number}> { + protected xmlKeys = {w: "w:w"}; +} + export class GridCol extends XmlComponent { constructor(width?: number) { - super('w:gridCol'); - this.root.push(new Attributes({w: width.toString()})) + super("w:gridCol"); + this.root.push(new GridColAttributes({w: width})); } } diff --git a/ts/docx/table/index.ts b/ts/docx/table/index.ts index 757af155eb..15656c6e45 100644 --- a/ts/docx/table/index.ts +++ b/ts/docx/table/index.ts @@ -1,21 +1,22 @@ -import {XmlComponent, Attributes} from "../xml-components"; -import {Paragraph} from "../paragraph"; -import {TableProperties} from "./properties"; -import {TableGrid, GridCol} from './grid'; +import { Paragraph } from "../paragraph"; +import { XmlComponent } from "../xml-components"; + +import { GridCol, TableGrid } from "./grid"; +import { TableProperties } from "./properties"; export class Table extends XmlComponent { - properties: TableProperties; - private rows: Array; + private properties: TableProperties; + private rows: TableRow[]; private grid: TableGrid; constructor(rows: number, cols: number) { - super('w:tbl'); + super("w:tbl"); this.properties = new TableProperties(); this.root.push(this.properties); - const gridCols = []; + const gridCols: number[] = []; for (let i = 0; i++; i < cols) { - gridCols.push(new GridCol()); + gridCols.push(0); } this.grid = new TableGrid(gridCols); this.root.push(this.grid); @@ -32,43 +33,46 @@ export class Table extends XmlComponent { } } - getRow(ix: number): TableRow { + public getRow(ix: number): TableRow { return this.rows[ix]; } + + public getCell(row: number, col: number): TableCell { + return this.getRow(row).getCell(col); + } } class TableRow extends XmlComponent { private properties: TableRowProperties; - private cells: Array; + private cells: TableCell[]; - constructor(cells: Array) { - super('w:tr'); + constructor(cells: TableCell[]) { + super("w:tr"); this.properties = new TableRowProperties(); this.root.push(this.properties); this.cells = cells; - cells.forEach(c => this.root.push(c)) + cells.forEach((c) => this.root.push(c)); } - getCell(ix: number): TableCell { + public getCell(ix: number): TableCell { return this.cells[ix]; } } class TableRowProperties extends XmlComponent { constructor() { - super('w:trPr'); + super("w:trPr"); } } class TableCell extends XmlComponent { + public content: XmlComponent; private properties: TableCellProperties; - content: any; constructor() { - super('w:tc'); + super("w:tc"); this.properties = new TableCellProperties(); this.root.push(this.properties); - this.root.push() // Table cells can have any block-level content, but for now // we only allow a single paragraph: this.content = new Paragraph(); @@ -78,6 +82,6 @@ class TableCell extends XmlComponent { class TableCellProperties extends XmlComponent { constructor() { - super('w:tcPr'); + super("w:tcPr"); } } diff --git a/ts/docx/table/properties.ts b/ts/docx/table/properties.ts index d37fef0c1b..7890f47696 100644 --- a/ts/docx/table/properties.ts +++ b/ts/docx/table/properties.ts @@ -1,24 +1,30 @@ -import {XmlComponent, Attributes} from "../xml-components"; +import { XmlAttributeComponent, XmlComponent } from "../xml-components"; + +type widthTypes = "dxa" | "pct" | "nil" | "auto"; export class TableProperties extends XmlComponent { - private width: PreferredTableWidth; - constructor() { - super('w:tblPr'); + super("w:tblPr"); } - setWidth(type: string, w: string) { - this.width = new PreferredTableWidth(type, w); - this.root.push(this.width); + public setWidth(type: widthTypes, w: number | string): TableProperties { + this.root.push(new PreferredTableWidth(type, w)); + return this; } } +interface ITableWidth { + type: widthTypes; + w: number | string; +} + +class TableWidthAttributes extends XmlAttributeComponent { + protected xmlKeys = {type: "w:type", w: "w:w"}; +} + class PreferredTableWidth extends XmlComponent { - constructor(type: string, w: string) { - super('w:tblW'); - this.root.push(new Attributes({ - type, - w, - })) + constructor(type: widthTypes, w: number | string) { + super("w:tblW"); + this.root.push(new TableWidthAttributes({type, w})); } } From c0b0649f375eb6c544d4d3052944ada9123b8270 Mon Sep 17 00:00:00 2001 From: felipe Date: Fri, 10 Mar 2017 17:38:04 +0100 Subject: [PATCH 03/12] added tests for table v1 --- ts/docx/table/index.ts | 6 +-- ts/tests/docx/table/testGrid.ts | 47 +++++++++++++++++++++++ ts/tests/docx/table/testProperties.ts | 25 +++++++++++++ ts/tests/docx/table/testTable.ts | 54 +++++++++++++++++++++++++++ 4 files changed, 129 insertions(+), 3 deletions(-) create mode 100644 ts/tests/docx/table/testGrid.ts create mode 100644 ts/tests/docx/table/testProperties.ts create mode 100644 ts/tests/docx/table/testTable.ts diff --git a/ts/docx/table/index.ts b/ts/docx/table/index.ts index 15656c6e45..f205f9a96b 100644 --- a/ts/docx/table/index.ts +++ b/ts/docx/table/index.ts @@ -1,7 +1,7 @@ import { Paragraph } from "../paragraph"; import { XmlComponent } from "../xml-components"; -import { GridCol, TableGrid } from "./grid"; +import { TableGrid } from "./grid"; import { TableProperties } from "./properties"; export class Table extends XmlComponent { @@ -15,7 +15,7 @@ export class Table extends XmlComponent { this.root.push(this.properties); const gridCols: number[] = []; - for (let i = 0; i++; i < cols) { + for (let i = 0; i < cols; i++) { gridCols.push(0); } this.grid = new TableGrid(gridCols); @@ -66,7 +66,7 @@ class TableRowProperties extends XmlComponent { } class TableCell extends XmlComponent { - public content: XmlComponent; + public content: Paragraph; private properties: TableCellProperties; constructor() { diff --git a/ts/tests/docx/table/testGrid.ts b/ts/tests/docx/table/testGrid.ts new file mode 100644 index 0000000000..edb0b50d4b --- /dev/null +++ b/ts/tests/docx/table/testGrid.ts @@ -0,0 +1,47 @@ +import { expect } from "chai"; +import { GridCol, TableGrid } from "../../../docx/table/grid"; +import { Formatter } from "../../../export/formatter"; + +describe("GridCol", () => { + describe("#constructor", () => { + it("sets the width attribute to the value given", () => { + const grid = new GridCol(1234); + const tree = new Formatter().format(grid); + expect(tree).to.deep.equal({ + "w:gridCol": [{_attr: {"w:w": 1234}}], + }); + }); + + it("does not set a width attribute if not given", () => { + const grid = new GridCol(); + const tree = new Formatter().format(grid); + expect(tree).to.deep.equal({ + "w:gridCol": [{_attr: {}}], + }); + }); + }); +}); + +describe("TableGrid", () => { + describe("#constructor", () => { + it("creates a column for each width given", () => { + const grid = new TableGrid([1234, 321, 123]); + const tree = new Formatter().format(grid); + expect(tree).to.deep.equal({ + "w:tblGrid": [ + {"w:gridCol": [{_attr: {"w:w": 1234}}]}, + {"w:gridCol": [{_attr: {"w:w": 321}}]}, + {"w:gridCol": [{_attr: {"w:w": 123}}]}, + ], + }); + }); + + it("does not set a width attribute if not given", () => { + const grid = new GridCol(); + const tree = new Formatter().format(grid); + expect(tree).to.deep.equal({ + "w:gridCol": [{_attr: {}}], + }); + }); + }); +}); diff --git a/ts/tests/docx/table/testProperties.ts b/ts/tests/docx/table/testProperties.ts new file mode 100644 index 0000000000..50da89489e --- /dev/null +++ b/ts/tests/docx/table/testProperties.ts @@ -0,0 +1,25 @@ +import { expect } from "chai"; +import { TableProperties } from "../../../docx/table/properties"; +import { Formatter } from "../../../export/formatter"; + +describe("TableProperties", () => { + describe("#constructor", () => { + it("creates an initially empty property object", () => { + const tp = new TableProperties(); + const tree = new Formatter().format(tp); + expect(tree).to.deep.equal({"w:tblPr": []}); + }); + }); + + describe("#setWidth", () => { + it("adds a table width property", () => { + const tp = new TableProperties().setWidth("dxa", 1234); + const tree = new Formatter().format(tp); + expect(tree).to.deep.equal({ + "w:tblPr": [ + {"w:tblW": [{_attr: {"w:type": "dxa", "w:w": 1234}}]}, + ], + }); + }); + }); +}); diff --git a/ts/tests/docx/table/testTable.ts b/ts/tests/docx/table/testTable.ts new file mode 100644 index 0000000000..9a5d855da0 --- /dev/null +++ b/ts/tests/docx/table/testTable.ts @@ -0,0 +1,54 @@ +import { expect } from "chai"; +import { Table } from "../../../docx/table"; +import { Formatter } from "../../../export/formatter"; + +describe("Table", () => { + describe("#constructor", () => { + it("creates a table with the correct number of rows and columns", () => { + const table = new Table(3, 2); + const tree = new Formatter().format(table); + const cell = {"w:tc": [{"w:tcPr": []}, {"w:p": [{"w:pPr": []}]}]}; + expect(tree).to.deep.equal({ + "w:tbl": [ + {"w:tblPr": []}, + {"w:tblGrid": [ + {"w:gridCol": [{_attr: {"w:w": 0}}]}, + {"w:gridCol": [{_attr: {"w:w": 0}}]}, + ]}, + {"w:tr": [{"w:trPr": []}, cell, cell]}, + {"w:tr": [{"w:trPr": []}, cell, cell]}, + {"w:tr": [{"w:trPr": []}, cell, cell]}, + ], + }); + }); + }); + + describe("#getRow and Row#getCell", () => { + it("returns the correct row", () => { + const table = new Table(2, 2); + table.getRow(0).getCell(0).content.createTextRun("A1"); + table.getRow(0).getCell(1).content.createTextRun("B1"); + table.getRow(1).getCell(0).content.createTextRun("A2"); + table.getRow(1).getCell(1).content.createTextRun("B2"); + const tree = new Formatter().format(table); + const cell = (c) => ({"w:tc": [ + {"w:tcPr": []}, + {"w:p": [ + {"w:pPr": []}, + {"w:r": [{"w:rPr": []}, {"w:t": [c]}]}, + ]}, + ]}); + expect(tree).to.deep.equal({ + "w:tbl": [ + {"w:tblPr": []}, + {"w:tblGrid": [ + {"w:gridCol": [{_attr: {"w:w": 0}}]}, + {"w:gridCol": [{_attr: {"w:w": 0}}]}, + ]}, + {"w:tr": [{"w:trPr": []}, cell("A1"), cell("B1")]}, + {"w:tr": [{"w:trPr": []}, cell("A2"), cell("B2")]}, + ], + }); + }); + }); +}); From a45048a4640a76fa1c39e0da475c9f273037db8d Mon Sep 17 00:00:00 2001 From: felipe Date: Fri, 10 Mar 2017 17:45:16 +0100 Subject: [PATCH 04/12] fix a handful of strict null errors; make test config also be strict These errors only arose because I didn't run `npm build`, only `npm test`. Then, when I tried building the null checks failed. Keeping the two config fiels in sync will help prevent this issue in the future --- ts/docx/table/grid.ts | 4 +++- ts/docx/table/index.ts | 2 +- ts/test-tsconfig.json | 5 +++++ ts/tests/docx/table/testGrid.ts | 12 +----------- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/ts/docx/table/grid.ts b/ts/docx/table/grid.ts index 91c38ddd21..3731891177 100644 --- a/ts/docx/table/grid.ts +++ b/ts/docx/table/grid.ts @@ -14,6 +14,8 @@ class GridColAttributes extends XmlAttributeComponent<{w: number}> { export class GridCol extends XmlComponent { constructor(width?: number) { super("w:gridCol"); - this.root.push(new GridColAttributes({w: width})); + if (width !== undefined) { + this.root.push(new GridColAttributes({w: width})); + } } } diff --git a/ts/docx/table/index.ts b/ts/docx/table/index.ts index f205f9a96b..485dc4fcf4 100644 --- a/ts/docx/table/index.ts +++ b/ts/docx/table/index.ts @@ -23,7 +23,7 @@ export class Table extends XmlComponent { this.rows = []; for (let i = 0; i < rows; i++) { - const cells = []; + const cells: TableCell[] = []; for (let j = 0; j < cols; j++) { cells.push(new TableCell()); } diff --git a/ts/test-tsconfig.json b/ts/test-tsconfig.json index 844ea999ff..255c0e089c 100644 --- a/ts/test-tsconfig.json +++ b/ts/test-tsconfig.json @@ -1,7 +1,12 @@ { "compilerOptions": { "target": "es6", + "strictNullChecks": true, + "sourceMap": true, + "removeComments": true, + "preserveConstEnums": true, "outDir": "../build-tests", + "sourceRoot": "./", "rootDir": "./", "module": "commonjs" } diff --git a/ts/tests/docx/table/testGrid.ts b/ts/tests/docx/table/testGrid.ts index edb0b50d4b..5eb234fd29 100644 --- a/ts/tests/docx/table/testGrid.ts +++ b/ts/tests/docx/table/testGrid.ts @@ -15,9 +15,7 @@ describe("GridCol", () => { it("does not set a width attribute if not given", () => { const grid = new GridCol(); const tree = new Formatter().format(grid); - expect(tree).to.deep.equal({ - "w:gridCol": [{_attr: {}}], - }); + expect(tree).to.deep.equal({"w:gridCol": []}); }); }); }); @@ -35,13 +33,5 @@ describe("TableGrid", () => { ], }); }); - - it("does not set a width attribute if not given", () => { - const grid = new GridCol(); - const tree = new Formatter().format(grid); - expect(tree).to.deep.equal({ - "w:gridCol": [{_attr: {}}], - }); - }); }); }); From 7b6f5bbaefbccd7344fb8580128a8492766b3f0a Mon Sep 17 00:00:00 2001 From: felipe Date: Fri, 10 Mar 2017 17:48:05 +0100 Subject: [PATCH 05/12] forgot to test #getCell --- ts/tests/docx/table/testTable.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/ts/tests/docx/table/testTable.ts b/ts/tests/docx/table/testTable.ts index 9a5d855da0..268c854d61 100644 --- a/ts/tests/docx/table/testTable.ts +++ b/ts/tests/docx/table/testTable.ts @@ -51,4 +51,33 @@ describe("Table", () => { }); }); }); + + describe("#getCell", () => { + it("returns the correct cell", () => { + const table = new Table(2, 2); + table.getCell(0, 0).content.createTextRun("A1"); + table.getCell(0, 1).content.createTextRun("B1"); + table.getCell(1, 0).content.createTextRun("A2"); + table.getCell(1, 1).content.createTextRun("B2"); + const tree = new Formatter().format(table); + const cell = (c) => ({"w:tc": [ + {"w:tcPr": []}, + {"w:p": [ + {"w:pPr": []}, + {"w:r": [{"w:rPr": []}, {"w:t": [c]}]}, + ]}, + ]}); + expect(tree).to.deep.equal({ + "w:tbl": [ + {"w:tblPr": []}, + {"w:tblGrid": [ + {"w:gridCol": [{_attr: {"w:w": 0}}]}, + {"w:gridCol": [{_attr: {"w:w": 0}}]}, + ]}, + {"w:tr": [{"w:trPr": []}, cell("A1"), cell("B1")]}, + {"w:tr": [{"w:trPr": []}, cell("A2"), cell("B2")]}, + ], + }); + }); + }); }); From 62a238de8452947af238aa51f82f46793741e357 Mon Sep 17 00:00:00 2001 From: felipe Date: Fri, 10 Mar 2017 18:54:35 +0100 Subject: [PATCH 06/12] more sane width management --- ts/docx/table/index.ts | 19 +++++++++++++++++-- ts/docx/table/properties.ts | 2 +- ts/tests/docx/table/testTable.ts | 25 +++++++++++++++++++------ 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/ts/docx/table/index.ts b/ts/docx/table/index.ts index 485dc4fcf4..82fb8e5f29 100644 --- a/ts/docx/table/index.ts +++ b/ts/docx/table/index.ts @@ -2,7 +2,7 @@ import { Paragraph } from "../paragraph"; import { XmlComponent } from "../xml-components"; import { TableGrid } from "./grid"; -import { TableProperties } from "./properties"; +import { TableProperties, widthTypes } from "./properties"; export class Table extends XmlComponent { private properties: TableProperties; @@ -16,7 +16,17 @@ export class Table extends XmlComponent { const gridCols: number[] = []; for (let i = 0; i < cols; i++) { - gridCols.push(0); + /* + 0-width columns don't get rendered correctly, so we need + to give them some value. A reasonable default would be + ~6in / numCols, but if we do that it becomes very hard + to resize the table using setWidth, unless the layout + algorithm is set to 'fixed'. Instead, the approach here + means even in 'auto' layout, setting a width on the + table will make it look reasonable, as the layout + algorithm will expand columns to fit its content + */ + gridCols.push(1); } this.grid = new TableGrid(gridCols); this.root.push(this.grid); @@ -40,6 +50,11 @@ export class Table extends XmlComponent { public getCell(row: number, col: number): TableCell { return this.getRow(row).getCell(col); } + + public setWidth(type: widthTypes, width: number | string): Table { + this.properties.setWidth(type, width); + return this; + } } class TableRow extends XmlComponent { diff --git a/ts/docx/table/properties.ts b/ts/docx/table/properties.ts index 7890f47696..5a9c8d4e26 100644 --- a/ts/docx/table/properties.ts +++ b/ts/docx/table/properties.ts @@ -1,6 +1,6 @@ import { XmlAttributeComponent, XmlComponent } from "../xml-components"; -type widthTypes = "dxa" | "pct" | "nil" | "auto"; +export type widthTypes = "dxa" | "pct" | "nil" | "auto"; export class TableProperties extends XmlComponent { constructor() { diff --git a/ts/tests/docx/table/testTable.ts b/ts/tests/docx/table/testTable.ts index 268c854d61..fd4bff95f6 100644 --- a/ts/tests/docx/table/testTable.ts +++ b/ts/tests/docx/table/testTable.ts @@ -12,8 +12,8 @@ describe("Table", () => { "w:tbl": [ {"w:tblPr": []}, {"w:tblGrid": [ - {"w:gridCol": [{_attr: {"w:w": 0}}]}, - {"w:gridCol": [{_attr: {"w:w": 0}}]}, + {"w:gridCol": [{_attr: {"w:w": 1}}]}, + {"w:gridCol": [{_attr: {"w:w": 1}}]}, ]}, {"w:tr": [{"w:trPr": []}, cell, cell]}, {"w:tr": [{"w:trPr": []}, cell, cell]}, @@ -42,8 +42,8 @@ describe("Table", () => { "w:tbl": [ {"w:tblPr": []}, {"w:tblGrid": [ - {"w:gridCol": [{_attr: {"w:w": 0}}]}, - {"w:gridCol": [{_attr: {"w:w": 0}}]}, + {"w:gridCol": [{_attr: {"w:w": 1}}]}, + {"w:gridCol": [{_attr: {"w:w": 1}}]}, ]}, {"w:tr": [{"w:trPr": []}, cell("A1"), cell("B1")]}, {"w:tr": [{"w:trPr": []}, cell("A2"), cell("B2")]}, @@ -71,8 +71,8 @@ describe("Table", () => { "w:tbl": [ {"w:tblPr": []}, {"w:tblGrid": [ - {"w:gridCol": [{_attr: {"w:w": 0}}]}, - {"w:gridCol": [{_attr: {"w:w": 0}}]}, + {"w:gridCol": [{_attr: {"w:w": 1}}]}, + {"w:gridCol": [{_attr: {"w:w": 1}}]}, ]}, {"w:tr": [{"w:trPr": []}, cell("A1"), cell("B1")]}, {"w:tr": [{"w:trPr": []}, cell("A2"), cell("B2")]}, @@ -80,4 +80,17 @@ describe("Table", () => { }); }); }); + + describe("#setWidth", () => { + it("sets the preferred width on the table", () => { + const table = new Table(2, 2).setWidth("pct", 1000) + const tree = new Formatter().format(table); + expect(tree).to.have.property("w:tbl").which.is.an("array").with.has.length.at.least(1); + expect(tree["w:tbl"][0]).to.deep.equal({ + "w:tblPr": [ + {"w:tblW": [{_attr: {"w:type": "pct", "w:w": 1000}}]}, + ], + }); + }); + }); }); From 210b97d00b54070aa223ff7b6fde2837bb20e65f Mon Sep 17 00:00:00 2001 From: felipe Date: Sat, 11 Mar 2017 09:02:36 +0100 Subject: [PATCH 07/12] added document#createTable --- ts/docx/document/index.ts | 7 +++++++ ts/tests/docx/document/documentTest.ts | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/ts/docx/document/index.ts b/ts/docx/document/index.ts index a6e8db42b4..7b0d99aaed 100644 --- a/ts/docx/document/index.ts +++ b/ts/docx/document/index.ts @@ -45,4 +45,11 @@ export class Document extends XmlComponent { public addTable(table: Table): void { this.body.push(table); } + + public createTable(rows: number, cols: number): Table { + const table = new Table(rows, cols); + this.addTable(table); + return table; + } + } diff --git a/ts/tests/docx/document/documentTest.ts b/ts/tests/docx/document/documentTest.ts index 98b9a9cf97..abb6ab568b 100644 --- a/ts/tests/docx/document/documentTest.ts +++ b/ts/tests/docx/document/documentTest.ts @@ -46,4 +46,28 @@ describe("Document", () => { }); }); }); + + describe("#createTable", () => { + it("should create a new table and append it to body", () => { + const table = document.createTable(2, 3); + expect(table).to.be.an.instanceof(docx.Table); + const body = new Formatter().format(document)["w:document"][1]["w:body"]; + expect(body).to.be.an("array").which.has.length.at.least(1); + expect(body[0]).to.have.property("w:tbl"); + }); + + it("should create a table with the correct dimensions", () => { + const table = document.createTable(2, 3); + const body = new Formatter().format(document)["w:document"][1]["w:body"]; + expect(body).to.be.an("array").which.has.length.at.least(1); + expect(body[0]).to.have.property("w:tbl").which.includes({ + "w:tblGrid": [ + {"w:gridCol": [{_attr: {"w:w": 1}}]}, + {"w:gridCol": [{_attr: {"w:w": 1}}]}, + {"w:gridCol": [{_attr: {"w:w": 1}}]}, + ], + }); + expect(body[0]["w:tbl"].filter((x) => x["w:tr"])).to.have.length(2); + }); + }); }); From c10b576a3ace3fe9a9321f704a86c8f767f69e63 Mon Sep 17 00:00:00 2001 From: felipe Date: Sat, 11 Mar 2017 09:59:29 +0100 Subject: [PATCH 08/12] added fixedWidthLayout option and method --- ts/docx/table/index.ts | 5 +++++ ts/docx/table/properties.ts | 18 ++++++++++++++++++ ts/tests/docx/table/testProperties.ts | 12 ++++++++++++ ts/tests/docx/table/testTable.ts | 13 +++++++++++++ 4 files changed, 48 insertions(+) diff --git a/ts/docx/table/index.ts b/ts/docx/table/index.ts index 82fb8e5f29..995fd2224b 100644 --- a/ts/docx/table/index.ts +++ b/ts/docx/table/index.ts @@ -55,6 +55,11 @@ export class Table extends XmlComponent { this.properties.setWidth(type, width); return this; } + + public fixedWidthLayout(): Table { + this.properties.fixedWidthLayout(); + return this; + } } class TableRow extends XmlComponent { diff --git a/ts/docx/table/properties.ts b/ts/docx/table/properties.ts index 5a9c8d4e26..ab487e58fb 100644 --- a/ts/docx/table/properties.ts +++ b/ts/docx/table/properties.ts @@ -11,6 +11,11 @@ export class TableProperties extends XmlComponent { this.root.push(new PreferredTableWidth(type, w)); return this; } + + public fixedWidthLayout(): TableProperties { + this.root.push(new TableLayout("fixed")); + return this; + } } interface ITableWidth { @@ -28,3 +33,16 @@ class PreferredTableWidth extends XmlComponent { this.root.push(new TableWidthAttributes({type, w})); } } + +type tableLayout = "autofit" | "fixed"; + +class TableLayoutAttributes extends XmlAttributeComponent<{type: tableLayout}> { + protected xmlKeys = {type: "w:type"}; +} + +class TableLayout extends XmlComponent { + constructor(type: tableLayout) { + super("w:tblLayout"); + this.root.push(new TableLayoutAttributes({type})); + } +} diff --git a/ts/tests/docx/table/testProperties.ts b/ts/tests/docx/table/testProperties.ts index 50da89489e..427419cbd8 100644 --- a/ts/tests/docx/table/testProperties.ts +++ b/ts/tests/docx/table/testProperties.ts @@ -22,4 +22,16 @@ describe("TableProperties", () => { }); }); }); + + describe("#fixedWidthLayout", () => { + it("sets the table to fixed width layout", () => { + const tp = new TableProperties().fixedWidthLayout(); + const tree = new Formatter().format(tp); + expect(tree).to.deep.equal({ + "w:tblPr": [ + {"w:tblLayout": [{_attr: {"w:type": "fixed"}}]}, + ], + }); + }); + }); }); diff --git a/ts/tests/docx/table/testTable.ts b/ts/tests/docx/table/testTable.ts index fd4bff95f6..aa108cf396 100644 --- a/ts/tests/docx/table/testTable.ts +++ b/ts/tests/docx/table/testTable.ts @@ -93,4 +93,17 @@ describe("Table", () => { }); }); }); + + describe("#fixedWidthLayout", () => { + it("sets the table to fixed width layout", () => { + const table = new Table(2, 2).fixedWidthLayout(); + const tree = new Formatter().format(table); + expect(tree).to.have.property("w:tbl").which.is.an("array").with.has.length.at.least(1); + expect(tree["w:tbl"][0]).to.deep.equal({ + "w:tblPr": [ + {"w:tblLayout": [{_attr: {"w:type": "fixed"}}]}, + ], + }); + }); + }); }); From bd3eb3e2147e990f8594c4b1151db5774f72d682 Mon Sep 17 00:00:00 2001 From: felipe Date: Sat, 11 Mar 2017 10:22:30 +0100 Subject: [PATCH 09/12] move the cell-paragraph validation into prepForXml Instead of forcing table cells to only have a single paragraph as their content, we now check whether they end in a paragraph (and insert one if necessary) during #prepForXml --- ts/docx/table/index.ts | 21 +++++++--- ts/tests/docx/table/testTable.ts | 70 ++++++++++++++++++++++++++++---- 2 files changed, 78 insertions(+), 13 deletions(-) diff --git a/ts/docx/table/index.ts b/ts/docx/table/index.ts index 995fd2224b..ec9d2dcfe0 100644 --- a/ts/docx/table/index.ts +++ b/ts/docx/table/index.ts @@ -86,17 +86,28 @@ class TableRowProperties extends XmlComponent { } class TableCell extends XmlComponent { - public content: Paragraph; private properties: TableCellProperties; constructor() { super("w:tc"); this.properties = new TableCellProperties(); this.root.push(this.properties); - // Table cells can have any block-level content, but for now - // we only allow a single paragraph: - this.content = new Paragraph(); - this.root.push(this.content); + } + + public push(content: Paragraph | Table): TableCell { + this.root.push(content); + return this + } + + public prepForXml(): object { + // Cells must end with a paragraph + const retval = super.prepForXml(); + const content = retval["w:tc"]; + if (!content[content.length - 1]["w:p"]) { + content.push(new Paragraph().prepForXml()); + } + return retval + } } } diff --git a/ts/tests/docx/table/testTable.ts b/ts/tests/docx/table/testTable.ts index aa108cf396..1c1eb0aba6 100644 --- a/ts/tests/docx/table/testTable.ts +++ b/ts/tests/docx/table/testTable.ts @@ -1,4 +1,5 @@ import { expect } from "chai"; +import { Paragraph } from "../../../docx/paragraph"; import { Table } from "../../../docx/table"; import { Formatter } from "../../../export/formatter"; @@ -26,10 +27,10 @@ describe("Table", () => { describe("#getRow and Row#getCell", () => { it("returns the correct row", () => { const table = new Table(2, 2); - table.getRow(0).getCell(0).content.createTextRun("A1"); - table.getRow(0).getCell(1).content.createTextRun("B1"); - table.getRow(1).getCell(0).content.createTextRun("A2"); - table.getRow(1).getCell(1).content.createTextRun("B2"); + table.getRow(0).getCell(0).push(new Paragraph("A1")); + table.getRow(0).getCell(1).push(new Paragraph("B1")); + table.getRow(1).getCell(0).push(new Paragraph("A2")); + table.getRow(1).getCell(1).push(new Paragraph("B2")); const tree = new Formatter().format(table); const cell = (c) => ({"w:tc": [ {"w:tcPr": []}, @@ -55,10 +56,10 @@ describe("Table", () => { describe("#getCell", () => { it("returns the correct cell", () => { const table = new Table(2, 2); - table.getCell(0, 0).content.createTextRun("A1"); - table.getCell(0, 1).content.createTextRun("B1"); - table.getCell(1, 0).content.createTextRun("A2"); - table.getCell(1, 1).content.createTextRun("B2"); + table.getCell(0, 0).push(new Paragraph("A1")); + table.getCell(0, 1).push(new Paragraph("B1")); + table.getCell(1, 0).push(new Paragraph("A2")); + table.getCell(1, 1).push(new Paragraph("B2")); const tree = new Formatter().format(table); const cell = (c) => ({"w:tc": [ {"w:tcPr": []}, @@ -106,4 +107,57 @@ describe("Table", () => { }); }); }); + + describe("Cell", () => { + describe("#prepForXml", () => { + it("inserts a paragraph at the end of the cell if it is empty", () => { + const table = new Table(1, 1); + const tree = new Formatter().format(table); + expect(tree).to.have.property("w:tbl").which.is.an("array"); + const row = tree["w:tbl"].find((x) => x["w:tr"]); + expect(row).not.to.be.undefined; + expect(row["w:tr"]).to.be.an("array").which.has.length.at.least(1); + expect(row["w:tr"].find((x) => x["w:tc"])).to.deep.equal({ + "w:tc": [ + {"w:tcPr": []}, + {"w:p": [{"w:pPr": []}]}, + ], + }); + }); + + it("inserts a paragraph at the end of the cell even if it has a child table", () => { + const parentTable = new Table(1, 1); + parentTable.getCell(0, 0).push(new Table(1, 1)); + const tree = new Formatter().format(parentTable); + expect(tree).to.have.property("w:tbl").which.is.an("array"); + const row = tree["w:tbl"].find((x) => x["w:tr"]); + expect(row).not.to.be.undefined; + expect(row["w:tr"]).to.be.an("array").which.has.length.at.least(1); + const cell = row["w:tr"].find((x) => x["w:tc"]); + expect(cell).not.to.be.undefined; + expect(cell["w:tc"][cell["w:tc"].length - 1]).to.deep.equal({ + "w:p": [{"w:pPr": []}], + }); + }); + + it("does not insert a paragraph if it already ends with one", () => { + const parentTable = new Table(1, 1); + parentTable.getCell(0, 0).push(new Paragraph("Hello")); + const tree = new Formatter().format(parentTable); + expect(tree).to.have.property("w:tbl").which.is.an("array"); + const row = tree["w:tbl"].find((x) => x["w:tr"]); + expect(row).not.to.be.undefined; + expect(row["w:tr"]).to.be.an("array").which.has.length.at.least(1); + expect(row["w:tr"].find((x) => x["w:tc"])).to.deep.equal({ + "w:tc": [ + {"w:tcPr": []}, + {"w:p": [ + {"w:pPr": []}, + {"w:r": [{"w:rPr": []}, {"w:t": ["Hello"]}]}, + ]}, + ], + }); + }); + }); + }); }); From bd9d6b74f5029bbe6beaea88c8198ae9da258390 Mon Sep 17 00:00:00 2001 From: felipe Date: Sat, 11 Mar 2017 10:30:08 +0100 Subject: [PATCH 10/12] added Cell#createParagraph method --- ts/docx/table/index.ts | 5 +++++ ts/tests/docx/table/testTable.ts | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/ts/docx/table/index.ts b/ts/docx/table/index.ts index ec9d2dcfe0..c407989b5e 100644 --- a/ts/docx/table/index.ts +++ b/ts/docx/table/index.ts @@ -108,6 +108,11 @@ class TableCell extends XmlComponent { } return retval } + + public createParagraph(text?: string): Paragraph { + const para = new Paragraph(text); + this.push(para); + return para; } } diff --git a/ts/tests/docx/table/testTable.ts b/ts/tests/docx/table/testTable.ts index 1c1eb0aba6..1bb9aaf41e 100644 --- a/ts/tests/docx/table/testTable.ts +++ b/ts/tests/docx/table/testTable.ts @@ -159,5 +159,27 @@ describe("Table", () => { }); }); }); + + describe("#createParagraph", () => { + it("inserts a new paragraph in the cell", () => { + const table = new Table(1, 1); + const para = table.getCell(0, 0).createParagraph("Test paragraph"); + expect(para).to.be.an.instanceof(Paragraph); + const tree = new Formatter().format(table); + expect(tree).to.have.property("w:tbl").which.is.an("array"); + const row = tree["w:tbl"].find((x) => x["w:tr"]); + expect(row).not.to.be.undefined; + expect(row["w:tr"]).to.be.an("array").which.has.length.at.least(1); + expect(row["w:tr"].find((x) => x["w:tc"])).to.deep.equal({ + "w:tc": [ + {"w:tcPr": []}, + {"w:p": [ + {"w:pPr": []}, + {"w:r": [{"w:rPr": []}, {"w:t": ["Test paragraph"]}]}, + ]}, + ], + }); + }); + }); }); }); From 70f4613e1ba0b7cdbaa6a728536ab79d2beae1bd Mon Sep 17 00:00:00 2001 From: felipe Date: Sat, 11 Mar 2017 10:30:25 +0100 Subject: [PATCH 11/12] appease the linter --- ts/docx/table/index.ts | 4 ++-- ts/tests/docx/document/documentTest.ts | 2 +- ts/tests/docx/table/testTable.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ts/docx/table/index.ts b/ts/docx/table/index.ts index c407989b5e..c3b403b7bb 100644 --- a/ts/docx/table/index.ts +++ b/ts/docx/table/index.ts @@ -96,7 +96,7 @@ class TableCell extends XmlComponent { public push(content: Paragraph | Table): TableCell { this.root.push(content); - return this + return this; } public prepForXml(): object { @@ -106,7 +106,7 @@ class TableCell extends XmlComponent { if (!content[content.length - 1]["w:p"]) { content.push(new Paragraph().prepForXml()); } - return retval + return retval; } public createParagraph(text?: string): Paragraph { diff --git a/ts/tests/docx/document/documentTest.ts b/ts/tests/docx/document/documentTest.ts index abb6ab568b..9531768e2d 100644 --- a/ts/tests/docx/document/documentTest.ts +++ b/ts/tests/docx/document/documentTest.ts @@ -57,7 +57,7 @@ describe("Document", () => { }); it("should create a table with the correct dimensions", () => { - const table = document.createTable(2, 3); + document.createTable(2, 3); const body = new Formatter().format(document)["w:document"][1]["w:body"]; expect(body).to.be.an("array").which.has.length.at.least(1); expect(body[0]).to.have.property("w:tbl").which.includes({ diff --git a/ts/tests/docx/table/testTable.ts b/ts/tests/docx/table/testTable.ts index 1bb9aaf41e..e6c9b23f0b 100644 --- a/ts/tests/docx/table/testTable.ts +++ b/ts/tests/docx/table/testTable.ts @@ -84,7 +84,7 @@ describe("Table", () => { describe("#setWidth", () => { it("sets the preferred width on the table", () => { - const table = new Table(2, 2).setWidth("pct", 1000) + const table = new Table(2, 2).setWidth("pct", 1000); const tree = new Formatter().format(table); expect(tree).to.have.property("w:tbl").which.is.an("array").with.has.length.at.least(1); expect(tree["w:tbl"][0]).to.deep.equal({ From fb6a4383ff8135f701fb2482ccbe011badb206c0 Mon Sep 17 00:00:00 2001 From: felipe Date: Sat, 11 Mar 2017 21:15:45 +0100 Subject: [PATCH 12/12] renamed cell#push to cell#addContent --- ts/docx/table/index.ts | 4 ++-- ts/tests/docx/table/testTable.ts | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/ts/docx/table/index.ts b/ts/docx/table/index.ts index c3b403b7bb..f6be964129 100644 --- a/ts/docx/table/index.ts +++ b/ts/docx/table/index.ts @@ -94,7 +94,7 @@ class TableCell extends XmlComponent { this.root.push(this.properties); } - public push(content: Paragraph | Table): TableCell { + public addContent(content: Paragraph | Table): TableCell { this.root.push(content); return this; } @@ -111,7 +111,7 @@ class TableCell extends XmlComponent { public createParagraph(text?: string): Paragraph { const para = new Paragraph(text); - this.push(para); + this.addContent(para); return para; } } diff --git a/ts/tests/docx/table/testTable.ts b/ts/tests/docx/table/testTable.ts index e6c9b23f0b..a88cc1b79b 100644 --- a/ts/tests/docx/table/testTable.ts +++ b/ts/tests/docx/table/testTable.ts @@ -27,10 +27,10 @@ describe("Table", () => { describe("#getRow and Row#getCell", () => { it("returns the correct row", () => { const table = new Table(2, 2); - table.getRow(0).getCell(0).push(new Paragraph("A1")); - table.getRow(0).getCell(1).push(new Paragraph("B1")); - table.getRow(1).getCell(0).push(new Paragraph("A2")); - table.getRow(1).getCell(1).push(new Paragraph("B2")); + table.getRow(0).getCell(0).addContent(new Paragraph("A1")); + table.getRow(0).getCell(1).addContent(new Paragraph("B1")); + table.getRow(1).getCell(0).addContent(new Paragraph("A2")); + table.getRow(1).getCell(1).addContent(new Paragraph("B2")); const tree = new Formatter().format(table); const cell = (c) => ({"w:tc": [ {"w:tcPr": []}, @@ -56,10 +56,10 @@ describe("Table", () => { describe("#getCell", () => { it("returns the correct cell", () => { const table = new Table(2, 2); - table.getCell(0, 0).push(new Paragraph("A1")); - table.getCell(0, 1).push(new Paragraph("B1")); - table.getCell(1, 0).push(new Paragraph("A2")); - table.getCell(1, 1).push(new Paragraph("B2")); + table.getCell(0, 0).addContent(new Paragraph("A1")); + table.getCell(0, 1).addContent(new Paragraph("B1")); + table.getCell(1, 0).addContent(new Paragraph("A2")); + table.getCell(1, 1).addContent(new Paragraph("B2")); const tree = new Formatter().format(table); const cell = (c) => ({"w:tc": [ {"w:tcPr": []}, @@ -127,7 +127,7 @@ describe("Table", () => { it("inserts a paragraph at the end of the cell even if it has a child table", () => { const parentTable = new Table(1, 1); - parentTable.getCell(0, 0).push(new Table(1, 1)); + parentTable.getCell(0, 0).addContent(new Table(1, 1)); const tree = new Formatter().format(parentTable); expect(tree).to.have.property("w:tbl").which.is.an("array"); const row = tree["w:tbl"].find((x) => x["w:tr"]); @@ -142,7 +142,7 @@ describe("Table", () => { it("does not insert a paragraph if it already ends with one", () => { const parentTable = new Table(1, 1); - parentTable.getCell(0, 0).push(new Paragraph("Hello")); + parentTable.getCell(0, 0).addContent(new Paragraph("Hello")); const tree = new Formatter().format(parentTable); expect(tree).to.have.property("w:tbl").which.is.an("array"); const row = tree["w:tbl"].find((x) => x["w:tr"]);