Skip to content

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

M6: calculation checks: an AFSimple string list with an argument after it is read and renamed, as an array is

Grzegorz Martens1808720d751d
diff --git a/crates/forms/src/af/calc.rs b/crates/forms/src/af/calc.rs
index c6556476a..d29956cbc 100644
--- a/crates/forms/src/af/calc.rs
+++ b/crates/forms/src/af/calc.rs
@@ -278,10 +278,13 @@ fn calculation_sites(js: &str) -> Vec<(Range<usize>, Site)> {
continue;
}
// One string of comma-separated names, when it is the whole list (not `"row" + suffix`).
+ // An argument after it leaves the list as written, as after an array: its names are read,
+ // and what follows is a name built at run time.
if let Some((range, list)) = string(k + 4) {
- if punct(k + 5, ')') {
+ if punct(k + 5, ')') || punct(k + 5, ',') {
sites.push((range, Site::Commas(list)));
- } else {
+ }
+ if !punct(k + 5, ')') {
sites.extend(built(k));
}
continue;
diff --git a/crates/forms/src/af/tests.rs b/crates/forms/src/af/tests.rs
index 50b3029ad..851c5c032 100644
--- a/crates/forms/src/af/tests.rs
+++ b/crates/forms/src/af/tests.rs
@@ -199,6 +199,12 @@ fn names_built_at_run_time_are_noticed() {
let js = "event.value = getField(\"a\").value + getField(\"row\" + i).value + AFSimple_Calculate(\"SUM\", new Array(\"a\", b));";
assert_eq!(calculation_reads(js), ["a"]);
assert_eq!(rename_in_calculation(js, "a", "qty"), Some(js.replace("\"a\"", "\"qty\"")));
+ // So are a list's, string or array, with another argument after it.
+ for js in ["AFSimple_Calculate(\"SUM\", \"a, b\", x);", "AFSimple_Calculate(\"SUM\", [\"a\", \"b\"], x);"] {
+ assert!(reads_built_names(js), "{js}");
+ assert_eq!(calculation_reads(js), ["a", "b"], "{js}");
+ assert_eq!(rename_in_calculation(js, "a", "qty"), Some(js.replace("\"a", "\"qty")), "{js}");
+ }
}
/// Scripts are read in one pass: a line of unfinished regular expressions, or calls nested in