From 8f6984580a09b21365e1a3e20ff5be6565583c87 Mon Sep 17 00:00:00 2001 From: Dolan Date: Tue, 15 Jan 2019 02:09:38 +0000 Subject: [PATCH 1/2] Add Number of pages element --- src/file/paragraph/run/page-number.ts | 8 ++++++++ src/file/paragraph/run/run.ts | 10 +++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/file/paragraph/run/page-number.ts b/src/file/paragraph/run/page-number.ts index 4ae0eaafd7..2fdc44e32b 100644 --- a/src/file/paragraph/run/page-number.ts +++ b/src/file/paragraph/run/page-number.ts @@ -12,3 +12,11 @@ export class Page extends XmlComponent { this.root.push("PAGE"); } } + +export class NumberOfPages extends XmlComponent { + constructor() { + super("w:instrText"); + this.root.push(new TextAttributes({ space: SpaceType.PRESERVE })); + this.root.push("NUMPAGES"); + } +} diff --git a/src/file/paragraph/run/run.ts b/src/file/paragraph/run/run.ts index 74e011b402..dbf12a0dae 100644 --- a/src/file/paragraph/run/run.ts +++ b/src/file/paragraph/run/run.ts @@ -14,7 +14,7 @@ import { SizeComplexScript, Strike, } from "./formatting"; -import { Page } from "./page-number"; +import { NumberOfPages, Page } from "./page-number"; import { RunProperties } from "./properties"; import { RunFonts } from "./run-fonts"; import { SubScript, SuperScript } from "./script"; @@ -84,6 +84,14 @@ export class Run extends XmlComponent { return this; } + public numberOfTotalPages(): Run { + this.root.push(new Begin()); + this.root.push(new NumberOfPages()); + this.root.push(new Separate()); + this.root.push(new End()); + return this; + } + public smallCaps(): Run { this.properties.push(new SmallCaps()); return this; From ca5f6a56a50d32ae9a2af3564cc327cc57711495 Mon Sep 17 00:00:00 2001 From: Dolan Date: Tue, 15 Jan 2019 21:40:19 +0000 Subject: [PATCH 2/2] Write tests --- src/file/paragraph/run/page-number.spec.ts | 23 ++++++++++++++++ src/file/paragraph/run/run.spec.ts | 32 ++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/file/paragraph/run/page-number.spec.ts diff --git a/src/file/paragraph/run/page-number.spec.ts b/src/file/paragraph/run/page-number.spec.ts new file mode 100644 index 0000000000..4a9bc39a3e --- /dev/null +++ b/src/file/paragraph/run/page-number.spec.ts @@ -0,0 +1,23 @@ +import { expect } from "chai"; + +import { Formatter } from "export/formatter"; + +import { NumberOfPages, Page } from "./page-number"; + +describe("Page", () => { + describe("#constructor()", () => { + it("uses the font name for both ascii and hAnsi", () => { + const tree = new Formatter().format(new Page()); + expect(tree).to.deep.equal({ "w:instrText": [{ _attr: { "xml:space": "preserve" } }, "PAGE"] }); + }); + }); +}); + +describe("NumberOfPages", () => { + describe("#constructor()", () => { + it("uses the font name for both ascii and hAnsi", () => { + const tree = new Formatter().format(new NumberOfPages()); + expect(tree).to.deep.equal({ "w:instrText": [{ _attr: { "xml:space": "preserve" } }, "NUMPAGES"] }); + }); + }); +}); diff --git a/src/file/paragraph/run/run.spec.ts b/src/file/paragraph/run/run.spec.ts index c4ff3e8b70..ae6696a860 100644 --- a/src/file/paragraph/run/run.spec.ts +++ b/src/file/paragraph/run/run.spec.ts @@ -156,6 +156,38 @@ describe("Run", () => { }); }); + describe("#numberOfTotalPages", () => { + it("should set the run to the RTL mode", () => { + run.numberOfTotalPages(); + const tree = new Formatter().format(run); + expect(tree).to.deep.equal({ + "w:r": [ + { "w:rPr": [] }, + { "w:fldChar": [{ _attr: { "w:fldCharType": "begin" } }] }, + { "w:instrText": [{ _attr: { "xml:space": "preserve" } }, "NUMPAGES"] }, + { "w:fldChar": [{ _attr: { "w:fldCharType": "separate" } }] }, + { "w:fldChar": [{ _attr: { "w:fldCharType": "end" } }] }, + ], + }); + }); + }); + + describe("#pageNumber", () => { + it("should set the run to the RTL mode", () => { + run.pageNumber(); + const tree = new Formatter().format(run); + expect(tree).to.deep.equal({ + "w:r": [ + { "w:rPr": [] }, + { "w:fldChar": [{ _attr: { "w:fldCharType": "begin" } }] }, + { "w:instrText": [{ _attr: { "xml:space": "preserve" } }, "PAGE"] }, + { "w:fldChar": [{ _attr: { "w:fldCharType": "separate" } }] }, + { "w:fldChar": [{ _attr: { "w:fldCharType": "end" } }] }, + ], + }); + }); + }); + describe("#style", () => { it("should set the style to the given styleId", () => { run.style("myRunStyle");