Skip to content

Nested FOR commands in the same paragraph cause infinite loop #416

@jwasnoggin

Description

@jwasnoggin

In the case where you have nested FOR commands in the same paragraph in a template, the document creation fails with error: INTERNAL ERROR: infinite loop or massive dataset detected. Please review and try again.

Eg this fails:

{FOR item IN items}{FOR subitem IN $item.subitems}

But this works:

{FOR item IN items}¶
{FOR subitem IN $item.subitems}

I think this might be related to #340 which looks like the same error but for IF commands.

Minimal reproduction

Code:

import createReport from 'docx-templates';
import fs from 'fs';

(async () => {
  const template = fs.readFileSync('./src/template.docx');

  const result = await createReport({
    template,
    data: {
      items: [
        { subitems: ['a', 'b', 'c'] },
        { subitems: ['d', 'e', 'f'] },
      ]
    },
    cmdDelimiter: ['{', '}'],
    maximumWalkingDepth: 1000, // So it doesn't take too long
  });

  fs.writeFileSync('./src/result.docx', result);
})()

Template contents:

{FOR item IN items}{FOR subitem IN $item.subitems}
{$subitem} 
{END-FOR subitem}{END-FOR item}

In the document.xml file, this is stored as

<w:p>
  <w:pPr>
    ...
  </w:pPr>
  <w:r>
    <w:rPr/>
    <w:t>{FOR item IN items}{FOR subitem IN $item.subitems}</w:t>
  </w:r>
</w:p>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions