Compare commits

...

16 Commits

Author SHA1 Message Date
9e8d67c4a9 Version bump 2019-06-28 01:59:18 +01:00
cbbc4a80e7 FIx up API for column count 2019-06-28 01:57:43 +01:00
1f51fd7a31 Remove usage of Utility 2019-06-27 01:35:58 +01:00
120d97bc64 Remove Utility 2019-06-27 01:10:39 +01:00
a531713214 Remove usage of Utility class 2019-06-26 22:12:18 +01:00
564a055316 Remove jsonify method 2019-06-26 02:11:43 +01:00
6c29c4fb1f Merge pull request #343 from markcharyk/no-pretty-xml
Make prettified XML a Packer-wide configurable option
2019-06-26 00:44:33 +01:00
552580bc47 Merge pull request #340 from filippomuscolino/feat/row-height
Add table row height
2019-06-25 20:18:17 +01:00
0998d43b8f Merge pull request #341 from filippomuscolino/fix/missing-exports
Fix: add missing exports
2019-06-25 15:38:13 +01:00
b08354494c Improve tests 2019-06-25 14:48:45 +02:00
f54b9a197d Fix: add missing exports 2019-06-25 14:35:35 +02:00
2d4412ce51 Add table row height 2019-06-25 12:02:53 +02:00
72c32378c5 Make prettified XML a Packer-wide configurable option
The xml() function was being passed a flag for pretty printing the XML in word/document.xml. This was causing some parsers (not Word itself) to break when they didn't expect whitepsace. It was also causing some files to be prettified and others to be ugly. This ensures consistent prettification.
2019-06-21 13:53:01 -07:00
0c79c0ac83 Merge pull request #337 from zahrafali/multiple-columns
Multiple columns in a section
2019-06-21 15:36:33 +01:00
1dfc27ba08 Multiple columns in a section 2019-06-21 01:22:27 +05:30
58dc6fe389 Format demo 2019-06-12 01:12:32 +01:00
38 changed files with 1016 additions and 297 deletions

View File

@ -1,7 +1,7 @@
// Example demonstrating line numbers. // Example demonstrating line numbers.
// Import from 'docx' rather than '../build' if you install from npm // Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs"; import * as fs from "fs";
import { Document, Paragraph, Packer, LineNumberRestartFormat } from "../build"; import { Document, LineNumberRestartFormat, Packer, Paragraph } from "../build";
const doc = new Document( const doc = new Document(
{}, {},
@ -11,12 +11,13 @@ const doc = new Document(
}, },
); );
doc.addParagraph(new Paragraph("Hello").heading1());
doc.createParagraph(
doc.addParagraph(new Paragraph('Hello').heading1()); "Himenaeos duis luctus nullam fermentum lobortis potenti vivamus non dis, sed facilisis ultricies scelerisque aenean risus hac senectus. Adipiscing id venenatis justo ante gravida placerat, ac curabitur dis pellentesque proin bibendum risus, aliquam porta taciti vulputate primis. Tortor ipsum fermentum quam vel convallis primis nisl praesent tincidunt, lobortis quisque felis vitae condimentum class ut sem nam, aenean potenti pretium ac amet lacinia himenaeos mi. Aliquam nisl turpis hendrerit est morbi malesuada, augue interdum mus inceptos curabitur tristique, parturient feugiat sodales nulla facilisi. Aliquam non pulvinar purus nulla ex integer, velit faucibus vitae at bibendum quam, risus elit aenean adipiscing posuere.",
doc.createParagraph("Himenaeos duis luctus nullam fermentum lobortis potenti vivamus non dis, sed facilisis ultricies scelerisque aenean risus hac senectus. Adipiscing id venenatis justo ante gravida placerat, ac curabitur dis pellentesque proin bibendum risus, aliquam porta taciti vulputate primis. Tortor ipsum fermentum quam vel convallis primis nisl praesent tincidunt, lobortis quisque felis vitae condimentum class ut sem nam, aenean potenti pretium ac amet lacinia himenaeos mi. Aliquam nisl turpis hendrerit est morbi malesuada, augue interdum mus inceptos curabitur tristique, parturient feugiat sodales nulla facilisi. Aliquam non pulvinar purus nulla ex integer, velit faucibus vitae at bibendum quam, risus elit aenean adipiscing posuere."); );
doc.createParagraph("Sed laoreet id mattis egestas nam mollis elit lacinia convallis dui tincidunt ultricies habitant, pharetra per maximus interdum neque tempor risus efficitur morbi imperdiet senectus. Lectus laoreet senectus finibus inceptos donec potenti fermentum, ultrices eleifend odio suscipit magnis tellus maximus nibh, ac sit nullam eget felis himenaeos. Diam class sem magnis aenean commodo faucibus id proin mi, nullam sodales nec mus parturient ornare ad inceptos velit hendrerit, bibendum placerat eleifend integer facilisis urna dictumst suspendisse."); doc.createParagraph(
"Sed laoreet id mattis egestas nam mollis elit lacinia convallis dui tincidunt ultricies habitant, pharetra per maximus interdum neque tempor risus efficitur morbi imperdiet senectus. Lectus laoreet senectus finibus inceptos donec potenti fermentum, ultrices eleifend odio suscipit magnis tellus maximus nibh, ac sit nullam eget felis himenaeos. Diam class sem magnis aenean commodo faucibus id proin mi, nullam sodales nec mus parturient ornare ad inceptos velit hendrerit, bibendum placerat eleifend integer facilisis urna dictumst suspendisse.",
);
const packer = new Packer(); const packer = new Packer();

36
demo/demo44.ts Normal file
View File

@ -0,0 +1,36 @@
// Sections with multiple columns
// Import from 'docx' rather than '../build' if you install from npm
import * as fs from "fs";
import { Document, Packer } from "../build";
const doc = new Document();
doc.addSection({
column: {
width: 708,
count: 2,
},
});
doc.createParagraph("This text will be split into 2 columns on a page.");
doc.createParagraph(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
);
doc.addSection({
column: {
width: 708,
count: 2,
},
});
doc.createParagraph("This text will be split into 3 columns on a page.");
doc.createParagraph(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
);
const packer = new Packer();
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});

View File

@ -1,6 +1,6 @@
{ {
"name": "docx", "name": "docx",
"version": "5.0.0-rc3", "version": "5.0.0-rc4",
"description": "Generate .docx documents with JavaScript (formerly Office-Clippy)", "description": "Generate .docx documents with JavaScript (formerly Office-Clippy)",
"main": "build/index.js", "main": "build/index.js",
"scripts": { "scripts": {

View File

@ -1,10 +1,9 @@
import { assert } from "chai"; import { assert, expect } from "chai";
import { Formatter } from "../export/formatter"; import { Formatter } from "export/formatter";
import * as file from "../file"; import * as file from "file";
import { CoreProperties } from "../file/core-properties"; import { CoreProperties } from "file/core-properties";
import { Attributes } from "../file/xml-components"; import { Attributes } from "file/xml-components";
import { Utility } from "../tests/utility";
describe("Formatter", () => { describe("Formatter", () => {
let formatter: Formatter; let formatter: Formatter;
@ -38,26 +37,24 @@ describe("Formatter", () => {
const attributes = new Attributes({ const attributes = new Attributes({
rsidSect: "test2", rsidSect: "test2",
}); });
let newJson = formatter.format(attributes); const tree = formatter.format(attributes);
newJson = Utility.jsonify(newJson); expect(tree).to.deep.equal({
if (newJson._attr === undefined) { _attr: {
assert.fail(); "w:rsidSect": "test2",
return; },
} });
assert.isDefined(newJson._attr["w:rsidSect"]);
}); });
it("should format attributes (val)", () => { it("should format attributes (val)", () => {
const attributes = new Attributes({ const attributes = new Attributes({
val: "test", val: "test",
}); });
let newJson = formatter.format(attributes); const tree = formatter.format(attributes);
newJson = Utility.jsonify(newJson); expect(tree).to.deep.equal({
if (newJson._attr === undefined) { _attr: {
assert.fail(); "w:val": "test",
return; },
} });
assert.isDefined(newJson._attr["w:val"]);
}); });
it("should should change 'p' tag into 'w:p' tag", () => { it("should should change 'p' tag into 'w:p' tag", () => {

View File

@ -30,10 +30,12 @@ interface IXmlifyedFileMapping {
export class Compiler { export class Compiler {
private readonly formatter: Formatter; private readonly formatter: Formatter;
private readonly imageReplacer: ImageReplacer; private readonly imageReplacer: ImageReplacer;
private readonly prettifyXml?: boolean;
constructor() { constructor(prettifyXml?: boolean) {
this.formatter = new Formatter(); this.formatter = new Formatter();
this.imageReplacer = new ImageReplacer(); this.imageReplacer = new ImageReplacer();
this.prettifyXml = prettifyXml;
} }
public compile(file: File): JSZip { public compile(file: File): JSZip {
@ -71,7 +73,7 @@ export class Compiler {
return { return {
Relationships: { Relationships: {
data: (() => { data: (() => {
const xmlData = xml(this.formatter.format(file.Document)); const xmlData = xml(this.formatter.format(file.Document), this.prettifyXml);
const mediaDatas = this.imageReplacer.getMediaData(xmlData, file.Media); const mediaDatas = this.imageReplacer.getMediaData(xmlData, file.Media);
mediaDatas.forEach((mediaData, i) => { mediaDatas.forEach((mediaData, i) => {
@ -82,13 +84,13 @@ export class Compiler {
); );
}); });
return xml(this.formatter.format(file.DocumentRelationships)); return xml(this.formatter.format(file.DocumentRelationships), this.prettifyXml);
})(), })(),
path: "word/_rels/document.xml.rels", path: "word/_rels/document.xml.rels",
}, },
Document: { Document: {
data: (() => { data: (() => {
const tempXmlData = xml(this.formatter.format(file.Document), true); const tempXmlData = xml(this.formatter.format(file.Document), this.prettifyXml);
const mediaDatas = this.imageReplacer.getMediaData(tempXmlData, file.Media); const mediaDatas = this.imageReplacer.getMediaData(tempXmlData, file.Media);
const xmlData = this.imageReplacer.replace(tempXmlData, mediaDatas, documentRelationshipCount); const xmlData = this.imageReplacer.replace(tempXmlData, mediaDatas, documentRelationshipCount);
@ -97,7 +99,7 @@ export class Compiler {
path: "word/document.xml", path: "word/document.xml",
}, },
Styles: { Styles: {
data: xml(this.formatter.format(file.Styles)), data: xml(this.formatter.format(file.Styles), this.prettifyXml),
path: "word/styles.xml", path: "word/styles.xml",
}, },
Properties: { Properties: {
@ -110,15 +112,15 @@ export class Compiler {
path: "docProps/core.xml", path: "docProps/core.xml",
}, },
Numbering: { Numbering: {
data: xml(this.formatter.format(file.Numbering)), data: xml(this.formatter.format(file.Numbering), this.prettifyXml),
path: "word/numbering.xml", path: "word/numbering.xml",
}, },
FileRelationships: { FileRelationships: {
data: xml(this.formatter.format(file.FileRelationships)), data: xml(this.formatter.format(file.FileRelationships), this.prettifyXml),
path: "_rels/.rels", path: "_rels/.rels",
}, },
HeaderRelationships: file.Headers.map((headerWrapper, index) => { HeaderRelationships: file.Headers.map((headerWrapper, index) => {
const xmlData = xml(this.formatter.format(headerWrapper.Header)); const xmlData = xml(this.formatter.format(headerWrapper.Header), this.prettifyXml);
const mediaDatas = this.imageReplacer.getMediaData(xmlData, file.Media); const mediaDatas = this.imageReplacer.getMediaData(xmlData, file.Media);
mediaDatas.forEach((mediaData, i) => { mediaDatas.forEach((mediaData, i) => {
@ -130,12 +132,12 @@ export class Compiler {
}); });
return { return {
data: xml(this.formatter.format(headerWrapper.Relationships)), data: xml(this.formatter.format(headerWrapper.Relationships), this.prettifyXml),
path: `word/_rels/header${index + 1}.xml.rels`, path: `word/_rels/header${index + 1}.xml.rels`,
}; };
}), }),
FooterRelationships: file.Footers.map((footerWrapper, index) => { FooterRelationships: file.Footers.map((footerWrapper, index) => {
const xmlData = xml(this.formatter.format(footerWrapper.Footer)); const xmlData = xml(this.formatter.format(footerWrapper.Footer), this.prettifyXml);
const mediaDatas = this.imageReplacer.getMediaData(xmlData, file.Media); const mediaDatas = this.imageReplacer.getMediaData(xmlData, file.Media);
mediaDatas.forEach((mediaData, i) => { mediaDatas.forEach((mediaData, i) => {
@ -147,12 +149,12 @@ export class Compiler {
}); });
return { return {
data: xml(this.formatter.format(footerWrapper.Relationships)), data: xml(this.formatter.format(footerWrapper.Relationships), this.prettifyXml),
path: `word/_rels/footer${index + 1}.xml.rels`, path: `word/_rels/footer${index + 1}.xml.rels`,
}; };
}), }),
Headers: file.Headers.map((headerWrapper, index) => { Headers: file.Headers.map((headerWrapper, index) => {
const tempXmlData = xml(this.formatter.format(headerWrapper.Header)); const tempXmlData = xml(this.formatter.format(headerWrapper.Header), this.prettifyXml);
const mediaDatas = this.imageReplacer.getMediaData(tempXmlData, file.Media); const mediaDatas = this.imageReplacer.getMediaData(tempXmlData, file.Media);
// TODO: 0 needs to be changed when headers get relationships of their own // TODO: 0 needs to be changed when headers get relationships of their own
const xmlData = this.imageReplacer.replace(tempXmlData, mediaDatas, 0); const xmlData = this.imageReplacer.replace(tempXmlData, mediaDatas, 0);
@ -163,7 +165,7 @@ export class Compiler {
}; };
}), }),
Footers: file.Footers.map((footerWrapper, index) => { Footers: file.Footers.map((footerWrapper, index) => {
const tempXmlData = xml(this.formatter.format(footerWrapper.Footer)); const tempXmlData = xml(this.formatter.format(footerWrapper.Footer), this.prettifyXml);
const mediaDatas = this.imageReplacer.getMediaData(tempXmlData, file.Media); const mediaDatas = this.imageReplacer.getMediaData(tempXmlData, file.Media);
// TODO: 0 needs to be changed when headers get relationships of their own // TODO: 0 needs to be changed when headers get relationships of their own
const xmlData = this.imageReplacer.replace(tempXmlData, mediaDatas, 0); const xmlData = this.imageReplacer.replace(tempXmlData, mediaDatas, 0);
@ -174,19 +176,19 @@ export class Compiler {
}; };
}), }),
ContentTypes: { ContentTypes: {
data: xml(this.formatter.format(file.ContentTypes)), data: xml(this.formatter.format(file.ContentTypes), this.prettifyXml),
path: "[Content_Types].xml", path: "[Content_Types].xml",
}, },
AppProperties: { AppProperties: {
data: xml(this.formatter.format(file.AppProperties)), data: xml(this.formatter.format(file.AppProperties), this.prettifyXml),
path: "docProps/app.xml", path: "docProps/app.xml",
}, },
FootNotes: { FootNotes: {
data: xml(this.formatter.format(file.FootNotes)), data: xml(this.formatter.format(file.FootNotes), this.prettifyXml),
path: "word/footnotes.xml", path: "word/footnotes.xml",
}, },
Settings: { Settings: {
data: xml(this.formatter.format(file.Settings)), data: xml(this.formatter.format(file.Settings), this.prettifyXml),
path: "word/settings.xml", path: "word/settings.xml",
}, },
}; };

View File

@ -4,8 +4,8 @@ import { Compiler } from "./next-compiler";
export class Packer { export class Packer {
private readonly compiler: Compiler; private readonly compiler: Compiler;
constructor() { constructor(prettifyXml?: boolean) {
this.compiler = new Compiler(); this.compiler = new Compiler(prettifyXml);
} }
public async toBuffer(file: File): Promise<Buffer> { public async toBuffer(file: File): Promise<Buffer> {

View File

@ -71,7 +71,7 @@ describe("Body", () => {
}, },
}, },
}, },
{ "w:cols": { _attr: { "w:space": 708 } } }, { "w:cols": { _attr: { "w:space": 708, "w:num": 1 } } },
{ "w:docGrid": { _attr: { "w:linePitch": 360 } } }, { "w:docGrid": { _attr: { "w:linePitch": 360 } } },
], ],
}, },
@ -96,7 +96,7 @@ describe("Body", () => {
}, },
}, },
}, },
{ "w:cols": { _attr: { "w:space": 708 } } }, { "w:cols": { _attr: { "w:space": 708, "w:num": 1 } } },
{ "w:docGrid": { _attr: { "w:linePitch": 360 } } }, { "w:docGrid": { _attr: { "w:linePitch": 360 } } },
], ],
}, },

View File

@ -2,10 +2,12 @@ import { XmlAttributeComponent } from "file/xml-components";
export interface IColumnsAttributes { export interface IColumnsAttributes {
readonly space?: number; readonly space?: number;
readonly num?: number;
} }
export class ColumnsAttributes extends XmlAttributeComponent<IColumnsAttributes> { export class ColumnsAttributes extends XmlAttributeComponent<IColumnsAttributes> {
protected readonly xmlKeys = { protected readonly xmlKeys = {
space: "w:space", space: "w:space",
num: "w:num",
}; };
} }

View File

@ -2,11 +2,12 @@ import { XmlComponent } from "file/xml-components";
import { ColumnsAttributes } from "./columns-attributes"; import { ColumnsAttributes } from "./columns-attributes";
export class Columns extends XmlComponent { export class Columns extends XmlComponent {
constructor(space: number) { constructor(space: number, num: number) {
super("w:cols"); super("w:cols");
this.root.push( this.root.push(
new ColumnsAttributes({ new ColumnsAttributes({
space: space, space: space,
num: num,
}), }),
); );
} }

View File

@ -25,7 +25,10 @@ describe("SectionProperties", () => {
footer: 708, footer: 708,
gutter: 0, gutter: 0,
mirror: false, mirror: false,
column: {
space: 708, space: 708,
count: 1,
},
linePitch: 360, linePitch: 360,
headers: { headers: {
default: new HeaderWrapper(media, 100), default: new HeaderWrapper(media, 100),
@ -55,7 +58,7 @@ describe("SectionProperties", () => {
}, },
}); });
expect(tree["w:sectPr"][2]).to.deep.equal({ "w:cols": { _attr: { "w:space": 708 } } }); expect(tree["w:sectPr"][2]).to.deep.equal({ "w:cols": { _attr: { "w:space": 708, "w:num": 1 } } });
expect(tree["w:sectPr"][3]).to.deep.equal({ "w:docGrid": { _attr: { "w:linePitch": 360 } } }); expect(tree["w:sectPr"][3]).to.deep.equal({ "w:docGrid": { _attr: { "w:linePitch": 360 } } });
expect(tree["w:sectPr"][4]).to.deep.equal({ "w:headerReference": { _attr: { "r:id": "rId100", "w:type": "default" } } }); expect(tree["w:sectPr"][4]).to.deep.equal({ "w:headerReference": { _attr: { "r:id": "rId100", "w:type": "default" } } });
expect(tree["w:sectPr"][5]).to.deep.equal({ "w:footerReference": { _attr: { "r:id": "rId200", "w:type": "even" } } }); expect(tree["w:sectPr"][5]).to.deep.equal({ "w:footerReference": { _attr: { "r:id": "rId200", "w:type": "even" } } });
@ -82,7 +85,7 @@ describe("SectionProperties", () => {
}, },
}, },
}); });
expect(tree["w:sectPr"][2]).to.deep.equal({ "w:cols": { _attr: { "w:space": 708 } } }); expect(tree["w:sectPr"][2]).to.deep.equal({ "w:cols": { _attr: { "w:space": 708, "w:num": 1 } } });
expect(tree["w:sectPr"][3]).to.deep.equal({ "w:docGrid": { _attr: { "w:linePitch": 360 } } }); expect(tree["w:sectPr"][3]).to.deep.equal({ "w:docGrid": { _attr: { "w:linePitch": 360 } } });
}); });

View File

@ -4,7 +4,6 @@ import { HeaderWrapper } from "file/header-wrapper";
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { Columns } from "./columns/columns"; import { Columns } from "./columns/columns";
import { IColumnsAttributes } from "./columns/columns-attributes";
import { DocumentGrid } from "./doc-grid/doc-grid"; import { DocumentGrid } from "./doc-grid/doc-grid";
import { IDocGridAttributesProperties } from "./doc-grid/doc-grid-attributes"; import { IDocGridAttributesProperties } from "./doc-grid/doc-grid-attributes";
import { FooterReferenceType } from "./footer-reference"; import { FooterReferenceType } from "./footer-reference";
@ -40,19 +39,24 @@ interface ITitlePageOptions {
export type SectionPropertiesOptions = IPageSizeAttributes & export type SectionPropertiesOptions = IPageSizeAttributes &
IPageMarginAttributes & IPageMarginAttributes &
IColumnsAttributes &
IDocGridAttributesProperties & IDocGridAttributesProperties &
IHeadersOptions & IHeadersOptions &
IFootersOptions & IFootersOptions &
IPageNumberTypeAttributes & IPageNumberTypeAttributes &
ILineNumberAttributes & ILineNumberAttributes &
IPageBordersOptions & IPageBordersOptions &
ITitlePageOptions; ITitlePageOptions & {
readonly column?: {
readonly space?: number;
readonly count?: number;
};
};
// Need to decouple this from the attributes
export class SectionProperties extends XmlComponent { export class SectionProperties extends XmlComponent {
private readonly options: SectionPropertiesOptions; private readonly options: SectionPropertiesOptions;
constructor(options: SectionPropertiesOptions = {}) { constructor(options: SectionPropertiesOptions = { column: {} }) {
super("w:sectPr"); super("w:sectPr");
const { const {
@ -66,7 +70,7 @@ export class SectionProperties extends XmlComponent {
footer = 708, footer = 708,
gutter = 0, gutter = 0,
mirror = false, mirror = false,
space = 708, column = {},
linePitch = 360, linePitch = 360,
orientation = PageOrientation.PORTRAIT, orientation = PageOrientation.PORTRAIT,
headers, headers,
@ -88,7 +92,7 @@ export class SectionProperties extends XmlComponent {
this.options = options; this.options = options;
this.root.push(new PageSize(width, height, orientation)); this.root.push(new PageSize(width, height, orientation));
this.root.push(new PageMargin(top, right, bottom, left, header, footer, gutter, mirror)); this.root.push(new PageMargin(top, right, bottom, left, header, footer, gutter, mirror));
this.root.push(new Columns(space)); this.root.push(new Columns(column.space ? column.space : 708, column.count ? column.count : 1));
this.root.push(new DocumentGrid(linePitch)); this.root.push(new DocumentGrid(linePitch));
this.addHeaders(headers); this.addHeaders(headers);

View File

@ -1,6 +1,6 @@
import { assert } from "chai"; import { expect } from "chai";
import { Utility } from "tests/utility"; import { Formatter } from "export/formatter";
import { Drawing, IDrawingOptions } from "./drawing"; import { Drawing, IDrawingOptions } from "./drawing";
@ -32,16 +32,181 @@ describe("Drawing", () => {
let currentBreak: Drawing; let currentBreak: Drawing;
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create a Drawing with correct root key", () => {
currentBreak = createDrawing();
const newJson = Utility.jsonify(currentBreak);
assert.equal(newJson.rootKey, "w:drawing");
});
it("should create a drawing with inline element when there are no options passed", () => { it("should create a drawing with inline element when there are no options passed", () => {
currentBreak = createDrawing(); currentBreak = createDrawing();
const newJson = Utility.jsonify(currentBreak); const tree = new Formatter().format(currentBreak);
assert.equal(newJson.root[0].rootKey, "wp:inline"); expect(tree).to.deep.equal({
"w:drawing": [
{
"wp:inline": [
{
_attr: {
distB: 0,
distL: 0,
distR: 0,
distT: 0,
},
},
{
"wp:extent": {
_attr: {
cx: 952500,
cy: 952500,
},
},
},
{
"wp:effectExtent": {
_attr: {
b: 0,
l: 0,
r: 0,
t: 0,
},
},
},
{
"wp:docPr": {
_attr: {
descr: "",
id: 0,
name: "",
},
},
},
{
"wp:cNvGraphicFramePr": [
{
"a:graphicFrameLocks": {
_attr: {
// tslint:disable-next-line:object-literal-key-quotes
noChangeAspect: 1,
"xmlns:a": "http://schemas.openxmlformats.org/drawingml/2006/main",
},
},
},
],
},
{
"a:graphic": [
{
_attr: {
"xmlns:a": "http://schemas.openxmlformats.org/drawingml/2006/main",
},
},
{
"a:graphicData": [
{
_attr: {
uri: "http://schemas.openxmlformats.org/drawingml/2006/picture",
},
},
{
"pic:pic": [
{
_attr: {
"xmlns:pic": "http://schemas.openxmlformats.org/drawingml/2006/picture",
},
},
{
"pic:nvPicPr": [
{
"pic:cNvPr": {
_attr: {
desc: "",
id: 0,
name: "",
},
},
},
{
"pic:cNvPicPr": [
{
"a:picLocks": {
_attr: {
noChangeArrowheads: 1,
noChangeAspect: 1,
},
},
},
],
},
],
},
{
"pic:blipFill": [
{
"a:blip": {
_attr: {
// tslint:disable-next-line:object-literal-key-quotes
cstate: "none",
"r:embed": "rId{test.jpg}",
},
},
},
{
"a:srcRect": {},
},
{
"a:stretch": [
{
"a:fillRect": {},
},
],
},
],
},
{
"pic:spPr": [
{
_attr: {
bwMode: "auto",
},
},
{
"a:xfrm": [
{
"a:ext": {
_attr: {
cx: 952500,
cy: 952500,
},
},
},
{
"a:off": {
_attr: {
x: 0,
y: 0,
},
},
},
],
},
{
"a:prstGeom": [
{
_attr: {
prst: "rect",
},
},
{
"a:avLst": {},
},
],
},
],
},
],
},
],
},
],
},
],
},
],
});
}); });
it("should create a drawing with anchor element when there options are passed", () => { it("should create a drawing with anchor element when there options are passed", () => {
@ -55,8 +220,220 @@ describe("Drawing", () => {
}, },
}, },
}); });
const newJson = Utility.jsonify(currentBreak); const tree = new Formatter().format(currentBreak);
assert.equal(newJson.root[0].rootKey, "wp:anchor"); expect(tree).to.deep.equal({
"w:drawing": [
{
"wp:anchor": [
{
_attr: {
allowOverlap: "1",
behindDoc: "0",
distB: 0,
distL: 0,
distR: 0,
distT: 0,
layoutInCell: "1",
locked: "0",
relativeHeight: 952500,
simplePos: "0",
},
},
{
"wp:simplePos": {
_attr: {
x: 0,
y: 0,
},
},
},
{
"wp:positionH": [
{
_attr: {
relativeFrom: "page",
},
},
{
"wp:posOffset": ["0"],
},
],
},
{
"wp:positionV": [
{
_attr: {
relativeFrom: "page",
},
},
{
"wp:posOffset": ["0"],
},
],
},
{
"wp:extent": {
_attr: {
cx: 952500,
cy: 952500,
},
},
},
{
"wp:effectExtent": {
_attr: {
b: 0,
l: 0,
r: 0,
t: 0,
},
},
},
{
"wp:wrapNone": {},
},
{
"wp:docPr": {
_attr: {
descr: "",
id: 0,
name: "",
},
},
},
{
"wp:cNvGraphicFramePr": [
{
"a:graphicFrameLocks": {
_attr: {
// tslint:disable-next-line:object-literal-key-quotes
noChangeAspect: 1,
"xmlns:a": "http://schemas.openxmlformats.org/drawingml/2006/main",
},
},
},
],
},
{
"a:graphic": [
{
_attr: {
"xmlns:a": "http://schemas.openxmlformats.org/drawingml/2006/main",
},
},
{
"a:graphicData": [
{
_attr: {
uri: "http://schemas.openxmlformats.org/drawingml/2006/picture",
},
},
{
"pic:pic": [
{
_attr: {
"xmlns:pic": "http://schemas.openxmlformats.org/drawingml/2006/picture",
},
},
{
"pic:nvPicPr": [
{
"pic:cNvPr": {
_attr: {
desc: "",
id: 0,
name: "",
},
},
},
{
"pic:cNvPicPr": [
{
"a:picLocks": {
_attr: {
noChangeArrowheads: 1,
noChangeAspect: 1,
},
},
},
],
},
],
},
{
"pic:blipFill": [
{
"a:blip": {
_attr: {
// tslint:disable-next-line:object-literal-key-quotes
cstate: "none",
"r:embed": "rId{test.jpg}",
},
},
},
{
"a:srcRect": {},
},
{
"a:stretch": [
{
"a:fillRect": {},
},
],
},
],
},
{
"pic:spPr": [
{
_attr: {
bwMode: "auto",
},
},
{
"a:xfrm": [
{
"a:ext": {
_attr: {
cx: 952500,
cy: 952500,
},
},
},
{
"a:off": {
_attr: {
x: 0,
y: 0,
},
},
},
],
},
{
"a:prstGeom": [
{
_attr: {
prst: "rect",
},
},
{
"a:avLst": {},
},
],
},
],
},
],
},
],
},
],
},
],
},
],
});
}); });
}); });
}); });

View File

@ -1,6 +1,6 @@
import { assert } from "chai"; import { expect } from "chai";
import { Utility } from "tests/utility"; import { Formatter } from "export/formatter";
import { Align } from "./align"; import { Align } from "./align";
import { VerticalPositionAlign } from "./floating-position"; import { VerticalPositionAlign } from "./floating-position";
@ -8,9 +8,10 @@ import { VerticalPositionAlign } from "./floating-position";
describe("Align", () => { describe("Align", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create a element with correct root key", () => { it("should create a element with correct root key", () => {
const newJson = Utility.jsonify(new Align(VerticalPositionAlign.CENTER)); const tree = new Formatter().format(new Align(VerticalPositionAlign.CENTER));
assert.equal(newJson.rootKey, "wp:align"); expect(tree).to.deep.equal({
assert.include(newJson.root[0], VerticalPositionAlign.CENTER); "wp:align": ["center"],
});
}); });
}); });
}); });

View File

@ -1,6 +1,6 @@
import { assert } from "chai"; import { expect } from "chai";
import { Utility } from "tests/utility"; import { Formatter } from "export/formatter";
import { HorizontalPositionAlign, HorizontalPositionRelativeFrom } from "./floating-position"; import { HorizontalPositionAlign, HorizontalPositionRelativeFrom } from "./floating-position";
import { HorizontalPosition } from "./horizontal-position"; import { HorizontalPosition } from "./horizontal-position";
@ -8,35 +8,45 @@ import { HorizontalPosition } from "./horizontal-position";
describe("HorizontalPosition", () => { describe("HorizontalPosition", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create a element with position align", () => { it("should create a element with position align", () => {
const newJson = Utility.jsonify( const tree = new Formatter().format(
new HorizontalPosition({ new HorizontalPosition({
relative: HorizontalPositionRelativeFrom.MARGIN, relative: HorizontalPositionRelativeFrom.MARGIN,
align: HorizontalPositionAlign.CENTER, align: HorizontalPositionAlign.CENTER,
}), }),
); );
assert.equal(newJson.rootKey, "wp:positionH"); expect(tree).to.deep.equal({
assert.include(newJson.root[0].root, { "wp:positionH": [
{
_attr: {
relativeFrom: "margin", relativeFrom: "margin",
},
},
{
"wp:align": ["center"],
},
],
}); });
assert.equal(newJson.root[1].rootKey, "wp:align");
assert.include(newJson.root[1].root, "center");
}); });
it("should create a element with offset", () => { it("should create a element with offset", () => {
const newJson = Utility.jsonify( const tree = new Formatter().format(
new HorizontalPosition({ new HorizontalPosition({
relative: HorizontalPositionRelativeFrom.MARGIN, relative: HorizontalPositionRelativeFrom.MARGIN,
offset: 40, offset: 40,
}), }),
); );
assert.equal(newJson.rootKey, "wp:positionH"); expect(tree).to.deep.equal({
assert.include(newJson.root[0].root, { "wp:positionH": [
{
_attr: {
relativeFrom: "margin", relativeFrom: "margin",
},
},
{
"wp:posOffset": ["40"],
},
],
}); });
assert.equal(newJson.root[1].rootKey, "wp:posOffset");
assert.include(newJson.root[1].root[0], 40);
}); });
}); });
}); });

View File

@ -1,15 +1,16 @@
import { assert } from "chai"; import { expect } from "chai";
import { Utility } from "tests/utility"; import { Formatter } from "export/formatter";
import { PositionOffset } from "./position-offset"; import { PositionOffset } from "./position-offset";
describe("PositionOffset", () => { describe("PositionOffset", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create a element with correct root key", () => { it("should create a element with correct root key", () => {
const newJson = Utility.jsonify(new PositionOffset(50)); const tree = new Formatter().format(new PositionOffset(50));
assert.equal(newJson.rootKey, "wp:posOffset"); expect(tree).to.deep.equal({
assert.equal(newJson.root[0], 50); "wp:posOffset": ["50"],
});
}); });
}); });
}); });

View File

@ -1,17 +1,20 @@
import { assert } from "chai"; import { expect } from "chai";
import { Utility } from "tests/utility"; import { Formatter } from "export/formatter";
import { SimplePos } from "./simple-pos"; import { SimplePos } from "./simple-pos";
describe("SimplePos", () => { describe("SimplePos", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create a element with correct root key", () => { it("should create a element with correct root key", () => {
const newJson = Utility.jsonify(new SimplePos()); const tree = new Formatter().format(new SimplePos());
assert.equal(newJson.rootKey, "wp:simplePos"); expect(tree).to.deep.equal({
assert.include(newJson.root[0].root, { "wp:simplePos": {
_attr: {
x: 0, x: 0,
y: 0, y: 0,
},
},
}); });
}); });
}); });

View File

@ -1,6 +1,6 @@
import { assert } from "chai"; import { expect } from "chai";
import { Utility } from "tests/utility"; import { Formatter } from "export/formatter";
import { VerticalPositionAlign, VerticalPositionRelativeFrom } from "./floating-position"; import { VerticalPositionAlign, VerticalPositionRelativeFrom } from "./floating-position";
import { VerticalPosition } from "./vertical-position"; import { VerticalPosition } from "./vertical-position";
@ -8,35 +8,45 @@ import { VerticalPosition } from "./vertical-position";
describe("VerticalPosition", () => { describe("VerticalPosition", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create a element with position align", () => { it("should create a element with position align", () => {
const newJson = Utility.jsonify( const tree = new Formatter().format(
new VerticalPosition({ new VerticalPosition({
relative: VerticalPositionRelativeFrom.MARGIN, relative: VerticalPositionRelativeFrom.MARGIN,
align: VerticalPositionAlign.INSIDE, align: VerticalPositionAlign.INSIDE,
}), }),
); );
assert.equal(newJson.rootKey, "wp:positionV"); expect(tree).to.deep.equal({
assert.include(newJson.root[0].root, { "wp:positionV": [
{
_attr: {
relativeFrom: "margin", relativeFrom: "margin",
},
},
{
"wp:align": ["inside"],
},
],
}); });
assert.equal(newJson.root[1].rootKey, "wp:align");
assert.include(newJson.root[1].root, "inside");
}); });
it("should create a element with offset", () => { it("should create a element with offset", () => {
const newJson = Utility.jsonify( const tree = new Formatter().format(
new VerticalPosition({ new VerticalPosition({
relative: VerticalPositionRelativeFrom.MARGIN, relative: VerticalPositionRelativeFrom.MARGIN,
offset: 40, offset: 40,
}), }),
); );
assert.equal(newJson.rootKey, "wp:positionV"); expect(tree).to.deep.equal({
assert.include(newJson.root[0].root, { "wp:positionV": [
{
_attr: {
relativeFrom: "margin", relativeFrom: "margin",
},
},
{
"wp:posOffset": ["40"],
},
],
}); });
assert.equal(newJson.root[1].rootKey, "wp:posOffset");
assert.include(newJson.root[1].root[0], 40);
}); });
}); });
}); });

View File

@ -9,3 +9,5 @@ export * from "./document";
export * from "./styles"; export * from "./styles";
export * from "./table-of-contents"; export * from "./table-of-contents";
export * from "./xml-components"; export * from "./xml-components";
export * from "./header-wrapper";
export * from "./footer-wrapper";

View File

@ -1,6 +1,6 @@
import { assert } from "chai"; import { assert, expect } from "chai";
import { Utility } from "tests/utility"; import { Formatter } from "export/formatter";
import { ThematicBreak } from "./border"; import { ThematicBreak } from "./border";
@ -28,14 +28,21 @@ describe("ThematicBreak", () => {
}); });
it("should create a Thematic Break with correct border properties", () => { it("should create a Thematic Break with correct border properties", () => {
const newJson = Utility.jsonify(thematicBreak); const tree = new Formatter().format(thematicBreak);
const attributes = { expect(tree).to.deep.equal({
color: "auto", "w:pBdr": [
space: "1", {
val: "single", "w:bottom": {
sz: "6", _attr: {
}; "w:color": "auto",
assert.equal(JSON.stringify(newJson.root[0].root[0].root), JSON.stringify(attributes)); "w:space": "1",
"w:sz": "6",
"w:val": "single",
},
},
},
],
});
}); });
}); });
}); });

View File

@ -1,6 +1,6 @@
import { assert } from "chai"; import { expect } from "chai";
import { Utility } from "tests/utility"; import { Formatter } from "export/formatter";
import { PageBreak, PageBreakBefore } from "./page-break"; import { PageBreak, PageBreakBefore } from "./page-break";
@ -13,21 +13,18 @@ describe("PageBreak", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create a Page Break with correct attributes", () => { it("should create a Page Break with correct attributes", () => {
const newJson = Utility.jsonify(pageBreak); const tree = new Formatter().format(pageBreak);
const attributes = { expect(tree).to.deep.equal({
type: "page", "w:r": [
}; {
assert.equal(JSON.stringify(newJson.root[1].root[0].root), JSON.stringify(attributes)); "w:br": {
_attr: {
"w:type": "page",
},
},
},
],
}); });
it("should create a Page Break with w:r", () => {
const newJson = Utility.jsonify(pageBreak);
assert.equal(newJson.rootKey, "w:r");
});
it("should create a Page Break with a Break inside", () => {
const newJson = Utility.jsonify(pageBreak);
assert.equal(newJson.root[1].rootKey, "w:br");
}); });
}); });
}); });
@ -35,7 +32,9 @@ describe("PageBreak", () => {
describe("PageBreakBefore", () => { describe("PageBreakBefore", () => {
it("should create page break before", () => { it("should create page break before", () => {
const pageBreakBefore = new PageBreakBefore(); const pageBreakBefore = new PageBreakBefore();
const newJson = Utility.jsonify(pageBreakBefore); const tree = new Formatter().format(pageBreakBefore);
assert.equal(newJson.rootKey, "w:pageBreakBefore"); expect(tree).to.deep.equal({
"w:pageBreakBefore": {},
});
}); });
}); });

View File

@ -1,6 +1,6 @@
import { assert } from "chai"; import { expect } from "chai";
import { Utility } from "tests/utility"; import { Formatter } from "export/formatter";
import { Style } from "./style"; import { Style } from "./style";
@ -10,14 +10,26 @@ describe("ParagraphStyle", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create a style with given value", () => { it("should create a style with given value", () => {
style = new Style("test"); style = new Style("test");
const newJson = Utility.jsonify(style); const tree = new Formatter().format(style);
assert.equal(newJson.root[0].root.val, "test"); expect(tree).to.deep.equal({
"w:pStyle": {
_attr: {
"w:val": "test",
},
},
});
}); });
it("should create a style with blank val", () => { it("should create a style with blank val", () => {
style = new Style(""); style = new Style("");
const newJson = Utility.jsonify(style); const tree = new Formatter().format(style);
assert.equal(newJson.root[0].root.val, ""); expect(tree).to.deep.equal({
"w:pStyle": {
_attr: {
"w:val": "",
},
},
});
}); });
}); });
}); });

View File

@ -1,6 +1,6 @@
import { assert } from "chai"; import { expect } from "chai";
import { Utility } from "tests/utility"; import { Formatter } from "export/formatter";
import { NumberProperties } from "./unordered-list"; import { NumberProperties } from "./unordered-list";
@ -13,20 +13,25 @@ describe("NumberProperties", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create a Number Properties with correct root key", () => { it("should create a Number Properties with correct root key", () => {
const newJson = Utility.jsonify(numberProperties); const tree = new Formatter().format(numberProperties);
assert.equal(newJson.rootKey, "w:numPr"); expect(tree).to.deep.equal({
"w:numPr": [
{
"w:ilvl": {
_attr: {
"w:val": 10,
},
},
},
{
"w:numId": {
_attr: {
"w:val": 5,
},
},
},
],
}); });
it("should create a Page Break with a Indent Level inside", () => {
const newJson = Utility.jsonify(numberProperties);
assert.equal(newJson.root[0].rootKey, "w:ilvl");
assert.equal(newJson.root[0].root[0].root.val, 10);
});
it("should create a Page Break with a Number Id inside", () => {
const newJson = Utility.jsonify(numberProperties);
assert.equal(newJson.root[1].rootKey, "w:numId");
assert.equal(newJson.root[1].root[0].root.val, 5);
}); });
}); });
}); });

View File

@ -1,7 +1,6 @@
import { assert, expect } from "chai"; import { expect } from "chai";
import { Formatter } from "export/formatter"; import { Formatter } from "export/formatter";
import { Utility } from "tests/utility";
import { Hyperlink } from "./"; import { Hyperlink } from "./";
@ -14,28 +13,23 @@ describe("Hyperlink", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create a hyperlink with correct root key", () => { it("should create a hyperlink with correct root key", () => {
const newJson = Utility.jsonify(hyperlink);
assert.equal(newJson.rootKey, "w:hyperlink");
});
it("should create a hyperlink with right attributes", () => {
const newJson = Utility.jsonify(hyperlink);
const attributes = {
history: 1,
id: "rId1",
};
assert.equal(JSON.stringify(newJson.root[0].root), JSON.stringify(attributes));
});
it("should create a hyperlink with a run component", () => {
const tree = new Formatter().format(hyperlink); const tree = new Formatter().format(hyperlink);
const runJson = { expect(tree).to.deep.equal({
"w:hyperlink": [
{
_attr: {
"w:history": 1,
"r:id": "rId1",
},
},
{
"w:r": [ "w:r": [
{ "w:rPr": [{ "w:rStyle": { _attr: { "w:val": "Hyperlink" } } }] }, { "w:rPr": [{ "w:rStyle": { _attr: { "w:val": "Hyperlink" } } }] },
{ "w:t": [{ _attr: { "xml:space": "preserve" } }, "https://example.com"] }, { "w:t": [{ _attr: { "xml:space": "preserve" } }, "https://example.com"] },
], ],
}; },
expect(tree["w:hyperlink"][1]).to.deep.equal(runJson); ],
});
}); });
describe("with optional anchor parameter", () => { describe("with optional anchor parameter", () => {
@ -44,12 +38,23 @@ describe("Hyperlink", () => {
}); });
it("should create an internal link with anchor tag", () => { it("should create an internal link with anchor tag", () => {
const newJson = Utility.jsonify(hyperlink); const tree = new Formatter().format(hyperlink);
const attributes = { expect(tree).to.deep.equal({
history: 1, "w:hyperlink": [
anchor: "anchor", {
}; _attr: {
assert.equal(JSON.stringify(newJson.root[0].root), JSON.stringify(attributes)); "w:history": 1,
"w:anchor": "anchor",
},
},
{
"w:r": [
{ "w:rPr": [{ "w:rStyle": { _attr: { "w:val": "Hyperlink" } } }] },
{ "w:t": [{ _attr: { "xml:space": "preserve" } }, "Anchor Text"] },
],
},
],
});
}); });
}); });
}); });

View File

@ -1,6 +1,6 @@
import { assert } from "chai"; import { expect } from "chai";
import { Utility } from "tests/utility"; import { Formatter } from "export/formatter";
import { OutlineLevel } from "./outline-level"; import { OutlineLevel } from "./outline-level";
@ -10,14 +10,14 @@ describe("ParagraphOutlineLevel", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create an outlineLevel with given value", () => { it("should create an outlineLevel with given value", () => {
outlineLevel = new OutlineLevel("0"); outlineLevel = new OutlineLevel("0");
const newJson = Utility.jsonify(outlineLevel); const tree = new Formatter().format(outlineLevel);
assert.equal(newJson.root[0].root.val, "0"); expect(tree).to.deep.equal({
"w:outlineLvl": {
_attr: {
"w:val": "0",
},
},
}); });
it("should create an outlineLevel with blank val", () => {
outlineLevel = new OutlineLevel("");
const newJson = Utility.jsonify(outlineLevel);
assert.equal(newJson.root[0].root.val, "");
}); });
}); });
}); });

View File

@ -1,6 +1,6 @@
import { assert } from "chai"; import { expect } from "chai";
import { Utility } from "tests/utility"; import { Formatter } from "export/formatter";
import { Break } from "./break"; import { Break } from "./break";
@ -13,8 +13,10 @@ describe("Break", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create a Break with correct root key", () => { it("should create a Break with correct root key", () => {
const newJson = Utility.jsonify(currentBreak); const tree = new Formatter().format(currentBreak);
assert.equal(newJson.rootKey, "w:br"); expect(tree).to.deep.equal({
"w:br": {},
});
}); });
}); });
}); });

View File

@ -1,7 +1,6 @@
import { assert, expect } from "chai"; import { expect } from "chai";
import { Formatter } from "export/formatter"; import { Formatter } from "export/formatter";
import { Utility } from "tests/utility";
import { Run } from "./"; import { Run } from "./";
@ -15,28 +14,50 @@ describe("Run", () => {
describe("#bold()", () => { describe("#bold()", () => {
it("it should add bold to the properties", () => { it("it should add bold to the properties", () => {
run.bold(); run.bold();
const newJson = Utility.jsonify(run); const tree = new Formatter().format(run);
assert.equal(newJson.root[0].root[0].rootKey, "w:b"); expect(tree).to.deep.equal({
assert.equal(newJson.root[0].root[1].rootKey, "w:bCs"); "w:r": [
{
"w:rPr": [
{ "w:b": { _attr: { "w:val": true } } },
{
"w:bCs": {
_attr: {
"w:val": true,
},
},
},
],
},
],
});
}); });
}); });
describe("#italics()", () => { describe("#italics()", () => {
it("it should add italics to the properties", () => { it("it should add italics to the properties", () => {
run.italics(); run.italics();
const newJson = Utility.jsonify(run); const tree = new Formatter().format(run);
assert.equal(newJson.root[0].root[0].rootKey, "w:i"); expect(tree).to.deep.equal({
assert.equal(newJson.root[0].root[1].rootKey, "w:iCs"); "w:r": [
{
"w:rPr": [
{ "w:i": { _attr: { "w:val": true } } },
{
"w:iCs": {
_attr: {
"w:val": true,
},
},
},
],
},
],
});
}); });
}); });
describe("#underline()", () => { describe("#underline()", () => {
it("it should add underline to the properties", () => {
run.underline();
const newJson = Utility.jsonify(run);
assert.equal(newJson.root[0].root[0].rootKey, "w:u");
});
it("should default to 'single' and no color", () => { it("should default to 'single' and no color", () => {
run.underline(); run.underline();
const tree = new Formatter().format(run); const tree = new Formatter().format(run);
@ -57,48 +78,60 @@ describe("Run", () => {
describe("#smallCaps()", () => { describe("#smallCaps()", () => {
it("it should add smallCaps to the properties", () => { it("it should add smallCaps to the properties", () => {
run.smallCaps(); run.smallCaps();
const newJson = Utility.jsonify(run); const tree = new Formatter().format(run);
assert.equal(newJson.root[0].root[0].rootKey, "w:smallCaps"); expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:smallCaps": {} }] }],
});
}); });
}); });
describe("#caps()", () => { describe("#caps()", () => {
it("it should add caps to the properties", () => { it("it should add caps to the properties", () => {
run.allCaps(); run.allCaps();
const newJson = Utility.jsonify(run); const tree = new Formatter().format(run);
assert.equal(newJson.root[0].root[0].rootKey, "w:caps"); expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:caps": {} }] }],
});
}); });
}); });
describe("#strike()", () => { describe("#strike()", () => {
it("it should add strike to the properties", () => { it("it should add strike to the properties", () => {
run.strike(); run.strike();
const newJson = Utility.jsonify(run); const tree = new Formatter().format(run);
assert.equal(newJson.root[0].root[0].rootKey, "w:strike"); expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:strike": { _attr: { "w:val": true } } }] }],
});
}); });
}); });
describe("#doubleStrike()", () => { describe("#doubleStrike()", () => {
it("it should add caps to the properties", () => { it("it should add caps to the properties", () => {
run.doubleStrike(); run.doubleStrike();
const newJson = Utility.jsonify(run); const tree = new Formatter().format(run);
assert.equal(newJson.root[0].root[0].rootKey, "w:dstrike"); expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:dstrike": { _attr: { "w:val": true } } }] }],
});
}); });
}); });
describe("#break()", () => { describe("#break()", () => {
it("it should add break to the run", () => { it("it should add break to the run", () => {
run.break(); run.break();
const newJson = Utility.jsonify(run); const tree = new Formatter().format(run);
assert.equal(newJson.root[1].rootKey, "w:br"); expect(tree).to.deep.equal({
"w:r": [{ "w:br": {} }],
});
}); });
}); });
describe("#tab()", () => { describe("#tab()", () => {
it("it should add break to the run", () => { it("it should add break to the run", () => {
run.tab(); run.tab();
const newJson = Utility.jsonify(run); const tree = new Formatter().format(run);
assert.equal(newJson.root[1].rootKey, "w:tab"); expect(tree).to.deep.equal({
"w:r": [{ "w:tab": {} }],
});
}); });
}); });

View File

@ -1,6 +1,6 @@
import { assert } from "chai"; import { expect } from "chai";
import { Utility } from "tests/utility"; import { Formatter } from "export/formatter";
import { DoubleStrike, Strike } from "./formatting"; import { DoubleStrike, Strike } from "./formatting";
@ -13,8 +13,14 @@ describe("Strike", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create a Strike with correct root key", () => { it("should create a Strike with correct root key", () => {
const newJson = Utility.jsonify(strike); const tree = new Formatter().format(strike);
assert.equal(newJson.rootKey, "w:strike"); expect(tree).to.deep.equal({
"w:strike": {
_attr: {
"w:val": true,
},
},
});
}); });
}); });
}); });
@ -28,8 +34,14 @@ describe("DoubleStrike", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create a Double Strike with correct root key", () => { it("should create a Double Strike with correct root key", () => {
const newJson = Utility.jsonify(strike); const tree = new Formatter().format(strike);
assert.equal(newJson.rootKey, "w:dstrike"); expect(tree).to.deep.equal({
"w:dstrike": {
_attr: {
"w:val": true,
},
},
});
}); });
}); });
}); });

View File

@ -1,6 +1,6 @@
import { assert } from "chai"; import { expect } from "chai";
import { Utility } from "tests/utility"; import { Formatter } from "export/formatter";
import { Tab } from "./tab"; import { Tab } from "./tab";
@ -13,8 +13,10 @@ describe("Tab", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create a Tab with correct root key", () => { it("should create a Tab with correct root key", () => {
const newJson = Utility.jsonify(tab); const tree = new Formatter().format(tab);
assert.equal(newJson.rootKey, "w:tab"); expect(tree).to.deep.equal({
"w:tab": {},
});
}); });
}); });
}); });

View File

@ -1,7 +1,6 @@
import { assert, expect } from "chai"; import { expect } from "chai";
import { Formatter } from "export/formatter"; import { Formatter } from "export/formatter";
import { Utility } from "tests/utility";
import * as u from "./underline"; import * as u from "./underline";
@ -9,8 +8,14 @@ describe("Underline", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create a new Underline object with u:u as the rootKey", () => { it("should create a new Underline object with u:u as the rootKey", () => {
const underline = new u.Underline(); const underline = new u.Underline();
const newJson = Utility.jsonify(underline); const tree = new Formatter().format(underline);
assert.equal(newJson.rootKey, "w:u"); expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "single",
},
},
});
}); });
it("should default to 'single' and no color", () => { it("should default to 'single' and no color", () => {
@ -33,16 +38,16 @@ describe("Underline", () => {
describe("DashDotDotHeavyUnderline", () => { describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should have u:u as the rootKey", () => {
const underline = new u.DashDotDotHeavyUnderline();
const newJson = Utility.jsonify(underline);
assert.equal(newJson.rootKey, "w:u");
});
it("should put value in attribute", () => { it("should put value in attribute", () => {
const underline = new u.DashDotDotHeavyUnderline(); const underline = new u.DashDotDotHeavyUnderline();
const newJson = Utility.jsonify(underline); const tree = new Formatter().format(underline);
assert.equal(newJson.root[0].root.val, "dashDotDotHeavy"); expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "dashDotDotHeavy",
},
},
});
}); });
}); });
}); });
@ -51,8 +56,14 @@ describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should put value in attribute", () => { it("should put value in attribute", () => {
const underline = new u.DashDotHeavyUnderline(); const underline = new u.DashDotHeavyUnderline();
const newJson = Utility.jsonify(underline); const tree = new Formatter().format(underline);
assert.equal(newJson.root[0].root.val, "dashDotHeavy"); expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "dashDotHeavy",
},
},
});
}); });
}); });
}); });
@ -61,8 +72,14 @@ describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should put value in attribute", () => { it("should put value in attribute", () => {
const underline = new u.DashLongHeavyUnderline(); const underline = new u.DashLongHeavyUnderline();
const newJson = Utility.jsonify(underline); const tree = new Formatter().format(underline);
assert.equal(newJson.root[0].root.val, "dashLongHeavy"); expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "dashLongHeavy",
},
},
});
}); });
}); });
}); });
@ -71,8 +88,14 @@ describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should put value in attribute", () => { it("should put value in attribute", () => {
const underline = new u.DashLongUnderline(); const underline = new u.DashLongUnderline();
const newJson = Utility.jsonify(underline); const tree = new Formatter().format(underline);
assert.equal(newJson.root[0].root.val, "dashLong"); expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "dashLong",
},
},
});
}); });
}); });
}); });
@ -81,8 +104,14 @@ describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should put value in attribute", () => { it("should put value in attribute", () => {
const underline = new u.DashUnderline(); const underline = new u.DashUnderline();
const newJson = Utility.jsonify(underline); const tree = new Formatter().format(underline);
assert.equal(newJson.root[0].root.val, "dash"); expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "dash",
},
},
});
}); });
}); });
}); });
@ -91,8 +120,14 @@ describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should put value in attribute", () => { it("should put value in attribute", () => {
const underline = new u.DotDashUnderline(); const underline = new u.DotDashUnderline();
const newJson = Utility.jsonify(underline); const tree = new Formatter().format(underline);
assert.equal(newJson.root[0].root.val, "dotDash"); expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "dotDash",
},
},
});
}); });
}); });
}); });
@ -101,8 +136,14 @@ describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should put value in attribute", () => { it("should put value in attribute", () => {
const underline = new u.DotDotDashUnderline(); const underline = new u.DotDotDashUnderline();
const newJson = Utility.jsonify(underline); const tree = new Formatter().format(underline);
assert.equal(newJson.root[0].root.val, "dotDotDash"); expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "dotDotDash",
},
},
});
}); });
}); });
}); });
@ -111,8 +152,14 @@ describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should put value in attribute", () => { it("should put value in attribute", () => {
const underline = new u.DottedHeavyUnderline(); const underline = new u.DottedHeavyUnderline();
const newJson = Utility.jsonify(underline); const tree = new Formatter().format(underline);
assert.equal(newJson.root[0].root.val, "dottedHeavy"); expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "dottedHeavy",
},
},
});
}); });
}); });
}); });
@ -121,8 +168,14 @@ describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should put value in attribute", () => { it("should put value in attribute", () => {
const underline = new u.DottedUnderline(); const underline = new u.DottedUnderline();
const newJson = Utility.jsonify(underline); const tree = new Formatter().format(underline);
assert.equal(newJson.root[0].root.val, "dotted"); expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "dotted",
},
},
});
}); });
}); });
}); });
@ -131,8 +184,14 @@ describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should put value in attribute", () => { it("should put value in attribute", () => {
const underline = new u.DoubleUnderline(); const underline = new u.DoubleUnderline();
const newJson = Utility.jsonify(underline); const tree = new Formatter().format(underline);
assert.equal(newJson.root[0].root.val, "double"); expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "double",
},
},
});
}); });
}); });
}); });
@ -141,8 +200,14 @@ describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should put value in attribute", () => { it("should put value in attribute", () => {
const underline = new u.SingleUnderline(); const underline = new u.SingleUnderline();
const newJson = Utility.jsonify(underline); const tree = new Formatter().format(underline);
assert.equal(newJson.root[0].root.val, "single"); expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "single",
},
},
});
}); });
}); });
}); });
@ -151,8 +216,14 @@ describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should put value in attribute", () => { it("should put value in attribute", () => {
const underline = new u.ThickUnderline(); const underline = new u.ThickUnderline();
const newJson = Utility.jsonify(underline); const tree = new Formatter().format(underline);
assert.equal(newJson.root[0].root.val, "thick"); expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "thick",
},
},
});
}); });
}); });
}); });
@ -161,8 +232,14 @@ describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should put value in attribute", () => { it("should put value in attribute", () => {
const underline = new u.WaveUnderline(); const underline = new u.WaveUnderline();
const newJson = Utility.jsonify(underline); const tree = new Formatter().format(underline);
assert.equal(newJson.root[0].root.val, "wave"); expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "wave",
},
},
});
}); });
}); });
}); });
@ -171,8 +248,14 @@ describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should put value in attribute", () => { it("should put value in attribute", () => {
const underline = new u.WavyDoubleUnderline(); const underline = new u.WavyDoubleUnderline();
const newJson = Utility.jsonify(underline); const tree = new Formatter().format(underline);
assert.equal(newJson.root[0].root.val, "wavyDouble"); expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "wavyDouble",
},
},
});
}); });
}); });
}); });
@ -181,8 +264,14 @@ describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should put value in attribute", () => { it("should put value in attribute", () => {
const underline = new u.WavyHeavyUnderline(); const underline = new u.WavyHeavyUnderline();
const newJson = Utility.jsonify(underline); const tree = new Formatter().format(underline);
assert.equal(newJson.root[0].root.val, "wavyHeavy"); expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "wavyHeavy",
},
},
});
}); });
}); });
}); });
@ -191,8 +280,14 @@ describe("DashDotDotHeavyUnderline", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should put value in attribute", () => { it("should put value in attribute", () => {
const underline = new u.WordsUnderline(); const underline = new u.WordsUnderline();
const newJson = Utility.jsonify(underline); const tree = new Formatter().format(underline);
assert.equal(newJson.root[0].root.val, "words"); expect(tree).to.deep.equal({
"w:u": {
_attr: {
"w:val": "words",
},
},
});
}); });
}); });
}); });

View File

@ -1 +1,3 @@
export * from "./styles"; export * from "./styles";
export * from "./style/character-style";
export * from "./style/paragraph-style";

View File

@ -2,3 +2,4 @@ export * from "./table";
export * from "./table-cell"; export * from "./table-cell";
export * from "./table-properties"; export * from "./table-properties";
export * from "./shading"; export * from "./shading";
export * from "./table-row";

View File

@ -1,2 +1,3 @@
export * from "./table-row"; export * from "./table-row";
export * from "./table-row-properties"; export * from "./table-row-properties";
export * from "./table-row-height";

View File

@ -0,0 +1,32 @@
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
export enum HeightRule {
/** Height is determined based on the content, so value is ignored. */
AUTO = "auto",
/** At least the value specified */
ATLEAST = "atLeast",
/** Exactly the value specified */
EXACT = "exact",
}
interface ITableRowHeight {
readonly height: number;
readonly rule: HeightRule;
}
export class TableRowHeightAttributes extends XmlAttributeComponent<ITableRowHeight> {
protected readonly xmlKeys = { height: "w:val", rule: "w:hRule" };
}
export class TableRowHeight extends XmlComponent {
constructor(value: number, rule: HeightRule) {
super("w:trHeight");
this.root.push(
new TableRowHeightAttributes({
height: value,
rule: rule,
}),
);
}
}

View File

@ -1,5 +1,6 @@
import { expect } from "chai"; import { expect } from "chai";
import { Formatter } from "export/formatter"; import { Formatter } from "export/formatter";
import { HeightRule } from "file/table/table-row/table-row-height";
import { TableRowProperties } from "./table-row-properties"; import { TableRowProperties } from "./table-row-properties";
describe("TableRowProperties", () => { describe("TableRowProperties", () => {
@ -31,4 +32,25 @@ describe("TableRowProperties", () => {
expect(tree).to.deep.equal({ "w:trPr": [{ "w:tblHeader": { _attr: { "w:val": true } } }] }); expect(tree).to.deep.equal({ "w:trPr": [{ "w:tblHeader": { _attr: { "w:val": true } } }] });
}); });
}); });
describe("#setHeight", () => {
it("sets row height exact", () => {
const rowProperties = new TableRowProperties();
rowProperties.setHeight(100, HeightRule.EXACT);
const tree = new Formatter().format(rowProperties);
expect(tree).to.deep.equal({ "w:trPr": [{ "w:trHeight": { _attr: { "w:val": 100, "w:hRule": "exact" } } }] });
});
it("sets row height auto", () => {
const rowProperties = new TableRowProperties();
rowProperties.setHeight(100, HeightRule.AUTO);
const tree = new Formatter().format(rowProperties);
expect(tree).to.deep.equal({ "w:trPr": [{ "w:trHeight": { _attr: { "w:val": 100, "w:hRule": "auto" } } }] });
});
it("sets row height at least", () => {
const rowProperties = new TableRowProperties();
rowProperties.setHeight(100, HeightRule.ATLEAST);
const tree = new Formatter().format(rowProperties);
expect(tree).to.deep.equal({ "w:trPr": [{ "w:trHeight": { _attr: { "w:val": 100, "w:hRule": "atLeast" } } }] });
});
});
}); });

View File

@ -1,3 +1,4 @@
import { HeightRule, TableRowHeight } from "file/table/table-row/table-row-height";
import { IgnoreIfEmptyXmlComponent, XmlAttributeComponent, XmlComponent } from "file/xml-components"; import { IgnoreIfEmptyXmlComponent, XmlAttributeComponent, XmlComponent } from "file/xml-components";
export class TableRowProperties extends IgnoreIfEmptyXmlComponent { export class TableRowProperties extends IgnoreIfEmptyXmlComponent {
@ -16,6 +17,12 @@ export class TableRowProperties extends IgnoreIfEmptyXmlComponent {
return this; return this;
} }
public setHeight(height: number, rule: HeightRule): TableRowProperties {
this.root.push(new TableRowHeight(height, rule));
return this;
}
} }
class CantSplitAttributes extends XmlAttributeComponent<{ readonly val: boolean }> { class CantSplitAttributes extends XmlAttributeComponent<{ readonly val: boolean }> {

View File

@ -2,11 +2,11 @@ import { expect } from "chai";
import { Formatter } from "export/formatter"; import { Formatter } from "export/formatter";
import { HeightRule } from "file/table/table-row/table-row-height";
import { EMPTY_OBJECT } from "file/xml-components";
import { TableCell } from "../table-cell"; import { TableCell } from "../table-cell";
import { TableRow } from "./table-row"; import { TableRow } from "./table-row";
import { EMPTY_OBJECT } from "file/xml-components";
describe("TableRow", () => { describe("TableRow", () => {
describe("#constructor", () => { describe("#constructor", () => {
it("should create with no cells", () => { it("should create with no cells", () => {
@ -67,4 +67,28 @@ describe("TableRow", () => {
expect(() => tableRow.getCell(1)).to.throw(); expect(() => tableRow.getCell(1)).to.throw();
}); });
}); });
describe("#setHeight", () => {
it("should set row height", () => {
const tableRow = new TableRow([]);
tableRow.setHeight(100, HeightRule.EXACT);
const tree = new Formatter().format(tableRow);
expect(tree).to.deep.equal({
"w:tr": [
{
"w:trPr": [
{
"w:trHeight": {
_attr: {
"w:hRule": "exact",
"w:val": 100,
},
},
},
],
},
],
});
});
});
}); });

View File

@ -1,5 +1,5 @@
import { HeightRule } from "file/table/table-row/table-row-height";
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { TableCell } from "../table-cell"; import { TableCell } from "../table-cell";
import { TableRowProperties } from "./table-row-properties"; import { TableRowProperties } from "./table-row-properties";
@ -49,4 +49,10 @@ export class TableRow extends XmlComponent {
return this; return this;
} }
public setHeight(height: number, rule: HeightRule): TableRow {
this.properties.setHeight(height, rule);
return this;
}
} }

View File

@ -1,6 +1,6 @@
import { expect } from "chai"; import { expect } from "chai";
import { Utility } from "tests/utility"; import { Formatter } from "export/formatter";
import { EMPTY_OBJECT, XmlComponent } from "./"; import { EMPTY_OBJECT, XmlComponent } from "./";
class TestComponent extends XmlComponent {} class TestComponent extends XmlComponent {}
@ -14,8 +14,10 @@ describe("XmlComponent", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create an Xml Component which has the correct rootKey", () => { it("should create an Xml Component which has the correct rootKey", () => {
const newJson = Utility.jsonify(xmlComponent); const tree = new Formatter().format(xmlComponent);
expect(newJson.rootKey).to.equal("w:test"); expect(tree).to.deep.equal({
"w:test": {},
});
}); });
}); });