diff --git a/demo/demo34.ts b/demo/demo34.ts index 098e1c2065..75fe77687c 100644 --- a/demo/demo34.ts +++ b/demo/demo34.ts @@ -20,7 +20,7 @@ const table = doc.createTable(2, 2).float({ relativeVerticalPosition: RelativeVerticalPosition.BOTTOM, }); table.setFixedWidthLayout(); -table.setWidth(WidthType.DXA, 4535); +table.setWidth(4535, WidthType.DXA); table.getCell(0, 0).addContent(new Paragraph("Hello")); table.getRow(0).mergeCells(0, 1); diff --git a/src/file/table/table-cell/table-cell-properties.spec.ts b/src/file/table/table-cell/table-cell-properties.spec.ts index 0884d28da7..504a0549d0 100644 --- a/src/file/table/table-cell/table-cell-properties.spec.ts +++ b/src/file/table/table-cell/table-cell-properties.spec.ts @@ -1,6 +1,7 @@ import { expect } from "chai"; import { Formatter } from "export/formatter"; +import { BorderStyle } from "file/styles"; import { VerticalAlign, VMergeType, WidthType } from "./table-cell-components"; import { TableCellProperties } from "./table-cell-properties"; @@ -42,12 +43,19 @@ describe("TableCellProperties", () => { }); describe("#setWidth", () => { - it("sets width", () => { + it("should set width", () => { const cellMargain = new TableCellProperties(); cellMargain.setWidth(1, WidthType.DXA); const tree = new Formatter().format(cellMargain); expect(tree).to.deep.equal({ "w:tcPr": [{ "w:tcW": [{ _attr: { "w:type": "dxa", "w:w": 1 } }] }] }); }); + + it("should set width using default of AUTO", () => { + const cellMargain = new TableCellProperties(); + cellMargain.setWidth(1); + const tree = new Formatter().format(cellMargain); + expect(tree).to.deep.equal({ "w:tcPr": [{ "w:tcW": [{ _attr: { "w:type": "auto", "w:w": 1 } }] }] }); + }); }); describe("#setShading", () => { @@ -61,4 +69,18 @@ describe("TableCellProperties", () => { expect(tree).to.deep.equal({ "w:tcPr": [{ "w:shd": [{ _attr: { "w:fill": "test", "w:color": "000" } }] }] }); }); }); + + describe("#Borders", () => { + it("should return the TableCellBorders if Border has borders", () => { + const cellMargain = new TableCellProperties(); + cellMargain.Borders.addTopBorder(BorderStyle.DASH_DOT_STROKED, 3, "red"); + const borders = cellMargain.Borders; + + const tree = new Formatter().format(borders); + + expect(tree).to.deep.equal({ + "w:tcBorders": [{ "w:top": [{ _attr: { "w:val": "dashDotStroked", "w:sz": 3, "w:color": "red" } }] }], + }); + }); + }); }); diff --git a/src/file/table/table-cell/table-cell-properties.ts b/src/file/table/table-cell/table-cell-properties.ts index 1f653b53bb..2d8d45ed2d 100644 --- a/src/file/table/table-cell/table-cell-properties.ts +++ b/src/file/table/table-cell/table-cell-properties.ts @@ -44,7 +44,7 @@ export class TableCellProperties extends XmlComponent { return this; } - public setWidth(width: string | number, type: WidthType): TableCellProperties { + public setWidth(width: string | number, type: WidthType = WidthType.AUTO): TableCellProperties { this.root.push(new TableCellWidth(width, type)); return this; diff --git a/src/file/table/table-properties/table-properties.spec.ts b/src/file/table/table-properties/table-properties.spec.ts index 8b9656d643..dbd80004eb 100644 --- a/src/file/table/table-properties/table-properties.spec.ts +++ b/src/file/table/table-properties/table-properties.spec.ts @@ -15,13 +15,21 @@ describe("TableProperties", () => { }); describe("#setWidth", () => { - it("adds a table width property", () => { - const tp = new TableProperties().setWidth(WidthType.DXA, 1234); + it("should add a table width property", () => { + const tp = new TableProperties().setWidth(1234, WidthType.DXA); const tree = new Formatter().format(tp); expect(tree).to.deep.equal({ "w:tblPr": [{ "w:tblW": [{ _attr: { "w:type": "dxa", "w:w": 1234 } }] }], }); }); + + it("should add a table width property with default of AUTO", () => { + const tp = new TableProperties().setWidth(1234); + const tree = new Formatter().format(tp); + expect(tree).to.deep.equal({ + "w:tblPr": [{ "w:tblW": [{ _attr: { "w:type": "auto", "w:w": 1234 } }] }], + }); + }); }); describe("#setFixedWidthLayout", () => { diff --git a/src/file/table/table-properties/table-properties.ts b/src/file/table/table-properties/table-properties.ts index f8198eea59..c6184d70c1 100644 --- a/src/file/table/table-properties/table-properties.ts +++ b/src/file/table/table-properties/table-properties.ts @@ -17,8 +17,8 @@ export class TableProperties extends XmlComponent { this.root.push(this.cellMargin); } - public setWidth(type: WidthType, w: number | string): TableProperties { - this.root.push(new PreferredTableWidth(type, w)); + public setWidth(width: number | string, type: WidthType = WidthType.AUTO): TableProperties { + this.root.push(new PreferredTableWidth(type, width)); return this; } diff --git a/src/file/table/table.spec.ts b/src/file/table/table.spec.ts index f1ddfe0ed6..3d21fc8f5d 100644 --- a/src/file/table/table.spec.ts +++ b/src/file/table/table.spec.ts @@ -176,8 +176,8 @@ describe("Table", () => { }); describe("#setWidth", () => { - it("sets the preferred width on the table", () => { - const table = new Table(2, 2).setWidth(WidthType.PERCENTAGE, 1000); + it("should set the preferred width on the table", () => { + const table = new Table(2, 2).setWidth(1000, WidthType.PERCENTAGE); const tree = new Formatter().format(table); expect(tree) .to.have.property("w:tbl") @@ -187,6 +187,15 @@ describe("Table", () => { "w:tblPr": [DEFAULT_TABLE_PROPERTIES, { "w:tblW": [{ _attr: { "w:type": "pct", "w:w": 1000 } }] }], }); }); + + it("sets the preferred width on the table with a default of AUTO", () => { + const table = new Table(2, 2).setWidth(1000); + const tree = new Formatter().format(table); + + expect(tree["w:tbl"][0]).to.deep.equal({ + "w:tblPr": [DEFAULT_TABLE_PROPERTIES, { "w:tblW": [{ _attr: { "w:type": "auto", "w:w": 1000 } }] }], + }); + }); }); describe("#setFixedWidthLayout", () => { diff --git a/src/file/table/table.ts b/src/file/table/table.ts index 8311b5ecfd..6b81d3991f 100644 --- a/src/file/table/table.ts +++ b/src/file/table/table.ts @@ -65,8 +65,8 @@ export class Table extends XmlComponent { return this.getRow(row).getCell(col); } - public setWidth(type: WidthType, width: number | string): Table { - this.properties.setWidth(type, width); + public setWidth(width: number | string, type: WidthType = WidthType.AUTO): Table { + this.properties.setWidth(width, type); return this; }