From 3ee5268a239e5056f706f67106163bc3dcb681ef Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Tue, 26 Sep 2023 11:02:14 +0200 Subject: [PATCH] [Editor] Don't try to add data to the struct tree when there is no accessibilityData (bug 1855157) --- src/core/struct_tree.js | 7 ++++- test/unit/api_spec.js | 70 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/src/core/struct_tree.js b/src/core/struct_tree.js index d036927cb..8be0950f3 100644 --- a/src/core/struct_tree.js +++ b/src/core/struct_tree.js @@ -313,11 +313,16 @@ class StructTreeRoot { for (const [pageIndex, elements] of newAnnotationsByPage) { const { ref: pageRef } = await pdfManager.getPage(pageIndex); for (const { - accessibilityData: { type, title, lang, alt, expanded, actualText }, + accessibilityData, ref, parentTreeId, structTreeParent, } of elements) { + if (!accessibilityData?.type) { + continue; + } + const { type, title, lang, alt, expanded, actualText } = + accessibilityData; nextKey = Math.max(nextKey, parentTreeId); const tagRef = xref.getNewTemporaryRef(); diff --git a/test/unit/api_spec.js b/test/unit/api_spec.js index 4f3334cf5..81a86aff0 100644 --- a/test/unit/api_spec.js +++ b/test/unit/api_spec.js @@ -2405,6 +2405,76 @@ describe("api", function () { await loadingTask.destroy(); }); + it("write a text and a stamp annotation but no alt text (bug 1855157)", async function () { + if (isNodeJS) { + pending("Cannot create a bitmap from Node.js."); + } + + const TEST_IMAGES_PATH = "../images/"; + const filename = "firefox_logo.png"; + const path = new URL(TEST_IMAGES_PATH + filename, window.location).href; + + const response = await fetch(path); + const blob = await response.blob(); + const bitmap = await createImageBitmap(blob); + + let loadingTask = getDocument(buildGetDocumentParams("empty.pdf")); + let pdfDoc = await loadingTask.promise; + pdfDoc.annotationStorage.setValue("pdfjs_internal_editor_0", { + annotationType: AnnotationEditorType.STAMP, + rect: [128, 400, 148, 420], + rotation: 0, + bitmap, + bitmapId: "im1", + pageIndex: 0, + structTreeParentId: null, + accessibilityData: { + type: "Figure", + alt: "Hello World", + }, + }); + pdfDoc.annotationStorage.setValue("pdfjs_internal_editor_1", { + annotationType: AnnotationEditorType.FREETEXT, + color: [0, 0, 0], + fontSize: 10, + value: "Hello World", + pageIndex: 0, + rect: [ + 133.2444863336475, 653.5583423367227, 191.03166882427766, + 673.363146394756, + ], + rotation: 0, + structTreeParentId: null, + id: null, + }); + + const data = await pdfDoc.saveDocument(); + await loadingTask.destroy(); + + loadingTask = getDocument(data); + pdfDoc = await loadingTask.promise; + const page = await pdfDoc.getPage(1); + const tree = await page.getStructTree(); + + expect(tree).toEqual({ + children: [ + { + role: "Figure", + children: [ + { + type: "annotation", + id: "pdfjs_internal_id_18R", + }, + ], + alt: "Hello World", + }, + ], + role: "Root", + }); + + await loadingTask.destroy(); + }); + describe("Cross-origin", function () { let loadingTask; function _checkCanLoad(expectSuccess, filename, options) {