Skip to content

Commit

Permalink
Merge pull request #14268 from calixteman/outline
Browse files Browse the repository at this point in the history
Remove non-displayable chars from outline title (#14267)
  • Loading branch information
calixteman authored Nov 13, 2021
2 parents db41c49 + 7041c62 commit 85c6dd5
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,16 +566,20 @@ class AbortException extends BaseException {
}
}

const NullCharactersRegExp = /\x00/g;
const NullCharactersRegExp = /\x00+/g;
const InvisibleCharactersRegExp = /[\x01-\x1F]/g;

/**
* @param {string} str
*/
function removeNullCharacters(str) {
function removeNullCharacters(str, replaceInvisible = false) {
if (typeof str !== "string") {
warn("The argument for removeNullCharacters must be a string.");
return str;
}
if (replaceInvisible) {
str = str.replace(InvisibleCharactersRegExp, " ");
}
return str.replace(NullCharactersRegExp, "");
}

Expand Down
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,4 @@
!pr12828.pdf
!secHandler.pdf
!rc_annotation.pdf
!issue14267.pdf
Binary file added test/pdfs/issue14267.pdf
Binary file not shown.
13 changes: 13 additions & 0 deletions test/unit/api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,19 @@ describe("api", function () {
await loadingTask.destroy();
});

it("gets outline with non-displayable chars", async function () {
const loadingTask = getDocument(buildGetDocumentParams("issue14267.pdf"));
const pdfDoc = await loadingTask.promise;
const outline = await pdfDoc.getOutline();
expect(Array.isArray(outline)).toEqual(true);
expect(outline.length).toEqual(1);

const outlineItem = outline[0];
expect(outlineItem.title).toEqual("hello\x11world");

await loadingTask.destroy();
});

it("gets non-existent permissions", async function () {
const permissions = await pdfDocument.getPermissions();
expect(permissions).toEqual(null);
Expand Down
12 changes: 12 additions & 0 deletions test/unit/util_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ describe("util", function () {
const str = "string\x00With\x00Null\x00Chars";
expect(removeNullCharacters(str)).toEqual("stringWithNullChars");
});

it("should modify string with non-displayable characters", function () {
const str = Array.from(Array(32).keys())
.map(x => String.fromCharCode(x) + "a")
.join("");
// \x00 is replaced by an empty string.
const expected =
"a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a";
expect(removeNullCharacters(str, /* replaceInvisible */ true)).toEqual(
expected
);
});
});

describe("ReadableStream", function () {
Expand Down
7 changes: 6 additions & 1 deletion web/base_tree_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ class BaseTreeViewer {
* @private
*/
_normalizeTextContent(str) {
return removeNullCharacters(str) || /* en dash = */ "\u2013";
// Chars in range [0x01-0x1F] will be replaced with a white space
// and 0x00 by "".
return (
removeNullCharacters(str, /* replaceInvisible */ true) ||
/* en dash = */ "\u2013"
);
}

/**
Expand Down

0 comments on commit 85c6dd5

Please sign in to comment.