Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

M6: clearing a field's value leaves it empty, not its parent field's

KenM7612c9051fd8b7
diff --git a/crates/forms/src/author.rs b/crates/forms/src/author.rs
index ce89fad80..76ae93724 100644
--- a/crates/forms/src/author.rs
+++ b/crates/forms/src/author.rs
@@ -15,6 +15,8 @@ mod lifecycle;
mod look;
mod tree;
+pub(crate) use tree::ancestor_sets;
+
pub use edit::set_props;
pub use lifecycle::{add_field, delete_field, delete_field_object, duplicate_field};
pub(crate) use look::button_appearance;
diff --git a/crates/forms/src/author/tree.rs b/crates/forms/src/author/tree.rs
index 605ce95c3..9eb0791b7 100644
--- a/crates/forms/src/author/tree.rs
+++ b/crates/forms/src/author/tree.rs
@@ -319,7 +319,7 @@ pub(super) fn inherited(doc: &Document, field: ObjRef) -> Dict {
/// Whether an ancestor of `field` sets the inheritable `key`, which `field` would take on without
/// its own entry.
-pub(super) fn ancestor_sets(doc: &Document, field: ObjRef, key: &[u8]) -> bool {
+pub(crate) fn ancestor_sets(doc: &Document, field: ObjRef, key: &[u8]) -> bool {
let mut seen = std::collections::HashSet::new();
let mut parent = doc.get(field).as_dict().and_then(|d| d.reference(b"Parent"));
while let Some(p) = parent {
diff --git a/crates/forms/src/tests.rs b/crates/forms/src/tests.rs
index 43f933fe0..b9153ff6e 100644
--- a/crates/forms/src/tests.rs
+++ b/crates/forms/src/tests.rs
@@ -4602,6 +4602,34 @@ fn clearing_a_kids_default_does_not_bring_back_its_parents() {
assert!(doc.get(name.obj).as_dict().unwrap().get(b"DV").is_none());
}
+#[test]
+fn clearing_a_kids_value_does_not_bring_back_its_parents() {
+ // A text kid, then a list box kid, under a parent with both a value and a default.
+ for (ft, set) in [("/FT /Tx", FieldValue::Text("Oak".into())), ("/FT /Ch /Opt [(Main St) (Oak)]", FieldValue::Choice(vec!["Oak".into()]))] {
+ let mut doc = document(&[
+ "<< /Type /Catalog /Pages 2 0 R /AcroForm 4 0 R >>".into(),
+ "<< /Type /Pages /Kids [3 0 R] /Count 1 /MediaBox [0 0 600 800] >>".into(),
+ "<< /Type /Page /Parent 2 0 R /Annots [6 0 R] >>".into(),
+ "<< /Fields [5 0 R] /DA (/Helv 0 Tf 0 g) >>".into(),
+ format!("<< /T (address) {ft} /V (Main St) /DV (Main St) /Kids [6 0 R] >>"),
+ "<< /Type /Annot /Subtype /Widget /T (city) /Parent 5 0 R /Rect [10 10 110 60] /P 3 0 R >>".into(),
+ ]);
+ set_props(&mut doc, "address.city", &FieldProps { default_value: Some(None), ..FieldProps::default() }).unwrap();
+ set_value(&mut doc, "address.city", &set).unwrap();
+ assert_eq!(field(&fields(&doc), "address.city").value, ["Oak"], "{ft}");
+ reset(&mut doc, None).unwrap();
+ let doc = reopen(&doc);
+ let f = field(&fields(&doc), "address.city").clone();
+ assert!(f.value.iter().all(String::is_empty), "{ft}: reset leaves it empty, not the parent's: {:?}", f.value);
+ let mut doc = doc;
+ set_value(&mut doc, "address.city", &set).unwrap();
+ let clear = if ft.contains("Ch") { FieldValue::Choice(Vec::new()) } else { FieldValue::Text(String::new()) };
+ set_value(&mut doc, "address.city", &clear).unwrap();
+ let f = field(&fields(&reopen(&doc)), "address.city").clone();
+ assert!(f.value.iter().all(String::is_empty), "{ft}: clearing leaves it empty: {:?}", f.value);
+ }
+}
+
#[test]
fn clearing_a_buttons_default_under_a_parent_with_one_leaves_it_off() {
// A check box, then a radio button (flags 49152, inherited from the parent).
diff --git a/crates/forms/src/values.rs b/crates/forms/src/values.rs
index 9dd79f0a7..738c3b742 100644
--- a/crates/forms/src/values.rs
+++ b/crates/forms/src/values.rs
@@ -207,8 +207,9 @@ pub(crate) fn write_value(doc: &mut Document, f: &Field, value: &FieldValue, scr
let t = &af::keystroke(&f.actions.format, &f.name, t).map_err(FormError::Format)?;
af::validate(&f.actions.validate, t).map_err(FormError::Invalid)?;
let t = &scripting::accept(doc, f, t, scripts)?;
+ let shadow = t.is_empty() && crate::author::ancestor_sets(doc, f.obj, b"V");
doc.update_dict(f.obj, |d| {
- if t.is_empty() {
+ if t.is_empty() && !shadow {
d.remove(b"V");
} else {
d.set(b"V".to_vec(), PdfString::text(t));
@@ -296,8 +297,11 @@ pub(crate) fn write_value(doc: &mut Document, f: &Field, value: &FieldValue, scr
}),
_ => None,
};
+ // An empty value of its own keeps a parent field's /V from showing through.
+ let shadow = exports.is_empty() && crate::author::ancestor_sets(doc, f.obj, b"V");
doc.update_dict(f.obj, |d| {
match exports.as_slice() {
+ [] if shadow => d.set(b"V".to_vec(), PdfString::text("")),
[] => {
d.remove(b"V");
}
diff --git a/parity/acrobat-features.toml b/parity/acrobat-features.toml
index 8dc8ce17f..977a1503f 100644
--- a/parity/acrobat-features.toml
+++ b/parity/acrobat-features.toml
@@ -4727,7 +4727,7 @@ milestone = "M6"
status = "shipped"
commands = ["form.clear"]
tools = ["form_reset"]
-evidence = ["crates/forms/src/tests.rs::reset_restores_defaults", "crates/ui-egui/tests/it/forms.rs::check_boxes_radios_and_choices"]
+evidence = ["crates/forms/src/tests.rs::reset_restores_defaults", "crates/ui-egui/tests/it/forms.rs::check_boxes_radios_and_choices", "crates/forms/src/tests.rs::clearing_a_kids_value_does_not_bring_back_its_parents"]
notes = "back to /DV; undoable"
[[feature]]