Replace \n and \r by \n and \r when saving a string
This commit is contained in:
parent
741ce4f7fc
commit
0c8de5aaf9
@ -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) {
|
||||
|
@ -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\\)"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user