From 28233075bd693cfa2639b0f4b32bc1fa08856026 Mon Sep 17 00:00:00 2001 From: Andrey Savin Date: Tue, 28 Sep 2021 15:57:33 +0300 Subject: [PATCH 1/2] Append % if type is "pct" (WidthType.PERCENTAGE) --- src/file/table/table-width.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/file/table/table-width.ts b/src/file/table/table-width.ts index ff203506eb..554654a583 100644 --- a/src/file/table/table-width.ts +++ b/src/file/table/table-width.ts @@ -38,6 +38,10 @@ export class TableWidthElement extends XmlComponent { constructor(name: string, { type = WidthType.AUTO, size }: ITableWidthProperties) { super(name); // super("w:tblW"); - this.root.push(new TableWidthAttributes({ type: type, size: measurementOrPercentValue(size) })); + let tableWidthValue = size; + if (type === WidthType.PERCENTAGE && typeof size === "number") { + tableWidthValue = `${size}%`; + } + this.root.push(new TableWidthAttributes({ type: type, size: measurementOrPercentValue(tableWidthValue) })); } } From 705b1f7cfc9272656af61464fedf7ec45e7ce0a9 Mon Sep 17 00:00:00 2001 From: Andrey Savin Date: Tue, 28 Sep 2021 16:16:50 +0300 Subject: [PATCH 2/2] Fix to table percent width test + additional test on DXA table width --- src/file/table/table.spec.ts | 39 ++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/file/table/table.spec.ts b/src/file/table/table.spec.ts index d3e703decd..78a273e4e1 100644 --- a/src/file/table/table.spec.ts +++ b/src/file/table/table.spec.ts @@ -257,7 +257,7 @@ describe("Table", () => { }); }); - it("should set the table to provided width", () => { + it("should set the table to provided 100% width", () => { const table = new Table({ rows: [ new TableRow({ @@ -282,7 +282,42 @@ describe("Table", () => { "w:tblW": { _attr: { "w:type": "pct", - "w:w": 100, + "w:w": "100%", + }, + }, + }, + BORDERS, + { "w:tblLayout": { _attr: { "w:type": "fixed" } } }, + ], + }); + }); + + it("should set the table to provided 1000 DXA", () => { + const table = new Table({ + rows: [ + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("hello")], + }), + ], + }), + ], + width: { + size: 1000, + type: WidthType.DXA, + }, + layout: TableLayoutType.FIXED, + }); + 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": "dxa", + "w:w": 1000, }, }, },