diff --git a/CHANGELOG.md b/CHANGELOG.md index 0211bc84..4b8ad16a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,27 @@ +### 3.31.5 + +Bugfix to correctly handle empty loops. + +Fixes https://github.com/open-xml-templating/docxtemplater/issues/680 + +Previously the following stack trace would be shown : + +```txt +TypeError: Cannot read properties of undefined (reading 'lIndex') + at .../docxtemplater/js/modules/loop.js:331:42 + at Array.some () + at LoopModule.postparse (.../docxtemplater/js/modules/loop.js:322:15) + at .../docxtemplater/js/parser.js:226:24 + at Array.reduce () + at _postparse (.../docxtemplater/js/parser.js:225:22) + at postparse (.../docxtemplater/js/parser.js:228:20) + at .../docxtemplater/js/modules/expand-pair-trait.js:268:30 + at Array.reduce () + at Object.postparse (.../docxtemplater/js/modules/expand-pair-trait.js:248:32) +``` + +Now the template is rendered correctly. + ### 3.31.4 Bugfix for table module : merge-cells-col did not work correctly when placed inside a loop. diff --git a/es6/modules/loop.js b/es6/modules/loop.js index 1dd53972..3c06f859 100644 --- a/es6/modules/loop.js +++ b/es6/modules/loop.js @@ -277,21 +277,21 @@ class LoopModule { }, []); } postparse(parsed, { basePart }) { - if (basePart && this.docxtemplater.fileType === "docx") { + if (basePart && this.docxtemplater.fileType === "docx" && parsed.length > 0) { basePart.sectPrCount = getSectPrHeaderFooterChangeCount(parsed); this.totalSectPr += basePart.sectPrCount; const { sects } = this; sects.some(function (sect, index) { - if (sect[0].lIndex > basePart.lIndex) { + if (basePart.lIndex < sect[0].lIndex) { if (index + 1 < sects.length && isContinuous(sects[index + 1])) { basePart.addContinuousType = true; } return true; } if ( - sect[0].lIndex > parsed[0].lIndex && - basePart.lIndex > sect[0].lIndex + parsed[0].lIndex < sect[0].lIndex && + sect[0].lIndex < basePart.lIndex ) { if (isNextPage(sects[index])) { basePart.addNextPage = { index }; diff --git a/es6/tests/e2e/integration.js b/es6/tests/e2e/integration.js index 4b3cf7a3..1f8978d5 100644 --- a/es6/tests/e2e/integration.js +++ b/es6/tests/e2e/integration.js @@ -984,3 +984,12 @@ describe("Load Office 365 file", function () { shouldBeSame({ doc, expectedName: "expected-header-without-digit.docx" }); }); }); + +describe("Loops", function () { + it("should work with template", function () { + const tags = { ice: [1, 2, 3] }; + const doc = createDocV4("empty-loop-regression.docx"); + doc.render(tags); + shouldBeSame({ doc, expectedName: "expected-loop-regression.docx" }); + }); +}); diff --git a/examples/empty-loop-regression.docx b/examples/empty-loop-regression.docx new file mode 100644 index 00000000..4050bd6c Binary files /dev/null and b/examples/empty-loop-regression.docx differ diff --git a/examples/expected-loop-regression.docx b/examples/expected-loop-regression.docx new file mode 100644 index 00000000..16c99161 Binary files /dev/null and b/examples/expected-loop-regression.docx differ