Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent bahaviour in "identical" documents, duplicating if's content #337

Open
KingofGnome opened this issue Nov 22, 2023 · 0 comments

Comments

@KingofGnome
Copy link

KingofGnome commented Nov 22, 2023

Hi, im experiencing a bug running docx-templates on two "identical" documents. I'm putting the identical into quotes because even so content is identical, i also unzipped the docx-files and looked into the xml representation of both, where you find the usual word xml construct slashing the text into smaller and smaller xml-nodes, the longer you work on a document ;)

But still, opening them in Word, both files contain identical text:

The payment we refer to is in regard to our {{IF period==Written}}written{{END-IF}}{{IF period==Person}}oral{{END-IF}}{{IF period==Telephone}}oral{{END-IF}} agreement.

But, for whatever reason, the result of this files are different and might duplicate the first and/or 2nd if, depending on the vaule of 'period' (see below).

Reproducable example

I've simpliefied the Template and our service into the small documents and the following script, still able to reproduce the error.

For creating the report, i'm using a custon errorHandler with the idea of replacing missing data in a way, that for missing INS we just use their variablename, missing loops are an emty list and missing booleans are simply true.
But this is unrelated to the issue, as shown in the four examples below.

import { createReport } from "docx-templates";

import fs from "fs";
let buffer, template;

async function reportWithErrorHandler(template) {
  return await createReport({
    template,
    data: {
    },
    cmdDelimiter: ["{{", "}}"],
    failFast: false,
    errorHandler: (err, command_code) => {
        console.log("Error: " + err.message + " in command: " + command_code)
        // handling missing data,
        // replacing INS with [variable_name], for booleans this is accepted as true
        // and missing loops are empty arrays
      if (err.message.startsWith("ReferenceError")) {
        return "[" + command_code.replace("$", "") + "]";
      }
      if (err.message.startsWith("Invalid FOR command (can only iterate over Array):")) {
        return [];
      }
      throw err;
    },
  });
}

console.log("Running Debt simplified_bugged.docx");
template = fs.readFileSync("Debt simplified_bugged.docx");
buffer = await reportWithErrorHandler(template);
fs.writeFileSync("report_bugged.docx", buffer);

console.log("Running Debt simplified_working.docx");
template = fs.readFileSync("Debt simplified_working.docx");
buffer = await reportWithErrorHandler(template);
fs.writeFileSync("report_working.docx", buffer);

here are two identical documents:
Debt simplified_working.docx
Debt simplified_bugged.docx

I'm using docx-templates in version 4.11.3 on Node.js v18.17.1. Also tested with docx-templates in v4.11.1

Result based on input-data

without data

Running the script without data generates two files, one with the expected outcome and one with weird duplication:
Result from the "working" file, as expected:

The payment we refer to is in regard to our writtenoraloral agreement.

Result from the bugged-docx:

The payment we refer to is in regard to our writtenwrittenoraloraloral agreement.


"period" : "Written"

If we provide the following data (and therefore avoiding the error handler):
json data: { "period" : "Written" }

Everythign is fine, outcome for both files is:

The payment we refer to is in regard to our written agreement.


"period" : "Person"

However if we provide the period "Person"
json data: { "period" : "Person" }

We get two different results, one as expected (from the _working-File):

The payment we refer to is in regard to our oral agreement.

but the bugged-one looks like this:

The payment we refer to is in regard to our writtenoral agreement.


"period" : "Telephone"

If we provide the third option: "Telephone"
json data: { "period" : "Telephone" }
Working:

The payment we refer to is in regard to our oral agreement.

As (kinda) expected one is working flawless, the bugged one now looks like this:

The payment we refer to is in regard to our oraloral agreement.

Summary

While we do have a working docx-File, i think this example might point to an underlying bug, that would be wounderfull if we can get rid of. Also im having a hard way reasoning to people using the docx-templates with two identical documents does not provide identical results :D

I hope i've included averything to reproduce and hopefully get rid of this bug.
Thank you for your work on this awesome package,

Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant