From aad694b6adbd15b990e8fa3a715a75b527d080f0 Mon Sep 17 00:00:00 2001 From: Eric Kaden <608341+EricKaden@users.noreply.github.com> Date: Sat, 24 Oct 2026 19:49:52 -0400 Subject: [PATCH] M12: a widget index the field doesn't have runs no focus scripts widget_som fell back to the field's own SOM path for an index out of range, so for a radio field it ran the group's scripts. It now returns None unless the field has that widget. --- crates/engine/src/tests.rs | 13 +++++++++++++ crates/engine/src/xfa.rs | 7 +++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 0f0ac30cdf..2dba866fe0 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -5266,6 +5266,19 @@ fn focus_scripts_that_change_nothing_leave_the_document_clean() { assert!(!s.get(id).unwrap().dirty); } +/// A widget the field doesn't have gains or loses no focus: no script runs, rather than the +/// field's own. +#[test] +fn a_widget_the_field_doesnt_have_runs_no_focus_scripts() { + use crate::js::FieldFocus::{Enter, Exit}; + let mut s = Session::new(); + let id = s.open("form.pdf", None, focus_form(), None).expect("opens"); + assert!(!s.field_focus(id, "probe", 3, Enter).unwrap(), "probe has one widget"); + assert!(!s.field_focus(id, "probe", usize::MAX, Exit).unwrap()); + assert_eq!((field_text(&s, id, "enters"), field_text(&s, id, "exits")), ("0".into(), "0".into())); + assert!(!s.get(id).unwrap().dirty); +} + /// Each radio button of a group is a field of its own: reaching the second button runs its /// enter script, reaching the first (which has none) runs nothing. Choosing the button changes /// the group's value, which runs the group's scripts, not the button's: leaving the button diff --git a/crates/engine/src/xfa.rs b/crates/engine/src/xfa.rs index b593f18c4a..bb5ed16e1e 100644 --- a/crates/engine/src/xfa.rs +++ b/crates/engine/src/xfa.rs @@ -936,13 +936,16 @@ pub(crate) fn on_focus(doc: &mut pdfcraft_cos::Document, tpl: &Template, som: &s } /// The SOM path of widget `widget` of field `field`: each radio button of a group has its own, -/// and a field's copies on several pages share one. +/// and a field's copies on several pages share one. `None` when the field has no widget +/// `widget`: the field's own path is only for a widget that records none, so a widget index +/// out of range never reaches the field (for a radio field, the group). pub(crate) fn widget_som(doc: &pdfcraft_cos::Document, field: &pdfcraft_forms::Field, widget: usize) -> Option { let som_of = |obj: pdfcraft_cos::ObjRef| { let object = doc.get(obj); object.as_dict()?.get(pdfcraft_xfa::SOM_KEY).map(|s| doc.resolve(s))?.as_string().map(|s| s.to_text()) }; - field.widgets.get(widget).and_then(|w| som_of(w.obj)).or_else(|| som_of(field.obj)) + let w = field.widgets.get(widget)?; + som_of(w.obj).or_else(|| som_of(field.obj)) } /// Does the live form give SOM path `som` an `activity` script?