Move the escapeString helper function into the worker-thread

Given that this helper function is only used on the worker-thread, there's no reason to duplicate it in both of the `pdf.js` and `pdf.worker.js` files.
This commit is contained in:
Jonas Jenwald 2022-11-16 12:18:13 +01:00
parent e5859e145d
commit 9adc7859c8
6 changed files with 31 additions and 27 deletions

View File

@ -23,7 +23,6 @@ import {
AnnotationType, AnnotationType,
assert, assert,
BASELINE_FACTOR, BASELINE_FACTOR,
escapeString,
FeatureTest, FeatureTest,
getModificationDate, getModificationDate,
IDENTITY_MATRIX, IDENTITY_MATRIX,
@ -39,6 +38,7 @@ import {
} from "../shared/util.js"; } from "../shared/util.js";
import { import {
collectActions, collectActions,
escapeString,
getInheritableProperty, getInheritableProperty,
getRotationMatrix, getRotationMatrix,
isAscii, isAscii,

View File

@ -313,6 +313,19 @@ function escapePDFName(str) {
return buffer.join(""); return buffer.join("");
} }
// Replace "(", ")", "\n", "\r" and "\" by "\(", "\)", "\\n", "\\r" and "\\"
// in order to write it in a PDF file.
function escapeString(str) {
return str.replace(/([()\\\n\r])/g, match => {
if (match === "\n") {
return "\\n";
} else if (match === "\r") {
return "\\r";
}
return `\\${match}`;
});
}
function _collectJS(entry, xref, list, parents) { function _collectJS(entry, xref, list, parents) {
if (!entry) { if (!entry) {
return; return;
@ -621,6 +634,7 @@ export {
DocStats, DocStats,
encodeToXmlString, encodeToXmlString,
escapePDFName, escapePDFName,
escapeString,
getArrayLookupTableFactory, getArrayLookupTableFactory,
getInheritableProperty, getInheritableProperty,
getLookupTableFactory, getLookupTableFactory,

View File

@ -13,9 +13,14 @@
* limitations under the License. * limitations under the License.
*/ */
import { bytesToString, escapeString, warn } from "../shared/util.js"; import { bytesToString, warn } from "../shared/util.js";
import { Dict, Name, Ref } from "./primitives.js"; import { Dict, Name, Ref } from "./primitives.js";
import { escapePDFName, numberToString, parseXFAPath } from "./core_utils.js"; import {
escapePDFName,
escapeString,
numberToString,
parseXFAPath,
} from "./core_utils.js";
import { SimpleDOMNode, SimpleXMLParser } from "./xml_parser.js"; import { SimpleDOMNode, SimpleXMLParser } from "./xml_parser.js";
import { BaseStream } from "./base_stream.js"; import { BaseStream } from "./base_stream.js";
import { calculateMD5 } from "./crypto.js"; import { calculateMD5 } from "./crypto.js";

View File

@ -1037,20 +1037,6 @@ function stringToPDFString(str) {
return strBuf.join(""); return strBuf.join("");
} }
function escapeString(str) {
// replace "(", ")", "\n", "\r" and "\"
// by "\(", "\)", "\\n", "\\r" and "\\"
// in order to write it in a PDF file.
return str.replace(/([()\\\n\r])/g, match => {
if (match === "\n") {
return "\\n";
} else if (match === "\r") {
return "\\r";
}
return `\\${match}`;
});
}
function stringToUTF8String(str) { function stringToUTF8String(str) {
return decodeURIComponent(escape(str)); return decodeURIComponent(escape(str));
} }
@ -1151,7 +1137,6 @@ export {
createPromiseCapability, createPromiseCapability,
createValidAbsoluteUrl, createValidAbsoluteUrl,
DocumentActionEventType, DocumentActionEventType,
escapeString,
FeatureTest, FeatureTest,
FONT_IDENTITY_MATRIX, FONT_IDENTITY_MATRIX,
FontType, FontType,

View File

@ -17,6 +17,7 @@ import { Dict, Ref } from "../../src/core/primitives.js";
import { import {
encodeToXmlString, encodeToXmlString,
escapePDFName, escapePDFName,
escapeString,
getInheritableProperty, getInheritableProperty,
isAscii, isAscii,
isWhiteSpace, isWhiteSpace,
@ -223,6 +224,14 @@ describe("core_utils", function () {
}); });
}); });
describe("escapeString", function () {
it("should escape (, ), \\n, \\r, and \\", function () {
expect(escapeString("((a\\a))\n(b(b\\b)\rb)")).toEqual(
"\\(\\(a\\\\a\\)\\)\\n\\(b\\(b\\\\b\\)\\rb\\)"
);
});
});
describe("encodeToXmlString", function () { describe("encodeToXmlString", function () {
it("should get a correctly encoded string with some entities", function () { it("should get a correctly encoded string with some entities", function () {
const str = "\"\u0397ell😂' & <W😂rld>"; const str = "\"\u0397ell😂' & <W😂rld>";

View File

@ -17,7 +17,6 @@ import {
bytesToString, bytesToString,
createPromiseCapability, createPromiseCapability,
createValidAbsoluteUrl, createValidAbsoluteUrl,
escapeString,
getModificationDate, getModificationDate,
isArrayBuffer, isArrayBuffer,
string32, string32,
@ -244,14 +243,6 @@ describe("util", function () {
}); });
}); });
describe("escapeString", function () {
it("should escape (, ), \\n, \\r, and \\", function () {
expect(escapeString("((a\\a))\n(b(b\\b)\rb)")).toEqual(
"\\(\\(a\\\\a\\)\\)\\n\\(b\\(b\\\\b\\)\\rb\\)"
);
});
});
describe("getModificationDate", function () { describe("getModificationDate", function () {
it("should get a correctly formatted date", function () { it("should get a correctly formatted date", function () {
const date = new Date(Date.UTC(3141, 5, 9, 2, 6, 53)); const date = new Date(Date.UTC(3141, 5, 9, 2, 6, 53));