2024-10-21 03:57:15 +01:00
|
|
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
|
|
2023-03-15 02:46:39 +00:00
|
|
|
|
import { IViewWrapper } from "@file/document-wrapper";
|
|
|
|
|
import { File } from "@file/file";
|
2023-03-16 01:55:18 +00:00
|
|
|
|
import { Paragraph, TextRun } from "@file/paragraph";
|
2023-03-15 02:46:39 +00:00
|
|
|
|
|
|
|
|
|
import { PatchType } from "./from-docx";
|
|
|
|
|
import { replacer } from "./replacer";
|
|
|
|
|
|
2023-12-31 23:16:48 +00:00
|
|
|
|
export const MOCK_JSON = {
|
2023-03-15 02:46:39 +00:00
|
|
|
|
elements: [
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:hdr",
|
|
|
|
|
elements: [
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:p",
|
|
|
|
|
attributes: { "w14:paraId": "3BE1A671", "w14:textId": "74E856C4", "w:rsidR": "000D38A7", "w:rsidRDefault": "000D38A7" },
|
|
|
|
|
elements: [
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:pPr",
|
|
|
|
|
elements: [{ type: "element", name: "w:pStyle", attributes: { "w:val": "Header" } }],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:r",
|
|
|
|
|
elements: [{ type: "element", name: "w:t", elements: [{ type: "text", text: "This is a {{head" }] }],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:r",
|
|
|
|
|
attributes: { "w:rsidR": "004A3A99" },
|
|
|
|
|
elements: [{ type: "element", name: "w:t", elements: [{ type: "text", text: "er" }] }],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:r",
|
|
|
|
|
elements: [
|
|
|
|
|
{ type: "element", name: "w:t", elements: [{ type: "text", text: "_adjective}} don’t you think?" }] },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
2023-10-18 12:58:01 -03:00
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:p",
|
|
|
|
|
elements: [
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:r",
|
|
|
|
|
elements: [
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:rPr",
|
|
|
|
|
elements: [{ type: "element", name: "w:b", attributes: { "w:val": "1" } }],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:t",
|
|
|
|
|
elements: [{ type: "text", text: "What a {{bold}} text!" }],
|
|
|
|
|
},
|
2024-11-20 12:19:25 +01:00
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:br",
|
|
|
|
|
},
|
2023-10-18 12:58:01 -03:00
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
2023-03-15 02:46:39 +00:00
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
describe("replacer", () => {
|
|
|
|
|
describe("replacer", () => {
|
2024-12-03 12:25:22 +01:00
|
|
|
|
it("should return { didFindOccurrence: false } if nothing is added", () => {
|
2024-11-28 11:35:11 +01:00
|
|
|
|
const { didFindOccurrence } = replacer({
|
|
|
|
|
json: {
|
|
|
|
|
elements: [],
|
|
|
|
|
},
|
|
|
|
|
patch: {
|
|
|
|
|
type: PatchType.PARAGRAPH,
|
|
|
|
|
children: [],
|
|
|
|
|
},
|
|
|
|
|
patchText: "hello",
|
2024-12-05 15:43:30 +00:00
|
|
|
|
context: vi.fn()(),
|
2024-11-28 11:35:11 +01:00
|
|
|
|
});
|
|
|
|
|
expect(didFindOccurrence).toBe(false);
|
2023-03-15 02:46:39 +00:00
|
|
|
|
});
|
|
|
|
|
|
2023-03-16 01:55:18 +00:00
|
|
|
|
it("should replace paragraph type", () => {
|
2024-11-28 11:35:11 +01:00
|
|
|
|
const { element, didFindOccurrence } = replacer({
|
2023-12-31 23:16:48 +00:00
|
|
|
|
json: JSON.parse(JSON.stringify(MOCK_JSON)),
|
|
|
|
|
patch: {
|
2023-03-15 02:46:39 +00:00
|
|
|
|
type: PatchType.PARAGRAPH,
|
|
|
|
|
children: [new TextRun("Delightful Header")],
|
|
|
|
|
},
|
2023-12-31 23:16:48 +00:00
|
|
|
|
patchText: "{{header_adjective}}",
|
|
|
|
|
context: {
|
2023-03-15 02:46:39 +00:00
|
|
|
|
file: {} as unknown as File,
|
|
|
|
|
viewWrapper: {
|
|
|
|
|
Relationships: {},
|
|
|
|
|
} as unknown as IViewWrapper,
|
|
|
|
|
stack: [],
|
|
|
|
|
},
|
2023-12-31 23:16:48 +00:00
|
|
|
|
});
|
2023-03-15 02:46:39 +00:00
|
|
|
|
|
2024-11-28 11:35:11 +01:00
|
|
|
|
expect(JSON.stringify(element)).to.contain("Delightful Header");
|
|
|
|
|
expect(didFindOccurrence).toBe(true);
|
2023-03-15 02:46:39 +00:00
|
|
|
|
});
|
2023-03-16 01:55:18 +00:00
|
|
|
|
|
2023-10-18 12:58:01 -03:00
|
|
|
|
it("should replace paragraph type keeping original styling if keepOriginalStyles is true", () => {
|
2024-11-28 11:35:11 +01:00
|
|
|
|
const { element, didFindOccurrence } = replacer({
|
2023-12-31 23:16:48 +00:00
|
|
|
|
json: JSON.parse(JSON.stringify(MOCK_JSON)),
|
|
|
|
|
patch: {
|
2023-10-18 12:58:01 -03:00
|
|
|
|
type: PatchType.PARAGRAPH,
|
|
|
|
|
children: [new TextRun("sweet")],
|
|
|
|
|
},
|
2023-12-31 23:16:48 +00:00
|
|
|
|
patchText: "{{bold}}",
|
|
|
|
|
context: {
|
2023-10-18 12:58:01 -03:00
|
|
|
|
file: {} as unknown as File,
|
|
|
|
|
viewWrapper: {
|
|
|
|
|
Relationships: {},
|
|
|
|
|
} as unknown as IViewWrapper,
|
|
|
|
|
stack: [],
|
|
|
|
|
},
|
2023-12-31 23:16:48 +00:00
|
|
|
|
keepOriginalStyles: true,
|
|
|
|
|
});
|
2023-10-18 12:58:01 -03:00
|
|
|
|
|
2024-11-28 11:35:11 +01:00
|
|
|
|
expect(JSON.stringify(element)).to.contain("sweet");
|
|
|
|
|
expect(element.elements![0].elements![1].elements).toMatchObject([
|
2023-10-18 12:58:01 -03:00
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:r",
|
|
|
|
|
elements: [
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:rPr",
|
|
|
|
|
elements: [{ type: "element", name: "w:b", attributes: { "w:val": "1" } }],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:t",
|
|
|
|
|
elements: [{ type: "text", text: "What a " }],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:r",
|
|
|
|
|
elements: [
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:rPr",
|
|
|
|
|
elements: [{ type: "element", name: "w:b", attributes: { "w:val": "1" } }],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:t",
|
|
|
|
|
elements: [{ type: "text", text: "sweet" }],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:r",
|
|
|
|
|
elements: [
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:rPr",
|
|
|
|
|
elements: [{ type: "element", name: "w:b", attributes: { "w:val": "1" } }],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:t",
|
|
|
|
|
elements: [{ type: "text", text: " text!" }],
|
|
|
|
|
},
|
2024-11-20 12:19:25 +01:00
|
|
|
|
{
|
|
|
|
|
name: "w:br",
|
|
|
|
|
type: "element",
|
|
|
|
|
},
|
2023-10-18 12:58:01 -03:00
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
]);
|
2024-11-28 11:35:11 +01:00
|
|
|
|
expect(didFindOccurrence).toBe(true);
|
2023-10-18 12:58:01 -03:00
|
|
|
|
});
|
|
|
|
|
|
2023-03-16 01:55:18 +00:00
|
|
|
|
it("should replace document type", () => {
|
2024-11-28 11:35:11 +01:00
|
|
|
|
const { element, didFindOccurrence } = replacer({
|
2023-12-31 23:16:48 +00:00
|
|
|
|
json: JSON.parse(JSON.stringify(MOCK_JSON)),
|
|
|
|
|
patch: {
|
2023-03-16 01:55:18 +00:00
|
|
|
|
type: PatchType.DOCUMENT,
|
|
|
|
|
children: [new Paragraph("Lorem ipsum paragraph")],
|
|
|
|
|
},
|
2023-12-31 23:16:48 +00:00
|
|
|
|
patchText: "{{header_adjective}}",
|
|
|
|
|
context: {
|
2023-03-16 01:55:18 +00:00
|
|
|
|
file: {} as unknown as File,
|
|
|
|
|
viewWrapper: {
|
|
|
|
|
Relationships: {},
|
|
|
|
|
} as unknown as IViewWrapper,
|
|
|
|
|
stack: [],
|
|
|
|
|
},
|
2023-12-31 23:16:48 +00:00
|
|
|
|
});
|
2023-03-16 01:55:18 +00:00
|
|
|
|
|
2024-11-28 11:35:11 +01:00
|
|
|
|
expect(JSON.stringify(element)).to.contain("Lorem ipsum paragraph");
|
|
|
|
|
expect(didFindOccurrence).toBe(true);
|
2023-03-16 01:55:18 +00:00
|
|
|
|
});
|
2024-10-13 01:29:32 +01:00
|
|
|
|
|
|
|
|
|
it("should replace", () => {
|
|
|
|
|
// cspell:disable
|
2024-11-28 11:35:11 +01:00
|
|
|
|
const { element, didFindOccurrence } = replacer({
|
2024-10-13 01:29:32 +01:00
|
|
|
|
json: {
|
|
|
|
|
elements: [
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:hdr",
|
|
|
|
|
elements: [
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:p",
|
|
|
|
|
elements: [
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:r",
|
|
|
|
|
elements: [
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:rPr",
|
|
|
|
|
elements: [
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:rFonts",
|
|
|
|
|
attributes: { "w:eastAsia": "Times New Roman" },
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:kern",
|
|
|
|
|
attributes: { "w:val": "0" },
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:sz",
|
|
|
|
|
attributes: { "w:val": "20" },
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:lang",
|
|
|
|
|
attributes: {
|
|
|
|
|
"w:val": "en-US",
|
|
|
|
|
"w:eastAsia": "en-US",
|
|
|
|
|
"w:bidi": "ar-SA",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:t",
|
|
|
|
|
elements: [{ type: "text", text: "{{" }],
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:r",
|
|
|
|
|
elements: [
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:rPr",
|
|
|
|
|
elements: [
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:rFonts",
|
|
|
|
|
attributes: { "w:eastAsia": "Times New Roman" },
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:kern",
|
|
|
|
|
attributes: { "w:val": "0" },
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:sz",
|
|
|
|
|
attributes: { "w:val": "20" },
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:lang",
|
|
|
|
|
attributes: {
|
|
|
|
|
"w:val": "en-US",
|
|
|
|
|
"w:eastAsia": "en-US",
|
|
|
|
|
"w:bidi": "ar-SA",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:t",
|
|
|
|
|
elements: [{ type: "text", text: "s" }],
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:r",
|
|
|
|
|
elements: [
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:rPr",
|
|
|
|
|
elements: [
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:rFonts",
|
|
|
|
|
attributes: { "w:eastAsia": "Times New Roman" },
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:kern",
|
|
|
|
|
attributes: { "w:val": "0" },
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:sz",
|
|
|
|
|
attributes: { "w:val": "20" },
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:lang",
|
|
|
|
|
attributes: {
|
|
|
|
|
"w:val": "en-US",
|
|
|
|
|
"w:eastAsia": "en-US",
|
|
|
|
|
"w:bidi": "ar-SA",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:t",
|
|
|
|
|
elements: [{ type: "text", text: "chool_" }],
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:r",
|
|
|
|
|
elements: [
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:rPr",
|
|
|
|
|
elements: [
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:rFonts",
|
|
|
|
|
attributes: { "w:eastAsia": "Times New Roman" },
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:kern",
|
|
|
|
|
attributes: { "w:val": "0" },
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:sz",
|
|
|
|
|
attributes: { "w:val": "20" },
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:lang",
|
|
|
|
|
attributes: {
|
|
|
|
|
"w:val": "en-US",
|
|
|
|
|
"w:eastAsia": "en-US",
|
|
|
|
|
"w:bidi": "ar-SA",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:t",
|
|
|
|
|
elements: [{ type: "text", text: "n" }],
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:r",
|
|
|
|
|
elements: [
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:t",
|
|
|
|
|
elements: [{ type: "text", text: "{{" }],
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:r",
|
|
|
|
|
elements: [
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:rPr",
|
|
|
|
|
elements: [
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:rFonts",
|
|
|
|
|
attributes: { "w:eastAsia": "Times New Roman" },
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:kern",
|
|
|
|
|
attributes: { "w:val": "0" },
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:sz",
|
|
|
|
|
attributes: { "w:val": "20" },
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:lang",
|
|
|
|
|
attributes: {
|
|
|
|
|
"w:val": "en-US",
|
|
|
|
|
"w:eastAsia": "en-US",
|
|
|
|
|
"w:bidi": "ar-SA",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:t",
|
|
|
|
|
elements: [{ type: "text", text: "a" }],
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:r",
|
|
|
|
|
elements: [
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:rPr",
|
|
|
|
|
elements: [
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:rFonts",
|
|
|
|
|
attributes: { "w:eastAsia": "Times New Roman" },
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:kern",
|
|
|
|
|
attributes: { "w:val": "0" },
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:sz",
|
|
|
|
|
attributes: { "w:val": "20" },
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:lang",
|
|
|
|
|
attributes: {
|
|
|
|
|
"w:val": "en-US",
|
|
|
|
|
"w:eastAsia": "en-US",
|
|
|
|
|
"w:bidi": "ar-SA",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:t",
|
|
|
|
|
elements: [{ type: "text", text: "ddr" }],
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:r",
|
|
|
|
|
elements: [
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:rPr",
|
|
|
|
|
elements: [
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:rFonts",
|
|
|
|
|
attributes: { "w:eastAsia": "Times New Roman" },
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:kern",
|
|
|
|
|
attributes: { "w:val": "0" },
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:sz",
|
|
|
|
|
attributes: { "w:val": "20" },
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:lang",
|
|
|
|
|
attributes: {
|
|
|
|
|
"w:val": "en-US",
|
|
|
|
|
"w:eastAsia": "en-US",
|
|
|
|
|
"w:bidi": "ar-SA",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:t",
|
|
|
|
|
elements: [{ type: "text", text: "ess" }],
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:r",
|
|
|
|
|
elements: [
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:rPr",
|
|
|
|
|
elements: [
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:rFonts",
|
|
|
|
|
attributes: { "w:eastAsia": "Times New Roman" },
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:kern",
|
|
|
|
|
attributes: { "w:val": "0" },
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:sz",
|
|
|
|
|
attributes: { "w:val": "20" },
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:lang",
|
|
|
|
|
attributes: {
|
|
|
|
|
"w:val": "en-US",
|
|
|
|
|
"w:eastAsia": "en-US",
|
|
|
|
|
"w:bidi": "ar-SA",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:t",
|
|
|
|
|
elements: [{ type: "text", text: "}}" }],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
// cspell:enable
|
|
|
|
|
patch: {
|
|
|
|
|
type: PatchType.PARAGRAPH,
|
|
|
|
|
children: [new Paragraph("Lorem ipsum paragraph")],
|
|
|
|
|
},
|
|
|
|
|
patchText: "{{address}}",
|
|
|
|
|
context: {
|
|
|
|
|
file: {} as unknown as File,
|
|
|
|
|
viewWrapper: {
|
|
|
|
|
Relationships: {},
|
|
|
|
|
} as unknown as IViewWrapper,
|
|
|
|
|
stack: [],
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-11-28 11:35:11 +01:00
|
|
|
|
expect(JSON.stringify(element)).to.contain("Lorem ipsum paragraph");
|
|
|
|
|
expect(didFindOccurrence).toBe(true);
|
2024-10-13 01:29:32 +01:00
|
|
|
|
});
|
2024-12-03 12:25:22 +01:00
|
|
|
|
|
|
|
|
|
it("should handle empty runs in patches", () => {
|
|
|
|
|
// cspell:disable
|
|
|
|
|
const { element, didFindOccurrence } = replacer({
|
|
|
|
|
json: {
|
|
|
|
|
elements: [
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:hdr",
|
|
|
|
|
elements: [
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:p",
|
|
|
|
|
elements: [
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:r",
|
|
|
|
|
elements: [
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:rPr",
|
|
|
|
|
elements: [
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:rFonts",
|
|
|
|
|
attributes: { "w:eastAsia": "Times New Roman" },
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
{
|
|
|
|
|
type: "element",
|
|
|
|
|
name: "w:t",
|
|
|
|
|
elements: [{ type: "text", text: "{{empty}}" }],
|
|
|
|
|
},
|
|
|
|
|
{ type: "text", text: "\n " },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
// cspell:enable
|
|
|
|
|
patch: {
|
|
|
|
|
type: PatchType.PARAGRAPH,
|
|
|
|
|
children: [new TextRun({})],
|
|
|
|
|
},
|
|
|
|
|
patchText: "{{empty}}",
|
|
|
|
|
context: {
|
|
|
|
|
file: {} as unknown as File,
|
|
|
|
|
viewWrapper: {
|
|
|
|
|
Relationships: {},
|
|
|
|
|
} as unknown as IViewWrapper,
|
|
|
|
|
stack: [],
|
|
|
|
|
},
|
|
|
|
|
keepOriginalStyles: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(JSON.stringify(element)).not.to.contain("{{empty}}");
|
|
|
|
|
expect(didFindOccurrence).toBe(true);
|
|
|
|
|
});
|
2023-03-15 02:46:39 +00:00
|
|
|
|
});
|
|
|
|
|
});
|