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:
@ -155,7 +155,7 @@ export class Run extends XmlComponent {
|
|||||||
|
|
||||||
this.root.push(child);
|
this.root.push(child);
|
||||||
}
|
}
|
||||||
} else if (options.text) {
|
} else if (options.text !== undefined) {
|
||||||
this.root.push(new Text(options.text));
|
this.root.push(new Text(options.text));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,14 @@ describe("TextRun", () => {
|
|||||||
"w:r": [{ "w:t": [{ _attr: { "xml:space": "preserve" } }, "test"] }],
|
"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()", () => {
|
describe("#referenceFootnote()", () => {
|
||||||
|
@ -1,14 +1,7 @@
|
|||||||
import { IRunOptions, Run } from "./run";
|
import { IRunOptions, Run } from "./run";
|
||||||
import { Text } from "./run-components/text";
|
|
||||||
|
|
||||||
export class TextRun extends Run {
|
export class TextRun extends Run {
|
||||||
public constructor(options: IRunOptions | string) {
|
public constructor(options: IRunOptions | string) {
|
||||||
if (typeof options === "string") {
|
super(typeof options === "string" ? { text: options } : options);
|
||||||
super({});
|
|
||||||
this.root.push(new Text(options));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
super(options);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -77,7 +77,7 @@ export const MOCK_JSON = {
|
|||||||
|
|
||||||
describe("replacer", () => {
|
describe("replacer", () => {
|
||||||
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({
|
const { didFindOccurrence } = replacer({
|
||||||
json: {
|
json: {
|
||||||
elements: [],
|
elements: [],
|
||||||
@ -660,5 +660,71 @@ describe("replacer", () => {
|
|||||||
expect(JSON.stringify(element)).to.contain("Lorem ipsum paragraph");
|
expect(JSON.stringify(element)).to.contain("Lorem ipsum paragraph");
|
||||||
expect(didFindOccurrence).toBe(true);
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -74,7 +74,7 @@ export const replacer = ({
|
|||||||
|
|
||||||
newRunElements = textJson.map((e) => ({
|
newRunElements = textJson.map((e) => ({
|
||||||
...e,
|
...e,
|
||||||
elements: [...runElementNonTextualElements, ...e.elements!],
|
elements: [...runElementNonTextualElements, ...(e.elements ?? [])],
|
||||||
}));
|
}));
|
||||||
|
|
||||||
patchedRightElement = {
|
patchedRightElement = {
|
||||||
|
Reference in New Issue
Block a user