Skip to content

[Editor] Avoid an extra new line when serializing a FreeText annotation (bug 1897909) #18740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/display/editor/freetext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
43 changes: 43 additions & 0 deletions test/integration/freetext_editor_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
);
});
});
});