Skip to content

Commit

Permalink
Keep the empty lines in the text fields
Browse files Browse the repository at this point in the history
It fixes #18036.
  • Loading branch information
calixteman committed Oct 5, 2024
1 parent 6aef332 commit 7dda9c7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
26 changes: 25 additions & 1 deletion src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,7 @@ class Annotation {
bbox,
matrix
);

this.data.textContent = text;
}
}
Expand Down Expand Up @@ -2299,7 +2300,6 @@ class WidgetAnnotation extends Annotation {
}

assert(typeof value === "string", "Expected `value` to be a string.");
value = value.trim();

if (this.data.combo) {
// The value can be one of the exportValue or any other values.
Expand Down Expand Up @@ -2934,6 +2934,30 @@ class TextWidgetAnnotation extends WidgetAnnotation {
return chunks;
}

async extractTextContent(evaluator, task, viewBox) {
await super.extractTextContent(evaluator, task, viewBox);
const text = this.data.textContent;
if (!text) {
return;
}

// The text extractor doesn't handle empty lines correctly, so if the
// content we get is more or less (modulo whitespaces) the same as the
// field value we just ignore it.
const allText = text.join("\n");
if (allText === this.data.fieldValue) {
return;
}
const regex = allText.replaceAll(/([.*+?^${}()|[\]\\])|(\s+)/g, (_m, p1) =>
p1 ? `\\${p1}` : "\\s+"
);
this.data.textContent = new RegExp(`^\\s*${regex}\\s*$`).test(
this.data.fieldValue
)
? this.data.fieldValue.split("\n")
: text;
}

getFieldObject() {
return {
id: this.data.id,
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -674,3 +674,4 @@
!issue18072.pdf
!stamps.pdf
!issue15096.pdf
!issue18036.pdf
Binary file added test/pdfs/issue18036.pdf
Binary file not shown.
14 changes: 11 additions & 3 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8477,13 +8477,13 @@
"value": "Hello World"
},
"33R": {
"value": "Hello World\nDlrow Olleh\nHello World"
"value": "\n\nHello World\nDlrow Olleh\nHello World"
},
"36R": {
"value": "Hello World\nDlrow Olleh\nHello World"
"value": "\n\nHello World\nDlrow Olleh\nHello World"
},
"39R": {
"value": "Hello World\nDlrow Olleh\nHello World"
"value": "\n\nHello World\nDlrow Olleh\nHello World"
}
}
},
Expand Down Expand Up @@ -10684,5 +10684,13 @@
"value": false
}
}
},
{
"id": "issue18036",
"file": "pdfs/issue18036.pdf",
"md5": "940ad97fc1a6f8d288a213af80313c7e",
"rounds": 1,
"type": "eq",
"annotations": true
}
]

0 comments on commit 7dda9c7

Please sign in to comment.