Replace the forEach method in Dict with "proper" iteration support

This commit is contained in:
Jonas Jenwald 2024-11-16 11:51:09 +01:00
parent 691be77f65
commit 2c0cc48d1b
6 changed files with 19 additions and 30 deletions

View File

@ -720,12 +720,12 @@ class Catalog {
} }
} }
} else if (obj instanceof Dict) { } else if (obj instanceof Dict) {
obj.forEach(function (key, value) { for (const [key, value] of obj) {
const dest = fetchDest(value); const dest = fetchDest(value);
if (dest) { if (dest) {
dests[key] = dest; dests[key] = dest;
} }
}); }
} }
return shadow(this, "destinations", dests); return shadow(this, "destinations", dests);
} }

View File

@ -1282,13 +1282,8 @@ class PDFDocument {
}, },
}; };
const fonts = new Map();
fontRes.forEach((fontName, font) => {
fonts.set(fontName, font);
});
const promises = []; const promises = [];
for (const [fontName, font] of fontRes) {
for (const [fontName, font] of fonts) {
const descriptor = font.get("FontDescriptor"); const descriptor = font.get("FontDescriptor");
if (!(descriptor instanceof Dict)) { if (!(descriptor instanceof Dict)) {
continue; continue;

View File

@ -203,14 +203,14 @@ class Dict {
return this._map.has(key); return this._map.has(key);
} }
forEach(callback) { *[Symbol.iterator]() {
for (const [key, value] of this._map) { for (const [key, value] of this._map) {
callback( yield [
key, key,
value instanceof Ref && this.xref value instanceof Ref && this.xref
? this.xref.fetch(value, this.suppressEncryption) ? this.xref.fetch(value, this.suppressEncryption)
: value : value,
); ];
} }
} }

View File

@ -62,12 +62,11 @@ class StructTreeRoot {
if (!(roleMapDict instanceof Dict)) { if (!(roleMapDict instanceof Dict)) {
return; return;
} }
roleMapDict.forEach((key, value) => { for (const [key, value] of roleMapDict) {
if (!(value instanceof Name)) { if (value instanceof Name) {
return; this.roleMap.set(key, value.name);
} }
this.roleMap.set(key, value.name); }
});
} }
static async canCreateStructureTree({ static async canCreateStructureTree({

View File

@ -688,11 +688,11 @@ class WorkerMessageHandler {
const infoObj = Object.create(null); const infoObj = Object.create(null);
const xrefInfo = xref.trailer.get("Info") || null; const xrefInfo = xref.trailer.get("Info") || null;
if (xrefInfo instanceof Dict) { if (xrefInfo instanceof Dict) {
xrefInfo.forEach((key, value) => { for (const [key, value] of xrefInfo) {
if (typeof value === "string") { if (typeof value === "string") {
infoObj[key] = stringToPDFString(value); infoObj[key] = stringToPDFString(value);
} }
}); }
} }
newXrefInfo = { newXrefInfo = {

View File

@ -221,17 +221,12 @@ describe("primitives", function () {
expect(values[2]).toEqual(testFontFile); expect(values[2]).toEqual(testFontFile);
}); });
it("should callback for each stored key", function () { it("should iterate through each stored key", function () {
const callbackSpy = jasmine.createSpy("spy on callback in dictionary"); expect([...dictWithManyKeys]).toEqual([
["FontFile", testFontFile],
dictWithManyKeys.forEach(callbackSpy); ["FontFile2", testFontFile2],
["FontFile3", testFontFile3],
expect(callbackSpy).toHaveBeenCalled(); ]);
const callbackSpyCalls = callbackSpy.calls;
expect(callbackSpyCalls.argsFor(0)).toEqual(["FontFile", testFontFile]);
expect(callbackSpyCalls.argsFor(1)).toEqual(["FontFile2", testFontFile2]);
expect(callbackSpyCalls.argsFor(2)).toEqual(["FontFile3", testFontFile3]);
expect(callbackSpyCalls.count()).toEqual(3);
}); });
it("should handle keys pointing to indirect objects, both sync and async", async function () { it("should handle keys pointing to indirect objects, both sync and async", async function () {