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

Slightly simplify the lookup of data in Dict.{get, getAsync, has} #11672

Merged
merged 1 commit into from
Mar 6, 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
2 changes: 1 addition & 1 deletion src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3017,7 +3017,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
// is a tagged pdf. Create a barbebones one to get by.
descriptor = new Dict(null);
descriptor.set("FontName", Name.get(type));
descriptor.set("FontBBox", dict.getArray("FontBBox"));
descriptor.set("FontBBox", dict.getArray("FontBBox") || [0, 0, 0, 0]);
} else {
// Before PDF 1.5 if the font was one of the base 14 fonts, having a
// FontDescriptor was not required.
Expand Down
19 changes: 13 additions & 6 deletions src/core/primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
/* uses XRef */

import { assert } from "../shared/util.js";
import { assert, unreachable } from "../shared/util.js";

var EOF = {};

Expand Down Expand Up @@ -85,9 +85,9 @@ var Dict = (function DictClosure() {
// automatically dereferences Ref objects
get(key1, key2, key3) {
let value = this._map[key1];
if (value === undefined && !(key1 in this._map) && key2 !== undefined) {
if (value === undefined && key2 !== undefined) {
value = this._map[key2];
if (value === undefined && !(key2 in this._map) && key3 !== undefined) {
if (value === undefined && key3 !== undefined) {
value = this._map[key3];
}
}
Expand All @@ -100,9 +100,9 @@ var Dict = (function DictClosure() {
// Same as get(), but returns a promise and uses fetchIfRefAsync().
async getAsync(key1, key2, key3) {
let value = this._map[key1];
if (value === undefined && !(key1 in this._map) && key2 !== undefined) {
if (value === undefined && key2 !== undefined) {
value = this._map[key2];
if (value === undefined && !(key2 in this._map) && key3 !== undefined) {
if (value === undefined && key3 !== undefined) {
value = this._map[key3];
}
}
Expand Down Expand Up @@ -138,11 +138,18 @@ var Dict = (function DictClosure() {
},

set: function Dict_set(key, value) {
if (
(typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || TESTING")) &&
value === undefined
) {
unreachable('Dict.set: The "value" cannot be undefined.');
}
this._map[key] = value;
},

has: function Dict_has(key) {
return key in this._map;
return this._map[key] !== undefined;
},

forEach: function Dict_forEach(callback) {
Expand Down
10 changes: 6 additions & 4 deletions test/unit/primitives_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,13 @@ describe("primitives", function() {
checkInvalidKeyValues(dictWithSizeKey);
});

it("should return correct value for stored Size key with undefined value", function() {
var dict = new Dict();
dict.set("Size");
it("should not accept to set a key with an undefined value", function() {
const dict = new Dict();
expect(function() {
dict.set("Size");
}).toThrow(new Error('Dict.set: The "value" cannot be undefined.'));

expect(dict.has("Size")).toBeTruthy();
expect(dict.has("Size")).toBeFalsy();

checkInvalidKeyValues(dict);
});
Expand Down