Files
docx-js/src/file/paragraph/paragraph.spec.ts

991 lines
34 KiB
TypeScript
Raw Normal View History

2017-03-09 12:25:59 +01:00
import { assert, expect } from "chai";
2021-03-12 03:58:05 +00:00
import { SinonStub, stub } from "sinon";
import * as convenienceFunctions from "@util/convenience-functions";
2022-10-29 03:10:29 +01:00
import { HorizontalPositionAlign, VerticalPositionAlign } from "@file/shared";
import { Formatter } from "@export/formatter";
import { BorderStyle } from "@file/border";
import { EMPTY_OBJECT } from "@file/xml-components";
2018-10-26 01:04:07 +01:00
import { IViewWrapper } from "../document-wrapper";
2021-03-11 01:06:55 +00:00
import { File } from "../file";
import { ShadingType } from "../shading";
2019-10-09 20:56:31 +01:00
import { AlignmentType, HeadingLevel, LeaderType, PageBreak, TabStopPosition, TabStopType } from "./formatting";
2021-03-14 17:00:42 +00:00
import { FrameAnchorType } from "./frame";
import { Bookmark, ExternalHyperlink } from "./links";
2019-06-12 01:03:36 +01:00
import { Paragraph } from "./paragraph";
import { TextRun } from "./run";
2016-04-03 05:24:56 +01:00
describe("Paragraph", () => {
2021-03-12 03:58:05 +00:00
before(() => {
2022-08-31 08:59:27 +01:00
stub(convenienceFunctions, "uniqueId").callsFake(() => "test-unique-id");
stub(convenienceFunctions, "uniqueNumericId").callsFake(() => -101);
2021-03-12 03:58:05 +00:00
});
after(() => {
(convenienceFunctions.uniqueId as SinonStub).restore();
2022-10-26 23:09:36 +01:00
(convenienceFunctions.uniqueNumericId as SinonStub).restore();
2021-03-12 03:58:05 +00:00
});
2016-05-26 15:08:34 +01:00
describe("#constructor()", () => {
2016-03-29 04:10:33 +01:00
it("should create valid JSON", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph("");
2017-03-09 12:25:59 +01:00
const stringifiedJson = JSON.stringify(paragraph);
2016-03-29 04:10:33 +01:00
try {
2018-01-31 23:24:55 +00:00
JSON.parse(stringifiedJson);
2016-03-29 04:10:33 +01:00
} catch (e) {
assert.isTrue(false);
}
assert.isTrue(true);
});
it("should create have valid properties", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph("");
2017-03-09 12:25:59 +01:00
const stringifiedJson = JSON.stringify(paragraph);
const newJson = JSON.parse(stringifiedJson);
2016-05-09 21:50:46 +01:00
assert.equal(newJson.root[0].rootKey, "w:pPr");
});
2016-03-29 04:10:33 +01:00
});
2017-03-10 14:35:37 +01:00
2016-03-29 04:10:33 +01:00
describe("#heading1()", () => {
it("should add heading style to JSON", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
heading: HeadingLevel.HEADING_1,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [{ "w:pStyle": { _attr: { "w:val": "Heading1" } } }],
},
],
});
2016-03-29 04:10:33 +01:00
});
});
describe("#heading2()", () => {
it("should add heading style to JSON", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
heading: HeadingLevel.HEADING_2,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [{ "w:pStyle": { _attr: { "w:val": "Heading2" } } }],
},
],
});
2016-03-29 04:10:33 +01:00
});
});
describe("#heading3()", () => {
it("should add heading style to JSON", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
heading: HeadingLevel.HEADING_3,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [{ "w:pStyle": { _attr: { "w:val": "Heading3" } } }],
},
],
});
2016-03-29 04:10:33 +01:00
});
});
2019-01-11 00:16:25 +00:00
describe("#heading4()", () => {
it("should add heading style to JSON", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
heading: HeadingLevel.HEADING_4,
});
2019-01-11 00:16:25 +00:00
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [{ "w:pStyle": { _attr: { "w:val": "Heading4" } } }],
2019-01-11 00:16:25 +00:00
},
],
});
});
});
describe("#heading5()", () => {
it("should add heading style to JSON", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
heading: HeadingLevel.HEADING_5,
});
2019-01-11 00:16:25 +00:00
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [{ "w:pStyle": { _attr: { "w:val": "Heading5" } } }],
2019-01-11 00:16:25 +00:00
},
],
});
});
});
describe("#heading6()", () => {
it("should add heading style to JSON", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
heading: HeadingLevel.HEADING_6,
});
2019-01-11 00:16:25 +00:00
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [{ "w:pStyle": { _attr: { "w:val": "Heading6" } } }],
2019-01-11 00:16:25 +00:00
},
],
});
});
});
2016-03-29 04:10:33 +01:00
describe("#title()", () => {
it("should add title style to JSON", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
heading: HeadingLevel.TITLE,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [{ "w:pStyle": { _attr: { "w:val": "Title" } } }],
},
],
});
2016-03-29 04:10:33 +01:00
});
});
2016-03-29 04:50:23 +01:00
describe("#center()", () => {
it("should add center alignment to JSON", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
alignment: AlignmentType.CENTER,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [{ "w:jc": { _attr: { "w:val": "center" } } }],
},
],
});
2016-03-29 04:50:23 +01:00
});
});
2016-03-29 05:01:33 +01:00
2019-01-11 00:16:25 +00:00
describe("#left()", () => {
it("should add left alignment to JSON", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
alignment: AlignmentType.LEFT,
});
2019-01-11 00:16:25 +00:00
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [{ "w:jc": { _attr: { "w:val": "left" } } }],
2019-01-11 00:16:25 +00:00
},
],
});
});
});
describe("#right()", () => {
it("should add right alignment to JSON", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
alignment: AlignmentType.RIGHT,
});
2019-01-11 00:16:25 +00:00
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [{ "w:jc": { _attr: { "w:val": "right" } } }],
2019-01-11 00:16:25 +00:00
},
],
});
});
});
describe("#start()", () => {
it("should add start alignment to JSON", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
alignment: AlignmentType.START,
});
2019-01-11 00:16:25 +00:00
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [{ "w:jc": { _attr: { "w:val": "start" } } }],
2019-01-11 00:16:25 +00:00
},
],
});
});
});
describe("#end()", () => {
it("should add end alignment to JSON", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
alignment: AlignmentType.END,
});
2019-01-11 00:16:25 +00:00
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [{ "w:jc": { _attr: { "w:val": "end" } } }],
2019-01-11 00:16:25 +00:00
},
],
});
});
});
describe("#distribute()", () => {
it("should add distribute alignment to JSON", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
alignment: AlignmentType.DISTRIBUTE,
});
2019-01-11 00:16:25 +00:00
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [{ "w:jc": { _attr: { "w:val": "distribute" } } }],
2019-01-11 00:16:25 +00:00
},
],
});
});
});
describe("#justified()", () => {
it("should add justified alignment to JSON", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
alignment: AlignmentType.JUSTIFIED,
});
2019-01-11 00:16:25 +00:00
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [{ "w:jc": { _attr: { "w:val": "both" } } }],
2019-01-11 00:16:25 +00:00
},
],
});
});
});
describe("#maxRightTabStop()", () => {
it("should add right tab stop to JSON", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
2019-10-09 20:56:31 +01:00
tabStops: [
{
type: TabStopType.RIGHT,
position: TabStopPosition.MAX,
},
2019-10-09 20:56:31 +01:00
],
2019-06-12 01:03:36 +01:00
});
2019-01-11 00:16:25 +00:00
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [
{
"w:tabs": [
{
"w:tab": {
_attr: {
"w:pos": 9026,
"w:val": "right",
2019-01-11 00:16:25 +00:00
},
},
2019-01-11 00:16:25 +00:00
},
],
},
],
},
],
});
});
});
describe("#leftTabStop()", () => {
it("should add leftTabStop to JSON", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
2019-10-09 20:56:31 +01:00
tabStops: [
{
type: TabStopType.LEFT,
2019-06-12 01:03:36 +01:00
position: 100,
leader: LeaderType.HYPHEN,
},
2019-10-09 20:56:31 +01:00
],
2019-06-12 01:03:36 +01:00
});
2019-01-11 00:16:25 +00:00
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [
{
"w:tabs": [
{
"w:tab": {
_attr: {
"w:pos": 100,
"w:val": "left",
"w:leader": "hyphen",
2019-01-11 00:16:25 +00:00
},
},
2019-01-11 00:16:25 +00:00
},
],
},
],
},
],
});
});
});
describe("#rightTabStop()", () => {
it("should add rightTabStop to JSON", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
2019-10-09 20:56:31 +01:00
tabStops: [
{
type: TabStopType.RIGHT,
2019-06-12 01:03:36 +01:00
position: 100,
leader: LeaderType.DOT,
},
2019-10-09 20:56:31 +01:00
],
2019-06-12 01:03:36 +01:00
});
2019-01-11 00:16:25 +00:00
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [
{
"w:tabs": [
{
"w:tab": {
_attr: {
"w:pos": 100,
"w:val": "right",
"w:leader": "dot",
2019-01-11 00:16:25 +00:00
},
},
2019-01-11 00:16:25 +00:00
},
],
},
],
},
],
});
});
});
describe("#centerTabStop()", () => {
it("should add centerTabStop to JSON", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
2019-10-09 20:56:31 +01:00
tabStops: [
{
type: TabStopType.CENTER,
2019-06-12 01:03:36 +01:00
position: 100,
leader: LeaderType.MIDDLE_DOT,
},
2019-10-09 20:56:31 +01:00
],
2019-06-12 01:03:36 +01:00
});
2019-01-11 00:16:25 +00:00
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [
{
"w:tabs": [
{
"w:tab": {
_attr: {
"w:pos": 100,
"w:val": "center",
"w:leader": "middleDot",
2019-01-11 00:16:25 +00:00
},
},
2019-01-11 00:16:25 +00:00
},
],
},
],
},
],
});
});
});
describe("#contextualSpacing()", () => {
it("should add contextualSpacing", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
contextualSpacing: true,
2019-01-11 00:16:25 +00:00
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [{ "w:contextualSpacing": {} }],
},
],
});
});
it("should remove contextualSpacing", () => {
const paragraph = new Paragraph({
contextualSpacing: false,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [{ "w:contextualSpacing": { _attr: { "w:val": false } } }],
2019-01-11 00:16:25 +00:00
},
],
});
});
});
2016-03-29 23:36:57 +01:00
describe("#thematicBreak()", () => {
2016-03-29 05:01:33 +01:00
it("should add thematic break to JSON", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
thematicBreak: true,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
2018-01-23 01:33:12 +00:00
"w:p": [
{
"w:pPr": [
{
"w:pBdr": [
{
"w:bottom": {
_attr: {
"w:val": "single",
"w:color": "auto",
2019-06-12 01:03:36 +01:00
"w:space": 1,
"w:sz": 6,
2018-01-23 01:33:12 +00:00
},
},
2018-01-23 01:33:12 +00:00
},
],
},
],
},
],
});
2016-03-29 05:01:33 +01:00
});
});
2016-03-30 00:28:05 +01:00
describe("#paragraphBorders()", () => {
it("should add a left and right border to a paragraph", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
border: {
left: {
color: "auto",
space: 1,
style: BorderStyle.SINGLE,
2019-06-12 01:03:36 +01:00
size: 6,
},
right: {
color: "auto",
space: 1,
style: BorderStyle.SINGLE,
2019-06-12 01:03:36 +01:00
size: 6,
},
},
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [
{
"w:pBdr": [
{
"w:left": {
_attr: {
"w:color": "auto",
2019-06-12 01:03:36 +01:00
"w:space": 1,
"w:sz": 6,
"w:val": "single",
},
},
},
{
"w:right": {
_attr: {
"w:color": "auto",
2019-06-12 01:03:36 +01:00
"w:space": 1,
"w:sz": 6,
"w:val": "single",
},
},
},
],
},
],
},
],
});
});
});
2016-03-30 00:28:05 +01:00
describe("#pageBreak()", () => {
it("should add page break to JSON", () => {
const paragraph = new Paragraph({
children: [new PageBreak()],
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
2018-01-23 01:33:12 +00:00
"w:p": [
{
"w:r": [{ "w:br": { _attr: { "w:type": "page" } } }],
2018-01-23 01:33:12 +00:00
},
],
});
2016-03-30 00:28:05 +01:00
});
});
2016-03-30 02:55:11 +01:00
describe("#pageBreakBefore()", () => {
it("should add page break before to JSON", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
pageBreakBefore: true,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
2018-05-17 15:34:47 +02:00
"w:pPr": [
{
"w:pageBreakBefore": EMPTY_OBJECT,
2018-05-17 15:34:47 +02:00
},
],
},
],
});
});
});
2016-03-30 02:55:11 +01:00
describe("#bullet()", () => {
2018-04-10 21:54:52 +01:00
it("should default to 0 indent level if no bullet was specified", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
bullet: {
level: 0,
},
});
2018-04-10 21:54:52 +01:00
const tree = new Formatter().format(paragraph);
2020-08-01 17:40:57 +01:00
expect(tree).to.have.property("w:p").which.is.an("array").which.has.length.at.least(1);
expect(tree["w:p"][0]).to.have.property("w:pPr").which.is.an("array").which.has.length.at.least(1);
2018-04-10 21:54:52 +01:00
expect(tree["w:p"][0]["w:pPr"][0]).to.deep.equal({
"w:pStyle": { _attr: { "w:val": "ListParagraph" } },
2018-04-10 21:54:52 +01:00
});
});
2016-03-30 02:55:11 +01:00
it("should add list paragraph style to JSON", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
bullet: {
level: 0,
},
});
const tree = new Formatter().format(paragraph);
2020-08-01 17:40:57 +01:00
expect(tree).to.have.property("w:p").which.is.an("array").which.has.length.at.least(1);
expect(tree["w:p"][0]).to.have.property("w:pPr").which.is.an("array").which.has.length.at.least(1);
expect(tree["w:p"][0]["w:pPr"][0]).to.deep.equal({
"w:pStyle": { _attr: { "w:val": "ListParagraph" } },
});
2016-03-30 02:55:11 +01:00
});
it("it should add numbered properties", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
bullet: {
level: 1,
},
});
const tree = new Formatter().format(paragraph);
2020-08-01 17:40:57 +01:00
expect(tree).to.have.property("w:p").which.is.an("array").which.has.length.at.least(1);
expect(tree["w:p"][0]).to.have.property("w:pPr").which.is.an("array").which.has.length.at.least(2);
expect(tree["w:p"][0]["w:pPr"][1]).to.deep.equal({
"w:numPr": [{ "w:ilvl": { _attr: { "w:val": 1 } } }, { "w:numId": { _attr: { "w:val": 1 } } }],
});
2016-03-30 02:55:11 +01:00
});
});
describe("#setNumbering", () => {
it("should add list paragraph style to JSON", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
numbering: {
2019-11-06 20:54:39 +00:00
reference: "test id",
2019-06-12 01:03:36 +01:00
level: 0,
},
});
const tree = new Formatter().format(paragraph);
2020-08-01 17:40:57 +01:00
expect(tree).to.have.property("w:p").which.is.an("array").which.has.length.at.least(1);
expect(tree["w:p"][0]).to.have.property("w:pPr").which.is.an("array").which.has.length.at.least(1);
expect(tree["w:p"][0]["w:pPr"][0]).to.deep.equal({
"w:pStyle": { _attr: { "w:val": "ListParagraph" } },
});
});
it("should add a style to the list paragraph when provided", () => {
const paragraph = new Paragraph({
numbering: {
reference: "test id",
level: 0,
},
style: "myFancyStyle",
});
const tree = new Formatter().format(paragraph);
expect(tree).to.have.property("w:p").which.is.an("array").which.has.length.at.least(1);
expect(tree["w:p"][0]).to.have.property("w:pPr").which.is.an("array").which.has.length.at.least(1);
expect(tree["w:p"][0]["w:pPr"][0]).to.deep.equal({
"w:pStyle": { _attr: { "w:val": "myFancyStyle" } },
});
});
it("should not add ListParagraph style to a list when using custom numbering", () => {
const paragraph = new Paragraph({
numbering: {
reference: "test id",
level: 0,
custom: true,
},
});
const tree = new Formatter().format(paragraph);
expect(tree).to.have.property("w:p").which.is.an("array").which.has.length.at.least(1);
expect(tree["w:p"][0]).to.have.property("w:pPr").which.is.an("array").which.has.length.at.least(1);
expect(tree["w:p"][0]["w:pPr"][0]).to.not.have.property("w:pStyle");
});
it("it should add numbered properties", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
numbering: {
2019-11-06 20:54:39 +00:00
reference: "test id",
2019-06-12 01:03:36 +01:00
level: 0,
2021-03-12 03:58:05 +00:00
instance: 4,
2019-06-12 01:03:36 +01:00
},
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [
{ "w:pStyle": { _attr: { "w:val": "ListParagraph" } } },
{
2021-03-12 03:58:05 +00:00
"w:numPr": [{ "w:ilvl": { _attr: { "w:val": 0 } } }, { "w:numId": { _attr: { "w:val": "{test id-4}" } } }],
},
],
},
2017-03-09 12:25:59 +01:00
],
});
});
2021-03-08 04:18:26 +00:00
it("should not add ListParagraph style when custom is true", () => {
const paragraph = new Paragraph({
numbering: {
reference: "test id",
level: 0,
custom: true,
},
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [
{
2021-03-12 03:58:05 +00:00
"w:numPr": [{ "w:ilvl": { _attr: { "w:val": 0 } } }, { "w:numId": { _attr: { "w:val": "{test id-0}" } } }],
2021-03-08 04:18:26 +00:00
},
],
},
],
});
});
});
2019-12-24 03:39:55 +00:00
it("it should add bookmark", () => {
const paragraph = new Paragraph({
children: [
new Bookmark({
id: "test-id",
children: [new TextRun("test")],
}),
],
2019-12-24 03:39:55 +00:00
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:bookmarkStart": {
_attr: {
"w:id": -101,
2019-12-24 03:39:55 +00:00
"w:name": "test-id",
},
},
},
{
"w:r": [
{
"w:t": [
{
_attr: {
"xml:space": "preserve",
},
},
"test",
],
},
],
},
{
"w:bookmarkEnd": {
_attr: {
"w:id": -101,
2019-12-24 03:39:55 +00:00
},
},
},
],
});
});
describe("#style", () => {
it("should set the paragraph style to the given styleId", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
style: "myFancyStyle",
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [{ "w:pStyle": { _attr: { "w:val": "myFancyStyle" } } }],
},
2017-03-09 12:25:59 +01:00
],
});
});
});
describe("#indent", () => {
it("should set the paragraph indent to the given values", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
indent: { left: 720 },
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [{ "w:ind": { _attr: { "w:left": 720 } } }],
},
2017-03-09 12:25:59 +01:00
],
});
});
});
describe("#spacing", () => {
it("should set the paragraph spacing to the given values", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
spacing: { before: 90, line: 50 },
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [{ "w:spacing": { _attr: { "w:before": 90, "w:line": 50 } } }],
},
2017-03-09 12:25:59 +01:00
],
});
});
});
describe("#keepLines", () => {
it("should set the paragraph keepLines sub-component", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
keepLines: true,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [{ "w:pPr": [{ "w:keepLines": EMPTY_OBJECT }] }],
});
});
});
describe("#keepNext", () => {
it("should set the paragraph keepNext sub-component", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
keepNext: true,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [{ "w:pPr": [{ "w:keepNext": EMPTY_OBJECT }] }],
});
});
});
2018-07-22 17:08:11 +03:00
2018-08-07 02:47:24 +01:00
describe("#bidirectional", () => {
2018-07-22 17:08:11 +03:00
it("set paragraph right to left layout", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
bidirectional: true,
});
2018-07-22 17:08:11 +03:00
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [{ "w:pPr": [{ "w:bidi": EMPTY_OBJECT }] }],
2018-07-22 17:08:11 +03:00
});
});
});
2019-01-15 15:39:48 +01:00
2021-09-28 12:21:51 -07:00
describe("#suppressLineNumbers", () => {
it("should disable line numbers", () => {
const paragraph = new Paragraph({
suppressLineNumbers: true,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [{ "w:pPr": [{ "w:suppressLineNumbers": EMPTY_OBJECT }] }],
});
});
});
2019-01-15 15:39:48 +01:00
describe("#outlineLevel", () => {
it("should set paragraph outline level to the given value", () => {
2019-06-12 01:03:36 +01:00
const paragraph = new Paragraph({
outlineLevel: 0,
});
2019-01-15 15:39:48 +01:00
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
2019-06-12 01:03:36 +01:00
"w:pPr": [{ "w:outlineLvl": { _attr: { "w:val": 0 } } }],
2019-01-15 15:39:48 +01:00
},
],
});
});
});
2020-10-12 22:48:38 +01:00
2020-10-28 01:05:31 +00:00
describe("#shading", () => {
it("should set shading to the given value", () => {
2020-10-28 01:05:31 +00:00
const paragraph = new Paragraph({
shading: {
2021-05-29 05:51:06 +03:00
type: ShadingType.REVERSE_DIAGONAL_STRIPE,
2020-10-28 01:05:31 +00:00
color: "00FFFF",
fill: "FF0000",
},
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [
{
"w:shd": {
_attr: {
"w:color": "00FFFF",
"w:fill": "FF0000",
"w:val": "reverseDiagStripe",
},
},
},
2021-03-14 17:00:42 +00:00
],
},
],
});
});
});
describe("#frame", () => {
it("should set frame attribute", () => {
const paragraph = new Paragraph({
frame: {
position: {
x: 1000,
y: 3000,
},
width: 4000,
height: 1000,
anchor: {
horizontal: FrameAnchorType.MARGIN,
vertical: FrameAnchorType.MARGIN,
},
alignment: {
x: HorizontalPositionAlign.CENTER,
y: VerticalPositionAlign.TOP,
},
},
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:pPr": [
{
"w:framePr": {
_attr: {
"w:h": 1000,
"w:hAnchor": "margin",
"w:vAnchor": "margin",
"w:w": 4000,
"w:x": 1000,
"w:xAlign": "center",
"w:y": 3000,
"w:yAlign": "top",
},
},
},
2020-10-28 01:05:31 +00:00
],
},
],
});
});
});
2020-10-12 22:48:38 +01:00
describe("#prepForXml", () => {
it("should set Internal Hyperlink", () => {
2020-10-12 22:48:38 +01:00
const paragraph = new Paragraph({
children: [
new ExternalHyperlink({
2021-08-27 16:53:11 -06:00
children: [new TextRun("test")],
link: "http://www.google.com",
}),
],
2020-10-12 22:48:38 +01:00
});
2021-06-15 00:25:44 +01:00
const viewWrapperMock = {
Relationships: {
createRelationship: () => ({}),
2020-10-12 22:48:38 +01:00
},
2021-06-15 00:25:44 +01:00
} as unknown as IViewWrapper;
2021-03-11 01:06:55 +00:00
2021-06-15 00:25:44 +01:00
const file = {} as unknown as File;
2021-03-11 01:06:55 +00:00
paragraph.prepForXml({
viewWrapper: viewWrapperMock,
file: file,
stack: [],
2021-03-11 01:06:55 +00:00
});
2020-10-12 22:48:38 +01:00
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [
{
"w:hyperlink": [
{
_attr: {
"r:id": "rIdtest-unique-id",
"w:history": 1,
},
},
{
"w:r": [
{
"w:t": [
{
_attr: {
"xml:space": "preserve",
},
},
"test",
],
},
],
},
],
},
],
2020-10-12 22:48:38 +01:00
});
});
});
});