Skip to content

Commit

Permalink
Bugfix to correctly handle empty loop
Browse files Browse the repository at this point in the history
  • Loading branch information
edi9999 committed Oct 5, 2022
1 parent 41fea07 commit 1603023
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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 (<anonymous>)
at LoopModule.postparse (.../docxtemplater/js/modules/loop.js:322:15)
at .../docxtemplater/js/parser.js:226:24
at Array.reduce (<anonymous>)
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 (<anonymous>)
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.
Expand Down
8 changes: 4 additions & 4 deletions es6/modules/loop.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
9 changes: 9 additions & 0 deletions es6/tests/e2e/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
});
});
Binary file added examples/empty-loop-regression.docx
Binary file not shown.
Binary file added examples/expected-loop-regression.docx
Binary file not shown.

0 comments on commit 1603023

Please sign in to comment.