Skip to content

Commit

Permalink
Fix replacer error on patches with empty Runs (#2875)
Browse files Browse the repository at this point in the history
* Add test to demonstrate empty strings not creating runs

* Fix Run not creating Text child for empty strings

* Simplify TextRun constructor

Since `Run` already has a option for instantiating a `Text` element, we don't need to push children here.

* Add replacer test for empty runs

* Add replacer test for empty runs

* replacer: handle patches with empty runs

* Fix incorrect test name
  • Loading branch information
SebKranz authored Dec 3, 2024
1 parent 64505a2 commit b89b571
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/file/paragraph/run/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/file/paragraph/run/text-run.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()", () => {
Expand Down
9 changes: 1 addition & 8 deletions src/file/paragraph/run/text-run.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
68 changes: 67 additions & 1 deletion src/patcher/replacer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down Expand Up @@ -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);
});
});
});
2 changes: 1 addition & 1 deletion src/patcher/replacer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const replacer = ({

newRunElements = textJson.map((e) => ({
...e,
elements: [...runElementNonTextualElements, ...e.elements!],
elements: [...runElementNonTextualElements, ...(e.elements ?? [])],
}));

patchedRightElement = {
Expand Down

0 comments on commit b89b571

Please sign in to comment.