From d0a675fde62fb948b56c65af875b5364b7da3074 Mon Sep 17 00:00:00 2001 From: Tom Hunkapiller Date: Thu, 20 May 2021 01:03:09 +0300 Subject: [PATCH] Fix ordering of elements in table, table-cell, table-properties, table-cell-properties, table-cell-margin --- .../cell-margin/table-cell-margins.spec.ts | 32 +++++++-------- .../cell-margin/table-cell-margins.ts | 2 +- .../table-cell/table-cell-properties.spec.ts | 16 ++++---- .../table/table-cell/table-cell-properties.ts | 7 +++- src/file/table/table-cell/table-cell.spec.ts | 16 ++++---- src/file/table/table-cell/table-cell.ts | 41 ++++++++++--------- .../table-properties/table-cell-margin.ts | 8 ++-- .../table-properties/table-properties.ts | 26 ++++++------ src/file/table/table.spec.ts | 34 +++++++-------- 9 files changed, 94 insertions(+), 88 deletions(-) diff --git a/src/file/table/table-cell/cell-margin/table-cell-margins.spec.ts b/src/file/table/table-cell/cell-margin/table-cell-margins.spec.ts index dc4a0a9ab9..8777ec5a9b 100644 --- a/src/file/table/table-cell/cell-margin/table-cell-margins.spec.ts +++ b/src/file/table/table-cell/cell-margin/table-cell-margins.spec.ts @@ -19,6 +19,14 @@ describe("TableCellMargin", () => { }, }, }, + { + "w:start": { + _attr: { + "w:type": "dxa", + "w:w": 0, + }, + }, + }, { "w:bottom": { _attr: { @@ -35,14 +43,6 @@ describe("TableCellMargin", () => { }, }, }, - { - "w:start": { - _attr: { - "w:type": "dxa", - "w:w": 0, - }, - }, - }, ], }); }); @@ -65,6 +65,14 @@ describe("TableCellMargin", () => { }, }, }, + { + "w:start": { + _attr: { + "w:type": "dxa", + "w:w": 5, + }, + }, + }, { "w:bottom": { _attr: { @@ -81,14 +89,6 @@ describe("TableCellMargin", () => { }, }, }, - { - "w:start": { - _attr: { - "w:type": "dxa", - "w:w": 5, - }, - }, - }, ], }); }); diff --git a/src/file/table/table-cell/cell-margin/table-cell-margins.ts b/src/file/table/table-cell/cell-margin/table-cell-margins.ts index b97c349c9c..efb2beb5bd 100644 --- a/src/file/table/table-cell/cell-margin/table-cell-margins.ts +++ b/src/file/table/table-cell/cell-margin/table-cell-margins.ts @@ -14,8 +14,8 @@ export class TableCellMargin extends XmlComponent { constructor({ top = 0, left = 0, right = 0, bottom = 0 }: ITableCellMarginOptions) { super("w:tcMar"); this.root.push(new TopCellMargin(top)); + this.root.push(new LeftCellMargin(left)); this.root.push(new BottomCellMargin(bottom)); this.root.push(new RightCellMargin(right)); - this.root.push(new LeftCellMargin(left)); } } 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 c3452e757c..24c17b674c 100644 --- a/src/file/table/table-cell/table-cell-properties.spec.ts +++ b/src/file/table/table-cell/table-cell-properties.spec.ts @@ -90,6 +90,14 @@ describe("TableCellProperties", () => { }, }, }, + { + "w:start": { + _attr: { + "w:type": "dxa", + "w:w": 0, + }, + }, + }, { "w:bottom": { _attr: { @@ -106,14 +114,6 @@ describe("TableCellProperties", () => { }, }, }, - { - "w:start": { - _attr: { - "w:type": "dxa", - "w:w": 0, - }, - }, - }, ], }, ], diff --git a/src/file/table/table-cell/table-cell-properties.ts b/src/file/table/table-cell/table-cell-properties.ts index e248290294..68f21e22dc 100644 --- a/src/file/table/table-cell/table-cell-properties.ts +++ b/src/file/table/table-cell/table-cell-properties.ts @@ -21,7 +21,6 @@ export class TableCellProperties extends IgnoreIfEmptyXmlComponent { constructor() { super("w:tcPr"); this.cellBorder = new TableCellBorders(); - this.root.push(this.cellBorder); } public get Borders(): TableCellBorders { @@ -69,4 +68,10 @@ export class TableCellProperties extends IgnoreIfEmptyXmlComponent { return this; } + + public addBorders(): TableCellProperties { + this.root.push(this.cellBorder); + + return this; + } } diff --git a/src/file/table/table-cell/table-cell.spec.ts b/src/file/table/table-cell/table-cell.spec.ts index 2ba34bc9d4..f320cfed6e 100644 --- a/src/file/table/table-cell/table-cell.spec.ts +++ b/src/file/table/table-cell/table-cell.spec.ts @@ -354,6 +354,14 @@ describe("TableCell", () => { }, }, }, + { + "w:start": { + _attr: { + "w:type": "dxa", + "w:w": 1, + }, + }, + }, { "w:bottom": { _attr: { @@ -370,14 +378,6 @@ describe("TableCell", () => { }, }, }, - { - "w:start": { - _attr: { - "w:type": "dxa", - "w:w": 1, - }, - }, - }, ], }, ], diff --git a/src/file/table/table-cell/table-cell.ts b/src/file/table/table-cell/table-cell.ts index c79468e208..7a1bd8b458 100644 --- a/src/file/table/table-cell/table-cell.ts +++ b/src/file/table/table-cell/table-cell.ts @@ -57,12 +57,12 @@ export class TableCell extends XmlComponent { this.root.push(child); } - if (options.verticalAlign) { - properties.setVerticalAlign(options.verticalAlign); + if (options.width) { + properties.setWidth(options.width.size, options.width.type); } - if (options.textDirection) { - properties.setTextDirection(options.textDirection); + if (options.columnSpan) { + properties.addGridSpan(options.columnSpan); } if (options.verticalMerge) { @@ -72,23 +72,8 @@ export class TableCell extends XmlComponent { properties.addVerticalMerge(VerticalMergeType.RESTART); } - if (options.margins) { - properties.addMargins(options.margins); - } - - if (options.shading) { - properties.setShading(options.shading); - } - - if (options.columnSpan) { - properties.addGridSpan(options.columnSpan); - } - - if (options.width) { - properties.setWidth(options.width.size, options.width.type); - } - if (options.borders) { + properties.addBorders(); if (options.borders.top) { properties.Borders.addTopBorder(options.borders.top.style, options.borders.top.size, options.borders.top.color); } @@ -102,6 +87,22 @@ export class TableCell extends XmlComponent { properties.Borders.addRightBorder(options.borders.right.style, options.borders.right.size, options.borders.right.color); } } + + if (options.shading) { + properties.setShading(options.shading); + } + + if (options.margins) { + properties.addMargins(options.margins); + } + + if (options.textDirection) { + properties.setTextDirection(options.textDirection); + } + + if (options.verticalAlign) { + properties.setVerticalAlign(options.verticalAlign); + } } public prepForXml(context: IContext): IXmlableObject | undefined { diff --git a/src/file/table/table-properties/table-cell-margin.ts b/src/file/table/table-properties/table-cell-margin.ts index 9d94e7c84a..ffa554a8de 100644 --- a/src/file/table/table-properties/table-cell-margin.ts +++ b/src/file/table/table-properties/table-cell-margin.ts @@ -35,10 +35,6 @@ export class TableCellMargin extends IgnoreIfEmptyXmlComponent { constructor(options: ITableCellMarginOptions) { super("w:tblCellMar"); - if (options.bottom) { - this.root.push(new BaseTableCellMargin("w:bottom", options.bottom)); - } - if (options.top) { this.root.push(new BaseTableCellMargin("w:top", options.top)); } @@ -47,6 +43,10 @@ export class TableCellMargin extends IgnoreIfEmptyXmlComponent { this.root.push(new BaseTableCellMargin("w:left", options.left)); } + if (options.bottom) { + this.root.push(new BaseTableCellMargin("w:bottom", options.bottom)); + } + if (options.right) { this.root.push(new BaseTableCellMargin("w:right", options.right)); } diff --git a/src/file/table/table-properties/table-properties.ts b/src/file/table/table-properties/table-properties.ts index cd0341a6ac..5d1aa048e1 100644 --- a/src/file/table/table-properties/table-properties.ts +++ b/src/file/table/table-properties/table-properties.ts @@ -35,34 +35,34 @@ export class TableProperties extends IgnoreIfEmptyXmlComponent { this.root.push(new TableStyle(options.style)); } - this.root.push(new TableCellMargin(options.cellMargin || {})); + if (options.float) { + this.root.push(new TableFloatProperties(options.float)); + } - if (options.borders) { - this.root.push(new TableBorders(options.borders)); + if (options.visuallyRightToLeft) { + this.root.push(new VisuallyRightToLeft()); } if (options.width) { this.root.push(new PreferredTableWidth(options.width.type, options.width.size)); } - if (options.float) { - this.root.push(new TableFloatProperties(options.float)); - } - - if (options.layout) { - this.root.push(new TableLayout(options.layout)); - } - if (options.alignment) { this.root.push(new Alignment(options.alignment)); } + if (options.borders) { + this.root.push(new TableBorders(options.borders)); + } + if (options.shading) { this.root.push(new TableShading(options.shading)); } - if (options.visuallyRightToLeft) { - this.root.push(new VisuallyRightToLeft()); + if (options.layout) { + this.root.push(new TableLayout(options.layout)); } + + this.root.push(new TableCellMargin(options.cellMargin || {})); } } diff --git a/src/file/table/table.spec.ts b/src/file/table/table.spec.ts index 3165330047..623540985f 100644 --- a/src/file/table/table.spec.ts +++ b/src/file/table/table.spec.ts @@ -14,14 +14,6 @@ import { TableRow } from "./table-row"; const DEFAULT_TABLE_PROPERTIES = { "w:tblCellMar": [ - { - "w:bottom": { - _attr: { - "w:type": "auto", - "w:w": 0, - }, - }, - }, { "w:top": { _attr: { @@ -38,6 +30,14 @@ const DEFAULT_TABLE_PROPERTIES = { }, }, }, + { + "w:bottom": { + _attr: { + "w:type": "auto", + "w:w": 0, + }, + }, + }, { "w:right": { _attr: { @@ -177,7 +177,7 @@ describe("Table", () => { }; expect(tree).to.deep.equal({ "w:tbl": [ - { "w:tblPr": [DEFAULT_TABLE_PROPERTIES, BORDERS, WIDTHS] }, + { "w:tblPr": [WIDTHS, BORDERS, DEFAULT_TABLE_PROPERTIES] }, { "w:tblGrid": [{ "w:gridCol": { _attr: { "w:w": 100 } } }, { "w:gridCol": { _attr: { "w:w": 100 } } }], }, @@ -223,7 +223,7 @@ describe("Table", () => { const cellP = { "w:p": [{ "w:r": [{ "w:t": [{ _attr: { "xml:space": "preserve" } }, "hello"] }] }] }; expect(tree).to.deep.equal({ "w:tbl": [ - { "w:tblPr": [DEFAULT_TABLE_PROPERTIES, BORDERS, WIDTHS] }, + { "w:tblPr": [WIDTHS, BORDERS, DEFAULT_TABLE_PROPERTIES] }, { "w:tblGrid": [{ "w:gridCol": { _attr: { "w:w": 100 } } }, { "w:gridCol": { _attr: { "w:w": 100 } } }], }, @@ -270,7 +270,7 @@ describe("Table", () => { 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": [DEFAULT_TABLE_PROPERTIES, BORDERS, WIDTHS, { "w:tblLayout": { _attr: { "w:type": "fixed" } } }], + "w:tblPr": [WIDTHS, BORDERS, { "w:tblLayout": { _attr: { "w:type": "fixed" } } }, DEFAULT_TABLE_PROPERTIES], }); }); @@ -290,7 +290,7 @@ describe("Table", () => { 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": [DEFAULT_TABLE_PROPERTIES, BORDERS, WIDTHS, { "w:jc": { _attr: { "w:val": "center" } } }], + "w:tblPr": [WIDTHS, { "w:jc": { _attr: { "w:val": "center" } } }, BORDERS, DEFAULT_TABLE_PROPERTIES], }); }); @@ -315,8 +315,6 @@ describe("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": [ - DEFAULT_TABLE_PROPERTIES, - BORDERS, { "w:tblW": { _attr: { @@ -325,7 +323,9 @@ describe("Table", () => { }, }, }, + BORDERS, { "w:tblLayout": { _attr: { "w:type": "fixed" } } }, + DEFAULT_TABLE_PROPERTIES, ], }); }); @@ -477,9 +477,6 @@ describe("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": [ - DEFAULT_TABLE_PROPERTIES, - BORDERS, - WIDTHS, { "w:tblpPr": { _attr: { @@ -496,6 +493,9 @@ describe("Table", () => { }, }, }, + WIDTHS, + BORDERS, + DEFAULT_TABLE_PROPERTIES, ], }); });