Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change NameOrNumberTree.getAll to return a Map rather than an Object #13283

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 20 additions & 26 deletions src/core/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,9 +514,8 @@ class Catalog {
const obj = this._readDests(),
dests = Object.create(null);
if (obj instanceof NameTree) {
const names = obj.getAll();
for (const name in names) {
dests[name] = fetchDestination(names[name]);
for (const [key, value] of obj.getAll()) {
dests[key] = fetchDestination(value);
}
} else if (obj instanceof Dict) {
obj.forEach(function (key, value) {
Expand Down Expand Up @@ -582,8 +581,9 @@ class Catalog {
currentIndex = 1;

for (let i = 0, ii = this.numPages; i < ii; i++) {
if (i in nums) {
const labelDict = nums[i];
const labelDict = nums.get(i);

if (labelDict !== undefined) {
if (!isDict(labelDict)) {
throw new FormatError("PageLabel is not a dictionary.");
}
Expand Down Expand Up @@ -879,34 +879,35 @@ class Catalog {
const obj = this._catDict.get("Names");
let attachments = null;

if (obj && obj.has("EmbeddedFiles")) {
if (obj instanceof Dict && obj.has("EmbeddedFiles")) {
const nameTree = new NameTree(obj.getRaw("EmbeddedFiles"), this.xref);
const names = nameTree.getAll();
for (const name in names) {
const fs = new FileSpec(names[name], this.xref);
for (const [key, value] of nameTree.getAll()) {
const fs = new FileSpec(value, this.xref);
if (!attachments) {
attachments = Object.create(null);
}
attachments[stringToPDFString(name)] = fs.serializable;
attachments[stringToPDFString(key)] = fs.serializable;
}
}
return shadow(this, "attachments", attachments);
}

_collectJavaScript() {
const obj = this._catDict.get("Names");

let javaScript = null;

function appendIfJavaScriptDict(name, jsDict) {
const type = jsDict.get("S");
if (!isName(type, "JavaScript")) {
if (!(jsDict instanceof Dict)) {
return;
}
if (!isName(jsDict.get("S"), "JavaScript")) {
return;
}

let js = jsDict.get("JS");
if (isStream(js)) {
js = bytesToString(js.getBytes());
} else if (!isString(js)) {
} else if (typeof js !== "string") {
return;
}

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

if (obj && obj.has("JavaScript")) {
if (obj instanceof Dict && obj.has("JavaScript")) {
const nameTree = new NameTree(obj.getRaw("JavaScript"), this.xref);
const names = nameTree.getAll();
for (const name in names) {
// We don't use most JavaScript in PDF documents. This code is
// defensive so we don't cause errors on document load.
const jsDict = names[name];
if (isDict(jsDict)) {
appendIfJavaScriptDict(name, jsDict);
}
for (const [key, value] of nameTree.getAll()) {
appendIfJavaScriptDict(key, value);
}
}

// Append OpenAction "JavaScript" actions to the JavaScript array.
// Append OpenAction "JavaScript" actions, if any, to the JavaScript map.
const openAction = this._catDict.get("OpenAction");
if (isDict(openAction) && isName(openAction.get("S"), "JavaScript")) {
if (openAction) {
appendIfJavaScriptDict("OpenAction", openAction);
}

Expand Down
15 changes: 8 additions & 7 deletions src/core/name_number_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class NameOrNumberTree {
}

getAll() {
const dict = Object.create(null);
const map = new Map();
if (!this.root) {
return dict;
return map;
}
const xref = this.xref;
// Reading Name/Number tree.
Expand All @@ -59,13 +59,14 @@ class NameOrNumberTree {
continue;
}
const entries = obj.get(this._type);
if (Array.isArray(entries)) {
for (let i = 0, ii = entries.length; i < ii; i += 2) {
dict[xref.fetchIfRef(entries[i])] = xref.fetchIfRef(entries[i + 1]);
}
if (!Array.isArray(entries)) {
continue;
}
for (let i = 0, ii = entries.length; i < ii; i += 2) {
map.set(xref.fetchIfRef(entries[i]), xref.fetchIfRef(entries[i + 1]));
}
}
return dict;
return map;
}

get(key) {
Expand Down