Skip to content

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

M12: showing an XFA subform leaves its unnamed hidden objects hidden

Eric Kaden35e04743d0f8
diff --git a/crates/xfa/src/fixtures.rs b/crates/xfa/src/fixtures.rs
index 54bb9bc21..a46bee5a2 100644
--- a/crates/xfa/src/fixtures.rs
+++ b/crates/xfa/src/fixtures.rs
@@ -118,6 +118,30 @@ fn assemble(objs: Vec<Vec<u8>>) -> Vec<u8> {
/// (5) = `total` (calculated); a check box `more` that shows the hidden `details` subform;
/// a table whose `row` (item, amount) repeats, with `addRow`, `removeRow` and `hello` buttons;
/// `grand`, the calculated sum of the amounts.
+/// A hidden subform `details` a script may show, holding a named field and three unnamed
+/// objects: a caption, an old caption the form keeps hidden, and a hidden subform. Unnamed
+/// objects have no SOM name of their own, so showing `details` must leave the hidden ones hidden.
+pub fn unnamed_hidden_template() -> String {
+ r##"<?xml version="1.0" encoding="UTF-8"?>
+<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
+<template xmlns="http://www.xfa.org/schema/xfa-template/3.3/">
+<subform name="form" layout="tb">
+ <pageSet><pageArea name="front"><contentArea x="0.5in" y="0.5in" w="7.5in" h="10in"/><medium short="8.5in" long="11in"/></pageArea></pageSet>
+ <subform name="page1" layout="tb">
+ <subform name="details" layout="tb" presence="hidden">
+ <draw w="4in" h="0.3in"><value><text>Current caption</text></value></draw>
+ <draw w="4in" h="0.3in" presence="hidden"><value><text>Old caption</text></value></draw>
+ <subform layout="tb" presence="hidden"><draw w="4in" h="0.3in"><value><text>Old section</text></value></draw></subform>
+ <field name="note" w="4in" h="0.3in"><ui><textEdit/></ui></field>
+ </subform>
+ </subform>
+</subform>
+</template>
+</xdp:xdp>
+"##
+ .to_string()
+}
+
pub fn scripted_template() -> String {
r##"<?xml version="1.0" encoding="UTF-8"?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
diff --git a/crates/xfa/src/script.rs b/crates/xfa/src/script.rs
index 17a037664..df7ba2fdf 100644
--- a/crates/xfa/src/script.rs
+++ b/crates/xfa/src/script.rs
@@ -227,8 +227,10 @@ impl Walker<'_> {
}
}
- fn presence(&self, som: &str, p: Presence) -> String {
- self.ov.presence.get(som).cloned().unwrap_or_else(|| presence_name(p).to_string())
+ /// The presence of the object named `name` at `som`: a script's, or else the template's. An
+ /// unnamed object shares its parent's SOM path, so a presence set there is not its own.
+ fn presence(&self, name: Option<&str>, som: &str, p: Presence) -> String {
+ name.and_then(|_| self.ov.presence.get(som)).cloned().unwrap_or_else(|| presence_name(p).to_string())
}
fn access(&self, som: &str, a: Access) -> String {
@@ -254,7 +256,7 @@ impl Walker<'_> {
kind: Some(NodeKind::Field),
value,
numeric: f.numeric,
- presence: self.presence(&som, f.common.presence),
+ presence: self.presence(Some("field"), &som, f.common.presence),
access: self.access(&som, f.access),
repeatable: false,
occur_min: 1,
@@ -307,7 +309,7 @@ impl Walker<'_> {
kind: Some(NodeKind::Subform),
value: String::new(),
numeric: false,
- presence: self.presence(&som, sf.common.presence),
+ presence: self.presence(sf.common.name.as_deref(), &som, sf.common.presence),
access: "open".into(),
repeatable: sf.occur.max != Some(1),
occur_min: sf.occur.min,
@@ -324,7 +326,7 @@ impl Walker<'_> {
name: a.common.name.clone().unwrap_or_else(|| "#area".into()),
som: som.clone(),
kind: Some(NodeKind::Area),
- presence: self.presence(&som, a.common.presence),
+ presence: self.presence(a.common.name.as_deref(), &som, a.common.presence),
access: "open".into(),
occur_min: 1,
occur_max: Some(1),
@@ -341,7 +343,7 @@ impl Walker<'_> {
som: som.clone(),
kind: Some(NodeKind::Draw),
value: d.value.plain().unwrap_or_default(),
- presence: self.presence(&som, d.common.presence),
+ presence: self.presence(d.common.name.as_deref(), &som, d.common.presence),
access: "open".into(),
occur_min: 1,
occur_max: Some(1),
@@ -368,7 +370,7 @@ impl Walker<'_> {
som: som.clone(),
kind: Some(NodeKind::ExclGroup),
value: selected.unwrap_or_default(),
- presence: self.presence(&som, g.common.presence),
+ presence: self.presence(Some("group"), &som, g.common.presence),
access: "open".into(),
occur_min: 1,
occur_max: Some(1),
@@ -455,9 +457,12 @@ pub fn apply_overrides(tpl: &Template, ov: &Overrides) -> Template {
Node::ExclGroup(_) => Some("group"),
_ => None,
};
- let som = child_som(parent_som, n.common().name.as_deref().or(name_default), sib);
- let presence = ov.presence.get(&som).map(|p| Presence::parse(Some(p)));
- let access = ov.access.get(&som).cloned();
+ let name = n.common().name.as_deref().or(name_default);
+ let som = child_som(parent_som, name, sib);
+ // An unnamed draw, area or subform has no SOM name: `som` is its parent's, and so are
+ // the overrides under it.
+ let presence = name.and_then(|_| ov.presence.get(&som)).map(|p| Presence::parse(Some(p)));
+ let access = name.and_then(|_| ov.access.get(&som)).cloned();
match n {
Node::Subform(s) => {
if let Some(p) = presence {
diff --git a/crates/xfa/src/tests.rs b/crates/xfa/src/tests.rs
index 03eb2c67a..d1503527c 100644
--- a/crates/xfa/src/tests.rs
+++ b/crates/xfa/src/tests.rs
@@ -7,7 +7,9 @@ use crate::data::{
DatasetsWrite, FieldData, FieldDatum, build_data, iso_to_pattern, parse_datasets, pattern_to_iso, read_values, som_to_path, write_datasets,
};
use crate::data::{add_data_instances, remove_data_instance, write_data_value};
-use crate::fixtures::{TINY_GIF, fields_template, scripted_template, shell, static_shell, template, template_with_data, tiny_png};
+use crate::fixtures::{
+ TINY_GIF, fields_template, scripted_template, shell, static_shell, template, template_with_data, tiny_png, unnamed_hidden_template,
+};
use crate::layout::Item;
use crate::layout::WidgetKind;
use crate::model::*;
@@ -714,6 +716,26 @@ fn overrides_live_in_the_acroform_and_hide_objects_in_the_layout() {
assert!(overrides(&doc).is_empty());
}
+#[test]
+fn showing_a_subform_leaves_its_unnamed_hidden_objects_hidden() {
+ let (tpl, _) = parse(&unnamed_hidden_template()).unwrap();
+ let mut ov = Overrides::default();
+ ov.presence.insert("form[0].page1[0].details[0]".into(), "visible".into());
+ let form = layout(&apply_overrides(&tpl, &ov), None).unwrap();
+ let shown = texts(&form.pages[0]);
+ assert!(shown.iter().any(|t| t == "Current caption"), "{shown:?}");
+ assert!(!shown.iter().any(|t| t == "Old caption" || t == "Old section"), "{shown:?}");
+ assert_eq!(form.fields, 1);
+ // Scripts see the same: the hidden draw stays hidden in the live form.
+ let root = form_tree(&tpl, None, &ov).root;
+ let details = &root.children[0].children[0];
+ assert_eq!(details.presence, "visible");
+ let presence = |text: &str| details.children.iter().find(|c| c.value == text).map(|c| c.presence.clone());
+ assert_eq!(presence("Current caption").as_deref(), Some("visible"));
+ assert_eq!(presence("Old caption").as_deref(), Some("hidden"));
+ assert_eq!(details.children.iter().find(|c| c.kind == Some(NodeKind::Subform)).map(|c| c.presence.as_str()), Some("hidden"));
+}
+
#[test]
fn data_instances_are_added_and_removed_in_the_packet() {
let mut doc = Document::open(Arc::new(shell(&scripted_template()))).unwrap();