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

817 lines
28 KiB
TypeScript
Raw Normal View History

import { expect } from "chai";
import { Formatter } from "export/formatter";
2020-05-22 12:22:45 +08:00
import { AlignmentType, EmphasisMarkType, TabStopPosition } from "file/paragraph";
2019-10-04 01:20:41 +01:00
import { UnderlineType } from "file/paragraph/run/underline";
import { ShadingType } from "file/table";
import { EMPTY_OBJECT } from "file/xml-components";
import { ParagraphStyle } from "./paragraph-style";
describe("ParagraphStyle", () => {
describe("#constructor", () => {
it("should set the style type to paragraph and use the given style id", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({ id: "myStyleId" });
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": { _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
});
});
it("should set the name of the style, if given", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
name: "Style Name",
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{ "w:name": { _attr: { "w:val": "Style Name" } } },
],
});
});
});
describe("formatting methods: style attributes", () => {
it("#basedOn", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({ id: "myStyleId", basedOn: "otherId" });
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{ "w:basedOn": { _attr: { "w:val": "otherId" } } },
],
});
});
it("#quickFormat", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({ id: "myStyleId", quickFormat: true });
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
2020-05-22 12:22:45 +08:00
"w:style": [
{
_attr: {
"w:type": "paragraph",
"w:styleId": "myStyleId",
},
},
{ "w:qFormat": EMPTY_OBJECT },
],
});
});
it("#next", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({ id: "myStyleId", next: "otherId" });
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{ "w:next": { _attr: { "w:val": "otherId" } } },
],
});
});
});
describe("formatting methods: paragraph properties", () => {
it("#indent", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
paragraph: {
indent: { left: 720 },
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:pPr": [{ "w:ind": { _attr: { "w:left": 720 } } }],
},
],
});
});
it("#spacing", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({ id: "myStyleId", paragraph: { spacing: { before: 50, after: 150 } } });
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:pPr": [{ "w:spacing": { _attr: { "w:before": 50, "w:after": 150 } } }],
},
],
});
});
it("#center", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
paragraph: {
alignment: AlignmentType.CENTER,
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:pPr": [{ "w:jc": { _attr: { "w:val": "center" } } }],
},
],
});
});
it("#character spacing", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
run: {
characterSpacing: 24,
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:rPr": [{ "w:spacing": { _attr: { "w:val": 24 } } }],
},
],
});
});
it("#left", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
paragraph: {
alignment: AlignmentType.LEFT,
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:pPr": [{ "w:jc": { _attr: { "w:val": "left" } } }],
},
],
});
});
it("#right", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
paragraph: {
alignment: AlignmentType.RIGHT,
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:pPr": [{ "w:jc": { _attr: { "w:val": "right" } } }],
},
],
});
});
it("#justified", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
paragraph: {
alignment: AlignmentType.JUSTIFIED,
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:pPr": [{ "w:jc": { _attr: { "w:val": "both" } } }],
},
],
});
});
it("#thematicBreak", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
paragraph: {
thematicBreak: true,
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:pPr": [
{
"w:pBdr": [
{
"w:bottom": {
_attr: {
"w:color": "auto",
2019-06-12 01:03:36 +01:00
"w:space": 1,
"w:val": "single",
2019-06-12 01:03:36 +01:00
"w:sz": 6,
},
},
},
],
},
],
},
],
});
});
2019-12-12 11:44:00 +13:00
it("#contextualSpacing", () => {
const style = new ParagraphStyle({
id: "myStyleId",
paragraph: {
contextualSpacing: true,
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:pPr": [
{
"w:contextualSpacing": {
_attr: {
"w:val": 1,
},
},
},
],
},
],
});
});
it("#leftTabStop", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
paragraph: {
leftTabStop: 1200,
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:pPr": [
{
"w:tabs": [{ "w:tab": { _attr: { "w:val": "left", "w:pos": 1200 } } }],
},
],
},
],
});
});
it("#maxRightTabStop", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
paragraph: {
rightTabStop: TabStopPosition.MAX,
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:pPr": [
{
"w:tabs": [{ "w:tab": { _attr: { "w:val": "right", "w:pos": 9026 } } }],
},
],
},
],
});
});
it("#keepLines", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
paragraph: {
keepLines: true,
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
2020-05-22 12:22:45 +08:00
"w:style": [
{
_attr: {
"w:type": "paragraph",
"w:styleId": "myStyleId",
},
},
{ "w:pPr": [{ "w:keepLines": EMPTY_OBJECT }] },
],
});
});
it("#keepNext", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
paragraph: {
keepNext: true,
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
2020-05-22 12:22:45 +08:00
"w:style": [
{
_attr: {
"w:type": "paragraph",
"w:styleId": "myStyleId",
},
},
{ "w:pPr": [{ "w:keepNext": EMPTY_OBJECT }] },
],
2019-02-13 17:16:45 +01:00
});
});
it("#outlineLevel", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
paragraph: {
outlineLevel: 1,
},
});
2019-02-13 17:16:45 +01:00
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
2019-06-12 01:03:36 +01:00
{ "w:pPr": [{ "w:outlineLvl": { _attr: { "w:val": 1 } } }] },
],
});
});
});
describe("formatting methods: run properties", () => {
it("#size", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
run: {
size: 24,
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:rPr": [{ "w:sz": { _attr: { "w:val": 24 } } }, { "w:szCs": { _attr: { "w:val": 24 } } }],
},
],
});
});
it("#smallCaps", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
run: {
smallCaps: true,
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:rPr": [{ "w:smallCaps": { _attr: { "w:val": true } } }],
},
],
});
});
it("#allCaps", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
run: {
allCaps: true,
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:rPr": [{ "w:caps": { _attr: { "w:val": true } } }],
},
],
});
});
it("#strike", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
run: {
strike: true,
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:rPr": [{ "w:strike": { _attr: { "w:val": true } } }],
},
],
});
});
it("#doubleStrike", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
run: {
doubleStrike: true,
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:rPr": [{ "w:dstrike": { _attr: { "w:val": true } } }],
},
],
});
});
it("#subScript", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
run: {
subScript: true,
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:rPr": [{ "w:vertAlign": { _attr: { "w:val": "subscript" } } }],
},
],
});
});
it("#superScript", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
run: {
superScript: true,
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:rPr": [{ "w:vertAlign": { _attr: { "w:val": "superscript" } } }],
},
],
});
});
2020-06-07 12:38:31 +08:00
it("#font by name", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
run: {
font: "Times",
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:rPr": [
2020-05-22 12:22:45 +08:00
{
"w:rFonts": {
_attr: {
"w:ascii": "Times",
"w:cs": "Times",
"w:eastAsia": "Times",
"w:hAnsi": "Times",
},
},
},
],
},
],
});
});
2020-06-07 12:38:31 +08:00
it("#font for ascii and eastAsia", () => {
const style = new ParagraphStyle({
id: "myStyleId",
run: {
font: {
ascii: "Times",
eastAsia: "KaiTi",
},
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:rPr": [
{
"w:rFonts": {
_attr: {
"w:ascii": "Times",
"w:eastAsia": "KaiTi",
},
},
},
],
},
],
});
});
it("#bold", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
run: {
bold: true,
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:rPr": [{ "w:b": { _attr: { "w:val": true } } }],
},
],
});
});
it("#italics", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
run: {
italics: true,
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:rPr": [{ "w:i": { _attr: { "w:val": true } } }],
},
],
});
});
2019-08-05 13:42:45 +03:00
it("#highlight", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
run: {
highlight: "005599",
},
});
2019-08-05 13:42:45 +03:00
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
2019-08-06 13:39:05 +03:00
"w:rPr": [{ "w:highlight": { _attr: { "w:val": "005599" } } }],
2019-08-05 13:42:45 +03:00
},
],
});
});
it("#shadow", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
run: {
shadow: {
type: ShadingType.PERCENT_10,
fill: "00FFFF",
color: "FF0000",
},
},
});
2019-08-05 13:42:45 +03:00
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
2020-05-22 12:22:45 +08:00
"w:rPr": [
{
"w:shd": {
_attr: {
"w:val": "pct10",
"w:fill": "00FFFF",
"w:color": "FF0000",
},
},
},
],
2019-08-05 13:42:45 +03:00
},
],
});
});
describe("#underline", () => {
it("should set underline to 'single' if no arguments are given", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
run: {
underline: {},
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:rPr": [{ "w:u": { _attr: { "w:val": "single" } } }],
},
],
});
});
it("should set the style if given", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
run: {
underline: {
type: UnderlineType.DOUBLE,
},
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:rPr": [{ "w:u": { _attr: { "w:val": "double" } } }],
},
],
});
});
it("should set the style and color if given", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
run: {
underline: {
type: UnderlineType.DOUBLE,
color: "005599",
},
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:rPr": [{ "w:u": { _attr: { "w:val": "double", "w:color": "005599" } } }],
},
],
});
});
});
2020-05-22 12:22:45 +08:00
describe("#emphasisMark", () => {
it("should set emphasisMark to 'dot' if no arguments are given", () => {
const style = new ParagraphStyle({
id: "myStyleId",
run: {
emphasisMark: {},
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:rPr": [{ "w:em": { _attr: { "w:val": "dot" } } }],
},
],
});
});
it("should set the style if given", () => {
const style = new ParagraphStyle({
id: "myStyleId",
run: {
emphasisMark: {
type: EmphasisMarkType.DOT,
},
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:rPr": [{ "w:em": { _attr: { "w:val": "dot" } } }],
},
],
});
});
});
it("#color", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({
id: "myStyleId",
run: {
color: "123456",
},
});
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:rPr": [{ "w:color": { _attr: { "w:val": "123456" } } }],
},
],
});
});
it("#link", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({ id: "myStyleId", link: "MyLink" });
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
2020-05-22 12:22:45 +08:00
"w:style": [
{
_attr: {
"w:type": "paragraph",
"w:styleId": "myStyleId",
},
},
{ "w:link": { _attr: { "w:val": "MyLink" } } },
],
});
});
it("#semiHidden", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({ id: "myStyleId", semiHidden: true });
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
2020-05-22 12:22:45 +08:00
"w:style": [
{
_attr: {
"w:type": "paragraph",
"w:styleId": "myStyleId",
},
},
{ "w:semiHidden": EMPTY_OBJECT },
],
});
});
it("#uiPriority", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({ id: "myStyleId", uiPriority: 99 });
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{
"w:uiPriority": {
_attr: {
2019-10-04 01:20:41 +01:00
"w:val": 99,
},
},
},
],
});
});
it("#unhideWhenUsed", () => {
2019-10-04 01:20:41 +01:00
const style = new ParagraphStyle({ id: "myStyleId", unhideWhenUsed: true });
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
2020-05-22 12:22:45 +08:00
"w:style": [
{
_attr: {
"w:type": "paragraph",
"w:styleId": "myStyleId",
},
},
{ "w:unhideWhenUsed": EMPTY_OBJECT },
],
});
});
});
});