Skip to content

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

M6: a radio group with an indirect /Kids keeps its buttons when joined

KenM762c054abdce3b
diff --git a/crates/forms/src/author/join.rs b/crates/forms/src/author/join.rs
index 99fa4d29a..f98bd42cb 100644
--- a/crates/forms/src/author/join.rs
+++ b/crates/forms/src/author/join.rs
@@ -5,7 +5,7 @@
use std::collections::{HashMap, HashSet};
use super::lifecycle::{drop_from_calculation_order, split_merged};
-use super::tree::detach;
+use super::tree::{detach, list_of};
use super::*;
/// The keys a field that is also its own widget sheds when it becomes only a widget. `/AA`
@@ -94,11 +94,10 @@ pub(super) fn join_radio_group(doc: &mut Document, f: &Field, target: &Field) ->
doc.set(w.obj, Object::Dict(d));
kids.push(Object::Ref(w.obj));
}
- doc.update_dict(target.obj, |d| {
- let mut list = d.get(b"Kids").and_then(|k| k.as_array().cloned()).unwrap_or_default();
- list.extend(kids);
- d.set(b"Kids".to_vec(), Object::Array(list));
- })?;
+ // `/Kids` may be an indirect array: read through it so the group's own buttons stay.
+ let mut list = list_of(doc, target.obj, b"Kids");
+ list.extend(kids);
+ doc.update_dict(target.obj, |d| d.set(b"Kids".to_vec(), Object::Array(list)))?;
Ok(())
}
diff --git a/crates/forms/src/author/tree.rs b/crates/forms/src/author/tree.rs
index 4f7db4d8e..005452be3 100644
--- a/crates/forms/src/author/tree.rs
+++ b/crates/forms/src/author/tree.rs
@@ -187,7 +187,7 @@ pub(super) fn clash(name: &str, other: &str) -> Option<String> {
}
/// The array `key` of `holder` (`/Fields` of the form, `/Kids` of a field).
-fn list_of(doc: &Document, holder: ObjRef, key: &[u8]) -> Vec<Object> {
+pub(super) fn list_of(doc: &Document, holder: ObjRef, key: &[u8]) -> Vec<Object> {
let d = doc.get(holder).as_dict().cloned().unwrap_or_default();
d.get(key).map(|o| doc.resolve(o)).and_then(|o| o.as_array().cloned()).unwrap_or_default()
}
diff --git a/crates/forms/src/tests.rs b/crates/forms/src/tests.rs
index 48148402b..4648167c4 100644
--- a/crates/forms/src/tests.rs
+++ b/crates/forms/src/tests.rs
@@ -4636,6 +4636,20 @@ fn a_joining_radio_button_with_a_taken_export_value_takes_a_free_one() {
assert!(normal.contains(b"Choice2") && !normal.contains(b"Choice1"));
}
+/// A group whose `/Kids` is an indirect array keeps its own buttons when another joins it.
+#[test]
+fn a_group_with_indirect_kids_keeps_its_buttons_when_joined() {
+ let mut doc = two_radio_groups("Yes", "No");
+ let group = field(&fields(&doc), "Group1").obj;
+ let kids = doc.get(group).as_dict().unwrap().get(b"Kids").cloned().unwrap();
+ let held = doc.add(kids);
+ doc.update_dict(group, |d| d.set(b"Kids".to_vec(), Object::Ref(held))).unwrap();
+ rename_to(&mut doc, "Group2", "Group1").unwrap();
+ let doc = reopen(&doc);
+ let all = fields(&doc);
+ assert_eq!(field(&all, "Group1").widgets.iter().filter_map(|w| w.on_state.clone()).collect::<Vec<_>>(), ["Yes", "No"]);
+}
+
/// Buttons of the joining group that share an export value (a button repeated on another page,
/// on together) still share it after the join, under one fresh name.
#[test]