Skip to content

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

Merge remote-tracking branch 'upstream/main' into fix/exec-menu-attachments

Eric Kaden44b7e0a40ff5
diff --cc crates/js/src/lib.rs
index aaacce0c9,13bdc4ba3..8272918c9
--- a/crates/js/src/lib.rs
+++ b/crates/js/src/lib.rs
@@@ -1037,9 -973,11 +1039,12 @@@ fn color_object(ctx: &mut Context) -> J
o.build()
}
- /// Installs the form object model's globals: the document's functions and properties
- /// (`getField`, `numFields`, `pageNum`, `info`, …), `app`, `util`, `console`, `display` and
- /// `color`.
+ /// The XFA version scripts see as `xfa_version`.
+ const XFA_VERSION: f64 = 3.5;
+
-/// Define the globals every script sees: the document's functions and properties (`getField`,
-/// `numPages`, `info`, the XFA version), and `app`, `util`, `console`, `display` and `color`.
++/// Installs the globals every script sees: the document's functions and properties
++/// (`getField`, `numFields`, `pageNum`, `numPages`, `info`, the XFA version, …), and `app`,
++/// `util`, `console`, `display` and `color`.
fn install(ctx: &mut Context) -> JsResult<()> {
let globals: [(&str, usize, NativeFunction); 9] = [
("getField", 1, NativeFunction::from_fn_ptr(get_field)),
diff --cc crates/js/src/tests.rs
index 5190c3ca8,b2e866fae..c46921c7f
--- a/crates/js/src/tests.rs
+++ b/crates/js/src/tests.rs
@@@ -1462,48 -1455,28 +1465,74 @@@ fn shared_field_snapshots_isolate_write
assert!(read.changed.is_empty());
}
+mod menu_items {
+ use super::*;
+ use crate::xfa::{XfaDoc, XfaEffect, XfaEvent, run_xfa};
+
+ /// Every side panel has a fixed id in automation output.
+ #[test]
+ fn each_panel_has_a_fixed_id_for_automation() {
+ let ids = [Panel::Bookmarks, Panel::Pages, Panel::Attachments, Panel::Signatures, Panel::Layers].map(Panel::id);
+ assert_eq!(ids, ["bookmarks", "pages", "attachments", "signatures", "layers"]);
+ }
+
+ /// Each side panel item, Print and SaveAs make their request, in order; an unknown item
+ /// makes none and is not an error.
+ #[test]
+ fn exec_menu_item_shows_side_panels_prints_and_saves() {
+ let o = go(
+ "app.execMenuItem('ShowHideFileAttachment'); app.execMenuItem('ShowHideBookmarks'); app.execMenuItem('ShowHideThumbnails'); \
+ app.execMenuItem('ShowHideSignatures'); app.execMenuItem('ShowHideOptCont'); app.execMenuItem('Print'); \
+ app.execMenuItem('SaveAs'); app.execMenuItem('NoSuchItem');",
+ &Event::field("Mouse Up", "total", ""),
+ );
+ assert_eq!(o.error, None);
+ assert_eq!(
+ o.requests,
+ [
+ Request::ShowPanel(Panel::Attachments),
+ Request::ShowPanel(Panel::Bookmarks),
+ Request::ShowPanel(Panel::Pages),
+ Request::ShowPanel(Panel::Signatures),
+ Request::ShowPanel(Panel::Layers),
+ Request::Print,
+ Request::SaveAs,
+ ]
+ );
+ }
+
+ /// `app.execMenuItem` works in XFA scripts too: a click shows the attachments panel.
+ #[test]
+ fn xfa_scripts_show_the_attachments_panel() {
+ let event = XfaEvent { activity: "click".into(), target: "form1[0].page1[0].go[0]".into(), ..Default::default() };
+ let o = run_xfa("app.execMenuItem('ShowHideFileAttachment')", &event, &XfaDoc::default(), &super::xfa_model::form(), Limits::default());
+ assert_eq!(o.error, None, "{o:?}");
+ assert!(o.effects.contains(&XfaEffect::ShowPanel(Panel::Attachments)), "{:?}", o.effects);
+ }
+}
++
+ /// The check XFA forms run on opening: without both globals, or with a version below 2.8, it
+ /// alerts that the viewer can't show the form.
+ const XFA_CHECK: &str = "if (typeof(xfa_installed) == 'undefined' || typeof(xfa_version) == 'undefined' || xfa_version < 2.8) app.alert('old viewer'); \
+ event.value = typeof xfa_installed + ' ' + typeof xfa_version + ' ' + xfa_version;";
+
+ /// An XFA form sees `xfa_installed` (true) and `xfa_version` (3.5), so the check passes.
+ #[test]
+ fn an_xfa_form_sees_the_xfa_plugin_installed() {
+ let xfa = DocInfo { xfa: true, ..doc() };
+ let o = run(XFA_CHECK, &Event::doc("Open"), &xfa, &fields(), &[], Limits::default());
+ assert_eq!(o.error, None);
+ assert_eq!(o.value, "boolean number 3.5");
+ assert!(o.alerts.is_empty(), "{:?}", o.alerts);
+ let o = run("event.value = String(xfa_installed);", &Event::doc("Open"), &xfa, &fields(), &[], Limits::default());
+ assert_eq!(o.value, "true");
+ }
+
+ /// A form without XFA sees `xfa_version` but no `xfa_installed`, so the check alerts.
+ #[test]
+ fn a_plain_form_sees_the_xfa_version_but_not_installed() {
+ let o = go(XFA_CHECK, &Event::doc("Open"));
+ assert_eq!(o.error, None);
+ assert_eq!(o.value, "undefined number 3.5");
+ assert_eq!(o.alerts, ["old viewer"]);
+ }
diff --cc parity/acrobat-features.toml
index d58fa14d1,973e0cc69..553388c5b
--- a/parity/acrobat-features.toml
+++ b/parity/acrobat-features.toml
@@@ -5165,8 -5165,8 +5165,8 @@@ milestone = "M6
status = "partial"
commands = ["tools.js_console"]
tools = ["js_run"]
- evidence = ["crates/js/src/tests.rs::calculate_scripts_read_fields_and_set_the_value", "crates/engine/src/tests.rs::form_javascript_validates_calculates_and_formats", "crates/automation/tests/it/automation.rs::javascript_through_tools"]
- notes = "boa (pure Rust) with the forms object model: event, getField/Field (value, readonly, display, colours, items, check boxes), app (with execMenuItem for the side panels and Save As, from a button only, and Print where this.print() works), util.printf/printd/printx, console, display, color; no annotations, layers or dialogs (app.response returns null) yet."
+ evidence = ["crates/js/src/tests.rs::calculate_scripts_read_fields_and_set_the_value", "crates/engine/src/tests.rs::form_javascript_validates_calculates_and_formats", "crates/automation/tests/it/automation.rs::javascript_through_tools", "crates/js/src/tests.rs::an_xfa_form_sees_the_xfa_plugin_installed", "crates/js/src/tests.rs::a_plain_form_sees_the_xfa_version_but_not_installed", "crates/engine/src/tests.rs::scripts_in_an_xfa_form_see_the_xfa_plugin_installed"]
-notes = "boa (pure Rust) with the forms object model: event, getField/Field (value, readonly, display, colours, items, check boxes), app, util.printf/printd/printx, console, display, color; xfa_version (3.5) in every document and xfa_installed in XFA forms, so their version check passes; no annotations, layers or dialogs (app.response returns null) yet, and no app.findComponent."
++notes = "boa (pure Rust) with the forms object model: event, getField/Field (value, readonly, display, colours, items, check boxes), app (with execMenuItem for the side panels and Save As, from a button only, and Print where this.print() works), util.printf/printd/printx, console, display, color; xfa_version (3.5) in every document and xfa_installed in XFA forms, so their version check passes; no annotations, layers or dialogs (app.response returns null) yet, and no app.findComponent."
[[feature]]
id = "form.js-console"