mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-19 02:26:48 +00:00
M12: page numbering is read from a layout:ready script's code, not its comments or strings
This commit is contained in:
1 parent
56c1a1e504
commit
91c15f0064
3 files changed
+72
-12
No files matched your search
@@ -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<ScriptEvent> = live.events.iter().filter(|e| e.activity == "layoutReady").cloned().collect();
|
||||
let numbered: HashSet<String> =
|
||||
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<String> = 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;
|
||||
|
||||
+54
-7
@@ -683,19 +683,66 @@ fn parse_node(c: XmlNode, warnings: &mut Vec<String>, depth: usize) -> Option<No
|
||||
}
|
||||
|
||||
/// Whether a `layout:ready` script uses page numbers (`xfa.layout.page(this)`, `pageCount()`): on a
|
||||
/// field, it makes the field one the layout numbers itself. It looks for the names in the
|
||||
/// script's text and doesn't parse it, so a script that only mentions one (in a comment or a
|
||||
/// string) counts too, and its writes to its field are dropped like a page number's.
|
||||
pub fn numbers_pages(script: &str) -> 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<PageRole> {
|
||||
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)
|
||||
|
||||
+13
-3
@@ -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
|
||||
|
||||
Reference in new issue
Block a user