Fix style being missed on patchDocument
There were scenarios in which patching a document would result in loss of style for the template runs and, possibly, their right-adjacent run as well (post-splitting). That was due to the run style elements not being added to the newly created runs. This commit addresses this issue by introducing a new, optional, flag for the `patchDocument` function: `keepOriginalStyles`. It defaults to `false` (current behavior) and, when `true`, ensures that there is no loss of styling. This should address https://github.com/dolanmiu/docx/issues/2293.
This commit is contained in:
@ -44,6 +44,28 @@ const MOCK_JSON = {
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
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!" }],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
@ -115,6 +137,93 @@ describe("replacer", () => {
|
||||
expect(JSON.stringify(output)).to.contain("Delightful Header");
|
||||
});
|
||||
|
||||
it("should replace paragraph type keeping original styling if keepOriginalStyles is true", () => {
|
||||
const output = replacer(
|
||||
MOCK_JSON,
|
||||
{
|
||||
type: PatchType.PARAGRAPH,
|
||||
children: [new TextRun("sweet")],
|
||||
},
|
||||
"{{bold}}",
|
||||
[
|
||||
{
|
||||
text: "What a {{bold}} text!",
|
||||
runs: [
|
||||
{
|
||||
text: "What a {{bold}} text!",
|
||||
parts: [{ text: "What a {{bold}} text!", index: 1, start: 0, end: 21 }],
|
||||
index: 0,
|
||||
start: 0,
|
||||
end: 21,
|
||||
},
|
||||
],
|
||||
index: 0,
|
||||
path: [0, 0, 1],
|
||||
},
|
||||
],
|
||||
{
|
||||
file: {} as unknown as File,
|
||||
viewWrapper: {
|
||||
Relationships: {},
|
||||
} as unknown as IViewWrapper,
|
||||
stack: [],
|
||||
},
|
||||
true,
|
||||
);
|
||||
|
||||
expect(JSON.stringify(output)).to.contain("sweet");
|
||||
expect(output.elements![0].elements![1].elements).toMatchObject([
|
||||
{
|
||||
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!" }],
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("should replace document type", () => {
|
||||
const output = replacer(
|
||||
MOCK_JSON,
|
||||
|
Reference in New Issue
Block a user