89 Commits
Author SHA1 Message Date
Calixte Denizet cf8aa3c835 Require a user gesture to print, save, download or follow a link
A script can trigger some events on its own, e.g. a timer, or focus an
element under the pointer or the keyboard, hence the viewer has to decide
by itself whether an action comes from the user.

The events caused by a trusted user gesture (Action, Mouse Down and Mouse
Up) and the Open event, which may only print, get a one-time token. The
host keeps it out of the sandbox and attaches it to the commands sent
while the event is handled. The annotation actions which print, save,
download or follow a link are checked the same way, and only one of them
is honored per gesture. A gesture is ignored when the keyboard activates
an element focused by a script, or when it follows a layout change made
by a script too closely.
2026-11-15 15:05:22 +01:00
Calixte Denizet 290290c247 Only show a dialog from the sandbox while handling an event allowed by the viewer
Nothing coming from the sandbox can be trusted: it grants itself a user
activation for any event, and a script can trigger some events on its own
(e.g. a timer calling setFocus).

Hence the host only shows a dialog, once, while handling an event for
which the viewer allows it: the ones caused by the user, Open, and the
print/save ones. A focus change requested by a script no longer counts
as a user activation.
2026-11-15 15:01:19 +01:00
calixteman 280c0cc08c [JS] Prevent WillPrint actions from saving the document
Disable saving while running the WillPrint actions, as for WillSave: they
run right before saving a document flattened for printing, so a SaveAs
action would start another save.
2026-11-04 15:48:42 +01:00
calixteman 2c36ea08e2 [JS] Use private class members in the scripting Field object
Replace the Proxy wrapping the fields with private fields and methods,
which simplifies the code: the obj/wrapped pairs, the Proxy handler and
the PDFObject base class are gone. The doc and the event dispatcher use
helpers defined in a static block for the few field internals they need.

The fields share a prototype with the methods and accessors of Field,
instead of getting bound methods from the Proxy. As in Acrobat, a field
method can't be called without its field as `this`, nor be overwritten.
2026-10-25 15:34:06 +01:00
calixteman 5ac4b7379d [JS] Update the view from the field setters instead of the Proxy
The Proxy wrapping the fields no longer sends every assigned property to
the view nor copies it to the descendant fields. Instead, the setters of
the properties used by the view update the widgets and the descendants,
and the built-in properties of a non-terminal field are accessors
forwarding their value to the descendants.

Hence, as in Acrobat, a custom property stays on the field it's set on,
and nothing is sent for a rejected radio button value or a property
unknown to the view. The event dispatcher and the doc set values without
sending them, and the messages no longer contain a null `siblings`.
2026-10-25 15:34:06 +01:00
calixteman 6c87279d8b [JS] Use private class members in the scripting Doc object
The Doc object uses private class members. initSandbox and the event
dispatcher reach the few methods they need through helpers defined in a
static block, and a field gets callbacks, instead of the Doc instance,
to get its terminal children and to report errors.

The doc.info target has a null prototype, so that an info key such as
`constructor` doesn't give an Object.prototype member.
2026-10-25 15:34:06 +01:00
calixteman 8905bbe959 [JS] Use private class members in the scripting App object
The app object is no longer wrapped in a Proxy: it uses private class
members and, like the helper objects, looks like a plain object with
bound methods. initSandbox calls the timer callback through a helper
defined in a static block.

The timers use the Map, WeakMap and FinalizationRegistry methods
captured before any script runs, so that a script can't redefine them
to get the timer internals, e.g. to forge the data of a timeout handle
and cancel the user activation expiry.
2026-10-25 15:34:06 +01:00
calixteman 4bd7ad0518 [JS] Use private class members in the scripting event dispatcher
The event dispatcher uses private class members, and the events are
plain objects tagged as `Event` instead of Event class instances, so
that, as for the other scripting objects, `event.constructor` is
`Object` rather than an implementation class.
2026-10-25 15:09:47 +01:00
calixteman 3a4b1936ee [JS] Create the scripting event dispatcher in initSandbox
The event dispatcher and the map from widget ids to fields are created
in initSandbox instead of the App constructor, and the user activation
state is shared by the app, the doc and the event dispatcher, so that
they don't access each other's internals anymore.

The doc seen by the scripts (field.doc, app.activeDocs, and the source
and target of the doc and page events) is now the global object, which
already has the doc properties, instead of a Proxy on the Doc instance.
2026-10-25 15:09:47 +01:00
calixteman 75625ee279 [JS] Use private class members in the scripting helper objects
The color, console, util, app.fs and app.thermometer objects are no
longer wrapped in a Proxy: they use private class members and, like the
print params, they look like plain objects whose prototype has the
methods, bound to them, and the accessors of their class. The AForm
helpers use private members too.

app.fs, app.thermometer and the print params are created with their
owner instead of on first use, and Function.prototype.toString shows the
API functions as native ones.
2026-10-25 15:09:27 +01:00
calixteman 8d7e4ef1ce Wait for WillSave actions before saving
GenericScripting returns before WillSave actions run, so saving could miss
their field changes. Wait for WillSaveFinished before saving, even when
an action throws.

It fixes #22212.
2026-10-18 19:11:10 +02:00
calixteman fd1673149e Add scripting API regression tests
Cover global constants, grouped button updates, prototype extensions,
detached methods, descendant field properties, ResetForm events,
list box multipleSelection updates, unparsable dates on document open,
and added color enumeration.
2026-10-15 22:31:09 +02:00
Tim van der Meij 86780c65b1 Merge pull request #22177 from calixteman/scripting-original-eval
Use the original eval for PDF actions
2026-10-15 22:19:18 +02:00
Tim van der Meij c68276ca34 Merge pull request #22176 from calixteman/scripting-clear-timeout
Ignore invalid scripting timer handles
2026-10-15 21:57:11 +02:00
calixteman d76af2fd20 Use the original eval for PDF actions
Capture eval during initialization so PDF scripts cannot replace the
evaluator used for subsequent actions.
2026-10-13 22:02:06 +02:00
calixteman 692cfcce9f Ignore invalid scripting timer handles
Avoid passing unknown handles to FinalizationRegistry.unregister.
Clearing a handle that its timeout callback had set to null previously
threw and stopped the script.

Use each handle as its unregister token to remove its finalizer on
explicit cancellation.
2026-10-13 21:53:21 +02:00
Calixte Denizet afb2925791 Uncheck checkboxes assigned non-export values
RadioButtonField's setter ignores unknown values, so setting a checkbox to a
value which isn't its export value left it checked.
2026-10-13 20:48:41 +02:00
Calixte Denizet db41d37386 Register PageOpen/PageClose actions for every radio and checkbox widget
Only the actions of the first widget were registered, on the first widget's
page, so the PageOpen/PageClose actions of the other widgets never ran.
2026-10-13 20:48:41 +02:00
Calixte Denizet 7c94b0bd74 Ignore invalid widget indices and accept canonical numeric strings
The range checks let non-integer indices (e.g. 0.5, NaN, "abc" or undefined)
through, so checkThisBox set and sent an undefined widget ID and value.
2026-10-13 20:46:41 +02:00
Calixte Denizet a25970083e Make isDefaultChecked independent of the current value
When the default value was "Off", the checkbox override returned whether the
checkbox was currently unchecked instead of comparing the default value with
the widget's export value.
2026-10-13 20:46:41 +02:00
Calixte Denizet 1d324c4c75 Check the export value as well as the widget ID in isBoxChecked
The current widget ID is always set, even when nothing is checked (e.g. a
radio group whose value is "Off", or a checkbox without a value), so
isBoxChecked returned true for an unchecked widget.
2026-10-13 20:46:41 +02:00
calixteman 1e846788a8 Fix script error reporting
Handle thrown null values and failures in string conversion or stack
access. Report field errors through the document, since fields without
widgets have no send callback.

This prevents error reporting from skipping subsequent actions, field
formatting, and OpenAction.
2026-10-13 20:27:56 +02:00
calixteman e0b4f473cf Merge pull request #22149 from calixteman/open-action-once
Run OpenAction once on document open (PR #15822 follow-up)
2026-10-11 23:21:57 +02:00
calixteman fdf69503e7 Merge pull request #22148 from calixteman/radio-initial-checked
Fix initial radio button selection (PR 12637 follow-up)
2026-10-11 23:21:24 +02:00
calixteman 9f933f6ebc Merge pull request #22147 from calixteman/choice-current-value-indices
Fix choice-field currentValueIndices (PR 12826 follow-up)
2026-10-11 23:20:58 +02:00
calixteman 579c46b433 Run OpenAction once on document open (PR #15822 follow-up)
PR #15822 added an OpenAction call in _initActions while retaining the
call in _dispatchDocEvent, causing it to run twice on document open.
Remove the initialization call so OpenAction runs once, after formatting.
2026-10-11 21:54:13 +02:00
calixteman 3f745c0452 Fix initial radio button selection (PR 12637 follow-up)
Compare export values with data.value because _value is still null during
construction. This fixes isBoxChecked and action selection when a later
widget is initially selected, plus the "Off" toggle in two-button groups.
2026-10-11 21:45:33 +02:00
calixteman d069cfd7f5 Fix choice-field currentValueIndices (PR 12826 follow-up)
Keep the first sorted index for single selection, sort numerically,
and use export values, matching the value setter.
2026-10-11 21:31:33 +02:00
calixteman 635454a37f Fix repeated util.scand calls (PR 21681 follow-up)
Remove the cached regex's global flag so repeated calls with the same
format don't fall back to date guessing after a successful match.
2026-10-11 21:25:55 +02:00
Calixte Denizet 4d9d17e0f7 Keep scripting internals out of the global scope (issue 22121) 2026-10-09 21:51:07 +02:00
calixteman 659451533e Merge pull request #22017 from kwy404/printf-negative-fraction
Keep the sign of negative numbers greater than -1 in `util.printf`
2026-10-04 12:41:38 +02:00
kwy404 04c1e4dbdd Keep the sign of negative numbers greater than -1 in util.printf
For such numbers `Math.trunc` returns -0, hence the sign was lost when using the "f" conversion; e.g. `util.printf("%.2f", -0.5)` returned "0.50".
This also affected the `AFNumber_Format` and `AFPercent_Format` functions.

The 160F-2019.pdf ESC integration test now expects "-0,69": field 471R
(T.21) is computed as T.TOTREM - T.COTDED - N.10 - ..., so typing 0.69314
in N.10 (436R) makes it negative, and the old expectation only passed
because the sign was lost.
2026-10-03 17:48:33 -03:00
calixteman 40cc2082df Reset the descendants of non-terminal fields in Doc.resetForm
When the array given to `resetForm` contains the name of a non-terminal
field, only that field was reset, so its terminal descendants kept their
values. It's the sandbox counterpart of #22014.

Walk the `_kidIds` of the collected fields and add every descendant
before resetting them.
2026-09-29 17:29:36 +02:00
calixteman 31d20bc5ac Merge pull request #21965 from Shubham-Padkonde/fix/printd-escaped-literals
Preserve escaped literals in util.printd
2026-09-24 21:23:23 +02:00
Tim van der Meij 7ba4589a47 Enable the unicorn/prefer-short-arrow-method linting rule
This makes the code a bit more compact, and consistent with newer code
where we already use this. For more information about the rule, see
https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-short-arrow-method.md.
2026-09-22 20:16:39 +02:00
Shubham Padkonde 52fad1bce7 Preserve escaped literals in util.printd 2026-09-16 14:12:56 +05:30
calixteman fc01f7d033 Enable the regexp/no-super-linear-move ESLint rule
It flags the regexes whose search is quadratic in the input length, like
the autolinker and XFA-path ones fixed recently.

The three existing offenders: `\s*` matched the CSS indentation but also
the line terminators that make `^` match with the `m` flag (the
preprocessed CSS is unchanged), `(\d+)` made every digit of a number a
candidate start position, and `/T.*$/` could fail on the `$` and
backtrack since `.` doesn't match a line terminator.
2026-08-04 18:29:27 +02:00
Jonas Jenwald 1f9fbc764e Add scripts correctly in Field.prototype.setAction (PR 12569 follow-up)
In PR 12569 the `_actions` class-field was changed from an Object into a Map, with *most* of the code updated to reflect that.
However, in the `setAction` method it's still treated as an Object which means that any added script will simply be ignored. Most likely that part of the scripting-implementation isn't being used, since this code has been "wrong" for close to six years now.
2026-08-02 20:31:15 +02:00
Jonas Jenwald 62d211ee8d Use the toBeTrue() matcher consistently in the unit/font/integration tests
This replaces all `toEqual(true)` and `toBe(true)` occurrences.
2026-07-25 23:52:28 +02:00
Jonas Jenwald 6963b0ef4f Use the toBeFalse() matcher consistently in the unit/integration tests
This replaces all `toEqual(false)` and `toBe(false)` occurrences.
2026-07-25 22:49:03 +02:00
Jonas Jenwald 60340d9f28 Use the toBeNull() matcher consistently in the unit/integration tests
This replaces all `toEqual(null)` and `toBe(null)` occurrences.
2026-07-25 12:58:43 +02:00
Calixte Denizet d5661f801f Fix parsing of ambiguous date formats like Hm
We support having hours/minutes numbers with one or two digits for strings like "1:30:31" but
if the format is for example Hm, then "12" is really ambiguous.
So the idea is to match the longest string as possible.
2026-07-20 15:23:23 +02:00
Tim van der Meij 232506f4e8 Optimize memory usage in the unit tests
This commit fixes a number of missing cleanup steps in the unit tests
that kept state alive for longer than necessary:

- the loading tasks were not all being destroyed;
- the find controllers were not being reset;
- the state set in `beforeAll`/`beforeEach` was not all being nulled in
  the correspoding `afterAll`/`afterEach` blocks.

Combined this resulted in a steady increase in memory usage of the test
process as the tests ran, climbing up to ~1.5 GB. After this patch the
memory usage remains stable at ~800 MB.
2026-04-30 12:35:59 +02:00
Calixte Denizet 57ce4f8f43 Use a HTML date/time input when a field requires a date or a time.
The user will be able to enter a date in the format corresponding to their locale
and it'll be formatted in using the format provided by the pdf.
2025-07-24 22:01:45 +02:00
Calixte Denizet 2541d96bf5 [JS] Make the date parser less strict
and display the expected date formt as a tooltip.
2025-06-20 08:57:07 +02:00
Jonas Jenwald 0845552ff9 Use Array-destructuring when computing MIN/MAX in AFSimple_Calculate
This appears to work fine with QuickJS, as evident by the added unit-test, and allows us to remove more `Array.prototype.reduce` usage.
2025-04-06 11:44:08 +02:00
Calixte Denizet 3dd8752283 Let be more tolerant with predefined phone number format
The long US phone number format should be '(999) 999-9999' but the parenthesis
can be omitted or the space replaced with a dash, ...
2025-01-06 15:24:19 +01:00
Calixte Denizet d1db8d6294 Avoid to display an alert or a confirm dialog if the message is empty
It fixes #19171.
2024-12-05 21:04:07 +01:00
Calixte Denizet 2a337082c0 Build date consistently (in term of tz) when executing some embedded JS (bug 1934157)
The date was create in UTC+0 and then amended in using set-Month/Date which take into account
the user timezone.
With this patch we build all the date in the user timezone.
2024-11-29 19:50:21 +01:00
Calixte Denizet 230d7f9229 Make util.scand a bit more flexible with dates which don't match the given format (bug 1910431) 2024-10-27 19:19:06 +01:00