@botiverse/porthole 0.15.2 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/CHANGELOG.md +89 -0
  2. package/dist/bridge/bridge.d.ts +1 -8
  3. package/dist/bridge/bridge.js +0 -7
  4. package/dist/check/page-source.js +1 -1
  5. package/dist/check/page.js +1 -1
  6. package/dist/compile/policy.d.ts +0 -7
  7. package/dist/compile/policy.js +1 -10
  8. package/dist/config/capabilities.d.ts +8 -14
  9. package/dist/config/capabilities.js +4 -5
  10. package/dist/config/schema.d.ts +2 -13
  11. package/dist/config/validate.js +0 -24
  12. package/dist/conformance/behavior.js +26 -37
  13. package/dist/conformance/clipboard.js +47 -1
  14. package/dist/conformance/harness-source.js +1 -1
  15. package/dist/displays/displays.js +1 -3
  16. package/dist/guide/limits.js +1 -1
  17. package/dist/guide/runtime.js +1 -1
  18. package/dist/host/create-host.d.ts +1 -1
  19. package/dist/host/create-host.js +6 -51
  20. package/dist/host/door.d.ts +1 -1
  21. package/dist/host/locate.d.ts +1 -1
  22. package/dist/host/locate.js +2 -2
  23. package/dist/host/outcome.d.ts +5 -12
  24. package/dist/host/outcome.js +8 -16
  25. package/dist/host/types.d.ts +6 -22
  26. package/dist/index.d.ts +2 -4
  27. package/dist/index.js +2 -4
  28. package/dist/isolation/served.d.ts +9 -14
  29. package/dist/isolation/served.js +17 -31
  30. package/dist/isolation/types.d.ts +2 -19
  31. package/dist/isolation/types.js +0 -17
  32. package/dist/platform/focus-field.d.ts +11 -0
  33. package/dist/platform/focus-field.js +18 -0
  34. package/dist/platform/types.d.ts +6 -22
  35. package/dist/platform/web.js +8 -98
  36. package/dist/presets/presets.d.ts +0 -2
  37. package/dist/presets/presets.js +0 -1
  38. package/dist/react/frame.d.ts +0 -8
  39. package/dist/react/frame.js +0 -4
  40. package/dist/runtime/contract.d.ts +2 -2
  41. package/dist/runtime/contract.js +2 -2
  42. package/dist/runtime/types.generated.js +6 -6
  43. package/dist/server/files.js +7 -23
  44. package/dist/server/host.d.ts +1 -1
  45. package/dist/server/index.d.ts +1 -1
  46. package/dist/server/index.js +4 -6
  47. package/dist/server/naming.js +1 -1
  48. package/dist/server/pages.d.ts +9 -2
  49. package/dist/server/pages.js +28 -8
  50. package/dist/server/server.js +28 -166
  51. package/dist/server/types.d.ts +7 -7
  52. package/dist/shell/plan.js +2 -2
  53. package/dist/shell/runtime.js +1 -9
  54. package/docs/ADOPTING.md +42 -135
  55. package/docs/ARCHITECTURE.md +0 -33
  56. package/docs/DECISIONS.md +296 -9
  57. package/docs/DESIGN.md +27 -50
  58. package/docs/THREAT_MODEL.md +140 -242
  59. package/package.json +1 -1
  60. package/src/bridge/bridge.ts +1 -17
  61. package/src/check/page.ts +1 -1
  62. package/src/compile/policy.ts +1 -11
  63. package/src/config/capabilities.ts +8 -14
  64. package/src/config/schema.ts +2 -13
  65. package/src/config/validate.ts +0 -28
  66. package/src/conformance/behavior.ts +50 -52
  67. package/src/conformance/clipboard.ts +56 -1
  68. package/src/displays/displays.ts +1 -3
  69. package/src/guide/limits.ts +1 -1
  70. package/src/guide/runtime.ts +1 -1
  71. package/src/host/create-host.ts +6 -52
  72. package/src/host/door.ts +1 -1
  73. package/src/host/locate.ts +2 -2
  74. package/src/host/outcome.ts +8 -21
  75. package/src/host/types.ts +6 -20
  76. package/src/index.ts +2 -4
  77. package/src/isolation/served.ts +18 -37
  78. package/src/isolation/types.ts +2 -24
  79. package/src/platform/focus-field.ts +18 -0
  80. package/src/platform/types.ts +6 -21
  81. package/src/platform/web.ts +8 -116
  82. package/src/presets/presets.ts +0 -3
  83. package/src/react/frame.tsx +0 -10
  84. package/src/runtime/contract.ts +4 -4
  85. package/src/runtime/types.generated.ts +6 -6
  86. package/src/server/files.ts +4 -19
  87. package/src/server/host.ts +1 -1
  88. package/src/server/index.ts +4 -16
  89. package/src/server/naming.ts +1 -1
  90. package/src/server/pages.ts +38 -12
  91. package/src/server/server.ts +29 -189
  92. package/src/server/types.ts +7 -7
  93. package/src/shell/plan.ts +2 -2
  94. package/src/shell/runtime.ts +1 -9
  95. package/types/runtime/v1/artifact.d.ts +2 -3
  96. package/types/runtime/v1/assets.d.ts +1 -1
  97. package/types/runtime/v1/chat.d.ts +2 -2
  98. package/types/runtime/v1/comments.d.ts +1 -1
  99. package/types/runtime/v1/downloads.d.ts +2 -2
  100. package/types/runtime/v1/links.d.ts +2 -2
  101. package/dist/platform/focus-guard.d.ts +0 -252
  102. package/dist/platform/focus-guard.js +0 -764
  103. package/dist/server/nested.d.ts +0 -66
  104. package/dist/server/nested.js +0 -120
  105. package/dist/shell/frame.d.ts +0 -28
  106. package/dist/shell/frame.js +0 -338
  107. package/src/platform/focus-guard.ts +0 -999
  108. package/src/server/nested.ts +0 -156
  109. package/src/shell/frame.ts +0 -371
@@ -1,999 +0,0 @@
1
- /**
2
- * The focus guard (P109): a display's frame can move the keyboard into itself whenever it likes
3
- * (`element.focus()` in its own realm; on Chromium and WebKit with no gesture at all), and what the
4
- * person goes on typing into the host's composer or chat box then goes to the page. Porthole can't
5
- * stop the call, which runs in the content's realm, but the host's page can undo it: when focus
6
- * leaves one of its text fields for a display's frame and the person didn't move it there, focus
7
- * goes back once the engine has finished moving it, the guard watches that it stays back, and the
8
- * display hears `focusTaken`; a display whose frame keeps doing it is stopped (`createHost`).
9
- *
10
- * What the host's page can see of a move into a frame, measured on Chromium, WebKit and Firefox
11
- * (P109): Chromium and WebKit blur the host's window as it happens, and a press on the frame (a
12
- * click, a tap, held or not) leaves the frame element `:active` then; a Tab is the host's own
13
- * keydown just before. Firefox doesn't blur the window and shows no `:active` (a focusout on the
14
- * field was seen in one review's runs, not in another's): its `document.activeElement` changes,
15
- * which the guard reads at each animation frame while a frame is guarded, and the pointer over the
16
- * frame (`:hover`, `pointerover`) is all it can tell of a press. A page that takes focus back on
17
- * its own blur (which a take-back fires) leaves Firefox's focus on nothing (`<body>`) or in the
18
- * frame again (the review of the guard, GK-1): the guard keeps watching after each take-back and
19
- * counts one that didn't hold. Every engine keeps a gesture's transient activation for 5 s
20
- * (measured: 5.04 to 5.10 s).
21
- *
22
- * A nested page's shell (P111) says when its activation turned on, which a press in the page does,
23
- * and so does a key the person typed into it: one meant for the host's composer, after the page took
24
- * the keyboard and before the guard took it back (the audit of P111's guard, NG-1). Neither the
25
- * shell nor the host's page can tell the two apart from the activation (the shell gets no event as
26
- * focus comes into its child, measured), so (P120): in a fight over a text field the shell's word
27
- * isn't asked; a wait on a move from a text field doesn't end with the frame holding focus unjudged;
28
- * and where the host's page sees the pointer over its frames, an activation while it isn't over the
29
- * frame and the person was typing is no press.
30
- */
31
-
32
- /** How long the person's Tab counts as moving focus (the move follows its keydown at once). */
33
- export const TAB_MS = 1_000;
34
-
35
- /**
36
- * How long transient activation lasts after a gesture (measured: 5 s on all three engines): a
37
- * host page with activation and no gesture of its own for longer had one in a frame.
38
- */
39
- export const ACTIVATION_SPAN_MS = 5_000;
40
-
41
- /** The person is typing: a key on the host's page within this long. */
42
- export const TYPING_MS = 1_000;
43
-
44
- /**
45
- * No press of the person's lands on a frame this soon after a key they typed on the host's page
46
- * (P122): a hand on the keys doesn't reach the pointer and press that fast (the Keystroke-Level
47
- * Model gives 400 ms to the hand's move from the keys to the mouse alone; Card, Moran and Newell,
48
- * 1980). A move into a nested page's frame, or its shell's activation turning on, within this long
49
- * is the person's next key landing in the page, which turns its activation on as a press does.
50
- * Where neither the host's page nor the shell sees the pointer (full Chromium), it holds a page that
51
- * takes the keyboard while the person types even when their key outruns the shell's answer.
52
- */
53
- export const KEY_PRESS_MS = 250;
54
-
55
- /**
56
- * After a take-back, the guard keeps watching (each animation frame) until focus has stayed in the
57
- * text field this long: a page that takes it again at once (on its own blur, which the take-back
58
- * fires) is caught as it does, on Firefox too (P109, after the review of the guard: GK-1).
59
- */
60
- export const HELD_MS = 300;
61
-
62
- /** A take-back that hasn't put focus back in the field after this long failed: counted, and tried again. */
63
- export const GIVE_BACK_MS = 50;
64
-
65
- /**
66
- * A frame that holds focus isn't the person's for this long after they press the host's page: the
67
- * press gives the page activation before focus leaves the frame (on Firefox, a move between
68
- * processes), and a call the frame sends in between would borrow it.
69
- */
70
- export const HOST_PRESS_MS = 1_000;
71
-
72
- /**
73
- * When a display is stopped for taking the keyboard (P109): focus given back can't stop its frame
74
- * calling `focus()` again (nothing the host sets on the frame does, `inert` included: measured),
75
- * and each try leaves it the keys typed until focus is back. So three undone moves within 10 s
76
- * that came while the person typed on the page or that kept focus from going back (a take-back that
77
- * failed: the keys then go to the frame, and the page sees no typing to count), or five at all
78
- * within 2 s, stop it. A person's own move never fails a take-back.
79
- */
80
- export const TAKES = {
81
- typing: { most: 3, withinMs: 10_000 },
82
- any: { most: 5, withinMs: 2_000 },
83
- } as const;
84
-
85
- /**
86
- * A nested page's shell (P111) says its own activation turned on: a press or key of the person's
87
- * in that page (activation goes up to ancestors, never down, so nothing on the host's page turns
88
- * it on). A move of focus into the frame within this long of it, with no press or key on the
89
- * host's page since, is the person's; so is a `comments.open` the page sends within it.
90
- */
91
- export const SHELL_GESTURE_MS = 1_000;
92
-
93
- /**
94
- * How long a move into a nested page's frame waits for its shell's word before it is taken for
95
- * the page's: the shell says what it saw at its next animation frame, after the host's page
96
- * already heard the move. A shell that says nothing in time (gone, stalled) has its frame's move
97
- * taken back, as any page's.
98
- */
99
- export const SHELL_WAIT_MS = 500;
100
-
101
- /**
102
- * A shell's word that came this long before the host's page heard the move is about that move
103
- * (the shell's frame can come first); one from before then is about an earlier one.
104
- */
105
- export const SHELL_EARLY_MS = 150;
106
-
107
- /**
108
- * A shell that went this long without a look, or without a word to the host (its heartbeat is every
109
- * 50 ms), was stalled (P111, after its review): where the page shares the shell's thread (WebKit,
110
- * Firefox), a page that holds it from its own click handler pushes the moment the shell sees its
111
- * activation turn on past the person's next keys on the host's page. An activation it saw then is
112
- * uncertain: it is no one's gesture until it lapses.
113
- */
114
- export const SHELL_STALL_MS = 100;
115
-
116
- /** What a nested page's shell last said (P111), on the host's clock. */
117
- export interface ShellWord {
118
- /** Whether its engine tells activation at all (`navigator.userActivation`). */
119
- readonly supported: boolean;
120
- /** When its activation last turned on; -Infinity if it hasn't, or is off. */
121
- readonly activatedAt: number;
122
- /** Whether it last said the page's focus is in it, and when it last said focus came in. */
123
- readonly focused: boolean;
124
- readonly focusedAt: number;
125
- /** Its activation turned on while it was stalled: uncertain until it goes off (`SHELL_STALL_MS`). */
126
- readonly uncertain?: boolean;
127
- /**
128
- * Its activation turned on within `TYPING_MS` of the person's typing on the host's page, the
129
- * pointer not come over the frame since (`pressPossible`, judged as it turned on): where the
130
- * host's page sees the pointer, their key landing in the page, no press (P120).
131
- */
132
- readonly keyed?: boolean;
133
- }
134
-
135
- /** One move of the keyboard the guard undid. */
136
- export interface FocusTake {
137
- /** The person pressed a key on the host's page within the last second. */
138
- readonly typing: boolean;
139
- /**
140
- * Focus didn't stay back in the field: the frame kept it, took it again within `HELD_MS`, or it
141
- * landed nowhere.
142
- */
143
- readonly failed: boolean;
144
- }
145
-
146
- /** What the host's page knew when focus left one of its text fields for a display's frame. */
147
- export interface FocusMove {
148
- readonly now: number;
149
- /** The engine told the host's page as it happened (its window blurred): Chromium, WebKit. */
150
- readonly seen: boolean;
151
- /** The frame element is `:active`: a press on it, pointer or touch (Chromium, WebKit). */
152
- readonly pressed: boolean;
153
- /** The pointer is over the frame element (`:hover`). */
154
- readonly over: boolean;
155
- /** When the pointer last came over the frame element (`pointerover`); -Infinity if never. */
156
- readonly overSince: number;
157
- /** The person's last key on the host's page, and when. */
158
- readonly lastKey: { readonly key: string; readonly at: number } | null;
159
- /** When the person last typed on the host's page (a key other than Tab and the modifiers). */
160
- readonly typedAt: number;
161
- /** When the text field focus left got it. */
162
- readonly focusedAt: number;
163
- /** When the host's page last saw a gesture of its own (a key, a press, a click, typed text). */
164
- readonly gestureAt: number;
165
- /** Whether the host's page has transient activation now; undefined where it can't tell. */
166
- readonly activation: boolean | undefined;
167
- /** The host moved focus into the frame itself (`DisplayHandle.focus()`), just now. */
168
- readonly hostMoved: boolean;
169
- /**
170
- * A nested page's frame (P111): what its shell said, and when the host's page last had a
171
- * trusted press or key of its own. Absent for any other frame.
172
- */
173
- readonly shell?: {
174
- readonly word: ShellWord;
175
- readonly hostInputAt: number;
176
- /** The host's page sees the pointer over its frames on this engine (`pressPossible`, P120). */
177
- readonly pointerShown?: boolean;
178
- };
179
- }
180
-
181
- /**
182
- * Whether the person moved focus into the frame themselves:
183
- * - the host did (`DisplayHandle.focus()`);
184
- * - by the keyboard: their last key on the host's page was Tab (or Shift+Tab), just now;
185
- * - by a press on the frame: where the engine tells the move as it happens (Chromium, WebKit),
186
- * the frame element is `:active`; where it doesn't (Firefox), the pointer is over the frame and
187
- * came over it after the text field got focus and after the person last typed in the host's page
188
- * (a page can't move the pointer; a composer that opened beside the frame while the pointer
189
- * rested over it, and a person typing with it there, keep their keys);
190
- * - by a gesture the host's own events don't explain: the host's page has transient activation,
191
- * and for longer than activation lasts it saw no gesture of its own and the text field has had
192
- * focus, so the person pressed or activated something in a frame since (an assistive
193
- * technology's activation, which moves no pointer and sends the host no key, is one). The
194
- * gesture that opened a composer (a selection in the frame, `selected`) is older than the
195
- * composer's focus, and doesn't count.
196
- * Anything else is the page's: focus goes back.
197
- */
198
- export function personMoved(move: FocusMove): boolean {
199
- if (move.hostMoved) {
200
- return true;
201
- }
202
- if (move.lastKey !== null && move.lastKey.key === "Tab" && move.now - move.lastKey.at <= TAB_MS) {
203
- return true;
204
- }
205
- // A nested page (P111): its shell's activation turned on within the last second, and the host's
206
- // page had no press or key of its own since (the person went back to it), where it may have been
207
- // a press (P120) and not within `KEY_PRESS_MS` of the person's last key (P122); or, where the
208
- // engine tells it, a press on the frame. Nothing else: no pointer over it, no host-wide
209
- // activation.
210
- if (move.shell !== undefined && move.shell.word.supported) {
211
- return (
212
- (move.seen && move.pressed) ||
213
- (move.now - move.typedAt >= KEY_PRESS_MS &&
214
- shellGesture(move.shell.word, move.shell.hostInputAt, move.now) &&
215
- !(move.shell.pointerShown === true && move.shell.word.keyed === true))
216
- );
217
- }
218
- if (
219
- move.seen ? move.pressed : move.over && move.overSince > Math.max(move.typedAt, move.focusedAt)
220
- ) {
221
- return true;
222
- }
223
- return (
224
- move.activation === true &&
225
- move.now - Math.max(move.gestureAt, move.focusedAt) > ACTIVATION_SPAN_MS
226
- );
227
- }
228
-
229
- /**
230
- * Whether a nested page's shell saw the person's gesture just now (P111): its activation turned on
231
- * within `SHELL_GESTURE_MS`, after the host's page's last trusted press or key.
232
- */
233
- export function shellGesture(word: ShellWord, hostInputAt: number, now: number): boolean {
234
- return (
235
- word.supported &&
236
- now - word.activatedAt <= SHELL_GESTURE_MS &&
237
- now >= word.activatedAt &&
238
- hostInputAt < word.activatedAt
239
- );
240
- }
241
-
242
- /**
243
- * Whether the person could have pressed the frame just before `at` (P120): a nested page's shell
244
- * says its activation turned on, which a key typed into the page does as well as a press. Where the
245
- * host's page sees the pointer over its frames (`shown`: Firefox; Chromium and WebKit where the frame
246
- * is in their process, not full Chromium), a press needs the pointer over the frame, come over it
247
- * since the person last typed on the host's page; within `TYPING_MS` of that typing, an activation
248
- * with the pointer elsewhere is their keys landing in the page. Anywhere else it may be a press.
249
- */
250
- export function pressPossible(
251
- shown: boolean,
252
- over: boolean,
253
- overSince: number,
254
- typedAt: number,
255
- at: number,
256
- ): boolean {
257
- return !shown || at - typedAt > TYPING_MS || (over && overSince > typedAt);
258
- }
259
-
260
- /** Keys that type nothing on their own: they don't count as typing (a gesture still). */
261
- const MODIFIERS = new Set(["Shift", "Control", "Alt", "Meta", "AltGraph", "CapsLock", "Fn"]);
262
-
263
- /** The `<input>` types that take typed text (an unknown type is `text`). */
264
- const TEXT_INPUTS = new Set(["text", "search", "url", "tel", "email", "password", "number"]);
265
-
266
- /** Whether `element` takes the person's typed text: a text field, a text area, an editable region. */
267
- export function takesText(element: Element | null): element is HTMLElement {
268
- if (element === null) {
269
- return false;
270
- }
271
- switch (element.localName) {
272
- case "textarea":
273
- return true;
274
- case "input":
275
- return TEXT_INPUTS.has((element as HTMLInputElement).type);
276
- default:
277
- return (element as Partial<HTMLElement>).isContentEditable ?? false;
278
- }
279
- }
280
-
281
- /** The element that has focus, inside open shadow roots too. */
282
- function deepActive(document: Document): Element | null {
283
- let active = document.activeElement;
284
- for (
285
- let inner = active?.shadowRoot?.activeElement;
286
- inner !== null && inner !== undefined;
287
- inner = active?.shadowRoot?.activeElement
288
- ) {
289
- active = inner;
290
- }
291
- return active;
292
- }
293
-
294
- /** One page's guard, shared by every display's frame on it. */
295
- export interface FocusGuard {
296
- /**
297
- * Guards `frame`: `taken` hears each move into it that was undone. `nested`: it holds a nested
298
- * page's shell (P111), whose word (`shell`) decides the moves into it.
299
- */
300
- add(
301
- frame: HTMLIFrameElement,
302
- taken: (take: FocusTake) => void,
303
- options?: {
304
- readonly nested?: boolean;
305
- /** Asks a nested page's shell to look now (P120); it answers with a report naming `n`. */
306
- readonly ask?: (n: number) => void;
307
- },
308
- ): () => void;
309
- /** What a nested page's shell said (P111): its activation, its focus. */
310
- shell(
311
- frame: HTMLIFrameElement,
312
- said: {
313
- readonly supported: boolean;
314
- readonly ago: number | null;
315
- readonly focused: boolean;
316
- /** How long the shell went without a look before this one (its own clock). */
317
- readonly gap: number;
318
- /** The ask this report answers (P120), if it answers one. */
319
- readonly asked?: number;
320
- },
321
- ): void;
322
- /** A nested page's shell's heartbeat (P111): it is looking. */
323
- beat(frame: HTMLIFrameElement): void;
324
- /** Whether a nested page's shell saw the person's gesture just now (`shellGesture`). */
325
- gesture(frame: HTMLIFrameElement): boolean;
326
- /** The host moves focus into `frame` itself. */
327
- focus(frame: HTMLIFrameElement): void;
328
- /**
329
- * Whether the page's focus is in `frame` now, the person's own (once any move into it was
330
- * judged; on an engine that tells the host's page nothing, here and then).
331
- */
332
- focused(frame: HTMLIFrameElement): boolean;
333
- /** The host moves focus into one of its text fields: see `focusField`. */
334
- field(field: HTMLElement): void;
335
- }
336
-
337
- interface Guarded {
338
- readonly taken: Set<(take: FocusTake) => void>;
339
- overSince: number;
340
- /** A nested page's shell's word (P111); null for any other frame. */
341
- word: ShellWord | null;
342
- /** When the host last heard the shell (a report or a heartbeat). */
343
- heardAt: number;
344
- /** Asks the shell to look now (P120), the last ask's number, and whether it was answered. */
345
- readonly ask: ((n: number) => void) | undefined;
346
- asked: number;
347
- answered: boolean;
348
- }
349
-
350
- const guards = new WeakMap<Window, FocusGuard>();
351
-
352
- /** The guard for `view`'s page, made on first use. */
353
- export function focusGuard(view: Window): FocusGuard {
354
- const existing = guards.get(view);
355
- if (existing !== undefined) {
356
- return existing;
357
- }
358
- const document = view.document;
359
- const frames = new Map<HTMLIFrameElement, Guarded>();
360
- const now = () => view.performance.now();
361
- let known: Element | null = deepActive(document);
362
- let focusedAt = now();
363
- let lastKey: { key: string; at: number } | null = null;
364
- let typedAt = -Infinity;
365
- /** When the person typed on the host's page, the last two seconds of it (P122). */
366
- const typings: number[] = [];
367
- const typed = (at: number) => {
368
- typedAt = at;
369
- typings.push(at);
370
- while (typings.length > 0 && at - (typings[0] as number) > 2 * TYPING_MS) {
371
- typings.shift();
372
- }
373
- };
374
- /** The person's last typing on the host's page at or before `at` (-Infinity: none known). */
375
- const typedBefore = (at: number): number => {
376
- for (let index = typings.length - 1; index >= 0; index -= 1) {
377
- const each = typings[index] as number;
378
- if (each <= at) {
379
- return each;
380
- }
381
- }
382
- return -Infinity;
383
- };
384
- let gestureAt = -Infinity;
385
- let pressAt = -Infinity;
386
- /** The host's page's last trusted press or key (P111: a nested page's gesture must be later). */
387
- let inputAt = -Infinity;
388
- /**
389
- * A move into a nested page's frame waiting for its shell's word (P111): from a text field
390
- * (`field`: taken back unless the shell says the person's), or from anything else (null: only
391
- * whether the frame is the person's for a gesture gate).
392
- */
393
- let waiting: {
394
- readonly field: HTMLElement | null;
395
- readonly frame: HTMLIFrameElement;
396
- readonly guarded: Guarded;
397
- readonly since: number;
398
- } | null = null;
399
- /**
400
- * Whether the host's page sees the pointer over its frames (P120): learned once the pointer comes
401
- * over one (`pointerover` on its element: Firefox, and Chromium and WebKit where frames are in their
402
- * process), or a move from a text field into one doesn't blur the window (Firefox, which shows
403
- * the pointer over a frame, measured). Full Chromium shows neither, and never learns it.
404
- */
405
- let pointerShown = false;
406
- /** When focus last went into a guarded frame, and whether the person moved it there. */
407
- let enteredAt = -Infinity;
408
- let earned = false;
409
- /** The text field the person last pressed, when, and what had focus as they pressed it. */
410
- let pressed: {
411
- readonly field: HTMLElement;
412
- readonly at: number;
413
- readonly from: Element | null;
414
- } | null = null;
415
- let hostMove: { frame: HTMLIFrameElement; at: number } | null = null;
416
- let polling = 0;
417
- /**
418
- * A take-back in progress: the field focus goes back to, the frame it came from, when focus was
419
- * last given back, and since when it has stayed in the field. Ends once it has stayed `HELD_MS`,
420
- * the person moves focus somewhere of their own, or the field or frame is gone. A `fight` undoes
421
- * the page's own move (a take-back, `focusField`, or the person leaving a frame that took focus
422
- * itself), not the person leaving a frame they moved into (P120).
423
- */
424
- let contest: {
425
- readonly field: HTMLElement;
426
- readonly frame: HTMLIFrameElement;
427
- readonly guarded: Guarded;
428
- readonly fight: boolean;
429
- gaveAt: number;
430
- heldSince: number | null;
431
- } | null = null;
432
-
433
- const activation = (): boolean | undefined => {
434
- try {
435
- const state = (view.navigator as Navigator & { userActivation?: { isActive: boolean } })
436
- .userActivation;
437
- return state === undefined ? undefined : state.isActive;
438
- } catch {
439
- return undefined;
440
- }
441
- };
442
-
443
- /**
444
- * What the person's move would need, read now: see `personMoved`. Into a nested page's frame
445
- * whose shell hasn't spoken of this move yet, `wait` (P111). In a fight over a text field
446
- * (`fight`, P120), a nested page's frame is judged as any frame is, its shell's word not asked:
447
- * an activation it says turned on then is the person's keys landing in it far more often than a
448
- * press (a press back into the page within `HELD_MS` of focus landing in the field).
449
- */
450
- const judge = (
451
- frame: HTMLIFrameElement,
452
- guarded: Guarded,
453
- seen: boolean,
454
- at: number,
455
- fight = false,
456
- ): boolean | "wait" => {
457
- const word = fight ? null : guarded.word;
458
- const over = frame.matches(":hover");
459
- const moved = personMoved({
460
- now: at,
461
- seen,
462
- pressed: frame.matches(":active"),
463
- over,
464
- overSince: guarded.overSince,
465
- lastKey,
466
- typedAt,
467
- focusedAt,
468
- gestureAt,
469
- activation: activation(),
470
- hostMoved: hostMove !== null && hostMove.frame === frame && at - hostMove.at <= TAB_MS,
471
- ...(word === null ? {} : { shell: { word, hostInputAt: inputAt, pointerShown } }),
472
- });
473
- hostMove = null;
474
- // The shell tells a move at its next frame, the host's page hears it first: until the shell
475
- // said focus came in (around now), its activation may be on its way; unless no press could
476
- // have made the move (P120), which nothing the shell says changes.
477
- if (
478
- !moved &&
479
- word !== null &&
480
- word.supported &&
481
- word.focusedAt < at - SHELL_EARLY_MS &&
482
- at - typedAt >= KEY_PRESS_MS &&
483
- pressPossible(pointerShown, over, guarded.overSince, typedAt, at)
484
- ) {
485
- return "wait";
486
- }
487
- return moved;
488
- };
489
-
490
- /** Waits for a nested page's shell's word on a move (P111): decided by `settle`. */
491
- const wait = (
492
- field: HTMLElement | null,
493
- frame: HTMLIFrameElement,
494
- guarded: Guarded,
495
- at: number,
496
- ) => {
497
- waiting = { field, frame, guarded, since: at };
498
- // The shell looks at its next frame, and a key the person types into the page before then
499
- // turns its activation on as a press does: asked to look now, it says what it saw as the move
500
- // was heard (P120).
501
- guarded.asked += 1;
502
- guarded.answered = false;
503
- guarded.ask?.(guarded.asked);
504
- known = frame;
505
- enteredAt = at;
506
- earned = false;
507
- schedule();
508
- };
509
-
510
- /**
511
- * A waiting move, once its shell spoke or the wait is over: the person's (the frame keeps
512
- * focus), or the page's (from a text field, taken back). A move elsewhere since ends it.
513
- */
514
- const settle = (at: number) => {
515
- const held = waiting;
516
- if (held === null) {
517
- return;
518
- }
519
- const { field, frame, guarded } = held;
520
- const active = deepActive(document);
521
- if (active !== frame || !frames.has(frame)) {
522
- waiting = null;
523
- // Moved on since to nothing, or back to the field, with no press of the person's on the
524
- // host's page: still the page's move from that field (P120, after the audit of P111's guard:
525
- // NG-1, where the frame took it again at once and was then let be), watched as a take-back
526
- // is, focus on nothing given back and counted.
527
- if (
528
- field !== null &&
529
- field.isConnected &&
530
- pressAt <= held.since &&
531
- (active === null || active === document.body || active === field)
532
- ) {
533
- known = field;
534
- contest = { field, frame, guarded, fight: true, gaveAt: held.since, heldSince: null };
535
- schedule();
536
- return;
537
- }
538
- // Anywhere else: from the frame, as the next look judges it.
539
- known = frame;
540
- return;
541
- }
542
- const word = guarded.word;
543
- // The shell's activation, where a press could have made it: within a second of the person's
544
- // typing on the host's page with the pointer elsewhere, it is their keys landing in the page
545
- // (P120). Read as of the move.
546
- const theirs =
547
- word !== null && shellGesture(word, inputAt, at) && !(pointerShown && word.keyed === true);
548
- // It said focus came in around the move, or answered the ask the move made with focus in it.
549
- const spoke =
550
- word !== null &&
551
- (word.focusedAt >= held.since - SHELL_EARLY_MS || (guarded.answered && word.focused));
552
- if (theirs) {
553
- waiting = null;
554
- known = frame;
555
- earned = true;
556
- return;
557
- }
558
- if (!spoke && at - held.since < SHELL_WAIT_MS) {
559
- return;
560
- }
561
- waiting = null;
562
- if (field !== null && field.isConnected) {
563
- takeBack(field, frame, guarded, at, false);
564
- }
565
- };
566
-
567
- /**
568
- * Focus goes back to `field` once the engine has finished the move: given back inside its blur
569
- * it stays with the frame on Chromium and WebKit, and Firefox takes it back only from a frame
570
- * that was blurred first (measured). Unless the person has since put focus somewhere of their own.
571
- */
572
- const giveBack = (field: HTMLElement, frame: HTMLIFrameElement) => {
573
- view.setTimeout(() => {
574
- const current = deepActive(document);
575
- if (
576
- field.isConnected &&
577
- (current === frame || current === null || current === document.body)
578
- ) {
579
- frame.blur();
580
- field.focus({ preventScroll: true });
581
- }
582
- }, 0);
583
- };
584
-
585
- /** A move the guard undoes: focus goes back, the display hears it, and the guard keeps watching. */
586
- const takeBack = (
587
- field: HTMLElement,
588
- frame: HTMLIFrameElement,
589
- guarded: Guarded,
590
- at: number,
591
- failed: boolean,
592
- ) => {
593
- // Until it is back, the field counts as the page's focus, so a move that didn't take is seen.
594
- known = field;
595
- focusedAt = at;
596
- contest = { field, frame, guarded, fight: true, gaveAt: at, heldSince: null };
597
- giveBack(field, frame);
598
- schedule();
599
- const take: FocusTake = { typing: at - typedAt <= TYPING_MS, failed };
600
- for (const taken of guarded.taken) {
601
- taken(take);
602
- }
603
- };
604
-
605
- /**
606
- * While a take-back is in progress: whether focus stayed in the field, or the frame has it again
607
- * (or it landed on nothing), which is the page's doing unless the person moved it there.
608
- */
609
- const watch = (seen: boolean, active: Element | null, at: number): boolean => {
610
- const watched = contest;
611
- if (watched === null) {
612
- return false;
613
- }
614
- const { field, frame, guarded } = watched;
615
- if (!field.isConnected || pressAt > watched.gaveAt) {
616
- // Gone, or the person pressed the page since: theirs to place focus now.
617
- contest = null;
618
- return false;
619
- }
620
- if (!frames.has(frame)) {
621
- // The frame was removed mid-fight (its display stopped): Firefox may leave focus on nothing
622
- // once the focused frame goes. Back into the field, for a moment after.
623
- if ((active === null || active === document.body) && at - watched.gaveAt <= HELD_MS) {
624
- field.focus({ preventScroll: true });
625
- return true;
626
- }
627
- contest = null;
628
- return false;
629
- }
630
- if (active === field) {
631
- watched.heldSince ??= at;
632
- if (at - watched.heldSince >= HELD_MS) {
633
- contest = null;
634
- }
635
- known = field;
636
- return true;
637
- }
638
- const lost = active === frame || active === null || active === document.body;
639
- if (!lost) {
640
- // Somewhere else of the person's (another field, a button, another frame): judged as any.
641
- contest = null;
642
- return false;
643
- }
644
- if (active === frame) {
645
- // The frame has it again. In a fight, the person's only by a Tab, the host's own move, or a
646
- // press the engine shows: a nested page's too, its shell's word not asked (P120, after the
647
- // audit of P111's guard, NG-1, where waiting for it let the frame keep the keyboard).
648
- const verdict = judge(frame, guarded, seen, at, watched.fight);
649
- if (verdict === true) {
650
- contest = null;
651
- known = frame;
652
- enteredAt = at;
653
- earned = true;
654
- return true;
655
- }
656
- if (verdict === "wait") {
657
- // The person left a nested page they had moved into, and focus went back in (P111): its
658
- // shell decides, as it would at first.
659
- if (watched.heldSince !== null || at - watched.gaveAt >= GIVE_BACK_MS) {
660
- contest = null;
661
- wait(field, frame, guarded, at);
662
- }
663
- return true;
664
- }
665
- }
666
- // Taken again once it was back (within `HELD_MS`), or not back yet after `GIVE_BACK_MS`: a
667
- // take-back that failed, given again and counted.
668
- if (watched.heldSince !== null || at - watched.gaveAt >= GIVE_BACK_MS) {
669
- takeBack(field, frame, guarded, at, true);
670
- }
671
- return true;
672
- };
673
-
674
- /** Judges a change of focus since the last look: `seen` when the engine told it (a blur). */
675
- const look = (seen: boolean) => {
676
- const active = deepActive(document);
677
- const at = now();
678
- if (waiting !== null) {
679
- settle(at);
680
- return;
681
- }
682
- if (watch(seen, active, at)) {
683
- return;
684
- }
685
- const was = known;
686
- // The person pressed a text field and focus is on nothing: on Firefox, a page that takes focus
687
- // as they press, or takes it back on its blur with a gesture of theirs, leaves it there
688
- // (measured, on a slow machine too), and their keys are lost. Into the field, once: as a
689
- // take-back the page failed if a frame had focus as they pressed, else simply.
690
- const press = pressed;
691
- if (
692
- press !== null &&
693
- (active === null || active === document.body) &&
694
- at - press.at <= HELD_MS &&
695
- press.field.isConnected
696
- ) {
697
- pressed = null;
698
- const from = press.from ?? was;
699
- const blamed = from === null ? undefined : frames.get(from as HTMLIFrameElement);
700
- if (blamed === undefined) {
701
- known = active;
702
- press.field.focus({ preventScroll: true });
703
- } else {
704
- takeBack(press.field, from as HTMLIFrameElement, blamed, at, true);
705
- }
706
- return;
707
- }
708
- known = active;
709
- if (active === was || active === null) {
710
- return;
711
- }
712
- const entered = frames.get(active as HTMLIFrameElement);
713
- if (entered !== undefined) {
714
- // From a text field it is judged below; from anything else it is let be, but only the
715
- // person's own move makes the frame theirs for a gesture gate (`focused`).
716
- enteredAt = at;
717
- const verdict = takesText(was) || judge(active as HTMLIFrameElement, entered, seen, at);
718
- if (verdict === "wait") {
719
- // A nested page's (P111): theirs once its shell says so; nothing to take back.
720
- wait(null, active as HTMLIFrameElement, entered, at);
721
- return;
722
- }
723
- earned = verdict;
724
- }
725
- // Into a text field from a display's frame (the person pressed the composer while the frame
726
- // had focus): watched as a take-back is, since a page that takes focus back on its own blur
727
- // leaves it on nothing on Firefox (measured), where the keys are lost to the page's doing.
728
- const left = was === null ? undefined : frames.get(was as HTMLIFrameElement);
729
- if (left !== undefined && takesText(active)) {
730
- contest = {
731
- field: active,
732
- frame: was as HTMLIFrameElement,
733
- guarded: left,
734
- fight: !earned,
735
- gaveAt: at,
736
- heldSince: at,
737
- };
738
- return;
739
- }
740
- const guarded = frames.get(active as HTMLIFrameElement);
741
- if (guarded === undefined || !takesText(was) || !was.isConnected) {
742
- return;
743
- }
744
- const frame = active as HTMLIFrameElement;
745
- if (!seen) {
746
- // A move out of a text field the window didn't blur for: Firefox, which shows the host's
747
- // page the pointer over a frame (P120).
748
- pointerShown = true;
749
- }
750
- const verdict = judge(frame, guarded, seen, at);
751
- if (verdict === true) {
752
- return;
753
- }
754
- if (verdict === "wait") {
755
- wait(was, frame, guarded, at);
756
- return;
757
- }
758
- takeBack(was, frame, guarded, at, false);
759
- };
760
-
761
- /**
762
- * Firefox moves focus into a frame without a word to the host's page: while a frame is guarded,
763
- * look at each animation frame (one read of the focused element), so the guard knows where focus
764
- * is when it next leaves a text field, or comes into one from a frame.
765
- */
766
- const poll = () => {
767
- polling = 0;
768
- look(false);
769
- schedule();
770
- };
771
- const schedule = () => {
772
- if (polling === 0 && (frames.size > 0 || contest !== null || waiting !== null)) {
773
- polling = view.requestAnimationFrame(poll);
774
- }
775
- };
776
-
777
- const gesture = () => {
778
- gestureAt = now();
779
- };
780
- const press = (event: Event) => {
781
- pressAt = now();
782
- if (event.isTrusted) {
783
- inputAt = pressAt;
784
- }
785
- gestureAt = pressAt;
786
- const target = event.composedPath()[0];
787
- pressed =
788
- target instanceof Element && takesText(target)
789
- ? { field: target, at: pressAt, from: deepActive(document) }
790
- : null;
791
- };
792
- view.addEventListener(
793
- "keydown",
794
- (event) => {
795
- const at = now();
796
- gestureAt = at;
797
- lastKey = { key: event.key, at };
798
- if (event.isTrusted) {
799
- inputAt = at;
800
- }
801
- if (event.key !== "Tab" && !MODIFIERS.has(event.key)) {
802
- typed(at);
803
- }
804
- },
805
- true,
806
- );
807
- // A click too (an assistive technology's click on the host's own UI dispatches no press), and
808
- // text that comes without a key (dictation, a paste, an input method): typing all the same.
809
- for (const type of ["pointerup", "touchend", "click"]) {
810
- view.addEventListener(type, gesture, true);
811
- }
812
- for (const type of ["pointerdown", "mousedown"]) {
813
- view.addEventListener(type, press, true);
814
- }
815
- view.addEventListener(
816
- "input",
817
- () => {
818
- typed(now());
819
- gestureAt = typedAt;
820
- },
821
- true,
822
- );
823
- // Focus moving within the page: what had it, for the next move out of a text field.
824
- document.addEventListener(
825
- "focusin",
826
- () => {
827
- focusedAt = now();
828
- look(false);
829
- schedule();
830
- },
831
- true,
832
- );
833
- // Chromium and WebKit blur the window as focus goes into a frame. Firefox may send the field a
834
- // focusout first (seen in one review's runs, not in another's): looked at once it's over.
835
- view.addEventListener("blur", (event) => {
836
- if (event.target === view) {
837
- look(true);
838
- }
839
- });
840
- document.addEventListener(
841
- "focusout",
842
- () => {
843
- view.setTimeout(() => look(false), 0);
844
- },
845
- true,
846
- );
847
-
848
- const guard: FocusGuard = {
849
- add(frame, taken, options) {
850
- let guarded = frames.get(frame);
851
- if (guarded === undefined) {
852
- const entry: Guarded = {
853
- taken: new Set(),
854
- overSince: -Infinity,
855
- heardAt: -Infinity,
856
- ask: options?.nested === true ? options.ask : undefined,
857
- asked: 0,
858
- answered: false,
859
- word:
860
- options?.nested === true
861
- ? { supported: true, activatedAt: -Infinity, focused: false, focusedAt: -Infinity }
862
- : null,
863
- };
864
- frame.addEventListener("pointerover", () => {
865
- entry.overSince = now();
866
- pointerShown = true;
867
- });
868
- frames.set(frame, entry);
869
- guarded = entry;
870
- }
871
- guarded.taken.add(taken);
872
- schedule();
873
- const own = guarded;
874
- return () => {
875
- own.taken.delete(taken);
876
- if (own.taken.size === 0) {
877
- frames.delete(frame);
878
- }
879
- };
880
- },
881
- shell(frame, said) {
882
- const guarded = frames.get(frame);
883
- const before = guarded?.word ?? null;
884
- if (guarded === undefined || before === null) {
885
- return;
886
- }
887
- const at = now();
888
- // Off to on after a stall, the shell's or the host's (no word for longer than its beat
889
- // allows): uncertain, and so until it goes off (P111, after its review). So is one that
890
- // turned on while the guard fights this frame for a text field (P120): the person's keys,
891
- // meant for the field, landing in the page.
892
- // So is one that turned on within `KEY_PRESS_MS` of a key the person typed on the host's page
893
- // before it (P122): that key's successor, landing in the page.
894
- const turnedOn = said.ago !== null && before.activatedAt === -Infinity && !before.uncertain;
895
- const stalled = said.gap > SHELL_STALL_MS || at - guarded.heardAt > SHELL_STALL_MS;
896
- const fought = contest !== null && contest.fight && contest.frame === frame;
897
- // The typing before it, never after: a key the person typed on the host's page after they
898
- // pressed the page, before the shell's word came, says nothing of that press (P122, after
899
- // CI). The shell saw it turn on at its look, so it turned on at most a look's gap before:
900
- // only a key before then counts.
901
- const turnedAt =
902
- said.ago === null ? -Infinity : at - said.ago - Math.min(said.gap, SHELL_STALL_MS);
903
- const typing = typedBefore(turnedAt);
904
- const soon = said.ago !== null && turnedAt - typing < KEY_PRESS_MS;
905
- const uncertain =
906
- said.ago !== null &&
907
- (before.uncertain === true || (turnedOn && (stalled || fought || soon)));
908
- guarded.heardAt = at;
909
- if (said.asked !== undefined && said.asked === guarded.asked) {
910
- guarded.answered = true;
911
- }
912
- // Whether a press could have turned it on, judged as it did (P120, after its review): the
913
- // person's typing then, and the pointer then, not when a move is later judged.
914
- const activatedAt = said.ago === null || uncertain ? -Infinity : at - said.ago;
915
- const keyed =
916
- activatedAt === -Infinity
917
- ? false
918
- : turnedOn
919
- ? !pressPossible(true, frame.matches(":hover"), guarded.overSince, typing, turnedAt)
920
- : before.keyed === true;
921
- guarded.word = {
922
- supported: said.supported,
923
- activatedAt,
924
- focused: said.focused,
925
- focusedAt: said.focused && !before.focused ? at : before.focusedAt,
926
- ...(uncertain ? { uncertain: true } : {}),
927
- ...(keyed ? { keyed: true } : {}),
928
- };
929
- if (waiting?.frame === frame) {
930
- settle(at);
931
- }
932
- },
933
- beat(frame) {
934
- const guarded = frames.get(frame);
935
- if (guarded !== undefined && guarded.word !== null) {
936
- guarded.heardAt = now();
937
- }
938
- },
939
- gesture(frame) {
940
- const guarded = frames.get(frame);
941
- const word = guarded?.word ?? null;
942
- const at = now();
943
- // Where a press could have made it (P120): a key the person typed into the page, meant for
944
- // the host's composer, turns the shell's activation on too.
945
- return (
946
- guarded !== undefined &&
947
- word !== null &&
948
- shellGesture(word, inputAt, at) &&
949
- !(pointerShown && word.keyed === true)
950
- );
951
- },
952
- focus(frame) {
953
- hostMove = { frame, at: now() };
954
- frame.focus();
955
- look(true);
956
- },
957
- focused(frame) {
958
- look(false);
959
- // A press on the host's page since focus went into the frame, just now: the activation is
960
- // that press's, and focus is about to leave (a call that lands in between doesn't count).
961
- const pressedHost = pressAt > enteredAt && now() - pressAt < HOST_PRESS_MS;
962
- return known === frame && document.activeElement === frame && earned && !pressedHost;
963
- },
964
- field(field) {
965
- const holder = deepActive(document);
966
- const guarded = holder === null ? undefined : frames.get(holder as HTMLIFrameElement);
967
- if (guarded === undefined) {
968
- field.focus({ preventScroll: true });
969
- return;
970
- }
971
- // Watched as a take-back: a frame that keeps focus fails it, and is counted.
972
- const frame = holder as HTMLIFrameElement;
973
- frame.blur();
974
- field.focus({ preventScroll: true });
975
- known = field;
976
- focusedAt = now();
977
- contest = { field, frame, guarded, fight: true, gaveAt: focusedAt, heldSince: null };
978
- schedule();
979
- },
980
- };
981
- guards.set(view, guard);
982
- return guard;
983
- }
984
-
985
- /**
986
- * Moves focus into one of the host page's text fields (a composer that opens beside a display,
987
- * P109): `field.focus()` for a host to call instead. Firefox doesn't move focus from a frame that
988
- * holds it with a gesture of the person's and takes it back on its own blur (measured: the keys
989
- * then go to the frame); this blurs the frame first, and the focus guard watches that focus lands,
990
- * counting a frame that keeps it toward stopping its display. Elsewhere it is `field.focus()`.
991
- */
992
- export function focusField(field: HTMLElement): void {
993
- const view = field.ownerDocument.defaultView;
994
- if (view === null) {
995
- field.focus({ preventScroll: true });
996
- return;
997
- }
998
- focusGuard(view).field(field);
999
- }