Skip to content
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

Remove terminal white spaces when extracting text from annotation appearances #17487

Merged
merged 1 commit into from
Jan 10, 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: 5 additions & 3 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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]) {
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions test/integration-boot.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ async function runTests(results) {
"ink_editor_spec.mjs",
"scripting_spec.mjs",
"stamp_editor_spec.mjs",
"text_field_spec.mjs",
],
});

Expand Down
39 changes: 39 additions & 0 deletions test/integration/text_field_spec.mjs
Original file line number Diff line number Diff line change
@@ -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("");
})
);
});
});
});
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -623,3 +623,4 @@
!bug1872721.pdf
!bug1871353.pdf
!bug1871353.1.pdf
!file_pdfjs_form.pdf
Binary file added test/pdfs/file_pdfjs_form.pdf
Binary file not shown.