Skip to content

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

Merge PR #3985: M6: a document script that throws no longer stops the form's other scripts (#3940)

aionggc24e90b59c98
diff --cc crates/automation/src/tools.rs
index 725dde4bf,e5c901717..57e1c4ae4
--- a/crates/automation/src/tools.rs
+++ b/crates/automation/src/tools.rs
@@@ -1110,7 -1094,7 +1110,7 @@@ pub fn tools() -> Vec<ToolDef>
t("form_merge_data", "Merge data files into spreadsheet", "Collect the field values of form data files (FDF, XFDF) or filled-in PDF forms into one CSV file at path: a column per field name, a row per file. Returns the row and column counts.")
.cmd("form.merge_data")
.with(schema(json!({ "paths": { "type": "array", "items": { "type": "string" }, "minItems": 1 }, "path": { "type": "string" } }), &["paths", "path"])),
- t("js_run", "Run JavaScript", "Run Acrobat JavaScript in the document, as the JavaScript console does (or as push button `field`'s Mouse Up script when field is given; on a laid-out XFA form, `field` runs that button's XFA click script instead, JavaScript or FormCalc, which can add and remove rows and show or hide subforms). The form object model is available: this/getField, event, app, util, console, display, color, and the document-level scripts (except the PDF container scripts when XFA layout is active; they remain available through js_document_scripts). Field changes and resetForm are applied as one undoable step; returns the script's alerts, console output, requests (print, page, url, submit) and error.")
- t("js_run", "Run JavaScript", "Run Acrobat JavaScript in the document, as the JavaScript console does (or as push button `field`'s Mouse Up script when field is given; on a laid-out XFA form, `field` runs that button's XFA click script instead, JavaScript or FormCalc, which can add and remove rows and show or hide subforms). The form object model is available: this/getField, event, app, util, console, display, color, and the document-level scripts. Field changes and resetForm are applied as one undoable step; returns the script's alerts, console output, requests (print, page, url, submit), error, and the errors of document-level scripts that failed (the rest still ran).")
++ t("js_run", "Run JavaScript", "Run Acrobat JavaScript in the document, as the JavaScript console does (or as push button `field`'s Mouse Up script when field is given; on a laid-out XFA form, `field` runs that button's XFA click script instead, JavaScript or FormCalc, which can add and remove rows and show or hide subforms). The form object model is available: this/getField, event, app, util, console, display, color, and the document-level scripts (except the PDF container scripts when XFA layout is active; they remain available through js_document_scripts). Field changes and resetForm are applied as one undoable step; returns the script's alerts, console output, requests (print, page, url, submit), error, and the errors of document-level scripts that failed (the rest still ran).")
.cmd("tools.js_console")
.with(schema(
json!({ "doc": doc(), "script": { "type": "string" }, "field": { "type": "string", "description": "Run as this button's Mouse Up event." } }),
diff --cc crates/engine/src/tests.rs
index 3d91b0c69,288ebec0d..e83dbe207
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@@ -4185,78 -4095,27 +4190,99 @@@ fn scripts_in_an_xfa_form_see_the_xfa_p
assert_eq!((o.error, o.result.as_deref()), (None, Some("undefined number 3.5")));
}
+ /// #3940: a document-level script that defines the helper and then calls a method PdfCraft lacks
+ /// (an Acrobat form copying a button icon at open) left every calculated field blank. The
+ /// helper stays defined, the form calculates, and the error is reported once.
+ #[test]
+ fn a_document_script_that_throws_still_lets_the_form_calculate() {
+ let helpers =
+ "function times\\(a, b\\) { return a * b; } this.getField\\('go'\\).buttonSetIcon\\(this.getField\\('go'\\).buttonGetIcon\\(\\)\\);";
+ let mut s = Session::new().with_clock(|| 1_700_000_000);
+ let id = s.open("order.pdf", None, Arc::new(scripted_form_with(helpers)), None).unwrap();
+ let _ = s.take_js_output(id);
+ s.apply(id, Edit::SetFieldValue { name: "qty".into(), value: FieldValue::Text("4".into()) }).unwrap();
+ let total = s.get(id).unwrap().form.iter().find(|f| f.name == "total").unwrap().value.clone();
+ assert_eq!(total, ["10"], "calculated through the helper the failing script defined");
+ let out = s.take_js_output(id);
+ assert_eq!(out.errors.len(), 1, "reported once, not once per script: {out:?}");
+ assert!(out.errors[0].starts_with("document script 1: TypeError"), "{out:?}");
+ // The console runs too, and says which document script failed.
+ let o = s.run_javascript(id, "times(2, 3)", None).unwrap();
+ assert_eq!((o.error, o.result.as_deref(), o.doc_errors.len()), (None, Some("6"), 1));
+ }
+
+#[test]
+fn active_xfa_does_not_run_pdf_container_scripts_but_keeps_requested_alerts() {
+ let template = pdfcraft_xfa::fixtures::scripted_template().replace(
+ r#"xfa.host.messageBox("Hello " + qty.rawValue);"#,
+ r#"xfa.host.messageBox("Hello " + qty.rawValue); xfa.host.print(1, "0", "0", 0, 0, 0, 0, 0);"#,
+ );
+ let mut cos = pdfcraft_cos::Document::open(Arc::new(pdfcraft_xfa::fixtures::shell(&template))).unwrap();
+ let fallback = "app.alert('This PDF form requires a newer version of Adobe Acrobat');";
+ pdfcraft_forms::set_document_script(&mut cos, "viewerFallback", Some(fallback)).unwrap();
+ let bytes = Arc::new(pdfcraft_cos::write_full(&cos, &Default::default()).unwrap());
+ let mut s = Session::new();
+ let id = s.open("xfa.pdf", None, bytes, None).unwrap();
+ assert!(s.get(id).unwrap().xfa.is_some());
+ assert!(s.take_js_output(id).alerts.is_empty());
+ let out = s.run_javascript(id, "app.alert('Console message');", None).unwrap();
+ assert!(out.error.is_none(), "{out:?}");
+ assert_eq!(out.alerts, ["Console message"]);
+ assert_eq!(s.get(id).unwrap().document_scripts(), [("viewerFallback".into(), fallback.into())]);
+ let out = s.run_javascript(id, "", Some("hello")).unwrap();
+ assert!(out.error.is_none(), "{out:?}");
+ assert_eq!(out.alerts, ["Hello 2"]);
+ assert_eq!(out.requests, [js::Request::Print]);
+ // The same context rule applies to format/validate/calculate through JsRunner.
+ s.apply(id, Edit::SetFieldScript { name: "qty".into(), event: "validate".into(), script: Some("app.alert('Field message');".into()) }).unwrap();
+ s.take_js_output(id);
+ s.apply(id, Edit::SetFieldValue { name: "qty".into(), value: FieldValue::Text("3".into()) }).unwrap();
+ assert_eq!(s.take_js_output(id).alerts, ["Field message"]);
+ // Saved XFA keeps its container scripts for inspection, but does not execute them.
+ let saved = s.get(id).unwrap().bytes.clone();
+ let reopened = s.open("saved.pdf", None, saved, None).unwrap();
+ assert!(s.run_javascript(reopened, "", None).unwrap().alerts.is_empty());
+ // Making the form static restores the ordinary PDF context; undo restores XFA's.
+ s.apply(id, Edit::XfaToStatic).unwrap();
+ assert_eq!(s.run_javascript(id, "", None).unwrap().alerts.len(), 1);
+ s.undo(id).unwrap();
+ assert!(s.run_javascript(id, "", None).unwrap().alerts.is_empty());
+}
+
+#[test]
+fn active_xfa_without_template_scripts_does_not_initialize_pdf_fallback_scripts() {
+ let template = r#"<template xmlns="http://www.xfa.org/schema/xfa-template/3.3/">
+<subform name="form" layout="tb"><pageSet><pageArea name="page">
+<contentArea x="36pt" y="36pt" w="540pt" h="720pt"/><medium short="612pt" long="792pt"/>
+</pageArea></pageSet><field name="text" w="200pt" h="30pt"><ui><textEdit/></ui></field></subform></template>"#;
+ let mut s = Session::new();
+ let id = s.open("no-scripts.pdf", None, Arc::new(pdfcraft_xfa::fixtures::shell(template)), None).unwrap();
+ assert!(s.get(id).unwrap().xfa.is_some());
+ assert!(s.get(id).unwrap().xfa_template.is_none());
+ s.apply(id, Edit::SetDocumentScript { name: "fallback".into(), script: Some("throw new Error('PDF fallback ran');".into()) }).unwrap();
+ let out = s.run_javascript(id, "app.alert('Console message');", None).unwrap();
+ assert!(out.error.is_none(), "{out:?}");
+ assert_eq!(out.alerts, ["Console message"]);
+}
+
+#[test]
+fn inactive_xfa_and_ordinary_pdfs_keep_document_helpers_and_alerts() {
+ for bytes in [
+ fixture(1),
+ pdfcraft_xfa::fixtures::static_shell("<form1><page1><name>Test</name></page1></form1>"),
+ pdfcraft_xfa::fixtures::shell(&pdfcraft_xfa::fixtures::scripted_template()),
+ ] {
+ let mut cos = pdfcraft_cos::Document::open(Arc::new(bytes)).unwrap();
+ pdfcraft_forms::set_document_script(&mut cos, "helper", Some("function twice(n) { return n * 2; } app.alert('PDF message');")).unwrap();
+ // The raw dynamic shell has not been laid out: XFA's presence alone is not enough.
+ let runner = js::JsRunner::new(&cos, "control.pdf");
+ let out = pdfcraft_js::run("twice(3)", &pdfcraft_js::Event::doc("Console"), &runner.doc, &[], &runner.doc_scripts, Default::default());
+ assert!(out.error.is_none(), "{out:?}");
+ assert_eq!(out.result.as_deref(), Some("6"));
+ assert_eq!(out.alerts, ["PDF message"]);
+ }
+}
+
#[test]
fn edit_history_does_not_replay_field_script_side_effects() {
let mut s = Session::new().with_clock(|| 1_700_000_000);