diff --git a/src/file/styles/external-styles-factory.spec.ts b/src/file/styles/external-styles-factory.spec.ts index 802b6c8bb0..0957c25d9f 100644 --- a/src/file/styles/external-styles-factory.spec.ts +++ b/src/file/styles/external-styles-factory.spec.ts @@ -51,6 +51,7 @@ describe("External styles factory", () => { expect(importedStyle.root.length).to.equal(5); expect(importedStyle.root[1]).to.eql({ + deleted: false, root: [], rootKey: "w:docDefaults", }); @@ -59,6 +60,7 @@ describe("External styles factory", () => { "w:defLockedState": "1", "w:defUIPriority": "99", }, + deleted: false, root: [], rootKey: "w:latentStyles", }); @@ -74,15 +76,18 @@ describe("External styles factory", () => { "w:styleId": "Normal", "w:type": "paragraph", }, + deleted: false, root: [ { _attr: { "w:val": "Normal", }, + deleted: false, root: [], rootKey: "w:name", }, { + deleted: false, root: [], rootKey: "w:qFormat", }, @@ -95,11 +100,13 @@ describe("External styles factory", () => { "w:styleId": "Heading1", "w:type": "paragraph", }, + deleted: false, root: [ { _attr: { "w:val": "heading 1", }, + deleted: false, root: [], rootKey: "w:name", }, @@ -107,20 +114,25 @@ describe("External styles factory", () => { _attr: { "w:val": "Normal", }, + deleted: false, root: [], rootKey: "w:basedOn", }, { + deleted: false, root: [ { + deleted: false, root: [], rootKey: "w:keepNext", }, { + deleted: false, root: [], rootKey: "w:keepLines", }, { + deleted: false, root: [ { _attr: { @@ -129,6 +141,7 @@ describe("External styles factory", () => { "w:sz": "4", "w:val": "single", }, + deleted: false, root: [], rootKey: "w:bottom", }, diff --git a/src/file/table/table.ts b/src/file/table/table.ts index 6a7bb1a928..aa6e760d27 100644 --- a/src/file/table/table.ts +++ b/src/file/table/table.ts @@ -8,26 +8,31 @@ export class Table extends XmlComponent { private readonly rows: TableRow[]; private readonly grid: TableGrid; - constructor(rows: number, cols: number) { + constructor(rows: number, cols: number, colSizes?: number[]) { super("w:tbl"); this.properties = new TableProperties(); this.root.push(this.properties); - const gridCols: number[] = []; - for (let i = 0; i < cols; i++) { - /* - 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); + if (colSizes && colSizes.length > 0) { + this.grid = new TableGrid(colSizes); + } else { + const gridCols: number[] = []; + for (let i = 0; i < cols; i++) { + /* + 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.grid = new TableGrid(gridCols); + this.root.push(this.grid); this.rows = []; @@ -111,6 +116,10 @@ export class TableCell extends XmlComponent { this.addContent(para); return para; } + + get cellProperties() { + return this.properties; + } } export class TableCellProperties extends XmlComponent { diff --git a/src/file/xml-components/base.ts b/src/file/xml-components/base.ts index d634a418a9..f6382f5e7e 100644 --- a/src/file/xml-components/base.ts +++ b/src/file/xml-components/base.ts @@ -2,10 +2,15 @@ import { IXmlableObject } from "./xmlable-object"; export abstract class BaseXmlComponent { protected rootKey: string; + protected deleted: boolean = false; constructor(rootKey: string) { this.rootKey = rootKey; } public abstract prepForXml(): IXmlableObject; + + get isDeleted() { + return this.deleted; + } } diff --git a/src/file/xml-components/xml-component.spec.ts b/src/file/xml-components/xml-component.spec.ts index 17d3d4d1cb..25bf442bc9 100644 --- a/src/file/xml-components/xml-component.spec.ts +++ b/src/file/xml-components/xml-component.spec.ts @@ -18,4 +18,15 @@ describe("XmlComponent", () => { assert.equal(newJson.rootKey, "w:test"); }); }); + + describe("#prepForXml()", () => { + it("should skip deleted elements", () => { + const child = new TestComponent("w:test1"); + child.delete(); + xmlComponent.addChildElement(child); + + const xml = xmlComponent.prepForXml(); + assert.equal(xml['w:test'].length, 0); + }); + }); }); diff --git a/src/file/xml-components/xml-component.ts b/src/file/xml-components/xml-component.ts index 833e368f23..b41bb01267 100644 --- a/src/file/xml-components/xml-component.ts +++ b/src/file/xml-components/xml-component.ts @@ -12,6 +12,12 @@ export abstract class XmlComponent extends BaseXmlComponent { public prepForXml(): IXmlableObject { const children = this.root + .filter(c => { + if (c instanceof BaseXmlComponent) { + return !c.isDeleted; + } + return true; + }) .map((comp) => { if (comp instanceof BaseXmlComponent) { return comp.prepForXml(); @@ -27,4 +33,8 @@ export abstract class XmlComponent extends BaseXmlComponent { public addChildElement(child: XmlComponent | string) { this.root.push(child); } + + public delete() { + this.deleted = true; + } }