@botiverse/porthole 0.15.3 → 0.16.1

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