Skip to content

Commit

Permalink
[Editor] Extract all the lines when adding a FreeText annotation
Browse files Browse the repository at this point in the history
Fixes #17079.
  • Loading branch information
calixteman committed Jan 14, 2024
1 parent 0d01147 commit 9765d57
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/display/editor/freetext.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,13 +386,14 @@ class FreeTextEditor extends AnnotationEditor {
* @returns {string}
*/
#extractText() {
const divs = this.editorDiv.getElementsByTagName("div");
if (divs.length === 0) {
return this.editorDiv.innerText;
}
// We don't use innerText because there are some bugs with line breaks.
const buffer = [];
for (const div of divs) {
buffer.push(div.innerText.replace(/\r\n?|\n/, ""));
this.editorDiv.normalize();
const EOL_PATTERN = /\r\n?|\n/g;
for (const child of this.editorDiv.childNodes) {
const content =
child.nodeType === Node.TEXT_NODE ? child.nodeValue : child.innerText;
buffer.push(content.replaceAll(EOL_PATTERN, ""));
}
return buffer.join("\n");
}
Expand Down
46 changes: 46 additions & 0 deletions test/integration/freetext_editor_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3246,4 +3246,50 @@ describe("FreeText Editor", () => {
);
});
});

describe("Freetext with several lines", () => {
let pages;

beforeAll(async () => {
pages = await loadAndWait("empty.pdf", ".annotationEditorLayer");
});

afterAll(async () => {
await closePages(pages);
});

it("must check that all lines are correctly exported", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
await switchToFreeText(page);

const rect = await page.$eval(".annotationEditorLayer", el => {
// With Chrome something is wrong when serializing a DomRect,
// hence we extract the values and just return them.
const { x, y } = el.getBoundingClientRect();
return { x, y };
});

const data = "Hello\nPDF.js\nWorld\n!!";
await page.mouse.click(rect.x + 100, rect.y + 100);
await page.waitForSelector(getEditorSelector(0), {
visible: true,
});
await page.type(`${getEditorSelector(0)} .internal`, data);

// Commit.
await page.keyboard.press("Escape");
await page.waitForSelector(
`${getEditorSelector(0)} .overlay.enabled`
);

await waitForSerialized(page, 1);
const serialized = (await getSerialized(page))[0];
expect(serialized.value)
.withContext(`In ${browserName}`)
.toEqual(data);
})
);
});
});
});

0 comments on commit 9765d57

Please sign in to comment.