fix(forms): keep a whitespace-only value in a text field without a format (#1323) (#1403)

The commit-time Keystroke check returned an empty value for any
whitespace-only text before looking at the field's format, so setting a
space, a tab or a non-breaking space in a plain text field reported
success and removed the field's value. Text and XFDF imports go through
the same setter and lost the value the same way.

Blank input now clears only a field with a format (number, percent,
date, time, special or mask), as before; a field without one stores the
text as given. An empty value still clears any field.

Adds tests::whitespace_only_text_is_kept_in_a_field_without_a_format
and a keystroke assertion in af::tests::numbers_format_like_acrobat,
both of which fail before this change.
This commit is contained in:
Marco Arnulfo authored and GitHub committed 2026-10-12 07:53:10 -04:00
1 parent 452ab51ab8
commit 2ee8ce1ac1
2 files changed
+20 -1

No files matched your search

+5 -1
View File
@@ -823,7 +823,8 @@ pub fn format_value(f: &Format, value: &str) -> String {
/// (numbers are normalised), or Acrobat's message.
pub fn keystroke(f: &Format, field: &str, value: &str) -> Result<String, String> {
let bad = || format!("The value entered does not match the format of the field [ {field} ]");
if value.trim().is_empty() {
// Blank input clears a formatted field; without a format, whitespace is text like any other.
if *f != Format::None && value.trim().is_empty() {
return Ok(String::new());
}
match f {
@@ -1045,6 +1046,9 @@ mod tests {
keystroke(&n(2, 0, 0, "$", true), "Total", "ten"),
Err("The value entered does not match the format of the field [ Total ]".into())
);
// Blank input clears a formatted field (#1323); without a format it is text like any other.
assert_eq!(keystroke(&n(2, 0, 0, "$", true), "Total", " \t"), Ok(String::new()));
assert_eq!(keystroke(&Format::None, "Name", " \t"), Ok(" \t".into()));
}
#[test]
+15
View File
@@ -232,6 +232,21 @@ fn opt_export_values_select_button_states() {
assert!(err.to_string().contains("options: Post, Pick-up"), "{err}");
}
/// Issue #1323: a text field without a format keeps a whitespace-only value as typed; only an
/// empty value clears it.
#[test]
fn whitespace_only_text_is_kept_in_a_field_without_a_format() {
for value in [" ", "\t", "\u{a0}"] {
let mut doc = fixture();
set_value(&mut doc, "name", &FieldValue::Text(value.into())).unwrap();
let doc = reopen(&doc);
assert_eq!(field(&fields(&doc), "name").value, [value], "{value:?}");
}
let mut doc = fixture();
set_value(&mut doc, "name", &FieldValue::Text(String::new())).unwrap();
assert!(field(&fields(&reopen(&doc)), "name").value.is_empty());
}
#[test]
fn text_fields_get_new_appearances() {
let mut doc = fixture();