#2028 Fix patcher offset indexes

This commit is contained in:
Dolan Miu
2023-03-23 21:01:24 +00:00
parent 528008406a
commit 16bfc78d8d
5 changed files with 173 additions and 2 deletions

View File

@ -71,6 +71,156 @@ describe("paragraph-token-replacer", () => {
});
});
it("should handle case where it cannot find any text to replace", () => {
const output = replaceTokenInParagraphElement({
paragraphElement: {
name: "w:p",
attributes: {
"w14:paraId": "2499FE9F",
"w14:textId": "27B4FBC2",
"w:rsidR": "00B51233",
"w:rsidRDefault": "007B52ED",
"w:rsidP": "007B52ED",
},
elements: [
{
type: "element",
name: "w:pPr",
elements: [{ type: "element", name: "w:pStyle", attributes: { "w:val": "Title" } }],
},
{
type: "element",
name: "w:r",
elements: [
{
type: "element",
name: "w:t",
attributes: { "xml:space": "preserve" },
elements: [{ type: "text", text: "Hello " }],
},
],
},
{
type: "element",
name: "w:r",
attributes: { "w:rsidR": "007F116B" },
elements: [
{
type: "element",
name: "w:t",
attributes: { "xml:space": "preserve" },
elements: [{ type: "text", text: "{{name}} " }],
},
],
},
{
type: "element",
name: "w:r",
elements: [{ type: "element", name: "w:t", elements: [{ type: "text", text: "World" }] }],
},
],
},
renderedParagraph: {
text: "Hello {{name}} World",
runs: [
{ text: "Hello ", parts: [{ text: "Hello ", index: 0, start: 0, end: 5 }], index: 1, start: 0, end: 5 },
{ text: "{{name}} ", parts: [{ text: "{{name}} ", index: 0, start: 6, end: 14 }], index: 2, start: 6, end: 14 },
{ text: "World", parts: [{ text: "World", index: 0, start: 15, end: 19 }], index: 3, start: 15, end: 19 },
],
index: 0,
path: [0, 1, 0, 0],
},
originalText: "{{name}}",
replacementText: "John",
});
expect(output).to.deep.equal({
attributes: {
"w14:paraId": "2499FE9F",
"w14:textId": "27B4FBC2",
"w:rsidP": "007B52ED",
"w:rsidR": "00B51233",
"w:rsidRDefault": "007B52ED",
},
elements: [
{
elements: [
{
attributes: {
"w:val": "Title",
},
name: "w:pStyle",
type: "element",
},
],
name: "w:pPr",
type: "element",
},
{
elements: [
{
attributes: {
"xml:space": "preserve",
},
elements: [
{
text: "Hello ",
type: "text",
},
],
name: "w:t",
type: "element",
},
],
name: "w:r",
type: "element",
},
{
attributes: {
"w:rsidR": "007F116B",
},
elements: [
{
attributes: {
"xml:space": "preserve",
},
elements: [
{
text: "John ",
type: "text",
},
],
name: "w:t",
type: "element",
},
],
name: "w:r",
type: "element",
},
{
elements: [
{
attributes: {
"xml:space": "preserve",
},
elements: [
{
text: "World",
type: "text",
},
],
name: "w:t",
type: "element",
},
],
name: "w:r",
type: "element",
},
],
name: "w:p",
});
});
// Try to fill rest of test coverage
// it("should replace token in paragraph", () => {
// const output = replaceTokenInParagraphElement({