diff --git a/src/file/document/body/section-properties/footer-reference/footer-reference.spec.ts b/src/file/document/body/section-properties/footer-reference/footer-reference.spec.ts new file mode 100644 index 0000000000..50570b4b77 --- /dev/null +++ b/src/file/document/body/section-properties/footer-reference/footer-reference.spec.ts @@ -0,0 +1,42 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; +import { FooterReference } from "./footer-reference"; +import { FooterReferenceType } from "./footer-reference-attributes"; + +describe("footerReference", () => { + it("should create", () => { + const footer = new FooterReference({ + footerType: FooterReferenceType.DEFAULT, + footerId: 1, + }); + + const tree = new Formatter().format(footer); + + expect(tree).to.deep.equal({ + "w:footerReference": { + _attr: { + "r:id": "rId1", + "w:type": "default", + }, + }, + }); + }); + + it("should create without a footer type", () => { + const footer = new FooterReference({ + footerId: 1, + }); + + const tree = new Formatter().format(footer); + + expect(tree).to.deep.equal({ + "w:footerReference": { + _attr: { + "r:id": "rId1", + "w:type": "default", + }, + }, + }); + }); +}); diff --git a/src/file/document/body/section-properties/header-reference/header-reference.spec.ts b/src/file/document/body/section-properties/header-reference/header-reference.spec.ts new file mode 100644 index 0000000000..2d6b39e553 --- /dev/null +++ b/src/file/document/body/section-properties/header-reference/header-reference.spec.ts @@ -0,0 +1,42 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; +import { HeaderReference } from "./header-reference"; +import { HeaderReferenceType } from "./header-reference-attributes"; + +describe("HeaderReference", () => { + it("should create", () => { + const footer = new HeaderReference({ + headerType: HeaderReferenceType.DEFAULT, + headerId: 1, + }); + + const tree = new Formatter().format(footer); + + expect(tree).to.deep.equal({ + "w:headerReference": { + _attr: { + "r:id": "rId1", + "w:type": "default", + }, + }, + }); + }); + + it("should create without a header type", () => { + const footer = new HeaderReference({ + headerId: 1, + }); + + const tree = new Formatter().format(footer); + + expect(tree).to.deep.equal({ + "w:headerReference": { + _attr: { + "r:id": "rId1", + "w:type": "default", + }, + }, + }); + }); +}); diff --git a/src/file/document/body/section-properties/section-properties.spec.ts b/src/file/document/body/section-properties/section-properties.spec.ts index 31828ff5f4..abdbf9e1ed 100644 --- a/src/file/document/body/section-properties/section-properties.spec.ts +++ b/src/file/document/body/section-properties/section-properties.spec.ts @@ -38,6 +38,7 @@ describe("SectionProperties", () => { }, pageNumberStart: 10, pageNumberFormatType: PageNumberFormat.CARDINAL_TEXT, + titlePage: true, }); const tree = new Formatter().format(properties); expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]); diff --git a/src/file/footer-wrapper.spec.ts b/src/file/footer-wrapper.spec.ts index 899bd22e70..4f3e95acdb 100644 --- a/src/file/footer-wrapper.spec.ts +++ b/src/file/footer-wrapper.spec.ts @@ -35,15 +35,6 @@ describe("FooterWrapper", () => { expect(spy.called).to.equal(true); }); - - it("should call the underlying footer's addImage", () => { - const file = new FooterWrapper(new Media(), 1); - const spy = sinon.spy(file.Footer, "add"); - // tslint:disable-next-line:no-any - file.addImage({} as any); - - expect(spy.called).to.equal(true); - }); }); describe("#addChildElement", () => { diff --git a/src/file/footer-wrapper.ts b/src/file/footer-wrapper.ts index 01087e9861..8390887ba4 100644 --- a/src/file/footer-wrapper.ts +++ b/src/file/footer-wrapper.ts @@ -2,7 +2,7 @@ import { XmlComponent } from "file/xml-components"; import { FooterReferenceType } from "./document"; import { Footer } from "./footer/footer"; -import { Image, Media } from "./media"; +import { Media } from "./media"; import { Paragraph } from "./paragraph"; import { Relationships } from "./relationships"; import { Table } from "./table"; @@ -25,11 +25,6 @@ export class FooterWrapper { this.footer.add(item); } - public addImage(image: Image): FooterWrapper { - this.footer.add(image.Paragraph); - return this; - } - public addChildElement(childElement: XmlComponent): void { this.footer.addChildElement(childElement); } diff --git a/src/file/footer/footer.spec.ts b/src/file/footer/footer.spec.ts new file mode 100644 index 0000000000..4f970a119a --- /dev/null +++ b/src/file/footer/footer.spec.ts @@ -0,0 +1,47 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { Paragraph } from "../paragraph"; +import { Footer } from "./footer"; + +describe("Footer", () => { + it("should create", () => { + const footer = new Footer(1); + + const tree = new Formatter().format(footer); + + expect(tree).to.deep.equal({ + "w:ftr": { + _attr: { + "xmlns:m": "http://schemas.openxmlformats.org/officeDocument/2006/math", + "xmlns:mc": "http://schemas.openxmlformats.org/markup-compatibility/2006", + "xmlns:o": "urn:schemas-microsoft-com:office:office", + "xmlns:r": "http://schemas.openxmlformats.org/officeDocument/2006/relationships", + "xmlns:v": "urn:schemas-microsoft-com:vml", + "xmlns:w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + "xmlns:w10": "urn:schemas-microsoft-com:office:word", + "xmlns:w14": "http://schemas.microsoft.com/office/word/2010/wordml", + "xmlns:w15": "http://schemas.microsoft.com/office/word/2012/wordml", + "xmlns:wne": "http://schemas.microsoft.com/office/word/2006/wordml", + "xmlns:wp": "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing", + "xmlns:wp14": "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing", + "xmlns:wpc": "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas", + "xmlns:wpg": "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup", + "xmlns:wpi": "http://schemas.microsoft.com/office/word/2010/wordprocessingInk", + "xmlns:wps": "http://schemas.microsoft.com/office/word/2010/wordprocessingShape", + }, + }, + }); + }); + + it("should create with initContent", () => { + const header = new Footer(1, new Paragraph({})); + + const tree = new Formatter().format(header); + + expect(tree).to.deep.equal({ + "w:ftr": {}, + }); + }); +}); diff --git a/src/file/header-wrapper.spec.ts b/src/file/header-wrapper.spec.ts index 7c3073a094..00ee776a95 100644 --- a/src/file/header-wrapper.spec.ts +++ b/src/file/header-wrapper.spec.ts @@ -37,17 +37,6 @@ describe("HeaderWrapper", () => { }); }); - describe("#addImage", () => { - it("should call the underlying header's addImage", () => { - const file = new HeaderWrapper(new Media(), 1); - const spy = sinon.spy(file.Header, "add"); - // tslint:disable-next-line:no-any - file.addImage({} as any); - - expect(spy.called).to.equal(true); - }); - }); - describe("#addChildElement", () => { it("should call the underlying header's addChildElement", () => { const file = new HeaderWrapper(new Media(), 1); diff --git a/src/file/header-wrapper.ts b/src/file/header-wrapper.ts index d698505e4a..407399a8fe 100644 --- a/src/file/header-wrapper.ts +++ b/src/file/header-wrapper.ts @@ -2,7 +2,7 @@ import { XmlComponent } from "file/xml-components"; import { HeaderReferenceType } from "./document"; import { Header } from "./header/header"; -import { Image, Media } from "./media"; +import { Media } from "./media"; import { Paragraph } from "./paragraph"; import { Relationships } from "./relationships"; import { Table } from "./table"; @@ -27,11 +27,6 @@ export class HeaderWrapper { return this; } - public addImage(image: Image): HeaderWrapper { - this.header.add(image.Paragraph); - return this; - } - public addChildElement(childElement: XmlComponent | string): void { this.header.addChildElement(childElement); } diff --git a/src/file/header/header.spec.ts b/src/file/header/header.spec.ts new file mode 100644 index 0000000000..651c21b545 --- /dev/null +++ b/src/file/header/header.spec.ts @@ -0,0 +1,58 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { Paragraph } from "../paragraph"; +import { Header } from "./header"; + +describe("Header", () => { + it("should create", () => { + const header = new Header(1); + + const tree = new Formatter().format(header); + + expect(tree).to.deep.equal({ + "w:hdr": { + _attr: { + "xmlns:cx": "http://schemas.microsoft.com/office/drawing/2014/chartex", + "xmlns:cx1": "http://schemas.microsoft.com/office/drawing/2015/9/8/chartex", + "xmlns:cx2": "http://schemas.microsoft.com/office/drawing/2015/10/21/chartex", + "xmlns:cx3": "http://schemas.microsoft.com/office/drawing/2016/5/9/chartex", + "xmlns:cx4": "http://schemas.microsoft.com/office/drawing/2016/5/10/chartex", + "xmlns:cx5": "http://schemas.microsoft.com/office/drawing/2016/5/11/chartex", + "xmlns:cx6": "http://schemas.microsoft.com/office/drawing/2016/5/12/chartex", + "xmlns:cx7": "http://schemas.microsoft.com/office/drawing/2016/5/13/chartex", + "xmlns:cx8": "http://schemas.microsoft.com/office/drawing/2016/5/14/chartex", + "xmlns:m": "http://schemas.openxmlformats.org/officeDocument/2006/math", + "xmlns:mc": "http://schemas.openxmlformats.org/markup-compatibility/2006", + "xmlns:o": "urn:schemas-microsoft-com:office:office", + "xmlns:r": "http://schemas.openxmlformats.org/officeDocument/2006/relationships", + "xmlns:v": "urn:schemas-microsoft-com:vml", + "xmlns:w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main", + "xmlns:w10": "urn:schemas-microsoft-com:office:word", + "xmlns:w14": "http://schemas.microsoft.com/office/word/2010/wordml", + "xmlns:w15": "http://schemas.microsoft.com/office/word/2012/wordml", + "xmlns:w16cid": "http://schemas.microsoft.com/office/word/2016/wordml/cid", + "xmlns:w16se": "http://schemas.microsoft.com/office/word/2015/wordml/symex", + "xmlns:wne": "http://schemas.microsoft.com/office/word/2006/wordml", + "xmlns:wp": "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing", + "xmlns:wp14": "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing", + "xmlns:wpc": "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas", + "xmlns:wpg": "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup", + "xmlns:wpi": "http://schemas.microsoft.com/office/word/2010/wordprocessingInk", + "xmlns:wps": "http://schemas.microsoft.com/office/word/2010/wordprocessingShape", + }, + }, + }); + }); + + it("should create with initContent", () => { + const header = new Header(1, new Paragraph({})); + + const tree = new Formatter().format(header); + + expect(tree).to.deep.equal({ + "w:hdr": {}, + }); + }); +}); diff --git a/src/file/media/image.ts b/src/file/media/image.ts deleted file mode 100644 index 8255fe7398..0000000000 --- a/src/file/media/image.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { ImageParagraph, PictureRun } from "../paragraph"; - -export class Image { - constructor(private readonly paragraph: ImageParagraph) {} - - public get Paragraph(): ImageParagraph { - return this.paragraph; - } - - public get Run(): PictureRun { - return this.paragraph.Run; - } -} diff --git a/src/file/media/index.ts b/src/file/media/index.ts index 2ccc436e68..3575274e26 100644 --- a/src/file/media/index.ts +++ b/src/file/media/index.ts @@ -1,3 +1,2 @@ export * from "./media"; export * from "./data"; -export * from "./image"; diff --git a/src/file/media/media.spec.ts b/src/file/media/media.spec.ts index b7ce0c106c..e566213ee5 100644 --- a/src/file/media/media.spec.ts +++ b/src/file/media/media.spec.ts @@ -80,6 +80,16 @@ describe("Media", () => { const image = new Media().addMedia(""); expect(image.stream).to.be.an.instanceof(Uint8Array); }); + + it("should use data as is if its not a string", () => { + // tslint:disable-next-line + ((process as any).atob as any) = () => "atob result"; + // tslint:disable-next-line:no-any + (Media as any).generateId = () => "test"; + + const image = new Media().addMedia(new Buffer("")); + expect(image.stream).to.be.an.instanceof(Uint8Array); + }); }); describe("#getMedia", () => { diff --git a/src/file/paragraph/formatting/border.spec.ts b/src/file/paragraph/formatting/border.spec.ts index f07a297479..6e89ffcfc8 100644 --- a/src/file/paragraph/formatting/border.spec.ts +++ b/src/file/paragraph/formatting/border.spec.ts @@ -1,11 +1,87 @@ -import { assert, expect } from "chai"; +import { expect } from "chai"; import { Formatter } from "export/formatter"; -import { ThematicBreak } from "./border"; +import { Border, ThematicBreak } from "./border"; describe("Border", () => { - // TODO: Need tests here + describe("#constructor", () => { + it("should create", () => { + const border = new Border({ + top: { + color: "red", + space: 1, + value: "test", + size: 2, + }, + bottom: { + color: "red", + space: 3, + value: "test", + size: 4, + }, + left: { + color: "red", + space: 5, + value: "test", + size: 6, + }, + right: { + color: "red", + space: 7, + value: "test", + size: 8, + }, + }); + + const tree = new Formatter().format(border); + + expect(tree).to.deep.equal({ + "w:pBdr": [ + { + "w:top": { + _attr: { + "w:color": "red", + "w:space": 1, + "w:sz": 2, + "w:val": "test", + }, + }, + }, + { + "w:bottom": { + _attr: { + "w:color": "red", + "w:space": 3, + "w:sz": 4, + "w:val": "test", + }, + }, + }, + { + "w:left": { + _attr: { + "w:color": "red", + "w:space": 5, + "w:sz": 6, + "w:val": "test", + }, + }, + }, + { + "w:right": { + _attr: { + "w:color": "red", + "w:space": 7, + "w:sz": 8, + "w:val": "test", + }, + }, + }, + ], + }); + }); + }); }); describe("ThematicBreak", () => { @@ -16,17 +92,6 @@ describe("ThematicBreak", () => { }); describe("#constructor()", () => { - it("should create valid JSON", () => { - const stringifiedJson = JSON.stringify(thematicBreak); - - try { - JSON.parse(stringifiedJson); - } catch (e) { - assert.isTrue(false); - } - assert.isTrue(true); - }); - it("should create a Thematic Break with correct border properties", () => { const tree = new Formatter().format(thematicBreak); expect(tree).to.deep.equal({ diff --git a/src/file/paragraph/image.spec.ts b/src/file/paragraph/image.spec.ts deleted file mode 100644 index 715c1c93ab..0000000000 --- a/src/file/paragraph/image.spec.ts +++ /dev/null @@ -1,39 +0,0 @@ -// tslint:disable:object-literal-key-quotes -import { assert } from "chai"; - -import { ImageParagraph } from "./image"; - -describe("Image", () => { - let image: ImageParagraph; - - beforeEach(() => { - image = new ImageParagraph({ - stream: new Buffer(""), - path: "", - fileName: "test.png", - dimensions: { - pixels: { - x: 10, - y: 10, - }, - emus: { - x: 10, - y: 10, - }, - }, - }); - }); - - describe("#constructor()", () => { - it("should create valid JSON", () => { - const stringifiedJson = JSON.stringify(image); - - try { - JSON.parse(stringifiedJson); - } catch (e) { - assert.isTrue(false); - } - assert.isTrue(true); - }); - }); -}); diff --git a/src/file/paragraph/image.ts b/src/file/paragraph/image.ts deleted file mode 100644 index 4fa3d97b9f..0000000000 --- a/src/file/paragraph/image.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { IDrawingOptions } from "../drawing"; -import { IMediaData } from "../media"; -import { Paragraph } from "./paragraph"; -import { PictureRun } from "./run"; - -export class ImageParagraph extends Paragraph { - private readonly pictureRun: PictureRun; - - constructor(imageData: IMediaData, drawingOptions?: IDrawingOptions) { - super({}); - this.pictureRun = new PictureRun(imageData, drawingOptions); - this.root.push(this.pictureRun); - } - - public get Run(): PictureRun { - return this.pictureRun; - } -} diff --git a/src/file/paragraph/index.ts b/src/file/paragraph/index.ts index 222cb1bf4f..68a1b0b800 100644 --- a/src/file/paragraph/index.ts +++ b/src/file/paragraph/index.ts @@ -3,4 +3,3 @@ export * from "./paragraph"; export * from "./properties"; export * from "./run"; export * from "./links"; -export * from "./image"; diff --git a/src/file/paragraph/paragraph.ts b/src/file/paragraph/paragraph.ts index 3f39ba68bc..24c3e1717d 100644 --- a/src/file/paragraph/paragraph.ts +++ b/src/file/paragraph/paragraph.ts @@ -1,6 +1,5 @@ // http://officeopenxml.com/WPparagraph.php import { FootnoteReferenceRun } from "file/footnotes/footnote/run/reference-run"; -import { Image } from "file/media"; import { Num } from "file/numbering/num"; import { XmlComponent } from "file/xml-components"; @@ -190,13 +189,6 @@ export class Paragraph extends XmlComponent { return this; } - public addImage(image: Image): PictureRun { - const run = image.Run; - this.addRun(run); - - return run; - } - public pageBreak(): Paragraph { this.root.push(new PageBreak()); 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 8ac54ed85a..4dd86ffc9c 100644 --- a/src/file/table/table-cell/table-cell.spec.ts +++ b/src/file/table/table-cell/table-cell.spec.ts @@ -3,7 +3,9 @@ import { expect } from "chai"; import { Formatter } from "export/formatter"; import { BorderStyle } from "file/styles"; -import { TableCellBorders, TableCellWidth, WidthType } from "./table-cell-components"; +import { ShadingType } from "../shading"; +import { TableCell } from "./table-cell"; +import { TableCellBorders, TableCellWidth, VerticalAlign, VerticalMergeType, WidthType } from "./table-cell-components"; describe("TableCellBorders", () => { describe("#prepForXml", () => { @@ -222,3 +224,332 @@ describe("TableCellWidth", () => { }); }); }); + +describe("TableCell", () => { + describe("#constructor", () => { + it("should create", () => { + const cell = new TableCell({ + children: [], + }); + + const tree = new Formatter().format(cell); + + expect(tree).to.deep.equal({ + "w:tc": [ + { + "w:p": {}, + }, + ], + }); + }); + + it("should create with vertical align", () => { + const cell = new TableCell({ + children: [], + verticalAlign: VerticalAlign.CENTER, + }); + + const tree = new Formatter().format(cell); + + expect(tree).to.deep.equal({ + "w:tc": [ + { + "w:tcPr": [ + { + "w:vAlign": { + _attr: { + "w:val": "center", + }, + }, + }, + ], + }, + { + "w:p": {}, + }, + ], + }); + }); + + it("should create with vertical merge", () => { + const cell = new TableCell({ + children: [], + verticalMerge: VerticalMergeType.RESTART, + }); + + const tree = new Formatter().format(cell); + + expect(tree).to.deep.equal({ + "w:tc": [ + { + "w:tcPr": [ + { + "w:vMerge": { + _attr: { + "w:val": "restart", + }, + }, + }, + ], + }, + { + "w:p": {}, + }, + ], + }); + }); + + it("should create with margins", () => { + const cell = new TableCell({ + children: [], + margins: { + top: 1, + left: 1, + bottom: 1, + right: 1, + }, + }); + + const tree = new Formatter().format(cell); + + expect(tree).to.deep.equal({ + "w:tc": [ + { + "w:tcPr": [ + { + "w:tcMar": [ + { + "w:top": { + _attr: { + "w:type": "dxa", + "w:w": 1, + }, + }, + }, + { + "w:bottom": { + _attr: { + "w:type": "dxa", + "w:w": 1, + }, + }, + }, + { + "w:end": { + _attr: { + "w:type": "dxa", + "w:w": 1, + }, + }, + }, + { + "w:start": { + _attr: { + "w:type": "dxa", + "w:w": 1, + }, + }, + }, + ], + }, + ], + }, + { + "w:p": {}, + }, + ], + }); + }); + + it("should create with shading", () => { + const cell = new TableCell({ + children: [], + shading: { + fill: "red", + color: "blue", + val: ShadingType.PERCENT_10, + }, + }); + + const tree = new Formatter().format(cell); + + expect(tree).to.deep.equal({ + "w:tc": [ + { + "w:tcPr": [ + { + "w:shd": { + _attr: { + "w:color": "blue", + "w:fill": "red", + "w:val": "pct10", + }, + }, + }, + ], + }, + { + "w:p": {}, + }, + ], + }); + }); + + it("should create with column span", () => { + const cell = new TableCell({ + children: [], + columnSpan: 2, + }); + + const tree = new Formatter().format(cell); + + expect(tree).to.deep.equal({ + "w:tc": [ + { + "w:tcPr": [ + { + "w:gridSpan": { + _attr: { + "w:val": 2, + }, + }, + }, + ], + }, + { + "w:p": {}, + }, + ], + }); + }); + + describe("rowSpan", () => { + it("should not create with row span if its less than 1", () => { + const cell = new TableCell({ + children: [], + rowSpan: 0, + }); + + const tree = new Formatter().format(cell); + + expect(tree).to.deep.equal({ + "w:tc": [ + { + "w:p": {}, + }, + ], + }); + }); + + it("should create with row span if its greater than 1", () => { + const cell = new TableCell({ + children: [], + rowSpan: 2, + }); + + const tree = new Formatter().format(cell); + + expect(tree).to.deep.equal({ + "w:tc": [ + { + "w:tcPr": [ + { + "w:vMerge": { + _attr: { + "w:val": "restart", + }, + }, + }, + ], + }, + { + "w:p": {}, + }, + ], + }); + }); + + it("should create with borders", () => { + const cell = new TableCell({ + children: [], + borders: { + top: { + style: BorderStyle.DASH_DOT_STROKED, + size: 3, + color: "red", + }, + bottom: { + style: BorderStyle.DOUBLE, + size: 3, + color: "blue", + }, + left: { + style: BorderStyle.DASH_DOT_STROKED, + size: 3, + color: "green", + }, + right: { + style: BorderStyle.DASH_DOT_STROKED, + size: 3, + color: "#ff8000", + }, + }, + }); + + const tree = new Formatter().format(cell); + + expect(tree).to.deep.equal({ + "w:tc": [ + { + "w:tcPr": [ + { + "w:tcBorders": [ + { + "w:top": { + _attr: { + "w:color": "red", + "w:sz": 3, + "w:val": "dashDotStroked", + }, + }, + }, + { + "w:bottom": { + _attr: { + "w:color": "blue", + "w:sz": 3, + "w:val": "double", + }, + }, + }, + { + "w:left": { + _attr: { + "w:color": "green", + "w:sz": 3, + "w:val": "dashDotStroked", + }, + }, + }, + { + "w:right": { + _attr: { + "w:color": "#ff8000", + "w:sz": 3, + "w:val": "dashDotStroked", + }, + }, + }, + ], + }, + ], + }, + { + "w:p": {}, + }, + ], + }); + }); + }); + }); +}); diff --git a/src/file/table/table-properties/table-cell-margin.spec.ts b/src/file/table/table-properties/table-cell-margin.spec.ts index 7f0d13d218..2a89db7a4a 100644 --- a/src/file/table/table-properties/table-cell-margin.spec.ts +++ b/src/file/table/table-properties/table-cell-margin.spec.ts @@ -14,38 +14,66 @@ describe("TableCellMargin", () => { }); describe("#addTopMargin", () => { - it("adds a table cell top margin", () => { + it("should add a table cell top margin", () => { const cellMargin = new TableCellMargin(); cellMargin.addTopMargin(1234, WidthType.DXA); const tree = new Formatter().format(cellMargin); expect(tree).to.deep.equal({ "w:tblCellMar": [{ "w:top": { _attr: { "w:type": "dxa", "w:w": 1234 } } }] }); }); + + it("should add a table cell top margin using default width type", () => { + const cellMargin = new TableCellMargin(); + cellMargin.addTopMargin(1234); + const tree = new Formatter().format(cellMargin); + expect(tree).to.deep.equal({ "w:tblCellMar": [{ "w:top": { _attr: { "w:type": "dxa", "w:w": 1234 } } }] }); + }); }); describe("#addLeftMargin", () => { - it("adds a table cell left margin", () => { + it("should add a table cell left margin", () => { const cellMargin = new TableCellMargin(); cellMargin.addLeftMargin(1234, WidthType.DXA); const tree = new Formatter().format(cellMargin); expect(tree).to.deep.equal({ "w:tblCellMar": [{ "w:left": { _attr: { "w:type": "dxa", "w:w": 1234 } } }] }); }); + + it("should add a table cell left margin using default width type", () => { + const cellMargin = new TableCellMargin(); + cellMargin.addLeftMargin(1234); + const tree = new Formatter().format(cellMargin); + expect(tree).to.deep.equal({ "w:tblCellMar": [{ "w:left": { _attr: { "w:type": "dxa", "w:w": 1234 } } }] }); + }); }); describe("#addBottomMargin", () => { - it("adds a table cell bottom margin", () => { + it("should add a table cell bottom margin", () => { const cellMargin = new TableCellMargin(); cellMargin.addBottomMargin(1234, WidthType.DXA); const tree = new Formatter().format(cellMargin); expect(tree).to.deep.equal({ "w:tblCellMar": [{ "w:bottom": { _attr: { "w:type": "dxa", "w:w": 1234 } } }] }); }); + + it("should add a table cell bottom margin using default width type", () => { + const cellMargin = new TableCellMargin(); + cellMargin.addBottomMargin(1234); + const tree = new Formatter().format(cellMargin); + expect(tree).to.deep.equal({ "w:tblCellMar": [{ "w:bottom": { _attr: { "w:type": "dxa", "w:w": 1234 } } }] }); + }); }); describe("#addRightMargin", () => { - it("adds a table cell right margin", () => { + it("should add a table cell right margin", () => { const cellMargin = new TableCellMargin(); cellMargin.addRightMargin(1234, WidthType.DXA); const tree = new Formatter().format(cellMargin); expect(tree).to.deep.equal({ "w:tblCellMar": [{ "w:right": { _attr: { "w:type": "dxa", "w:w": 1234 } } }] }); }); + + it("should add a table cell right margin using default width type", () => { + const cellMargin = new TableCellMargin(); + cellMargin.addRightMargin(1234); + const tree = new Formatter().format(cellMargin); + expect(tree).to.deep.equal({ "w:tblCellMar": [{ "w:right": { _attr: { "w:type": "dxa", "w:w": 1234 } } }] }); + }); }); }); diff --git a/src/file/table/table-row/table-row.spec.ts b/src/file/table/table-row/table-row.spec.ts index 3c732c6217..e013153cd8 100644 --- a/src/file/table/table-row/table-row.spec.ts +++ b/src/file/table/table-row/table-row.spec.ts @@ -2,6 +2,7 @@ import { expect } from "chai"; import { Formatter } from "export/formatter"; +import { Paragraph } from "file/paragraph"; import { HeightRule } from "file/table/table-row/table-row-height"; import { EMPTY_OBJECT } from "file/xml-components"; import { TableCell } from "../table-cell"; @@ -41,6 +42,52 @@ describe("TableRow", () => { }); }); + it("should create with cant split", () => { + const tableRow = new TableRow({ + children: [], + cantSplit: true, + }); + const tree = new Formatter().format(tableRow); + expect(tree).to.deep.equal({ + "w:tr": [ + { + "w:trPr": [ + { + "w:cantSplit": { + _attr: { + "w:val": true, + }, + }, + }, + ], + }, + ], + }); + }); + + it("should create with table header", () => { + const tableRow = new TableRow({ + children: [], + tableHeader: true, + }); + const tree = new Formatter().format(tableRow); + expect(tree).to.deep.equal({ + "w:tr": [ + { + "w:trPr": [ + { + "w:tblHeader": { + _attr: { + "w:val": true, + }, + }, + }, + ], + }, + ], + }); + }); + it("should set row height", () => { const tableRow = new TableRow({ children: [], @@ -69,21 +116,70 @@ describe("TableRow", () => { }); }); - // describe("#mergeCells", () => { - // it("should merge the cell", () => { - // const tableRow = new TableRow({ - // children: [ - // new TableCell({ - // children: [], - // }), - // new TableCell({ - // children: [], - // }), - // ], - // }); + describe("#addCellToIndex", () => { + it("should add cell to correct index with no initial properties", () => { + const tableRow = new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("test")], + }), + ], + tableHeader: true, + }); - // tableRow.mergeCells(0, 1); - // expect(() => tableRow.getCell(1)).to.throw(); - // }); - // }); + tableRow.addCellToIndex( + new TableCell({ + children: [], + }), + 0, + ); + + const tree = new Formatter().format(tableRow); + + expect(tree).to.deep.equal({ + "w:tr": [ + { + "w:trPr": [ + { + "w:tblHeader": { + _attr: { + "w:val": true, + }, + }, + }, + ], + }, + { + "w:tc": [ + { + "w:p": {}, + }, + ], + }, + { + "w:tc": [ + { + "w:p": [ + { + "w:r": [ + { + "w:t": [ + { + _attr: { + "xml:space": "preserve", + }, + }, + "test", + ], + }, + ], + }, + ], + }, + ], + }, + ], + }); + }); + }); }); diff --git a/src/file/table/table.spec.ts b/src/file/table/table.spec.ts index b88bda964a..d2aec26051 100644 --- a/src/file/table/table.spec.ts +++ b/src/file/table/table.spec.ts @@ -8,7 +8,7 @@ import { Table } from "./table"; // import { WidthType } from "./table-cell"; import { RelativeHorizontalPosition, RelativeVerticalPosition, TableAnchorType } from "./table-properties"; -import { TableCell } from "./table-cell"; +import { TableCell, WidthType } from "./table-cell"; import { TableLayoutType } from "./table-properties/table-layout"; import { TableRow } from "./table-row"; @@ -210,6 +210,45 @@ describe("Table", () => { "w:tblPr": [DEFAULT_TABLE_PROPERTIES, BORDERS, WIDTHS, { "w:tblLayout": { _attr: { "w:type": "fixed" } } }], }); }); + + it("should set the table to provided width", () => { + const table = new Table({ + rows: [ + new TableRow({ + children: [ + new TableCell({ + children: [new Paragraph("hello")], + }), + ], + }), + ], + width: { + size: 100, + type: WidthType.PERCENTAGE, + }, + 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": [ + DEFAULT_TABLE_PROPERTIES, + BORDERS, + { + "w:tblW": { + _attr: { + "w:type": "pct", + "w:w": "100%", + }, + }, + }, + { "w:tblLayout": { _attr: { "w:type": "fixed" } } }, + ], + }); + }); }); describe("Cell", () => {