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

Use a Map, rather than an Object, internally in the Catalog.openAction getter (PR 11644 follow-up) #12543

Merged
merged 2 commits into from
Oct 28, 2020
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
24 changes: 10 additions & 14 deletions src/core/obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
isBool,
isNum,
isString,
objectFromEntries,
PermissionFlag,
shadow,
stringToPDFString,
Expand Down Expand Up @@ -786,7 +787,7 @@ class Catalog {
*/
get openAction() {
const obj = this._catDict.get("OpenAction");
let openAction = null;
const openActionMap = new Map();

if (isDict(obj)) {
// Convert the OpenAction dictionary into a format that works with
Expand All @@ -798,23 +799,18 @@ class Catalog {
Catalog.parseDestDictionary({ destDict, resultObj });

if (Array.isArray(resultObj.dest)) {
if (!openAction) {
openAction = Object.create(null);
}
openAction.dest = resultObj.dest;
openActionMap.set("dest", resultObj.dest);
} else if (resultObj.action) {
if (!openAction) {
openAction = Object.create(null);
}
openAction.action = resultObj.action;
openActionMap.set("action", resultObj.action);
}
} else if (Array.isArray(obj)) {
if (!openAction) {
openAction = Object.create(null);
}
openAction.dest = obj;
openActionMap.set("dest", obj);
}
return shadow(this, "openAction", openAction);
return shadow(
this,
"openAction",
openActionMap.size > 0 ? objectFromEntries(openActionMap) : null
);
}

get attachments() {
Expand Down
4 changes: 3 additions & 1 deletion src/display/annotation_storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* limitations under the License.
*/

import { objectFromEntries } from "../shared/util.js";

/**
* Key/value storage for annotation data in forms.
*/
Expand Down Expand Up @@ -67,7 +69,7 @@ class AnnotationStorage {
if (this._storage.size === 0) {
return null;
}
return Object.fromEntries(this._storage);
return objectFromEntries(this._storage);
}

get size() {
Expand Down
4 changes: 2 additions & 2 deletions src/display/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* limitations under the License.
*/

import { assert } from "../shared/util.js";
import { assert, objectFromEntries } from "../shared/util.js";
import { SimpleXMLParser } from "../shared/xml_parser.js";

class Metadata {
Expand Down Expand Up @@ -118,7 +118,7 @@ class Metadata {
}

getAll() {
return Object.fromEntries(this._metadataMap);
return objectFromEntries(this._metadataMap);
}

has(name) {
Expand Down
4 changes: 2 additions & 2 deletions src/display/optional_content_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { warn } from "../shared/util.js";
import { objectFromEntries, warn } from "../shared/util.js";

class OptionalContentGroup {
constructor(name, intent) {
Expand Down Expand Up @@ -145,7 +145,7 @@ class OptionalContentConfig {
if (!this._groups.size) {
return null;
}
return Object.fromEntries(this._groups);
return objectFromEntries(this._groups);
}

getGroup(id) {
Expand Down
6 changes: 6 additions & 0 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,11 @@ function string32(value) {
);
}

// Ensures that the returned Object has a `null` prototype.
function objectFromEntries(iterable) {
return Object.assign(Object.create(null), Object.fromEntries(iterable));
}

// Checks the endianness of the platform.
function isLittleEndian() {
const buffer8 = new Uint8Array(4);
Expand Down Expand Up @@ -1035,6 +1040,7 @@ export {
isString,
isSameOrigin,
createValidAbsoluteUrl,
objectFromEntries,
IsLittleEndianCached,
IsEvalSupportedCached,
removeNullCharacters,
Expand Down