Skip to content

Commit

Permalink
Merge pull request #17487 from calixteman/fix_final_spaces
Browse files Browse the repository at this point in the history
Remove terminal white spaces when extracting text from annotation appearances
  • Loading branch information
calixteman authored Jan 10, 2024
2 parents 29faa38 + 0392fea commit 903af4e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
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.

0 comments on commit 903af4e

Please sign in to comment.