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

760 lines
25 KiB
TypeScript
Raw Normal View History

2023-06-05 00:33:43 +01:00
import { describe, expect, it } from "vitest";
import { Formatter } from "@export/formatter";
import { BorderStyle } from "@file/border";
import { ShadingType } from "@file/shading";
2018-10-26 01:04:07 +01:00
2020-05-22 12:22:45 +08:00
import { EmphasisMarkType } from "./emphasis-mark";
2022-08-31 08:59:27 +01:00
import { PageNumber, Run } from "./run";
2019-06-17 01:51:57 +01:00
import { UnderlineType } from "./underline";
import { HighlightColor, TextEffect } from "./properties";
2016-07-13 00:59:53 +01:00
describe("Run", () => {
2023-06-14 01:55:47 +01:00
describe("#noProof()", () => {
2023-05-11 20:49:09 -08:00
it("turns off spelling and grammar checkers for a run", () => {
const run = new Run({
noProof: true,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
2023-06-13 20:55:20 +01:00
"w:rPr": [{ "w:noProof": {} }],
2023-05-11 20:49:09 -08:00
},
],
});
});
});
2016-05-26 15:08:34 +01:00
describe("#bold()", () => {
2016-03-30 04:43:56 +01:00
it("it should add bold to the properties", () => {
2019-06-17 01:51:57 +01:00
const run = new Run({
bold: true,
});
2019-06-26 22:12:18 +01:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{ "w:b": {} },
2019-06-26 22:12:18 +01:00
{
"w:bCs": {},
2019-06-26 22:12:18 +01:00
},
],
},
],
});
2016-03-30 04:43:56 +01:00
});
});
2016-03-30 03:58:53 +01:00
describe("#italics()", () => {
2016-03-30 04:43:56 +01:00
it("it should add italics to the properties", () => {
2019-06-17 01:51:57 +01:00
const run = new Run({
italics: true,
});
2019-06-26 22:12:18 +01:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{ "w:i": {} },
2019-06-26 22:12:18 +01:00
{
"w:iCs": {},
2019-06-26 22:12:18 +01:00
},
],
},
],
});
2016-03-30 04:43:56 +01:00
});
});
2016-05-26 15:08:34 +01:00
describe("#underline()", () => {
it("should default to 'single' and no color", () => {
2019-06-17 01:51:57 +01:00
const run = new Run({
underline: {},
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:u": { _attr: { "w:val": "single" } } }] }],
});
});
it("should set the style type and color if given", () => {
2019-06-17 01:51:57 +01:00
const run = new Run({
underline: {
type: UnderlineType.DOUBLE,
color: "990011",
},
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:u": { _attr: { "w:val": "double", "w:color": "990011" } } }] }],
});
});
2016-03-30 03:58:53 +01:00
});
2020-05-22 12:22:45 +08:00
describe("#emphasisMark()", () => {
it("should default to 'dot'", () => {
const run = new Run({
emphasisMark: {},
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:em": { _attr: { "w:val": "dot" } } }] }],
});
});
it("should set the style type if given", () => {
const run = new Run({
emphasisMark: {
type: EmphasisMarkType.DOT,
},
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:em": { _attr: { "w:val": "dot" } } }] }],
});
});
});
2016-07-12 08:46:26 +01:00
describe("#smallCaps()", () => {
it("it should add smallCaps to the properties", () => {
2019-06-17 01:51:57 +01:00
const run = new Run({
smallCaps: true,
});
2019-06-26 22:12:18 +01:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:smallCaps": {} }] }],
2019-06-26 22:12:18 +01:00
});
2016-07-12 08:46:26 +01:00
});
});
describe("#caps()", () => {
it("it should add caps to the properties", () => {
2019-06-17 01:51:57 +01:00
const run = new Run({
allCaps: true,
});
2019-06-26 22:12:18 +01:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:caps": {} }] }],
2019-06-26 22:12:18 +01:00
});
2016-07-12 08:46:26 +01:00
});
});
describe("#strike()", () => {
it("it should add strike to the properties", () => {
2019-06-17 01:51:57 +01:00
const run = new Run({
strike: true,
});
2019-06-26 22:12:18 +01:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:strike": {} }] }],
2019-06-26 22:12:18 +01:00
});
2016-07-12 08:46:26 +01:00
});
});
2016-03-30 03:58:53 +01:00
2016-07-12 08:46:26 +01:00
describe("#doubleStrike()", () => {
2021-03-13 20:23:15 +00:00
it("it should add double strike to the properties", () => {
2019-06-17 01:51:57 +01:00
const run = new Run({
doubleStrike: true,
});
2019-06-26 22:12:18 +01:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:dstrike": {} }] }],
2019-06-26 22:12:18 +01:00
});
2021-03-13 20:23:15 +00:00
});
});
describe("#emboss()", () => {
it("it should add emboss to the properties", () => {
const run = new Run({
emboss: true,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:emboss": {} }] }],
2021-03-13 20:23:15 +00:00
});
});
});
describe("#imprint()", () => {
it("it should add imprint to the properties", () => {
const run = new Run({
imprint: true,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:imprint": {} }] }],
2021-03-13 20:23:15 +00:00
});
2016-07-12 08:46:26 +01:00
});
});
describe("#subScript()", () => {
it("it should add subScript to the properties", () => {
const run = new Run({
subScript: true,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:vertAlign": { _attr: { "w:val": "subscript" } } }] }],
});
});
});
describe("#superScript()", () => {
it("it should add superScript to the properties", () => {
const run = new Run({
superScript: true,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:vertAlign": { _attr: { "w:val": "superscript" } } }] }],
});
});
});
2019-08-05 13:42:45 +03:00
describe("#highlight()", () => {
it("it should add highlight to the properties", () => {
2019-08-06 21:37:33 +01:00
const run = new Run({
highlight: HighlightColor.YELLOW,
2019-08-06 21:37:33 +01:00
});
2019-08-05 13:42:45 +03:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{ "w:highlight": { _attr: { "w:val": "yellow" } } },
2019-08-05 13:42:45 +03:00
{
"w:highlightCs": {
_attr: {
"w:val": "yellow",
2019-08-05 13:42:45 +03:00
},
},
},
],
},
],
});
});
});
describe("#shadow()", () => {
it("it should add shadow to the properties", () => {
2019-08-06 21:37:33 +01:00
const run = new Run({
2019-08-06 23:08:21 +01:00
shading: {
2021-05-29 05:51:06 +03:00
type: ShadingType.PERCENT_10,
2019-08-06 21:37:33 +01:00
fill: "00FFFF",
color: "FF0000",
},
});
2019-08-05 13:42:45 +03:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
2021-05-20 04:08:00 +03:00
"w:rPr": [{ "w:shd": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "FF0000" } } }],
2019-08-05 13:42:45 +03:00
},
],
});
});
});
2016-07-12 08:46:26 +01:00
describe("#break()", () => {
it("it should add break to the run", () => {
2020-12-23 23:31:28 +00:00
const run = new Run({
break: 1,
});
2019-06-26 22:12:18 +01:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:br": {} }],
});
2016-07-12 08:46:26 +01:00
});
2020-12-23 23:31:28 +00:00
it("it should add two breaks to the run", () => {
const run = new Run({
break: 2,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{ "w:br": {} },
{
"w:br": {},
},
],
});
});
2016-07-12 08:46:26 +01:00
});
2016-03-30 03:58:53 +01:00
2017-03-08 20:29:12 +01:00
describe("#font()", () => {
it("should set the font as named", () => {
2019-06-17 01:51:57 +01:00
const run = new Run({
font: {
name: "Times",
},
});
2017-03-08 20:29:12 +01:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
2018-07-24 12:37:15 +08:00
"w:r": [
{
"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",
},
},
},
2018-07-24 12:37:15 +08:00
],
},
],
2017-03-08 20:29:12 +01:00
});
});
2020-06-07 12:38:31 +08:00
it("should set the font for ascii and eastAsia", () => {
const run = new Run({
font: {
ascii: "Times",
eastAsia: "KaiTi",
},
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{
"w:rFonts": {
_attr: {
"w:ascii": "Times",
"w:eastAsia": "KaiTi",
},
},
},
],
},
],
});
});
2017-03-08 20:29:12 +01:00
});
2017-03-12 17:31:36 +01:00
describe("#color", () => {
it("should set the run to the color given", () => {
2019-06-17 01:51:57 +01:00
const run = new Run({
color: "001122",
});
2017-03-12 17:31:36 +01:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:color": { _attr: { "w:val": "001122" } } }] }],
2017-03-12 17:31:36 +01:00
});
});
});
describe("#size", () => {
it("should set the run to the given size", () => {
2019-06-17 01:51:57 +01:00
const run = new Run({
size: 24,
});
2017-03-12 17:31:36 +01:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
2018-07-25 15:02:58 +03:00
"w:r": [
{
"w:rPr": [{ "w:sz": { _attr: { "w:val": 24 } } }, { "w:szCs": { _attr: { "w:val": 24 } } }],
2018-07-25 15:02:58 +03:00
},
],
2017-03-12 17:31:36 +01:00
});
});
});
2017-03-12 17:35:15 +01:00
2018-07-24 18:52:45 +03:00
describe("#rtl", () => {
it("should set the run to the RTL mode", () => {
2019-06-17 01:51:57 +01:00
const run = new Run({
rightToLeft: true,
});
2018-07-24 18:52:45 +03:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:rtl": {} }] }],
2018-07-24 18:52:45 +03:00
});
});
});
2019-01-15 21:40:19 +00:00
describe("#numberOfTotalPages", () => {
it("should set the run to the RTL mode", () => {
2019-11-21 01:02:46 +00:00
const run = new Run({
children: [PageNumber.TOTAL_PAGES],
});
2019-01-15 21:40:19 +00:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{ "w:fldChar": { _attr: { "w:fldCharType": "begin" } } },
2019-01-15 21:40:19 +00:00
{ "w:instrText": [{ _attr: { "xml:space": "preserve" } }, "NUMPAGES"] },
{ "w:fldChar": { _attr: { "w:fldCharType": "separate" } } },
{ "w:fldChar": { _attr: { "w:fldCharType": "end" } } },
2019-01-15 21:40:19 +00:00
],
2019-08-09 11:56:22 +03:00
});
});
});
describe("#numberOfTotalPagesSection", () => {
it("should set the run to the RTL mode", () => {
2019-11-21 01:02:46 +00:00
const run = new Run({
children: [PageNumber.TOTAL_PAGES_IN_SECTION],
});
2019-08-09 11:56:22 +03:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{ "w:fldChar": { _attr: { "w:fldCharType": "begin" } } },
{ "w:instrText": [{ _attr: { "xml:space": "preserve" } }, "SECTIONPAGES"] },
{ "w:fldChar": { _attr: { "w:fldCharType": "separate" } } },
{ "w:fldChar": { _attr: { "w:fldCharType": "end" } } },
],
2019-01-15 21:40:19 +00:00
});
});
});
describe("#pageNumber", () => {
it("should set the run to the RTL mode", () => {
2019-11-21 01:02:46 +00:00
const run = new Run({
children: [PageNumber.CURRENT],
});
2019-01-15 21:40:19 +00:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{ "w:fldChar": { _attr: { "w:fldCharType": "begin" } } },
2019-01-15 21:40:19 +00:00
{ "w:instrText": [{ _attr: { "xml:space": "preserve" } }, "PAGE"] },
{ "w:fldChar": { _attr: { "w:fldCharType": "separate" } } },
{ "w:fldChar": { _attr: { "w:fldCharType": "end" } } },
2019-01-15 21:40:19 +00:00
],
});
});
});
describe("#section", () => {
it("should set the run to the RTL mode", () => {
const run = new Run({
children: [PageNumber.CURRENT_SECTION],
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{ "w:fldChar": { _attr: { "w:fldCharType": "begin" } } },
{ "w:instrText": [{ _attr: { "xml:space": "preserve" } }, "SECTION"] },
{ "w:fldChar": { _attr: { "w:fldCharType": "separate" } } },
{ "w:fldChar": { _attr: { "w:fldCharType": "end" } } },
],
});
});
});
2017-03-12 17:35:15 +01:00
describe("#style", () => {
it("should set the style to the given styleId", () => {
2019-06-17 01:51:57 +01:00
const run = new Run({
style: "myRunStyle",
});
2017-03-12 17:35:15 +01:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:rStyle": { _attr: { "w:val": "myRunStyle" } } }] }],
2017-03-12 17:35:15 +01:00
});
});
});
2021-11-26 12:29:11 +01:00
describe("#revisions", () => {
it("should add style revisions", () => {
const run = new Run({
bold: true,
italics: true,
revision: {
id: 0,
author: "Firstname Lastname",
date: "123",
bold: false,
italics: true,
},
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{ "w:b": {} },
{
"w:bCs": {},
},
{ "w:i": {} },
{
"w:iCs": {},
},
{
"w:rPrChange": [
{
_attr: {
"w:author": "Firstname Lastname",
"w:date": "123",
"w:id": 0,
},
},
{
"w:rPr": [
{ "w:b": { _attr: { "w:val": false } } },
{
"w:bCs": {
_attr: {
"w:val": false,
},
},
},
{ "w:i": {} },
{
"w:iCs": {},
},
],
},
],
},
],
},
],
});
});
});
2022-01-06 04:36:55 +00:00
describe("#border", () => {
it("should correctly set the border", () => {
const run = new Run({
border: {
color: "auto",
space: 1,
style: BorderStyle.SINGLE,
size: 6,
},
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{
"w:bdr": {
_attr: {
"w:color": "auto",
"w:space": 1,
"w:sz": 6,
"w:val": "single",
},
},
},
],
},
],
});
});
});
2022-10-28 22:20:16 +01:00
describe("#vanish and #specVanish", () => {
it("should correctly set vanish", () => {
const run = new Run({
vanish: true,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{
"w:vanish": {},
},
],
},
],
});
});
it("should correctly set specVanish", () => {
const run = new Run({
specVanish: true,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{
"w:specVanish": {},
},
],
},
],
});
});
2022-11-03 00:30:16 +00:00
describe("#scale", () => {
it("should correctly set the border", () => {
const run = new Run({
scale: 200,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{
"w:w": {
_attr: {
"w:val": 200,
},
},
},
],
},
],
});
});
});
2022-12-18 00:52:29 +00:00
describe("#language", () => {
it("should correctly set the language", () => {
const run = new Run({
language: {
value: "en-US",
eastAsia: "zh-CN",
bidirectional: "ar-SA",
},
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{
"w:lang": {
_attr: {
"w:val": "en-US",
"w:eastAsia": "zh-CN",
"w:bidi": "ar-SA",
},
},
},
],
},
],
});
});
});
describe("#position", () => {
it("should correctly set the position", () => {
const run = new Run({
position: "2mm",
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{
"w:position": {
_attr: {
"w:val": "2mm",
},
},
},
],
},
],
});
});
});
describe("#effect", () => {
it("should correctly set the effect", () => {
const run = new Run({
effect: TextEffect.ANTS_BLACK,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{
"w:effect": {
_attr: {
"w:val": "antsBlack",
},
},
},
],
},
],
});
});
});
describe("#math", () => {
it("should correctly set the math", () => {
const run = new Run({
math: true,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{
"w:oMath": {},
},
],
},
],
});
});
});
describe("#kern", () => {
it("should correctly set the kern", () => {
const run = new Run({
kern: "2mm",
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{
"w:kern": {
_attr: {
"w:val": "2mm",
},
},
},
],
},
],
});
});
});
describe("#snapToGrid", () => {
it("should correctly set the snapToGrid", () => {
const run = new Run({
snapToGrid: true,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{
"w:snapToGrid": {},
},
],
},
],
});
});
});
2022-10-28 22:20:16 +01:00
});
2017-03-08 20:28:59 +01:00
});