diff --git a/crates/xfa/src/proto.rs b/crates/xfa/src/proto.rs index ae1505804e..a34294ae71 100644 --- a/crates/xfa/src/proto.rs +++ b/crates/xfa/src/proto.rs @@ -75,6 +75,7 @@ pub(crate) fn expand(template: Node) -> (Option, Vec) { search: MAX_SEARCH, work: MAX_WORK, active: HashMap::new(), + non_rendering_children: HashSet::new(), missing: false, external: false, looped: false, @@ -126,6 +127,9 @@ struct Writer<'a> { work: usize, /// The elements being written around the current one: inheriting from one is a loop. active: HashMap, + /// Nodes whose direct children are all omitted prototype definitions (or absent). + /// The DOM is immutable; remembering these avoids repeated no-output child walks. + non_rendering_children: HashSet, missing: bool, external: bool, looped: bool, @@ -465,7 +469,25 @@ impl<'a> Writer<'a> { self.out.push('>'); // Rich text is taken whole from the nearest layer that has any. let cut = if tag == "exData" { layers.iter().position(|l| has_content(l, true)).map_or(layers.len(), |i| i + 1) } else { layers.len() }; - let merged = layers.get(..cut).unwrap_or(&layers); + // Do this only after charging every original child and selecting the rich-text + // layer boundary. Omitting a proto-only layer from child traversal must never + // reduce its work charge or change which exData layer wins. + let merged: Vec<_> = layers + .get(..cut) + .unwrap_or(&layers) + .iter() + .copied() + .filter(|layer| { + if self.non_rendering_children.contains(&layer.id()) { + return false; + } + if layer.children().all(|child| child.is_element() && child.tag_name().name() == "proto") { + self.non_rendering_children.insert(layer.id()); + return false; + } + true + }) + .collect(); // Data content: the nearest layer's that has any (empty content overrides nothing). let text_layer = merged.iter().position(|l| has_content(l, false)).unwrap_or(0); let chosen = if matches!(tag, "ui" | "value") { diff --git a/crates/xfa/src/proto/tests.rs b/crates/xfa/src/proto/tests.rs index ba7f6f8316..35d3f64562 100644 --- a/crates/xfa/src/proto/tests.rs +++ b/crates/xfa/src/proto/tests.rs @@ -399,3 +399,16 @@ fn a_som_expression_past_its_length_limit_is_not_followed() { assert_eq!(field(&t, "long").common.h, None); assert!(close(field(&t, "short").common.h, 2.0 * MM)); } + +#[test] +fn proto_only_child_cache_keeps_attributes_text_and_rich_text_boundary() { + let xml = form(r#""#, r##""##); + let (t, warnings) = parse(&xml).unwrap(); + assert!(warnings.is_empty(), "{warnings:?}"); + assert_eq!(field(&t, "first").common.w, field(&t, "second").common.w); + assert!(field(&t, "first").common.w.is_some()); + let text = expanded(r##""##); + assert!(text.matches("kept").count() >= 2, "{text}"); + let rich = expanded(r##""##); + assert_eq!(rich.matches("hidden").count(), 1, "nearest nonempty exData still wins: {rich}"); +}