diff --git a/src/core/annotation.js b/src/core/annotation.js index 6e1bc1147..6307dd36f 100644 --- a/src/core/annotation.js +++ b/src/core/annotation.js @@ -1195,7 +1195,7 @@ class Annotation { firstPosition ||= item.transform.slice(-2); buffer.push(item.str); if (item.hasEOL) { - text.push(buffer.join("")); + text.push(buffer.join("").trimEnd()); buffer.length = 0; } } @@ -1214,7 +1214,7 @@ class Annotation { this.reset(); if (buffer.length) { - text.push(buffer.join("")); + text.push(buffer.join("").trimEnd()); } if (text.length > 1 || text[0]) { @@ -3788,7 +3788,9 @@ class FreeTextAnnotation extends MarkupAnnotation { this.data.defaultAppearanceData.fontSize ||= 10; const { fontColor, fontSize } = this.data.defaultAppearanceData; if (this._contents.str) { - this.data.textContent = this._contents.str.split(/\r\n?|\n/); + this.data.textContent = this._contents.str + .split(/\r\n?|\n/) + .map(line => line.trimEnd()); const { coords, bbox, matrix } = FakeUnicodeFont.getFirstPositionInfo( this.rectangle, this.rotation, diff --git a/test/integration-boot.mjs b/test/integration-boot.mjs index 6d7dad4a5..f50ece885 100644 --- a/test/integration-boot.mjs +++ b/test/integration-boot.mjs @@ -33,6 +33,7 @@ async function runTests(results) { "ink_editor_spec.mjs", "scripting_spec.mjs", "stamp_editor_spec.mjs", + "text_field_spec.mjs", ], }); diff --git a/test/integration/text_field_spec.mjs b/test/integration/text_field_spec.mjs new file mode 100644 index 000000000..aa8fd8725 --- /dev/null +++ b/test/integration/text_field_spec.mjs @@ -0,0 +1,39 @@ +/* Copyright 2024 Mozilla Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { closePages, getSelector, loadAndWait } from "./test_utils.mjs"; + +describe("Text field", () => { + describe("Empty text field", () => { + let pages; + + beforeAll(async () => { + pages = await loadAndWait("file_pdfjs_form.pdf", getSelector("7R")); + }); + + afterAll(async () => { + await closePages(pages); + }); + + it("must check that the field is empty although its appearance contains a white space", async () => { + await Promise.all( + pages.map(async ([browserName, page]) => { + const text = await page.$eval(getSelector("7R"), el => el.value); + expect(text).withContext(`In ${browserName}`).toEqual(""); + }) + ); + }); + }); +}); diff --git a/test/pdfs/.gitignore b/test/pdfs/.gitignore index 1387f44ba..b4c5725b5 100644 --- a/test/pdfs/.gitignore +++ b/test/pdfs/.gitignore @@ -623,3 +623,4 @@ !bug1872721.pdf !bug1871353.pdf !bug1871353.1.pdf +!file_pdfjs_form.pdf diff --git a/test/pdfs/file_pdfjs_form.pdf b/test/pdfs/file_pdfjs_form.pdf new file mode 100755 index 000000000..c6eff988a Binary files /dev/null and b/test/pdfs/file_pdfjs_form.pdf differ