diff --git a/src/file/paragraph/run/run.ts b/src/file/paragraph/run/run.ts index 9f79925113..b16fb63224 100644 --- a/src/file/paragraph/run/run.ts +++ b/src/file/paragraph/run/run.ts @@ -155,7 +155,7 @@ export class Run extends XmlComponent { this.root.push(child); } - } else if (options.text) { + } else if (options.text !== undefined) { this.root.push(new Text(options.text)); } } diff --git a/src/file/paragraph/run/text-run.spec.ts b/src/file/paragraph/run/text-run.spec.ts index 59ecb1fa08..4108c174c6 100644 --- a/src/file/paragraph/run/text-run.spec.ts +++ b/src/file/paragraph/run/text-run.spec.ts @@ -16,6 +16,14 @@ describe("TextRun", () => { "w:r": [{ "w:t": [{ _attr: { "xml:space": "preserve" } }, "test"] }], }); }); + + it("should add empty text into run", () => { + run = new TextRun({ text: "" }); + const f = new Formatter().format(run); + expect(f).to.deep.equal({ + "w:r": [{ "w:t": [{ _attr: { "xml:space": "preserve" } }, ""] }], + }); + }); }); describe("#referenceFootnote()", () => { diff --git a/src/file/paragraph/run/text-run.ts b/src/file/paragraph/run/text-run.ts index 27bf8c4729..a8195c2262 100644 --- a/src/file/paragraph/run/text-run.ts +++ b/src/file/paragraph/run/text-run.ts @@ -1,14 +1,7 @@ import { IRunOptions, Run } from "./run"; -import { Text } from "./run-components/text"; export class TextRun extends Run { public constructor(options: IRunOptions | string) { - if (typeof options === "string") { - super({}); - this.root.push(new Text(options)); - return this; - } - - super(options); + super(typeof options === "string" ? { text: options } : options); } } diff --git a/src/patcher/replacer.spec.ts b/src/patcher/replacer.spec.ts index 314a83c7e2..ec7577489e 100644 --- a/src/patcher/replacer.spec.ts +++ b/src/patcher/replacer.spec.ts @@ -77,7 +77,7 @@ export const MOCK_JSON = { describe("replacer", () => { describe("replacer", () => { - it("should throw an error if nothing is added", () => { + it("should return { didFindOccurrence: false } if nothing is added", () => { const { didFindOccurrence } = replacer({ json: { elements: [], @@ -660,5 +660,71 @@ describe("replacer", () => { expect(JSON.stringify(element)).to.contain("Lorem ipsum paragraph"); expect(didFindOccurrence).toBe(true); }); + + 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); + }); }); }); diff --git a/src/patcher/replacer.ts b/src/patcher/replacer.ts index 7d47f4eb76..5a4e503da6 100644 --- a/src/patcher/replacer.ts +++ b/src/patcher/replacer.ts @@ -74,7 +74,7 @@ export const replacer = ({ newRunElements = textJson.map((e) => ({ ...e, - elements: [...runElementNonTextualElements, ...e.elements!], + elements: [...runElementNonTextualElements, ...(e.elements ?? [])], })); patchedRightElement = {