diff --git a/src/file/border/border.spec.ts b/src/file/border/border.spec.ts new file mode 100644 index 0000000000..e652c50422 --- /dev/null +++ b/src/file/border/border.spec.ts @@ -0,0 +1,54 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; +import { BorderStyle } from "file/border"; + +import { BorderElement } from "./border"; + +describe("BorderElement", () => { + describe("#constructor", () => { + it("should create a simple border element", () => { + const border = new BorderElement("w:top", { + style: BorderStyle.SINGLE, + }); + const tree = new Formatter().format(border); + expect(tree).to.deep.equal({ + "w:top": { + _attr: { + "w:val": "single", + }, + }, + }); + }); + it("should create a simple border element with a size", () => { + const border = new BorderElement("w:top", { + style: BorderStyle.SINGLE, + size: 22, + }); + const tree = new Formatter().format(border); + expect(tree).to.deep.equal({ + "w:top": { + _attr: { + "w:val": "single", + "w:sz": 22, + }, + }, + }); + }); + it("should create a simple border element with space", () => { + const border = new BorderElement("w:top", { + style: BorderStyle.SINGLE, + space: 22, + }); + const tree = new Formatter().format(border); + expect(tree).to.deep.equal({ + "w:top": { + _attr: { + "w:val": "single", + "w:space": 22, + }, + }, + }); + }); + }); +}); diff --git a/src/file/core-properties/properties.ts b/src/file/core-properties/properties.ts index e7ce1f3779..57a869a167 100644 --- a/src/file/core-properties/properties.ts +++ b/src/file/core-properties/properties.ts @@ -7,6 +7,7 @@ import { ISectionOptions } from "../file"; import { INumberingOptions } from "../numbering"; import { Paragraph } from "../paragraph"; import { IStylesOptions } from "../styles"; +import { dateTimeValue } from "../values"; export interface IPropertiesOptions { readonly sections: ISectionOptions[]; @@ -103,6 +104,6 @@ class TimestampElement extends XmlComponent { type: "dcterms:W3CDTF", }), ); - this.root.push(new Date().toISOString()); + this.root.push(dateTimeValue(new Date())); } } diff --git a/src/file/document/body/body.ts b/src/file/document/body/body.ts index ff56962d12..deb2e573cf 100644 --- a/src/file/document/body/body.ts +++ b/src/file/document/body/body.ts @@ -1,6 +1,6 @@ import { IContext, IXmlableObject, XmlComponent } from "file/xml-components"; -import { Paragraph, ParagraphProperties, TableOfContents } from "../.."; +import { Paragraph, ParagraphProperties } from "../.."; import { ISectionPropertiesOptions, SectionProperties } from "./section-properties/section-properties"; export class Body extends XmlComponent { @@ -38,10 +38,6 @@ export class Body extends XmlComponent { this.root.push(component); } - public getTablesOfContents(): TableOfContents[] { - return this.root.filter((child) => child instanceof TableOfContents) as TableOfContents[]; - } - private createSectionParagraph(section: SectionProperties): Paragraph { const paragraph = new Paragraph({}); const properties = new ParagraphProperties({}); diff --git a/src/file/document/document.ts b/src/file/document/document.ts index 6fc78b8b1a..4059eb5183 100644 --- a/src/file/document/document.ts +++ b/src/file/document/document.ts @@ -69,8 +69,4 @@ export class Document extends XmlComponent { public get Body(): Body { return this.body; } - - public getTablesOfContents(): TableOfContents[] { - return this.body.getTablesOfContents(); - } } diff --git a/src/file/styles/style/paragraph-style.spec.ts b/src/file/styles/style/paragraph-style.spec.ts index da24b63d5c..c125e803ad 100644 --- a/src/file/styles/style/paragraph-style.spec.ts +++ b/src/file/styles/style/paragraph-style.spec.ts @@ -78,7 +78,7 @@ describe("ParagraphStyle", () => { const style = new StyleForParagraph({ id: "myStyleId", paragraph: { - indent: { left: 720 }, + indent: { left: 720, right: 500 }, }, }); const tree = new Formatter().format(style); @@ -86,7 +86,7 @@ describe("ParagraphStyle", () => { "w:style": [ { _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } }, { - "w:pPr": [{ "w:ind": { _attr: { "w:left": 720 } } }], + "w:pPr": [{ "w:ind": { _attr: { "w:left": 720, "w:right": 500 } } }], }, ], }); diff --git a/src/file/values.spec.ts b/src/file/values.spec.ts index 9ed751cb51..3ef06a6abc 100644 --- a/src/file/values.spec.ts +++ b/src/file/values.spec.ts @@ -1,8 +1,14 @@ import { expect } from "chai"; import { + dateTimeValue, hexColorValue, hpsMeasureValue, + longHexNumber, + measurementOrPercentValue, + percentageValue, positiveUniversalMeasureValue, + shortHexNumber, + signedHpsMeasureValue, signedTwipsMeasureValue, twipsMeasureValue, universalMeasureValue, @@ -48,6 +54,29 @@ describe("values", () => { }); }); + describe("longHexNumber", () => { + it("should allow valid values", () => { + expect(longHexNumber("112233FF")).to.eq("112233FF"); + }); + it("should throw on invalid values", () => { + expect(() => longHexNumber("112233GG")).to.throw(); + expect(() => longHexNumber("112233F")).to.throw(); + expect(() => longHexNumber("112233FFF")).to.throw(); + }); + }); + + describe("shortHexNumber", () => { + it("should allow valid values", () => { + expect(shortHexNumber("1122")).to.eq("1122"); + expect(shortHexNumber("FFFF")).to.eq("FFFF"); + }); + it("should throw on invalid values", () => { + expect(() => shortHexNumber("11")).to.throw(); + expect(() => shortHexNumber("112233")).to.throw(); + expect(() => shortHexNumber("FFFG")).to.throw(); + }); + }); + describe("hexColorValue", () => { it("should allow valid values", () => { expect(hexColorValue("auto")).to.eq("auto"); @@ -114,4 +143,51 @@ describe("values", () => { expect(() => hpsMeasureValue("-5mm")).to.throw(); }); }); + + describe("signedHpsMeasureValue", () => { + it("should allow valid values", () => { + expect(signedHpsMeasureValue(1243)).to.eq(1243); + expect(signedHpsMeasureValue(-1243)).to.eq(-1243); + expect(signedHpsMeasureValue("5mm")).to.eq("5mm"); + expect(signedHpsMeasureValue("-5mm")).to.eq("-5mm"); + }); + it("should throw on invalid values", () => { + expect(() => hpsMeasureValue(NaN)).to.throw(); + expect(() => hpsMeasureValue("5FF")).to.throw(); + }); + }); + + describe("percentageValue", () => { + it("should allow valid values", () => { + expect(percentageValue("0%")).to.eq("0%"); + expect(percentageValue("-20%")).to.eq("-20%"); + expect(percentageValue("100%")).to.eq("100%"); + expect(percentageValue("1000%")).to.eq("1000%"); + }); + it("should throw on invalid values", () => { + expect(() => percentageValue("0%%")).to.throw(); + expect(() => percentageValue("20")).to.throw(); + expect(() => percentageValue("FF%")).to.throw(); + }); + }); + + describe("measurementOrPercentValue", () => { + it("should allow valid values", () => { + expect(measurementOrPercentValue(1243)).to.eq(1243); + expect(measurementOrPercentValue(-1243)).to.eq(-1243); + expect(measurementOrPercentValue("10%")).to.eq("10%"); + expect(measurementOrPercentValue("5mm")).to.eq("5mm"); + }); + it("should throw on invalid values", () => { + expect(() => measurementOrPercentValue(NaN)).to.throw(); + expect(() => measurementOrPercentValue("10%%")).to.throw(); + expect(() => measurementOrPercentValue("10F")).to.throw(); + }); + }); + + describe("dateTimeValue", () => { + it("should allow valid values", () => { + expect(dateTimeValue(new Date())).to.match(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:.\d+)?Z/); + }); + }); }); diff --git a/src/file/xml-components/xml-component.spec.ts b/src/file/xml-components/xml-component.spec.ts index c1b4f5d053..497f14eab2 100644 --- a/src/file/xml-components/xml-component.spec.ts +++ b/src/file/xml-components/xml-component.spec.ts @@ -1,23 +1,62 @@ import { expect } from "chai"; import { Formatter } from "export/formatter"; -import { XmlComponent } from "./"; +import { Attributes, BaseXmlComponent, XmlComponent } from "./"; -class TestComponent extends XmlComponent {} +class TestComponent extends XmlComponent { + public push(el: BaseXmlComponent): void { + this.root.push(el); + } +} describe("XmlComponent", () => { - let xmlComponent: TestComponent; - - beforeEach(() => { - xmlComponent = new TestComponent("w:test"); - }); - describe("#constructor()", () => { it("should create an Xml Component which has the correct rootKey", () => { + const xmlComponent = new TestComponent("w:test"); const tree = new Formatter().format(xmlComponent); expect(tree).to.deep.equal({ "w:test": {}, }); }); + it("should handle children elements", () => { + const xmlComponent = new TestComponent("w:test"); + xmlComponent.push( + new Attributes({ + val: "test", + }), + ); + xmlComponent.push(new TestComponent("innerTest")); + + const tree = new Formatter().format(xmlComponent); + expect(tree).to.deep.equal({ + "w:test": [ + { + _attr: { + "w:val": "test", + }, + }, + { + innerTest: {}, + }, + ], + }); + }); + it("should hoist attrs if only attrs are present", () => { + const xmlComponent = new TestComponent("w:test"); + xmlComponent.push( + new Attributes({ + val: "test", + }), + ); + + const tree = new Formatter().format(xmlComponent); + expect(tree).to.deep.equal({ + "w:test": { + _attr: { + "w:val": "test", + }, + }, + }); + }); }); });