mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-12-01 10:45:31 +01:00
## What this changes Part of #1429. The form in that report keeps pages it shows on demand as **page templates**: the name dictionary's `/Templates` tree names pages kept out of the page tree, and `/Pages` names visible ones (ISO 32000-2 §7.7.4). Until now their fields were listed as unplaced, and the form's JavaScript could not reach them, so buttons that add a page or show a section did nothing. - **organize**: `templates`, `spawn_template` and `set_template_hidden`. - A spawned page is a copy of the template page. - With renaming, its fields become fields of their own under `P<page>.<template>.<name>`. - Without renaming, its widgets join the template's fields and share their values. A template field that is its own widget is split into field + widget first. - Hiding a template takes its page out of the tree, keeps what it inherited (MediaBox, Resources, …) and refuses to hide the last page. - **js**: `this.templates`, `getTemplate(name)`, `Template.name`, `Template.hidden` (setting it shows or hides the page), `Template.spawn(nPage, bRename, bOverlay)` and `spawnPageFromTemplate`, with positional or named arguments and the documented defaults (0 / true / true). Non-number pages are clamped. - **engine**: new `Edit::SpawnTemplate` and `Edit::SetTemplateHidden`. - Both are checked against the assemble permission and applied in the same undo step as the script's field changes. - Only a button's or the console's script may change the pages. A field's own scripts (format, keystroke, validate, calculate) are told so on the console. - At most 100 template pages per script run, because each spawn copies the document. A loop that asks for more stops there with one console note. - **automation**: `page_templates` (read-only), `page_template_spawn` and `page_template_set_hidden`. - Undo labels translated in all 34 catalogues. Not in this PR: - `bOverlay` true (laying a template over an existing page). The script is told so on the console once and nothing changes. - `createTemplate` / `removeTemplate`. - An Organize-panel UI for templates. - `numPages` is not updated inside the running script. - Mouse Up actions on radio buttons and checkboxes (the other half of #1429) are in the open #2546. ## How you verified it - [x] `cargo fmt --check` - [x] clippy with `-D warnings` on the crates I changed (organize, js, engine, automation, ui-egui) - [x] tests pass on the crates I changed | Crate | Result | |---|---| | organize | 188 passed | | js | 97 passed | | engine lib | 382 passed | | engine it | 21 passed | | automation | 45 + 282 passed | | ui-egui i18n | 65 passed | - [x] `cargo xtask layers`, `cargo xtask assets` and `cargo xtask parity` **Tests.** - organize has 9 unit tests: listing, renamed and shared spawns, appending past the end, show/hide, the last page and unknown names refused, hostile template trees, repeated unrenamed copies, and inherited attributes kept when hiding. - js has 5 tests: listing, `spawn` defaults, both argument forms, `hidden` read back, and clamping. - engine has 5 integration tests: one undo step, renamed and shared spawns surviving save + reopen, overlay noted, field scripts refused, and the 100-page budget. - automation has 2 tool tests. Each behaviour was seen failing by reverting it: - no renaming - no field sharing - no MediaBox fallback - hidden page dropping inherited attributes - no budget - field scripts allowed to spawn - requests not applied - wrong default flags Under a loaded machine `tests::xfa_model::a_slow_engine_start_does_not_count_against_the_script` (an existing timing test) failed once and passes on its own. It is not touched here. ## Never-crash - [x] No new `unwrap`/`expect`/`panic!`/`unreachable!`/`todo!` outside tests, unless it is provably infallible with a comment saying why - [x] No indexing or slicing with input-derived positions; checked or saturating arithmetic; allocations sized by input are capped; recursion is bounded - Name trees are capped at 4096 names. - Field walks are capped at 32 levels and 100 000 nodes, with seen-sets. - Spawns are capped at 100 per script run. - Template names are clipped and charged to the script's output budget. - [x] No `unsafe` - [x] Any crash this fixes has a small synthetic regression test (no crash fixed; the hostile-tree test covers cyclic and malformed `/Templates`) ## Provenance - [x] No asset from an Adobe product, and no Adobe icon recreated, traced or imitated - [x] No font files committed here - [x] No GPL/AGPL code copied in - [x] Behaviour comes from the specification or black-box observation, **not** from reading another implementation's source. The sources are ISO 32000-2 §7.7.4 and §12.7.6 and the public Acrobat JavaScript API reference for `Template`/`spawnPageFromTemplate`. - [x] Every new asset has an `ATTRIBUTION.toml` entry and `cargo xtask assets` passes (no new assets) ## Security - [x] `--root` confinement still holds on every path, including ones reached by nested tool dispatch (the new tools take a document id, no paths) - [x] MCP and the UI control channel stay opt-in, loopback-only and token-authenticated - [x] No new outbound network access ## Parity and reach - [x] `parity/acrobat-features.toml` updated: `organize.page-templates` is now `partial`, with notes on what is still missing - [x] Reachable headlessly through an `automation` tool (`page_templates`, `page_template_spawn`, `page_template_set_hidden`, and `js_run` for scripts) - [x] Docs updated if this changes a documented behaviour (crate doc comments) ## Anything a reviewer should look at first - `share_fields_with_originals` in `crates/organize/src/import/fields.rs`: how an unrenamed copy's widgets are reparented onto the template's fields. When a template field is its own widget, it is split into a field with two widget kids. - The 100-page budget per script run in `crates/engine/src/js.rs`. Each spawn copies the document, so a `for` loop over `spawn` in a hostile form is bounded there.
89 lines
3.6 KiB
Rust
89 lines
3.6 KiB
Rust
//! pdfcraft-js — Acrobat JavaScript for forms (L3).
|
|
//!
|
|
//! Field scripts (format, keystroke, validate, calculate), button actions and document-level
|
|
//! scripts run in [boa](https://boajs.dev), a JavaScript engine written in Rust, with the subset
|
|
//! of Acrobat's object model that forms use, as the public *JavaScript for Acrobat API Reference*
|
|
//! describes it:
|
|
//!
|
|
//! - `event`: `value`, `change`, `willCommit`, `rc`, `name`, `type`, `target`, `targetName`.
|
|
//! - the document (`this`, also the global object): `getField`, `numFields`,
|
|
//! `getNthFieldName`, `numPages`, `pageNum`, `documentFileName`, `info`, `resetForm`,
|
|
//! `calculateNow`, `getPageBox`, `print`, `submitForm`, `mailDoc`, `exportDataObject`, `templates`,
|
|
//! `getTemplate`, `spawnPageFromTemplate`.
|
|
//! - Template objects: `name`, `hidden` (setting it shows or hides the template's page), `spawn`.
|
|
//! - Field objects: `name`, `type`, `value`, `valueAsString`, `defaultValue`, `readonly`,
|
|
//! `required`, `display`, `hidden`, `numItems`, `getItemAt`, `currentValueIndices`,
|
|
//! `isBoxChecked`, `checkThisBox`, `exportValues`, `charLimit`, `textColor`, `fillColor`,
|
|
//! `setFocus`, `rect`, appearance properties, and normal button caption/icon methods.
|
|
//! - `app` (`alert`, `beep`, `response`, `launchURL`, `execMenuItem`, `viewerType`, `viewerVersion`,
|
|
//! `platform`, `setTimeOut`), `util` (`printf`, `printd`, `printx`, `scand`), `console` (`println`,
|
|
//! `show`, `clear`), `display` and `color`.
|
|
//!
|
|
//! The native engine uses [`session::ScriptSession`] to preserve document globals across events.
|
|
//! Stateless runners remain available for callers that need isolation per invocation.
|
|
//!
|
|
//! The engine is a sandbox: no file, network or timer access exists, and loops and recursion
|
|
//! are bounded. Side effects (alerts, field changes, resets, printing, navigation) are returned
|
|
//! in an [`Outcome`] for the caller to apply.
|
|
|
|
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
|
|
|
use std::cell::RefCell;
|
|
use std::collections::BTreeSet;
|
|
use std::sync::Arc;
|
|
|
|
use boa_engine::context::HostHooks;
|
|
use boa_engine::object::builtins::{JsArray, JsDate, JsFunction};
|
|
use boa_engine::object::{FunctionObjectBuilder, ObjectInitializer};
|
|
use boa_engine::property::Attribute;
|
|
use boa_engine::realm::Realm;
|
|
use boa_engine::{Context, JsError, JsNativeError, JsObject, JsResult, JsString, JsValue, NativeFunction, Source, js_string};
|
|
|
|
mod document;
|
|
mod format;
|
|
mod globals;
|
|
mod host;
|
|
mod model;
|
|
mod nesting;
|
|
mod run;
|
|
mod source;
|
|
|
|
#[cfg(test)]
|
|
pub(crate) use format::PRINTD_SCANNED;
|
|
use format::{printd, printf, printx, scand};
|
|
pub use format::{set_date_names, with_english_printd};
|
|
pub(crate) use globals::menu_item;
|
|
use globals::platform;
|
|
use host::{arg, as_number, error, function, s, text};
|
|
pub use model::{
|
|
DISPLAY_HIDDEN, DISPLAY_NO_PRINT, DISPLAY_NO_VIEW, DISPLAY_VISIBLE, DocInfo, Event, FieldState, FieldType, Limits, Message, MessageButtons,
|
|
MessageIcon, Outcome, Panel, Request,
|
|
};
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
use run::SCRIPT_STACK;
|
|
use run::refuse;
|
|
pub(crate) use run::{CheckedCodeFromStrings, MAX_SCRIPT_BYTES};
|
|
pub use run::{run, run_shared, run_shared_calculations};
|
|
|
|
pub mod formcalc;
|
|
pub mod xfa;
|
|
pub mod xfa_session;
|
|
|
|
#[cfg(test)]
|
|
mod tests;
|
|
|
|
mod geometry;
|
|
pub use geometry::{rotate_rect, unrotate_rect};
|
|
|
|
mod icons;
|
|
mod ocg;
|
|
pub use ocg::Layer;
|
|
mod timers;
|
|
pub use timers::{MAX_TIMERS, MIN_TIMER_MS, Timer};
|
|
|
|
pub mod interaction;
|
|
pub mod session;
|
|
|
|
/// Maximum reset calls admitted by one document invocation, before argument conversion or field scans.
|
|
pub const MAX_RESET_CALLS: usize = 64;
|