From 91c15f00644cb7fa935c81165dc0393f827a45ac Mon Sep 17 00:00:00 2001 From: Eric Kaden <608341+EricKaden@users.noreply.github.com> Date: Sat, 24 Oct 2026 15:36:28 -0400 Subject: [PATCH] M12: page numbering is read from a layout:ready script's code, not its comments or strings --- crates/engine/src/xfa.rs | 7 +++-- crates/xfa/src/parse.rs | 61 +++++++++++++++++++++++++++++++++++----- crates/xfa/src/tests.rs | 16 +++++++++-- 3 files changed, 72 insertions(+), 12 deletions(-) diff --git a/crates/engine/src/xfa.rs b/crates/engine/src/xfa.rs index d0e2153181..5c442fca5d 100644 --- a/crates/engine/src/xfa.rs +++ b/crates/engine/src/xfa.rs @@ -857,8 +857,11 @@ pub(crate) fn on_open(doc: &mut pdfcraft_cos::Document, tpl: &Template, run: &mu let (ready, numbered) = { let live = r.live(); let ready: Vec = live.events.iter().filter(|e| e.activity == "layoutReady").cloned().collect(); - let numbered: HashSet = - ready.iter().filter(|e| pdfcraft_xfa::numbers_pages(&e.script) && is_field(&live.root, &e.som, 0)).map(|e| e.som.clone()).collect(); + let numbered: HashSet = ready + .iter() + .filter(|e| pdfcraft_xfa::numbers_pages(&e.script, e.formcalc) && is_field(&live.root, &e.som, 0)) + .map(|e| e.som.clone()) + .collect(); (ready, numbered) }; r.numbered = numbered; diff --git a/crates/xfa/src/parse.rs b/crates/xfa/src/parse.rs index 616734c8d5..796c9827c8 100644 --- a/crates/xfa/src/parse.rs +++ b/crates/xfa/src/parse.rs @@ -683,19 +683,66 @@ fn parse_node(c: XmlNode, warnings: &mut Vec, depth: usize) -> Option bool { - script.contains("pageCount") || script.contains("layout.page(") || script.contains("layout.absPage(") +/// field, it makes the field one the layout numbers itself. Its code is read (JavaScript, or +/// FormCalc when `formcalc`), not its comments and strings, so a script that only mentions one of +/// the names writes to its field as any other does. +pub fn numbers_pages(script: &str, formcalc: bool) -> bool { + let code = code_of(script, formcalc); + code.contains("pageCount") || code.contains("layout.page(") || code.contains("layout.absPage(") +} + +/// `script` with its comments and string literals blanked out: `//` and `/* */` comments and +/// `'…'`, `"…"` and `` `…` `` strings (with `\` escapes) in JavaScript; `;` and `//` comments and +/// `"…"` strings (a doubled `""` inside) in FormCalc. Line breaks stay. An unclosed comment or +/// string runs to the end. +fn code_of(script: &str, formcalc: bool) -> String { + let mut out = String::with_capacity(script.len()); + let mut chars = script.chars().peekable(); + while let Some(c) = chars.next() { + let line_comment = (c == '/' && chars.peek() == Some(&'/')) || (formcalc && c == ';'); + if line_comment { + for n in chars.by_ref() { + if n == '\n' { + out.push('\n'); + break; + } + } + } else if !formcalc && c == '/' && chars.peek() == Some(&'*') { + chars.next(); + let mut star = false; + for n in chars.by_ref() { + if star && n == '/' { + break; + } + star = n == '*'; + } + out.push(' '); + } else if c == '"' || (!formcalc && (c == '\'' || c == '`')) { + while let Some(n) = chars.next() { + if !formcalc && n == '\\' { + chars.next(); + } else if n == c { + if formcalc && chars.peek() == Some(&c) { + chars.next(); + } else { + break; + } + } + } + out.push(' '); + } else { + out.push(c); + } + } + out } /// Which page-number role a field with a layout:ready script plays, if any. fn page_role(f: &Field) -> Option { let script = f.scripts.iter().find(|s| s.activity == "ready" || s.activity == "layoutReady")?; - if !numbers_pages(&script.text) { + if !numbers_pages(&script.text, script.formcalc) { None - } else if script.text.contains("pageCount") { + } else if code_of(&script.text, script.formcalc).contains("pageCount") { Some(PageRole::Count) } else { Some(PageRole::Number) diff --git a/crates/xfa/src/tests.rs b/crates/xfa/src/tests.rs index 21e79b0653..d79b5a3e37 100644 --- a/crates/xfa/src/tests.rs +++ b/crates/xfa/src/tests.rs @@ -3552,9 +3552,19 @@ fn a_layout_ready_event_is_told_apart_from_form_ready() { let live = form_tree(&tpl, None, &Overrides::default()); assert!(live.events.iter().any(|e| e.som == "form[0]" && e.activity == "layoutReady"), "{:?}", live.events); // Page numbering is what the layout does itself. - assert!(crate::numbers_pages("this.rawValue = xfa.layout.page(this);")); - assert!(crate::numbers_pages("this.rawValue = xfa.layout.pageCount();")); - assert!(!crate::numbers_pages("this.rawValue = \"laid out\";")); + let js = |s: &str| crate::numbers_pages(s, false); + let formcalc = |s: &str| crate::numbers_pages(s, true); + assert!(js("this.rawValue = xfa.layout.page(this);")); + assert!(js("this.rawValue = xfa.layout.pageCount();")); + assert!(!js("this.rawValue = \"laid out\";")); + // Only the code counts: a mention in a comment or a string is not a page number. + assert!(!js("// xfa.layout.pageCount() was here\nthis.rawValue = \"ready\";")); + assert!(!js("/* xfa.layout.page(this) */ this.rawValue = 'see pageCount';")); + assert!(!js("this.rawValue = \"it's \\\"pageCount\\\"\";")); + assert!(js("var s = 'pageCount'; this.rawValue = xfa.layout.pageCount();"), "a string before the call"); + assert!(formcalc("$ = xfa.layout.pageCount()")); + assert!(!formcalc("; xfa.layout.pageCount()\n$ = \"pageCount \"\" here\"")); + assert!(formcalc("$ = \"a;b\" ; not a comment's end\n$ = xfa.layout.page($)"), "a ; inside a string isn't a comment"); } /// `typed_date` rewrites a date typed by another of a field's edit-picture patterns in the first