Skip to content

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

M6: form_set_props refuses a calculate script given beside a notation

Grzegorz Martens64c26761a406
diff --git a/crates/automation/src/forms.rs b/crates/automation/src/forms.rs
index d13f059a3..3399505ec 100644
--- a/crates/automation/src/forms.rs
+++ b/crates/automation/src/forms.rs
@@ -113,12 +113,13 @@ fn calculate_arg(v: &Value) -> Result<Calculate> {
return Ok(Calculate::None);
}
let o = v.as_object().ok_or_else(|| bad("calculate must be {\"op\": …, \"fields\": […]}, {\"notation\": …} or \"none\""))?;
- if let Some(n) = o.get("notation").and_then(Value::as_str) {
- return Ok(Calculate::Notation(n.to_string()));
- }
+ // Before anything else is read, so a script given beside a notation isn't dropped unnoticed.
if o.contains_key("script") {
return Err(bad("a custom calculate script is set with form_set_script (event: calculate)"));
}
+ if let Some(n) = o.get("notation").and_then(Value::as_str) {
+ return Ok(Calculate::Notation(n.to_string()));
+ }
let op = match o.get("op").and_then(Value::as_str).unwrap_or("") {
"sum" => CalcOp::Sum,
"product" => CalcOp::Product,
diff --git a/crates/automation/tests/it/automation.rs b/crates/automation/tests/it/automation.rs
index fd29cdce1..f46187d3e 100644
--- a/crates/automation/tests/it/automation.rs
+++ b/crates/automation/tests/it/automation.rs
@@ -5123,9 +5123,12 @@ fn the_calculation_order_shows_formulas_and_what_they_cannot_read() {
let fields = ok(&mut a, "form_fields", json!({ "doc": doc }));
let tax = fields["fields"].as_array().unwrap().iter().find(|f| f["name"] == "tax").unwrap();
assert_eq!(tax["calculate"], json!({ "script": script }));
- // A script is set with form_set_script, and form_set_props says so.
- let e = err(&mut a, "form_set_props", json!({ "doc": doc, "field": "tax", "calculate": { "script": script } }));
- assert!(e.to_string().contains("form_set_script"), "{e}");
+ // A script is set with form_set_script, and form_set_props says so, also when a notation
+ // comes with it.
+ for calculate in [json!({ "script": script }), json!({ "notation": "qty * 2", "script": script })] {
+ let e = err(&mut a, "form_set_props", json!({ "doc": doc, "field": "tax", "calculate": calculate }));
+ assert!(e.to_string().contains("form_set_script"), "{calculate}: {e}");
+ }
}
#[test]