Skip to content

Commit a6e3ad4

Browse files
Merge pull request #13283 from Snuffleupagus/NameOrNumberTree-getAll-map
Change `NameOrNumberTree.getAll` to return a `Map` rather than an Object
2 parents 762cfd2 + 4ec0a4f commit a6e3ad4

File tree

2 files changed

+28
-33
lines changed

2 files changed

+28
-33
lines changed

src/core/catalog.js

+20-26
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,8 @@ class Catalog {
514514
const obj = this._readDests(),
515515
dests = Object.create(null);
516516
if (obj instanceof NameTree) {
517-
const names = obj.getAll();
518-
for (const name in names) {
519-
dests[name] = fetchDestination(names[name]);
517+
for (const [key, value] of obj.getAll()) {
518+
dests[key] = fetchDestination(value);
520519
}
521520
} else if (obj instanceof Dict) {
522521
obj.forEach(function (key, value) {
@@ -582,8 +581,9 @@ class Catalog {
582581
currentIndex = 1;
583582

584583
for (let i = 0, ii = this.numPages; i < ii; i++) {
585-
if (i in nums) {
586-
const labelDict = nums[i];
584+
const labelDict = nums.get(i);
585+
586+
if (labelDict !== undefined) {
587587
if (!isDict(labelDict)) {
588588
throw new FormatError("PageLabel is not a dictionary.");
589589
}
@@ -879,34 +879,35 @@ class Catalog {
879879
const obj = this._catDict.get("Names");
880880
let attachments = null;
881881

882-
if (obj && obj.has("EmbeddedFiles")) {
882+
if (obj instanceof Dict && obj.has("EmbeddedFiles")) {
883883
const nameTree = new NameTree(obj.getRaw("EmbeddedFiles"), this.xref);
884-
const names = nameTree.getAll();
885-
for (const name in names) {
886-
const fs = new FileSpec(names[name], this.xref);
884+
for (const [key, value] of nameTree.getAll()) {
885+
const fs = new FileSpec(value, this.xref);
887886
if (!attachments) {
888887
attachments = Object.create(null);
889888
}
890-
attachments[stringToPDFString(name)] = fs.serializable;
889+
attachments[stringToPDFString(key)] = fs.serializable;
891890
}
892891
}
893892
return shadow(this, "attachments", attachments);
894893
}
895894

896895
_collectJavaScript() {
897896
const obj = this._catDict.get("Names");
898-
899897
let javaScript = null;
898+
900899
function appendIfJavaScriptDict(name, jsDict) {
901-
const type = jsDict.get("S");
902-
if (!isName(type, "JavaScript")) {
900+
if (!(jsDict instanceof Dict)) {
901+
return;
902+
}
903+
if (!isName(jsDict.get("S"), "JavaScript")) {
903904
return;
904905
}
905906

906907
let js = jsDict.get("JS");
907908
if (isStream(js)) {
908909
js = bytesToString(js.getBytes());
909-
} else if (!isString(js)) {
910+
} else if (typeof js !== "string") {
910911
return;
911912
}
912913

@@ -916,22 +917,15 @@ class Catalog {
916917
javaScript.set(name, stringToPDFString(js));
917918
}
918919

919-
if (obj && obj.has("JavaScript")) {
920+
if (obj instanceof Dict && obj.has("JavaScript")) {
920921
const nameTree = new NameTree(obj.getRaw("JavaScript"), this.xref);
921-
const names = nameTree.getAll();
922-
for (const name in names) {
923-
// We don't use most JavaScript in PDF documents. This code is
924-
// defensive so we don't cause errors on document load.
925-
const jsDict = names[name];
926-
if (isDict(jsDict)) {
927-
appendIfJavaScriptDict(name, jsDict);
928-
}
922+
for (const [key, value] of nameTree.getAll()) {
923+
appendIfJavaScriptDict(key, value);
929924
}
930925
}
931-
932-
// Append OpenAction "JavaScript" actions to the JavaScript array.
926+
// Append OpenAction "JavaScript" actions, if any, to the JavaScript map.
933927
const openAction = this._catDict.get("OpenAction");
934-
if (isDict(openAction) && isName(openAction.get("S"), "JavaScript")) {
928+
if (openAction) {
935929
appendIfJavaScriptDict("OpenAction", openAction);
936930
}
937931

src/core/name_number_tree.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ class NameOrNumberTree {
3232
}
3333

3434
getAll() {
35-
const dict = Object.create(null);
35+
const map = new Map();
3636
if (!this.root) {
37-
return dict;
37+
return map;
3838
}
3939
const xref = this.xref;
4040
// Reading Name/Number tree.
@@ -59,13 +59,14 @@ class NameOrNumberTree {
5959
continue;
6060
}
6161
const entries = obj.get(this._type);
62-
if (Array.isArray(entries)) {
63-
for (let i = 0, ii = entries.length; i < ii; i += 2) {
64-
dict[xref.fetchIfRef(entries[i])] = xref.fetchIfRef(entries[i + 1]);
65-
}
62+
if (!Array.isArray(entries)) {
63+
continue;
64+
}
65+
for (let i = 0, ii = entries.length; i < ii; i += 2) {
66+
map.set(xref.fetchIfRef(entries[i]), xref.fetchIfRef(entries[i + 1]));
6667
}
6768
}
68-
return dict;
69+
return map;
6970
}
7071

7172
get(key) {

0 commit comments

Comments
 (0)