2023-05-31 22:56:58 +01:00
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
2023-06-05 00:33:43 +01:00
|
|
|
import { describe, expect, it } from "vitest";
|
2018-01-25 01:21:03 +00:00
|
|
|
|
2022-06-26 23:26:42 +01:00
|
|
|
import { Formatter } from "@export/formatter";
|
|
|
|
import { FooterWrapper } from "@file/footer-wrapper";
|
|
|
|
import { HeaderWrapper } from "@file/header-wrapper";
|
|
|
|
import { Media } from "@file/media";
|
|
|
|
import { NumberFormat } from "@file/shared/number-format";
|
2025-05-02 13:58:10 -05:00
|
|
|
import { VerticalAlignSection } from "@file/vertical-align";
|
2022-06-26 23:26:42 +01:00
|
|
|
import { convertInchesToTwip } from "@util/convenience-functions";
|
2018-10-26 01:04:07 +01:00
|
|
|
|
2021-05-25 03:41:12 +03:00
|
|
|
import { PageOrientation } from "./properties";
|
2021-11-30 21:11:06 +09:00
|
|
|
import { DocumentGridType } from "./properties/doc-grid";
|
2021-05-25 03:41:12 +03:00
|
|
|
import { LineNumberRestartFormat } from "./properties/line-number";
|
|
|
|
import { PageBorderOffsetFrom } from "./properties/page-borders";
|
2021-11-26 15:07:15 +00:00
|
|
|
import { PageTextDirectionType } from "./properties/page-text-direction";
|
2021-05-25 03:41:12 +03:00
|
|
|
import { SectionType } from "./properties/section-type";
|
2024-10-21 03:57:15 +01:00
|
|
|
import { SectionProperties, sectionMarginDefaults, sectionPageSizeDefaults } from "./section-properties";
|
2021-05-25 03:41:12 +03:00
|
|
|
|
|
|
|
const DEFAULT_MARGINS = {
|
|
|
|
"w:bottom": sectionMarginDefaults.BOTTOM,
|
|
|
|
"w:footer": sectionMarginDefaults.FOOTER,
|
|
|
|
"w:top": sectionMarginDefaults.TOP,
|
|
|
|
"w:right": sectionMarginDefaults.RIGHT,
|
|
|
|
"w:left": sectionMarginDefaults.LEFT,
|
|
|
|
"w:header": sectionMarginDefaults.HEADER,
|
|
|
|
"w:gutter": sectionMarginDefaults.GUTTER,
|
|
|
|
};
|
|
|
|
|
|
|
|
const PAGE_SIZE_DEFAULTS = {
|
|
|
|
"w:h": sectionPageSizeDefaults.HEIGHT,
|
|
|
|
"w:orient": sectionPageSizeDefaults.ORIENTATION,
|
|
|
|
"w:w": sectionPageSizeDefaults.WIDTH,
|
|
|
|
};
|
2018-01-25 01:21:03 +00:00
|
|
|
|
|
|
|
describe("SectionProperties", () => {
|
|
|
|
describe("#constructor()", () => {
|
|
|
|
it("should create section properties with options", () => {
|
2018-10-23 00:31:51 +01:00
|
|
|
const media = new Media();
|
|
|
|
|
2018-01-25 01:21:03 +00:00
|
|
|
const properties = new SectionProperties({
|
2021-03-19 20:53:56 +00:00
|
|
|
page: {
|
|
|
|
size: {
|
2021-05-25 03:41:12 +03:00
|
|
|
width: 1190,
|
|
|
|
height: 1680,
|
|
|
|
orientation: PageOrientation.PORTRAIT,
|
2021-03-19 20:53:56 +00:00
|
|
|
},
|
|
|
|
margin: {
|
2021-05-25 03:41:12 +03:00
|
|
|
top: "2in",
|
|
|
|
right: "2in",
|
|
|
|
bottom: "2in",
|
|
|
|
left: "2in",
|
|
|
|
header: 808,
|
|
|
|
footer: 808,
|
|
|
|
gutter: 10,
|
2021-03-19 20:53:56 +00:00
|
|
|
},
|
|
|
|
pageNumbers: {
|
|
|
|
start: 10,
|
2021-05-25 03:41:12 +03:00
|
|
|
formatType: NumberFormat.CARDINAL_TEXT,
|
2021-03-19 20:53:56 +00:00
|
|
|
},
|
|
|
|
},
|
2019-06-28 01:57:43 +01:00
|
|
|
column: {
|
2021-05-25 03:41:12 +03:00
|
|
|
space: 208,
|
|
|
|
count: 2,
|
2021-03-04 03:02:07 +00:00
|
|
|
separate: true,
|
2019-06-28 01:57:43 +01:00
|
|
|
},
|
2021-03-19 20:53:56 +00:00
|
|
|
grid: {
|
|
|
|
linePitch: convertInchesToTwip(0.25),
|
2021-11-30 21:11:06 +09:00
|
|
|
type: DocumentGridType.LINES,
|
2021-03-19 20:53:56 +00:00
|
|
|
},
|
|
|
|
headerWrapperGroup: {
|
2018-10-23 00:31:51 +01:00
|
|
|
default: new HeaderWrapper(media, 100),
|
2018-10-05 01:33:17 +01:00
|
|
|
},
|
2021-03-19 20:53:56 +00:00
|
|
|
footerWrapperGroup: {
|
2018-10-23 00:31:51 +01:00
|
|
|
even: new FooterWrapper(media, 200),
|
2018-10-05 01:33:17 +01:00
|
|
|
},
|
2019-09-29 04:17:21 +01:00
|
|
|
titlePage: true,
|
2025-05-02 13:58:10 -05:00
|
|
|
verticalAlign: VerticalAlignSection.TOP,
|
2018-01-25 01:21:03 +00:00
|
|
|
});
|
2021-03-19 20:53:56 +00:00
|
|
|
|
2018-01-25 01:21:03 +00:00
|
|
|
const tree = new Formatter().format(properties);
|
2021-03-19 20:53:56 +00:00
|
|
|
|
2018-01-25 01:21:03 +00:00
|
|
|
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
|
|
|
|
expect(tree["w:sectPr"]).to.be.an.instanceof(Array);
|
2021-05-20 02:18:17 +03:00
|
|
|
expect(tree["w:sectPr"][0]).to.deep.equal({ "w:headerReference": { _attr: { "r:id": "rId100", "w:type": "default" } } });
|
|
|
|
expect(tree["w:sectPr"][1]).to.deep.equal({ "w:footerReference": { _attr: { "r:id": "rId200", "w:type": "even" } } });
|
2021-05-25 03:41:12 +03:00
|
|
|
expect(tree["w:sectPr"][2]).to.deep.equal({ "w:pgSz": { _attr: { "w:h": 1680, "w:w": 1190, "w:orient": "portrait" } } });
|
2021-05-20 02:18:17 +03:00
|
|
|
expect(tree["w:sectPr"][3]).to.deep.equal({
|
2019-04-09 05:27:18 -04:00
|
|
|
"w:pgMar": {
|
|
|
|
_attr: {
|
2021-05-25 03:41:12 +03:00
|
|
|
"w:bottom": "2in",
|
|
|
|
"w:footer": 808,
|
|
|
|
"w:top": "2in",
|
|
|
|
"w:right": "2in",
|
|
|
|
"w:left": "2in",
|
|
|
|
"w:header": 808,
|
|
|
|
"w:gutter": 10,
|
2018-01-25 01:21:03 +00:00
|
|
|
},
|
2019-04-09 05:27:18 -04:00
|
|
|
},
|
2018-01-25 01:21:03 +00:00
|
|
|
});
|
2018-06-21 12:03:34 +02:00
|
|
|
|
2021-05-20 02:18:17 +03:00
|
|
|
expect(tree["w:sectPr"][4]).to.deep.equal({ "w:pgNumType": { _attr: { "w:fmt": "cardinalText", "w:start": 10 } } });
|
2021-05-25 03:41:12 +03:00
|
|
|
expect(tree["w:sectPr"][5]).to.deep.equal({ "w:cols": { _attr: { "w:space": 208, "w:sep": true, "w:num": 2 } } });
|
2021-05-20 02:18:17 +03:00
|
|
|
expect(tree["w:sectPr"][6]).to.deep.equal({ "w:vAlign": { _attr: { "w:val": "top" } } });
|
2021-05-25 03:41:12 +03:00
|
|
|
expect(tree["w:sectPr"][7]).to.deep.equal({ "w:titlePg": {} });
|
2021-11-30 21:11:06 +09:00
|
|
|
expect(tree["w:sectPr"][8]).to.deep.equal({ "w:docGrid": { _attr: { "w:linePitch": 360, "w:type": "lines" } } });
|
2018-01-25 01:21:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should create section properties with no options", () => {
|
|
|
|
const properties = new SectionProperties();
|
|
|
|
const tree = new Formatter().format(properties);
|
|
|
|
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
|
|
|
|
expect(tree["w:sectPr"]).to.be.an.instanceof(Array);
|
2021-05-25 03:41:12 +03:00
|
|
|
expect(tree["w:sectPr"][0]).to.deep.equal({ "w:pgSz": { _attr: PAGE_SIZE_DEFAULTS } });
|
2018-01-25 01:21:03 +00:00
|
|
|
expect(tree["w:sectPr"][1]).to.deep.equal({
|
2021-05-25 03:41:12 +03:00
|
|
|
"w:pgMar": { _attr: DEFAULT_MARGINS },
|
2018-01-25 01:21:03 +00:00
|
|
|
});
|
2021-05-25 03:41:12 +03:00
|
|
|
// expect(tree["w:sectPr"][3]).to.deep.equal({ "w:cols": { _attr: { "w:space": 708, "w:sep": false, "w:num": 1 } } });
|
|
|
|
expect(tree["w:sectPr"][3]).to.deep.equal({ "w:docGrid": { _attr: { "w:linePitch": 360 } } });
|
2018-01-25 01:21:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should create section properties with changed options", () => {
|
|
|
|
const properties = new SectionProperties({
|
2021-03-19 20:53:56 +00:00
|
|
|
page: {
|
|
|
|
margin: {
|
|
|
|
top: 0,
|
|
|
|
},
|
|
|
|
},
|
2018-01-25 01:21:03 +00:00
|
|
|
});
|
|
|
|
const tree = new Formatter().format(properties);
|
|
|
|
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
|
|
|
|
expect(tree["w:sectPr"]).to.be.an.instanceof(Array);
|
2021-05-25 03:41:12 +03:00
|
|
|
expect(tree["w:sectPr"][0]).to.deep.equal({ "w:pgSz": { _attr: PAGE_SIZE_DEFAULTS } });
|
2018-01-25 01:21:03 +00:00
|
|
|
expect(tree["w:sectPr"][1]).to.deep.equal({
|
2019-04-09 05:27:18 -04:00
|
|
|
"w:pgMar": {
|
|
|
|
_attr: {
|
2021-05-25 03:41:12 +03:00
|
|
|
...DEFAULT_MARGINS,
|
2019-04-09 05:27:18 -04:00
|
|
|
"w:top": 0,
|
2018-01-25 01:21:03 +00:00
|
|
|
},
|
2019-04-09 05:27:18 -04:00
|
|
|
},
|
2018-01-25 01:21:03 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should create section properties with changed options", () => {
|
|
|
|
const properties = new SectionProperties({
|
2021-03-19 20:53:56 +00:00
|
|
|
page: {
|
|
|
|
margin: {
|
|
|
|
bottom: 0,
|
|
|
|
},
|
|
|
|
},
|
2018-01-25 01:21:03 +00:00
|
|
|
});
|
|
|
|
const tree = new Formatter().format(properties);
|
|
|
|
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
|
|
|
|
expect(tree["w:sectPr"]).to.be.an.instanceof(Array);
|
2021-05-25 03:41:12 +03:00
|
|
|
expect(tree["w:sectPr"][0]).to.deep.equal({ "w:pgSz": { _attr: PAGE_SIZE_DEFAULTS } });
|
2018-01-25 01:21:03 +00:00
|
|
|
expect(tree["w:sectPr"][1]).to.deep.equal({
|
2019-04-09 05:27:18 -04:00
|
|
|
"w:pgMar": {
|
|
|
|
_attr: {
|
2021-05-25 03:41:12 +03:00
|
|
|
...DEFAULT_MARGINS,
|
2019-04-09 05:27:18 -04:00
|
|
|
"w:bottom": 0,
|
2018-01-25 01:21:03 +00:00
|
|
|
},
|
2019-04-09 05:27:18 -04:00
|
|
|
},
|
2018-01-25 01:21:03 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should create section properties with changed options", () => {
|
|
|
|
const properties = new SectionProperties({
|
2021-03-19 20:53:56 +00:00
|
|
|
page: {
|
|
|
|
size: {
|
|
|
|
width: 0,
|
|
|
|
height: 0,
|
2021-05-25 03:41:12 +03:00
|
|
|
orientation: PageOrientation.LANDSCAPE,
|
2021-03-19 20:53:56 +00:00
|
|
|
},
|
|
|
|
},
|
2018-01-25 01:21:03 +00:00
|
|
|
});
|
|
|
|
const tree = new Formatter().format(properties);
|
|
|
|
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
|
|
|
|
expect(tree["w:sectPr"]).to.be.an.instanceof(Array);
|
2021-05-25 03:41:12 +03:00
|
|
|
expect(tree["w:sectPr"][0]).to.deep.equal({
|
|
|
|
"w:pgSz": {
|
2019-04-09 05:27:18 -04:00
|
|
|
_attr: {
|
2021-05-25 03:41:12 +03:00
|
|
|
"w:h": 0,
|
|
|
|
"w:orient": PageOrientation.LANDSCAPE,
|
|
|
|
"w:w": 0,
|
2018-01-25 01:21:03 +00:00
|
|
|
},
|
2019-04-09 05:27:18 -04:00
|
|
|
},
|
2018-01-25 01:21:03 +00:00
|
|
|
});
|
2021-05-25 03:41:12 +03:00
|
|
|
expect(tree["w:sectPr"][1]).to.deep.equal({
|
|
|
|
"w:pgMar": {
|
|
|
|
_attr: DEFAULT_MARGINS,
|
|
|
|
},
|
|
|
|
});
|
2018-01-25 01:21:03 +00:00
|
|
|
});
|
2018-07-03 13:48:31 +02:00
|
|
|
|
|
|
|
it("should create section properties with page borders", () => {
|
|
|
|
const properties = new SectionProperties({
|
2021-03-19 20:53:56 +00:00
|
|
|
page: {
|
|
|
|
borders: {
|
|
|
|
pageBorders: {
|
|
|
|
offsetFrom: PageBorderOffsetFrom.PAGE,
|
|
|
|
},
|
|
|
|
},
|
2018-07-03 13:48:31 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
const tree = new Formatter().format(properties);
|
|
|
|
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
|
2023-05-31 22:56:58 +01:00
|
|
|
const pgBorders = tree["w:sectPr"].find((item: any) => item["w:pgBorders"] !== undefined);
|
2018-09-17 11:24:56 +03:00
|
|
|
expect(pgBorders).to.deep.equal({
|
2019-04-09 05:27:18 -04:00
|
|
|
"w:pgBorders": { _attr: { "w:offsetFrom": "page" } },
|
2018-07-03 13:48:31 +02:00
|
|
|
});
|
|
|
|
});
|
2018-11-09 09:13:27 -02:00
|
|
|
|
|
|
|
it("should create section properties with page number type, but without start attribute", () => {
|
|
|
|
const properties = new SectionProperties({
|
2021-03-19 20:53:56 +00:00
|
|
|
page: {
|
|
|
|
pageNumbers: {
|
2021-05-25 03:41:12 +03:00
|
|
|
formatType: NumberFormat.UPPER_ROMAN,
|
2021-03-19 20:53:56 +00:00
|
|
|
},
|
|
|
|
},
|
2018-11-09 09:13:27 -02:00
|
|
|
});
|
|
|
|
const tree = new Formatter().format(properties);
|
|
|
|
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
|
2023-05-31 22:56:58 +01:00
|
|
|
const pgNumType = tree["w:sectPr"].find((item: any) => item["w:pgNumType"] !== undefined);
|
2018-11-09 09:13:27 -02:00
|
|
|
expect(pgNumType).to.deep.equal({
|
2019-04-09 05:27:18 -04:00
|
|
|
"w:pgNumType": { _attr: { "w:fmt": "upperRoman" } },
|
2018-11-09 09:13:27 -02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-09 22:37:37 +00:00
|
|
|
it("should create section properties with a page number type by default", () => {
|
2018-11-09 09:13:27 -02:00
|
|
|
const properties = new SectionProperties({});
|
|
|
|
const tree = new Formatter().format(properties);
|
|
|
|
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
|
2023-05-31 22:56:58 +01:00
|
|
|
const pgNumType = tree["w:sectPr"].find((item: any) => item["w:pgNumType"] !== undefined);
|
2021-03-09 22:37:37 +00:00
|
|
|
expect(pgNumType).to.deep.equal({ "w:pgNumType": { _attr: {} } });
|
2018-11-09 09:13:27 -02:00
|
|
|
});
|
2021-02-22 21:04:02 +00:00
|
|
|
|
|
|
|
it("should create section properties with section type", () => {
|
|
|
|
const properties = new SectionProperties({
|
|
|
|
type: SectionType.CONTINUOUS,
|
|
|
|
});
|
|
|
|
const tree = new Formatter().format(properties);
|
|
|
|
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
|
2023-05-31 22:56:58 +01:00
|
|
|
const type = tree["w:sectPr"].find((item: any) => item["w:type"] !== undefined);
|
2021-02-22 21:04:02 +00:00
|
|
|
expect(type).to.deep.equal({
|
|
|
|
"w:type": { _attr: { "w:val": "continuous" } },
|
|
|
|
});
|
2018-11-09 09:13:27 -02:00
|
|
|
});
|
2021-03-09 22:59:50 +00:00
|
|
|
|
|
|
|
it("should create section properties line number type", () => {
|
|
|
|
const properties = new SectionProperties({
|
2021-03-19 20:53:56 +00:00
|
|
|
lineNumbers: {
|
|
|
|
countBy: 2,
|
|
|
|
start: 2,
|
|
|
|
restart: LineNumberRestartFormat.CONTINUOUS,
|
|
|
|
distance: 4,
|
|
|
|
},
|
2021-03-09 22:59:50 +00:00
|
|
|
});
|
|
|
|
const tree = new Formatter().format(properties);
|
|
|
|
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
|
2023-05-31 22:56:58 +01:00
|
|
|
const type = tree["w:sectPr"].find((item: any) => item["w:lnNumType"] !== undefined);
|
2021-03-09 22:59:50 +00:00
|
|
|
expect(type).to.deep.equal({
|
|
|
|
"w:lnNumType": { _attr: { "w:countBy": 2, "w:distance": 4, "w:restart": "continuous", "w:start": 2 } },
|
|
|
|
});
|
|
|
|
});
|
2021-11-19 22:30:38 +09:00
|
|
|
|
|
|
|
it("should create section properties with text flow direction", () => {
|
|
|
|
const properties = new SectionProperties({
|
|
|
|
page: {
|
2021-11-26 15:07:15 +00:00
|
|
|
textDirection: PageTextDirectionType.TOP_TO_BOTTOM_RIGHT_TO_LEFT,
|
2021-11-19 22:30:38 +09:00
|
|
|
},
|
|
|
|
});
|
|
|
|
const tree = new Formatter().format(properties);
|
|
|
|
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
|
2023-05-31 22:56:58 +01:00
|
|
|
const type = tree["w:sectPr"].find((item: any) => item["w:textDirection"] !== undefined);
|
2021-11-19 22:30:38 +09:00
|
|
|
expect(type).to.deep.equal({
|
|
|
|
"w:textDirection": { _attr: { "w:val": "tbRl" } },
|
|
|
|
});
|
|
|
|
});
|
2018-01-25 01:21:03 +00:00
|
|
|
});
|
|
|
|
});
|