diff --git a/src/core/annotation.js b/src/core/annotation.js index 06efc2c2a..790c0936f 100644 --- a/src/core/annotation.js +++ b/src/core/annotation.js @@ -999,6 +999,9 @@ class Annotation { enqueue(chunk, size) { for (const item of chunk.items) { + if (item.str === undefined) { + continue; + } buffer.push(item.str); if (item.hasEOL) { text.push(buffer.join("")); @@ -1022,7 +1025,7 @@ class Annotation { text.push(buffer.join("")); } - if (text.length > 0) { + if (text.length > 1 || text[0]) { this.data.textContent = text; } } diff --git a/src/display/editor/freetext.js b/src/display/editor/freetext.js index f7e7c4fbf..9de780658 100644 --- a/src/display/editor/freetext.js +++ b/src/display/editor/freetext.js @@ -541,6 +541,8 @@ class FreeTextEditor extends AnnotationEditor { page: { pageNumber }, }, } = data; + // textContent is supposed to be an array of strings containing each line + // of text. However, it can be null or empty. if (!textContent || textContent.length === 0) { // Empty annotation. return null; diff --git a/test/integration/freetext_editor_spec.js b/test/integration/freetext_editor_spec.js index 2172cfd4c..6bfeb4b20 100644 --- a/test/integration/freetext_editor_spec.js +++ b/test/integration/freetext_editor_spec.js @@ -1116,4 +1116,42 @@ describe("FreeText Editor", () => { } }); }); + + describe("FreeText with popup", () => { + let pages; + + beforeAll(async () => { + pages = await loadAndWait( + "annotation-freetext.pdf", + ".annotationEditorLayer" + ); + }); + + afterAll(async () => { + await closePages(pages); + }); + + it("must not remove an empty annotation", async () => { + await Promise.all( + pages.map(async ([browserName, page]) => { + await page.hover("[data-annotation-id='23R']"); + // Wait for the popup to be displayed. + await page.waitForFunction( + `document.querySelector("[data-annotation-id='popup_23R']").hidden === false` + ); + + // Enter in editing mode. + await page.click("#editorFreeText"); + await page.waitForTimeout(10); + await page.click("#editorFreeText"); + + await page.hover("[data-annotation-id='23R']"); + // Wait for the popup to be displayed. + await page.waitForFunction( + `document.querySelector("[data-annotation-id='popup_23R']").hidden === false` + ); + }) + ); + }); + }); });