guigrpa / docx-templates

Template-based docx report creation
MIT License
882 stars 145 forks source link

Bug Report: Duplicate Commands in Docx-Templates #374

Open Amerlander opened 2 months ago

Amerlander commented 2 months ago

Description

When commands are included inside a text field, docx-templates processes the command twice. This results in the command appearing twice in the command list and being executed twice. Although the final output appears correct, this redundancy leads to unnecessary parsing, thereby increasing resource usage.

Steps to Reproduce

  1. Create a DOCX file with a docx-templates-command inside a text-field
  2. Use the listCommands function to parse the template.
  3. Observe the command list output.

Sample

In the provided sample, the first two commands ("artist project ID" and "Bezeichnung") are correctly listed once. However, the third command ("Titelbild"), despite being present only once in the document, is returned twice by listCommands.

Code

import { createReport, listCommands } from 'https://unpkg.com/docx-templates/lib/browser.js';

async function prepareFile() {
    const template = await readFileIntoArrayBuffer(file);
    const cmdDelimiter = ['{#', '#}'];
    const templateMeta = {};
    templateMeta.commands = await listCommands(template, cmdDelimiter);
    console.log(templateMeta);
}

Docx-File

Test (3).docx

Docx-Screenshot

image

Output

image

Expected Behavior

Each command should appear only once in the command list, reflecting its single occurrence in the document.

Actual Behavior

Commands within text fields are listed and executed twice, resulting in duplicated entries in the command list.

Environment

Library Version: https://unpkg.com/docx-templates/lib/browser.js Browser: Opera One (Version: 111.0.5168.25) OS: Windows 11

jjhbw commented 2 months ago

@Amerlander Thanks for your detailed report. I'll try to reproduce this when I have time. On the face of it, I expect that this happens because of how Word structures text box-related data in the underlying XML.