Skip to content

Commit

Permalink
Replace \n and \r by \n and \r when saving a string
Browse files Browse the repository at this point in the history
  • Loading branch information
calixteman committed Sep 14, 2020
1 parent 741ce4f commit 0c8de5a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -794,9 +794,17 @@ function stringToPDFString(str) {
}

function escapeString(str) {
// replace "(", ")" and "\" by "\(", "\)" and "\\"
// replace "(", ")", "\n", "\r" and "\"
// by "\(", "\)", "\\n", "\\r" and "\\"
// in order to write it in a PDF file.
return str.replace(/([\(\)\\])/g, "\\$1");
return str.replace(/([\(\)\\\n\r])/g, match => {
if (match === "\n") {
return "\\n";
} else if (match === "\r") {
return "\\r";
}
return `\\${match}`;
});
}

function stringToUTF8String(str) {
Expand Down
6 changes: 3 additions & 3 deletions test/unit/util_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,9 @@ describe("util", function () {
});

describe("escapeString", function () {
it("should escape (, ) and \\", function () {
expect(escapeString("((a\\a))(b(b\\b)b)")).toEqual(
"\\(\\(a\\\\a\\)\\)\\(b\\(b\\\\b\\)b\\)"
it("should escape (, ), \n, \r and \\", function () {
expect(escapeString("((a\\a))\n(b(b\\b)\rb)")).toEqual(
"\\(\\(a\\\\a\\)\\)\\n\\(b\\(b\\\\b\\)\\rb\\)"
);
});
});
Expand Down

0 comments on commit 0c8de5a

Please sign in to comment.