From 18b525de2e2fd7d272821a6fc4e5d99f159e7b4e Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Tue, 24 Nov 2020 18:25:26 +0100 Subject: [PATCH] Parenthesis in names are not escaped when saving --- src/core/core_utils.js | 17 ++++++++++++++++- test/unit/core_utils_spec.js | 3 +++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/core/core_utils.js b/src/core/core_utils.js index 932f75576..609ef9f6e 100644 --- a/src/core/core_utils.js +++ b/src/core/core_utils.js @@ -205,7 +205,22 @@ function escapePDFName(str) { let start = 0; for (let i = 0, ii = str.length; i < ii; i++) { const char = str.charCodeAt(i); - if (char < 0x21 || char > 0x7e || char === 0x23) { + // Whitespace or delimiters aren't regular chars, so escape them. + if ( + char < 0x21 || + char > 0x7e || + char === 0x23 /* # */ || + char === 0x28 /* ( */ || + char === 0x29 /* ) */ || + char === 0x3c /* < */ || + char === 0x3e /* > */ || + char === 0x5b /* [ */ || + char === 0x5d /* ] */ || + char === 0x7b /* { */ || + char === 0x7d /* } */ || + char === 0x2f /* / */ || + char === 0x25 /* % */ + ) { if (start < i) { buffer.push(str.substring(start, i)); } diff --git a/test/unit/core_utils_spec.js b/test/unit/core_utils_spec.js index 74c99558d..a9d813cb3 100644 --- a/test/unit/core_utils_spec.js +++ b/test/unit/core_utils_spec.js @@ -237,6 +237,9 @@ describe("core_utils", function () { "#fehe#fell#ffo#ff" ); expect(escapePDFName("#h#e#l#l#o")).toEqual("#23h#23e#23l#23l#23o"); + expect(escapePDFName("#()<>[]{}/%")).toEqual( + "#23#28#29#3c#3e#5b#5d#7b#7d#2f#25" + ); }); }); });