From 5552f9d834c9d4c15209e533c3fa920089440de1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mendon=C3=A7a?= Date: Fri, 9 Nov 2018 09:13:27 -0200 Subject: [PATCH] The page number type attribute of the sections was always been created, leading Word to always reset page numbers to zero in new sections. The page number type DECIMAL is already the default in Word, there is no need to force this to be the default option in the default section, like it was. --- src/file/document/body/body.spec.ts | 4 +--- .../section-properties.spec.ts | 21 ++++++++++++++++++- .../section-properties/section-properties.ts | 8 ++++--- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/file/document/body/body.spec.ts b/src/file/document/body/body.spec.ts index 137ef298ba..b82944ff78 100644 --- a/src/file/document/body/body.spec.ts +++ b/src/file/document/body/body.spec.ts @@ -17,7 +17,7 @@ describe("Body", () => { expect(formatted) .to.have.property("w:sectPr") .and.to.be.an.instanceof(Array); - expect(formatted["w:sectPr"]).to.have.length(5); + expect(formatted["w:sectPr"]).to.have.length(4); }); }); @@ -76,7 +76,6 @@ describe("Body", () => { }, { "w:cols": [{ _attr: { "w:space": 708 } }] }, { "w:docGrid": [{ _attr: { "w:linePitch": 360 } }] }, - { "w:pgNumType": [{ _attr: { "w:fmt": "decimal" } }] }, ], }, ], @@ -104,7 +103,6 @@ describe("Body", () => { }, { "w:cols": [{ _attr: { "w:space": 708 } }] }, { "w:docGrid": [{ _attr: { "w:linePitch": 360 } }] }, - { "w:pgNumType": [{ _attr: { "w:fmt": "decimal" } }] }, ], }, ], 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 d767744e34..81135daba6 100644 --- a/src/file/document/body/section-properties/section-properties.spec.ts +++ b/src/file/document/body/section-properties/section-properties.spec.ts @@ -88,7 +88,6 @@ describe("SectionProperties", () => { }); expect(tree["w:sectPr"][2]).to.deep.equal({ "w:cols": [{ _attr: { "w:space": 708 } }] }); expect(tree["w:sectPr"][3]).to.deep.equal({ "w:docGrid": [{ _attr: { "w:linePitch": 360 } }] }); - expect(tree["w:sectPr"][4]).to.deep.equal({ "w:pgNumType": [{ _attr: { "w:fmt": "decimal" } }] }); }); it("should create section properties with changed options", () => { @@ -183,5 +182,25 @@ describe("SectionProperties", () => { "w:pgBorders": [{ _attr: { "w:offsetFrom": "page" } }], }); }); + + it("should create section properties with page number type, but without start attribute", () => { + const properties = new SectionProperties({ + pageNumberFormatType: PageNumberFormat.UPPER_ROMAN, + }); + const tree = new Formatter().format(properties); + expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]); + const pgNumType = tree["w:sectPr"].find((item) => item["w:pgNumType"] !== undefined); + expect(pgNumType).to.deep.equal({ + "w:pgNumType": [{ _attr: { "w:fmt": "upperRoman" } }], + }); + }); + + it("should create section properties without page number type", () => { + const properties = new SectionProperties({}); + const tree = new Formatter().format(properties); + expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]); + const pgNumType = tree["w:sectPr"].find((item) => item["w:pgNumType"] !== undefined); + expect(pgNumType).to.equal(undefined); + }); }); }); diff --git a/src/file/document/body/section-properties/section-properties.ts b/src/file/document/body/section-properties/section-properties.ts index 4056570de2..9ae9e2bfb8 100644 --- a/src/file/document/body/section-properties/section-properties.ts +++ b/src/file/document/body/section-properties/section-properties.ts @@ -14,7 +14,7 @@ import { HeaderReference } from "./header-reference/header-reference"; import { IPageBordersOptions, PageBorders } from "./page-border"; import { PageMargin } from "./page-margin/page-margin"; import { IPageMarginAttributes } from "./page-margin/page-margin-attributes"; -import { IPageNumberTypeAttributes, PageNumberFormat, PageNumberType } from "./page-number"; +import { IPageNumberTypeAttributes, PageNumberType } from "./page-number"; import { PageSize } from "./page-size/page-size"; import { IPageSizeAttributes, PageOrientation } from "./page-size/page-size-attributes"; import { TitlePage } from "./title-page/title-page"; @@ -69,7 +69,7 @@ export class SectionProperties extends XmlComponent { orientation = PageOrientation.PORTRAIT, headers, footers, - pageNumberFormatType = PageNumberFormat.DECIMAL, + pageNumberFormatType, pageNumberStart, pageBorders, pageBorderTop, @@ -88,7 +88,9 @@ export class SectionProperties extends XmlComponent { this.addHeaders(headers); this.addFooters(footers); - this.root.push(new PageNumberType(pageNumberStart, pageNumberFormatType)); + if (pageNumberStart || pageNumberFormatType) { + this.root.push(new PageNumberType(pageNumberStart, pageNumberFormatType)); + } if (pageBorders || pageBorderTop || pageBorderRight || pageBorderBottom || pageBorderLeft) { this.root.push(