Skip to content

Commit

Permalink
Do not throw error if using XML corrupt characters on Text files
Browse files Browse the repository at this point in the history
edi9999 committed Jan 24, 2025
1 parent 51ae8aa commit 432fff9
Showing 4 changed files with 34 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 3.58.3

Do not throw an error if sending xml invalid character such as "\u0002" when using TxtTemplater.

### 3.58.2

Bugfix Inspectmodule so that it shows image tags inside xlsx files.
29 changes: 17 additions & 12 deletions es6/modules/render.js
Original file line number Diff line number Diff line change
@@ -99,18 +99,23 @@ class Render {
return { errors: [e] };
}
value ??= nullGetter(part);
if (stripInvalidXMLChars) {
value = removeCorruptCharacters(value);
} else if (hasCorruptCharacters(value)) {
return {
errors: [
getCorruptCharactersException({
tag: part.value,
value,
offset: part.offset,
}),
],
};
if (typeof value === "string") {
if (stripInvalidXMLChars) {
value = removeCorruptCharacters(value);
} else if (
["docx", "pptx", "xlsx"].indexOf(fileType) !== -1 &&
hasCorruptCharacters(value)
) {
return {
errors: [
getCorruptCharactersException({
tag: part.value,
value,
offset: part.offset,
}),
],
};
}
}
if (fileType === "text") {
return { value };
7 changes: 7 additions & 0 deletions es6/tests/e2e/text.js
Original file line number Diff line number Diff line change
@@ -12,6 +12,13 @@ describe("Text templating", () => {
);
});

it("should not regress if data contains XML corrupt characters", () => {
const doc = new TxtTemplater("Hello {user}, how are you ?");
expect(doc.render({ user: "John\u0002" })).to.be.equal(
"Hello John\u0002, how are you ?"
);
});

it("should be possible to template text files with expressionParser", () => {
const doc = new TxtTemplater("Hello {user + age}, how are you ?", {
parser: expressionParser,
8 changes: 6 additions & 2 deletions es6/text.js
Original file line number Diff line number Diff line change
@@ -14,8 +14,11 @@ const XmlTemplater = require("./xml-templater.js");

function TxtTemplater(text, options = {}) {
const filePath = "text";
const xmlt = new XmlTemplater(text, { modules: [] });
xmlt.fileType = "text";
const xmltOptions = {
fileType: "text",
modules: [],
};
const xmlt = new XmlTemplater(text, xmltOptions);
this.fileTypeConfig =
xmlt.fileTypeConfig =
options.fileTypeConfig =
@@ -33,6 +36,7 @@ function TxtTemplater(text, options = {}) {
for (const module of xmlt.modules) {
module.optionsTransformer(options, {
fileTypeConfig: xmlt.fileTypeConfig,
...xmltOptions,
parser: xmlt.parser,
options: xmlt,
});

0 comments on commit 432fff9

Please sign in to comment.