Fix replacer error on patches with empty Runs (#2875)

* 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
This commit is contained in:
SebKranz
2024-12-03 12:25:22 +01:00
committed by GitHub
parent 64505a295f
commit b89b571c4e
5 changed files with 78 additions and 11 deletions

View File

@ -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));
}
}

View File

@ -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()", () => {

View File

@ -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);
}
}