mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-19 22:58:07 +02:00
Merge pull request #18740 from calixteman/bug1897909
[Editor] Avoid an extra new line when serializing a FreeText annotation (bug 1897909)
This commit is contained in:
commit
4275424456
2 changed files with 51 additions and 0 deletions
|
@ -403,8 +403,16 @@ class FreeTextEditor extends AnnotationEditor {
|
|||
// We don't use innerText because there are some bugs with line breaks.
|
||||
const buffer = [];
|
||||
this.editorDiv.normalize();
|
||||
let prevChild = null;
|
||||
for (const child of this.editorDiv.childNodes) {
|
||||
if (prevChild?.nodeType === Node.TEXT_NODE && child.nodeName === "BR") {
|
||||
// It can happen if the user uses shift+enter to add a new line.
|
||||
// If we don't skip it, we'll end up with an extra line (one for the
|
||||
// text and one for the br element).
|
||||
continue;
|
||||
}
|
||||
buffer.push(FreeTextEditor.#getNodeContent(child));
|
||||
prevChild = child;
|
||||
}
|
||||
return buffer.join("\n");
|
||||
}
|
||||
|
|
|
@ -3644,4 +3644,47 @@ describe("FreeText Editor", () => {
|
|||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Freetext and shift+enter", () => {
|
||||
let pages;
|
||||
|
||||
beforeAll(async () => {
|
||||
pages = await loadAndWait("empty.pdf", ".annotationEditorLayer");
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await closePages(pages);
|
||||
});
|
||||
|
||||
it("must check that a freetext has the correct data", async () => {
|
||||
await Promise.all(
|
||||
pages.map(async ([browserName, page]) => {
|
||||
await switchToFreeText(page);
|
||||
|
||||
const rect = await getRect(page, ".annotationEditorLayer");
|
||||
const editorSelector = getEditorSelector(0);
|
||||
|
||||
const data = "Hello\nPDF.js\nWorld\n!!";
|
||||
await page.mouse.click(rect.x + 100, rect.y + 100);
|
||||
await page.waitForSelector(editorSelector, {
|
||||
visible: true,
|
||||
});
|
||||
for (const line of data.split("\n")) {
|
||||
await page.type(`${editorSelector} .internal`, line);
|
||||
await page.keyboard.down("Shift");
|
||||
await page.keyboard.press("Enter");
|
||||
await page.keyboard.up("Shift");
|
||||
}
|
||||
|
||||
// Commit.
|
||||
await page.keyboard.press("Escape");
|
||||
await page.waitForSelector(`${editorSelector} .overlay.enabled`);
|
||||
await waitForSerialized(page, 1);
|
||||
|
||||
const serialized = await getSerialized(page, x => x.value);
|
||||
expect(serialized[0]).withContext(`In ${browserName}`).toEqual(data);
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue