From 0c8de5aaf9a73119d5911d65716d39f347070f75 Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Mon, 14 Sep 2020 17:34:39 +0200 Subject: [PATCH] Replace \n and \r by \n and \r when saving a string --- src/shared/util.js | 12 ++++++++++-- test/unit/util_spec.js | 6 +++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/shared/util.js b/src/shared/util.js index 500ca3ec491ec..af3be187325ff 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -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) { diff --git a/test/unit/util_spec.js b/test/unit/util_spec.js index 819862f0bb242..752c03ddbbe2e 100644 --- a/test/unit/util_spec.js +++ b/test/unit/util_spec.js @@ -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\\)" ); }); });