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

patchDocument: incorrect output when patches have no text #2855

Closed
SebKranz opened this issue Nov 25, 2024 · 0 comments
Closed

patchDocument: incorrect output when patches have no text #2855

SebKranz opened this issue Nov 25, 2024 · 0 comments

Comments

@SebKranz
Copy link
Contributor

SebKranz commented Nov 25, 2024

I encountered weird results when using the document replacer:

  • the SPLIT_TOKEN (ɵ) appeared in the final document.
  • Placeholders within the same paragraph appeared out of order.

The following seems to be happening:

  1. I was instantiating a TextRun with an empty-string
    {
      type: PatchType.PARAGRAPH,
      children: new TextRun({ text: "" }),
    }
  2. since I was calling the constructor with an object, rather than a plain string (new TextRun("")), the TextRun doesn't contain a Text child.
  3. the replacer inserts the SPLIT_TOKEN, and then crashes when trying to access the now missing elements property here: https://github.com/dolanmiu/docx/blob/c6bb2556419f49f0ea42e774a20f5fa5f7450350/src/patcher/replacer.ts#L70C1-L74C1
  4. because patchDocument catches all errors, the merge continues and future runs on the same paragraph use the incorrect split-token.

I think there are three problems to be addressed:

  • the code for detecting when all patches are found is masking all unintended errors. This should be communicated in the returned value, not an error.
  • the replacer should handle empty Runs.
  • there should be an option for controlling the white-space TextRun({ space: "preserve", text: "..." }) EDIT: I mistakenly thought the w:t was missing due to the formatter

This issue does not occur when:

  • keepOriginalStyles is off
  • we instantiate the Run with a string argument (new TextRun("...")), since that will run through a different code path in the constructor.
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