@botiverse/porthole 0.8.0 → 0.10.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.
- package/dist/annotate/types.d.ts +42 -4
- package/dist/annotate/types.js +6 -1
- package/dist/annotate/validate.d.ts +16 -1
- package/dist/annotate/validate.js +33 -3
- package/dist/bridge/bridge.js +1 -51
- package/dist/bridge/unseen.d.ts +18 -0
- package/dist/bridge/unseen.js +71 -0
- package/dist/check/page-source.js +1 -1
- package/dist/conformance/behavior.js +107 -68
- package/dist/conformance/clipboard.js +16 -14
- package/dist/conformance/harness-source.js +1 -1
- package/dist/conformance/limits.js +117 -64
- package/dist/conformance/probes.js +66 -49
- package/dist/conformance/served.js +66 -63
- package/dist/host/create-host.js +75 -3
- package/dist/platform/types.d.ts +12 -1
- package/dist/platform/web.js +8 -0
- package/dist/react/frame.d.ts +6 -2
- package/dist/shell/annotate.d.ts +10 -3
- package/dist/shell/annotate.js +610 -39
- package/docs/ADOPTING.md +76 -0
- package/docs/ARCHITECTURE.md +2 -1
- package/docs/DECISIONS.md +516 -1
- package/docs/DESIGN.md +178 -20
- package/docs/THREAT_MODEL.md +92 -9
- package/package.json +1 -1
- package/src/annotate/types.ts +39 -5
- package/src/annotate/validate.ts +39 -3
- package/src/bridge/bridge.ts +1 -60
- package/src/bridge/unseen.ts +82 -0
- package/src/conformance/behavior.ts +221 -69
- package/src/conformance/clipboard.ts +44 -14
- package/src/conformance/limits.ts +169 -76
- package/src/conformance/probes.ts +97 -49
- package/src/conformance/served.ts +112 -63
- package/src/host/create-host.ts +75 -3
- package/src/platform/types.ts +9 -1
- package/src/platform/web.ts +10 -0
- package/src/react/frame.tsx +6 -2
- package/src/shell/annotate.ts +610 -39
package/dist/shell/annotate.js
CHANGED
|
@@ -5,21 +5,28 @@
|
|
|
5
5
|
* in one).
|
|
6
6
|
*
|
|
7
7
|
* The host drives it (`porthole/annotate.start|stop|show|focus|clear`) and it reports
|
|
8
|
-
* (`porthole/annotate.selected|unselected|cancelled|anchored`):
|
|
8
|
+
* (`porthole/annotate.selected|unselected|cancelled|anchored|activated`):
|
|
9
9
|
* - picking an element: the element under the pointer (or finger) is outlined with its name; a
|
|
10
|
-
* click or tap picks it, and the content's own handlers never see that click
|
|
10
|
+
* click or tap picks it, and the content's own handlers never see that click (unless the content
|
|
11
|
+
* patched the DOM's built-ins, P105); Escape cancels;
|
|
11
12
|
* - selecting text: each selection that settles is reported, with where it is, and its clearing;
|
|
12
13
|
* - marks: each anchor is found in the content as it is now (its CSS selector, its text where it
|
|
13
14
|
* was, its text anywhere, its words with any spacing) and drawn over it, in a layer of the
|
|
14
15
|
* shell's own (a closed shadow root), again whenever the content changes; what can't be found
|
|
15
|
-
* is reported `orphaned
|
|
16
|
+
* is reported `orphaned`;
|
|
17
|
+
* - markers (P104): a mark with a count gets a small bubble with the number beside its anchor's
|
|
18
|
+
* last line (P105: in the line's gutter); a click on it is reported (`activated`), and the
|
|
19
|
+
* content's own handlers never see it;
|
|
20
|
+
* - following: while a mark has a marker, once a scroll, a resize or a change of the content
|
|
21
|
+
* settles, the marks that moved since the host was last told are reported, at most once a
|
|
22
|
+
* second, so the host's own UI beside a mark can follow.
|
|
16
23
|
*
|
|
17
24
|
* It shares the realm with the content, which can interfere with all of it: the host takes its
|
|
18
25
|
* reports as UI hints only.
|
|
19
26
|
*/
|
|
20
27
|
export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
21
28
|
(() => {
|
|
22
|
-
const LIMIT = { exact: 4000, quote: 300, context: 32, label: 120, css: 1000, marks: 500, boxes: 200 };
|
|
29
|
+
const LIMIT = { exact: 4000, quote: 300, context: 32, label: 120, css: 1000, marks: 500, boxes: 200, count: 999 };
|
|
23
30
|
const M = {
|
|
24
31
|
start: "porthole/annotate.start",
|
|
25
32
|
stop: "porthole/annotate.stop",
|
|
@@ -30,6 +37,7 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
30
37
|
unselected: "porthole/annotate.unselected",
|
|
31
38
|
cancelled: "porthole/annotate.cancelled",
|
|
32
39
|
anchored: "porthole/annotate.anchored",
|
|
40
|
+
activated: "porthole/annotate.activated",
|
|
33
41
|
};
|
|
34
42
|
// The DOM's own functions, taken before content runs.
|
|
35
43
|
const elementFromPoint = Document.prototype.elementFromPoint;
|
|
@@ -37,14 +45,19 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
37
45
|
const createRange = Document.prototype.createRange;
|
|
38
46
|
const createTreeWalker = Document.prototype.createTreeWalker;
|
|
39
47
|
const createElement = Document.prototype.createElement;
|
|
48
|
+
const createElementNS = Document.prototype.createElementNS;
|
|
40
49
|
const selectionOf = Document.prototype.getSelection;
|
|
41
50
|
const elementRect = Element.prototype.getBoundingClientRect;
|
|
42
51
|
const rangeRect = Range.prototype.getBoundingClientRect;
|
|
43
52
|
const rangeRects = Range.prototype.getClientRects;
|
|
44
53
|
const attachShadow = Element.prototype.attachShadow;
|
|
54
|
+
const getAttribute = Element.prototype.getAttribute;
|
|
55
|
+
const listenOn = EventTarget.prototype.addEventListener;
|
|
45
56
|
const post = window.parent.postMessage.bind(window.parent);
|
|
57
|
+
// Read once: content can replace window.parent (it is [Replaceable]), never what was read.
|
|
58
|
+
const framed = window.parent !== window;
|
|
46
59
|
const tell = (method, params) => {
|
|
47
|
-
if (
|
|
60
|
+
if (framed) {
|
|
48
61
|
post({ jsonrpc: "2.0", method, params }, "*");
|
|
49
62
|
}
|
|
50
63
|
};
|
|
@@ -191,7 +204,13 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
191
204
|
for (const each of classes) {
|
|
192
205
|
name += "." + each;
|
|
193
206
|
}
|
|
194
|
-
|
|
207
|
+
// An element without words (an image, an icon button, a chart) is named by what it says it is.
|
|
208
|
+
let words = squash(element.textContent || "");
|
|
209
|
+
for (const attribute of ["alt", "aria-label", "title"]) {
|
|
210
|
+
if (words) break;
|
|
211
|
+
words = squash(getAttribute.call(element, attribute) || "");
|
|
212
|
+
}
|
|
213
|
+
words = words.slice(0, 40);
|
|
195
214
|
return (words ? name + " " + words : name).slice(0, LIMIT.label);
|
|
196
215
|
};
|
|
197
216
|
const quoteOf = (index, start, end) => {
|
|
@@ -343,16 +362,24 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
343
362
|
// Marks lie over the content's text, so their fill is light (the text under them keeps its
|
|
344
363
|
// contrast, on a light page and a dark one alike) and an underline or outline says where
|
|
345
364
|
// they are; the active one and a flash add a ring around it, not more fill over its words.
|
|
346
|
-
|
|
365
|
+
// An element's mark with a marker is quieter (a thin line): the marker says what it is.
|
|
366
|
+
// A marker is a small bubble with a number, drawn in its own colors and font (only what the
|
|
367
|
+
// layer inherits from the page could reach it, so each of those is set here).
|
|
368
|
+
const STYLE = ".mark{position:absolute;border-radius:2px;background:rgba(250,204,21,.18);box-shadow:inset 0 -2px 0 rgba(234,179,8,.95)}.mark.element{background:rgba(250,204,21,.07);box-shadow:none;outline:2px solid rgba(234,179,8,.9);outline-offset:1px}.mark.active{background:rgba(250,204,21,.26);box-shadow:inset 0 -3px 0 rgb(234,179,8),0 0 0 2px rgba(234,179,8,.45)}.mark.element.active{background:rgba(250,204,21,.12);box-shadow:none;outline-width:3px}.mark.element.quiet{background:rgba(250,204,21,.05);outline:1px solid rgba(234,179,8,.6);outline-offset:2px}.mark.element.quiet.active{background:rgba(250,204,21,.1);outline:2px solid rgba(234,179,8,.9)}.marker{position:absolute;box-sizing:border-box;height:18px;min-width:18px;display:flex;align-items:center;justify-content:center;gap:3px;margin:0;padding:0 5px;border:1px solid rgba(161,98,7,.6);border-radius:9px 9px 9px 2px;background:#fde68a;color:#422006;box-shadow:0 1px 2px rgba(0,0,0,.3);font:600 11px/16px system-ui,-apple-system,Segoe UI,Roboto,sans-serif;font-style:normal;letter-spacing:0;word-spacing:0;text-transform:none;text-indent:0;text-shadow:none;direction:ltr;writing-mode:horizontal-tb;white-space:nowrap;user-select:none;-webkit-user-select:none;cursor:pointer;pointer-events:auto}.marker:hover{background:#fcd34d}.marker.active{background:#facc15;box-shadow:0 0 0 2px rgba(234,179,8,.55),0 1px 2px rgba(0,0,0,.3)}.marker.compact{padding:0 4px}.marker.inert{pointer-events:none}.marker svg{display:block;width:11px;height:11px;flex:none}.mark.flash{animation:flash 1.8s ease-out}@keyframes flash{from{box-shadow:inset 0 -3px 0 rgb(234,179,8),0 0 0 6px rgba(250,204,21,.6)}}.hover{position:absolute;outline:2px solid #2563eb;outline-offset:1px;background:rgba(37,99,235,.12)}.tag{position:absolute;max-width:70vw;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font:12px/18px ui-monospace,SFMono-Regular,Menlo,monospace;padding:0 6px;border-radius:4px;background:#1e293b;color:#fff}";
|
|
369
|
+
// Inline and important: the content's own style sheets can't hide, shrink or move it by
|
|
370
|
+
// accident. Set again at each draw, since its scripts can rewrite it (narrowing only: the
|
|
371
|
+
// content can still cover the layer, or move it with its ancestors, P104).
|
|
372
|
+
const LAYER_STYLE = "position:fixed!important;inset:0!important;width:auto!important;height:auto!important;min-width:0!important;min-height:0!important;max-width:none!important;max-height:none!important;display:block!important;visibility:visible!important;opacity:1!important;pointer-events:none!important;z-index:2147483647!important;overflow:hidden!important;margin:0!important;padding:0!important;border:0!important;background:none!important;transform:none!important;scale:none!important;translate:none!important;rotate:none!important;zoom:1!important;clip-path:none!important;mask:none!important;filter:none!important;content-visibility:visible!important;contain:none!important";
|
|
347
373
|
const ensureLayer = () => {
|
|
348
374
|
if (layer === null) {
|
|
349
375
|
layer = createElement.call(document, "div");
|
|
350
376
|
layer.setAttribute("aria-hidden", "true");
|
|
351
377
|
layer.setAttribute("data-porthole-annotations", "");
|
|
352
|
-
// Inline and important: the content's own styles can't hide or move it by accident.
|
|
353
|
-
layer.style.cssText = "position:fixed!important;inset:0!important;display:block!important;visibility:visible!important;opacity:1!important;pointer-events:none!important;z-index:2147483647!important;overflow:hidden!important;margin:0!important;padding:0!important;border:0!important;background:none!important;transform:none!important";
|
|
354
378
|
shadow = attachShadow.call(layer, { mode: "closed" });
|
|
355
379
|
}
|
|
380
|
+
if (layer.style.cssText !== LAYER_STYLE) {
|
|
381
|
+
layer.style.cssText = LAYER_STYLE;
|
|
382
|
+
}
|
|
356
383
|
if (!layer.isConnected) {
|
|
357
384
|
document.documentElement.appendChild(layer);
|
|
358
385
|
}
|
|
@@ -362,22 +389,326 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
362
389
|
let mode = { elements: false, text: false };
|
|
363
390
|
let hover = null;
|
|
364
391
|
let marks = [];
|
|
392
|
+
// The markers as last drawn, for a click to find: { id, element }. Each mark keeps its
|
|
393
|
+
// marker's node across draws, so a press that spans a redraw ends on the node it began on.
|
|
394
|
+
let markers = [];
|
|
395
|
+
const markerNodes = new Map();
|
|
396
|
+
const MARKER_HEIGHT = 18;
|
|
397
|
+
const SVG = "http://www.w3.org/2000/svg";
|
|
398
|
+
const labelOf = (count) => (count === 0 ? "" : count > 99 ? "99+" : String(count));
|
|
399
|
+
// Its size, near enough to place it: the padding and the glyph, then the number.
|
|
400
|
+
const markerWidth = (label) => Math.max(MARKER_HEIGHT, 23 + (label === "" ? 0 : 3 + label.length * 7));
|
|
401
|
+
// Compact (P105): the number alone, or the glyph alone for none, with less padding.
|
|
402
|
+
const compactWidth = (label) => (label === "" ? 21 : Math.max(MARKER_HEIGHT, 10 + label.length * 7));
|
|
403
|
+
// A rect's last line with words on it: its last box with a width, else any with a size.
|
|
404
|
+
const lastBox = (rects) => {
|
|
405
|
+
let any = null;
|
|
406
|
+
for (let i = rects.length - 1; i >= 0; i -= 1) {
|
|
407
|
+
if (rects[i].width > 0) {
|
|
408
|
+
return rects[i];
|
|
409
|
+
}
|
|
410
|
+
if (any === null && rects[i].height > 0) {
|
|
411
|
+
any = rects[i];
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
return any;
|
|
415
|
+
};
|
|
416
|
+
// Where a text anchor's marker goes (P105, #36). At its words' end, raised (0.9), it lay over
|
|
417
|
+
// the first letters of the next word, or between two lines. Now it sits beside the anchor's
|
|
418
|
+
// last line, centred on it, clear of that line's words, and over nothing of the content's but
|
|
419
|
+
// the background of a box holding the anchor, with nothing else between it and the words:
|
|
420
|
+
// in the line's gutter, just past the right edge of the anchor's block (its content box);
|
|
421
|
+
// where the frame is too narrow for that (text that runs to the frame's edges), at the
|
|
422
|
+
// frame's right edge; where something else is beside the block (a table's next cell, a card
|
|
423
|
+
// in a row of them), just past the line's last word. Markers on one line of one block go
|
|
424
|
+
// together, in the order their words end. Where the bubble with its glyph fits in none of
|
|
425
|
+
// these, the number alone does (compact); where that fits in none either (a full line on a
|
|
426
|
+
// narrow screen), it goes as far right as it can, over the line's end.
|
|
427
|
+
const computedOf = window.getComputedStyle;
|
|
428
|
+
const elementsAt = Document.prototype.elementsFromPoint;
|
|
429
|
+
const containsOf = Node.prototype.contains;
|
|
430
|
+
const INLINE = /^(inline|contents|none|ruby|table-(row|column|header|footer))/;
|
|
431
|
+
const blockOf = (range) => {
|
|
432
|
+
let node = range.endContainer;
|
|
433
|
+
if (node.nodeType !== 1) {
|
|
434
|
+
node = node.parentElement;
|
|
435
|
+
}
|
|
436
|
+
while (node !== null && node !== document.body && node !== document.documentElement && INLINE.test(computedOf.call(window, node).display)) {
|
|
437
|
+
node = node.parentElement;
|
|
438
|
+
}
|
|
439
|
+
return node === null || node === document.documentElement ? document.body : node;
|
|
440
|
+
};
|
|
441
|
+
// The right edge of its content box: past it is the block's padding, then what is beside it.
|
|
442
|
+
const contentRight = (block) => {
|
|
443
|
+
const style = computedOf.call(window, block);
|
|
444
|
+
return elementRect.call(block).right - (parseFloat(style.borderRightWidth) || 0) - (parseFloat(style.paddingRight) || 0);
|
|
445
|
+
};
|
|
446
|
+
// How far the words reach on the anchor's last line (at), in its block: at least its own.
|
|
447
|
+
// Read forward from the anchor's end, 64 characters at a time, only until the text leaves
|
|
448
|
+
// that line (P105: the whole rest of a long block, for each marker, cost 100 ms and more).
|
|
449
|
+
const lineEnd = (range, block, at) => {
|
|
450
|
+
let end = at.right;
|
|
451
|
+
let node = range.endContainer;
|
|
452
|
+
if (node.nodeType !== 3) {
|
|
453
|
+
return end;
|
|
454
|
+
}
|
|
455
|
+
const middle = at.top + at.height / 2;
|
|
456
|
+
const walker = createTreeWalker.call(document, block, NodeFilter.SHOW_TEXT);
|
|
457
|
+
walker.currentNode = node;
|
|
458
|
+
const piece = createRange.call(document);
|
|
459
|
+
let from = range.endOffset;
|
|
460
|
+
for (let pieces = 0; node !== null && pieces < 64; pieces += 1) {
|
|
461
|
+
const length = node.nodeValue.length;
|
|
462
|
+
if (from >= length) {
|
|
463
|
+
node = walker.nextNode();
|
|
464
|
+
from = 0;
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
467
|
+
const to = Math.min(length, from + 64);
|
|
468
|
+
try {
|
|
469
|
+
piece.setStart(node, from);
|
|
470
|
+
piece.setEnd(node, to);
|
|
471
|
+
} catch (_) {
|
|
472
|
+
return end;
|
|
473
|
+
}
|
|
474
|
+
for (const rect of Array.from(rangeRects.call(piece))) {
|
|
475
|
+
if (rect.top > middle) {
|
|
476
|
+
return end;
|
|
477
|
+
}
|
|
478
|
+
if (rect.width > 0 && rect.bottom >= middle && rect.right > end) {
|
|
479
|
+
end = rect.right;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
from = to;
|
|
483
|
+
}
|
|
484
|
+
return end;
|
|
485
|
+
};
|
|
486
|
+
// Nothing of the content's at a point but a box that holds the block (its background); a
|
|
487
|
+
// point out of view hides nothing.
|
|
488
|
+
const clearAt = (x, y, block) => {
|
|
489
|
+
if (x < 0 || y < 0 || x >= document.documentElement.clientWidth || y >= document.documentElement.clientHeight) {
|
|
490
|
+
return true;
|
|
491
|
+
}
|
|
492
|
+
const hits = elementsAt.call(document, x, y);
|
|
493
|
+
const first = hits[0] === layer ? hits[1] : hits[0];
|
|
494
|
+
return first === undefined || containsOf.call(first, block);
|
|
495
|
+
};
|
|
496
|
+
// Its box: two opposite corners and its middle (the line's middle, up to it, is the path's).
|
|
497
|
+
const clearOver = (left, top, width, block) =>
|
|
498
|
+
clearAt(left + width / 2, top + MARKER_HEIGHT / 2, block) && clearAt(left + 1, top + 1, block) && clearAt(left + width - 1, top + MARKER_HEIGHT - 1, block);
|
|
499
|
+
const GAP = 6;
|
|
500
|
+
const NEAR = 3;
|
|
501
|
+
const BETWEEN = 3;
|
|
502
|
+
const EDGE = 2;
|
|
503
|
+
const STEP = 8;
|
|
504
|
+
const overlapping = (placed, left, top, width) => placed.filter((other) => Math.abs(other.top - top) < MARKER_HEIGHT && left < other.left + other.width + BETWEEN && other.left < left + width + BETWEEN)[0];
|
|
505
|
+
// The first place, of those above, where a line's markers fit, or null.
|
|
506
|
+
const placeLine = (line, compact, gap, placed, room) => {
|
|
507
|
+
const block = line.block;
|
|
508
|
+
let width = -BETWEEN;
|
|
509
|
+
for (const each of line.items) {
|
|
510
|
+
width += (compact ? each.small : each.width) + BETWEEN;
|
|
511
|
+
}
|
|
512
|
+
const most = room - EDGE - width;
|
|
513
|
+
const right = Math.max(contentRight(block), line.words);
|
|
514
|
+
// Along the line's middle, past the block, how far nothing stands in the way.
|
|
515
|
+
const reach = (to) => {
|
|
516
|
+
for (let x = right + 1; x < to; x += STEP) {
|
|
517
|
+
if (!clearAt(x, line.middle, block)) {
|
|
518
|
+
return x;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
return to;
|
|
522
|
+
};
|
|
523
|
+
// A place, moved past the markers already there (rightwards; at the frame's edge, leftwards).
|
|
524
|
+
const fits = (left, leftwards) => {
|
|
525
|
+
for (let tries = 0; tries < 20; tries += 1) {
|
|
526
|
+
if (left < line.words + gap || left > most || reach(left + width) < left + width) {
|
|
527
|
+
return null;
|
|
528
|
+
}
|
|
529
|
+
const hit = overlapping(placed, left, line.top, width);
|
|
530
|
+
if (!hit) {
|
|
531
|
+
return clearOver(left, line.top, width, block) ? left : null;
|
|
532
|
+
}
|
|
533
|
+
left = leftwards ? hit.left - BETWEEN - width : hit.left + hit.width + BETWEEN;
|
|
534
|
+
}
|
|
535
|
+
return null;
|
|
536
|
+
};
|
|
537
|
+
const gutter = right + gap;
|
|
538
|
+
let left = fits(gutter, false);
|
|
539
|
+
if (left === null && gutter > most) {
|
|
540
|
+
left = fits(most, true);
|
|
541
|
+
}
|
|
542
|
+
if (left === null) {
|
|
543
|
+
left = fits(line.words + gap, false);
|
|
544
|
+
}
|
|
545
|
+
return left === null ? null : { left, width };
|
|
546
|
+
};
|
|
547
|
+
const placeLines = (wanted, placed, room) => {
|
|
548
|
+
// One line of one block: markers whose last lines share a middle.
|
|
549
|
+
const lines = [];
|
|
550
|
+
wanted.sort((a, b) => a.at.top - b.at.top || a.at.right - b.at.right);
|
|
551
|
+
for (const each of wanted) {
|
|
552
|
+
const middle = each.at.top + each.at.height / 2;
|
|
553
|
+
let line = null;
|
|
554
|
+
for (const one of lines) {
|
|
555
|
+
if (line === null && one.block === each.block && Math.abs(one.middle - middle) < Math.min(one.height, each.at.height) / 2) {
|
|
556
|
+
line = one;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
if (line === null) {
|
|
560
|
+
line = { block: each.block, middle, height: each.at.height, bottom: each.at.bottom, range: each.range, at: each.at, items: [] };
|
|
561
|
+
lines.push(line);
|
|
562
|
+
}
|
|
563
|
+
line.items.push(each);
|
|
564
|
+
if (each.at.right > line.at.right) {
|
|
565
|
+
line.at = each.at;
|
|
566
|
+
line.range = each.range;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
const seen = document.documentElement.clientHeight || window.innerHeight;
|
|
570
|
+
for (const line of lines) {
|
|
571
|
+
line.top = line.middle - MARKER_HEIGHT / 2;
|
|
572
|
+
// Not cut off at the frame's top while its anchor is in view; with its anchor as it goes.
|
|
573
|
+
if (line.top < 2) {
|
|
574
|
+
line.top = Math.min(2, line.bottom - MARKER_HEIGHT);
|
|
575
|
+
}
|
|
576
|
+
let compact = false;
|
|
577
|
+
let found = null;
|
|
578
|
+
let left;
|
|
579
|
+
if (line.top + MARKER_HEIGHT < 0 || line.top > seen) {
|
|
580
|
+
// Out of view, nothing is measured: it is placed again as it comes into view.
|
|
581
|
+
found = { left: Math.max(EDGE, Math.min(contentRight(line.block) + GAP, room - EDGE - line.items.length * MARKER_HEIGHT)) };
|
|
582
|
+
} else {
|
|
583
|
+
line.words = lineEnd(line.range, line.block, line.at);
|
|
584
|
+
found = placeLine(line, false, GAP, placed, room);
|
|
585
|
+
if (found === null) {
|
|
586
|
+
compact = true;
|
|
587
|
+
found = placeLine(line, true, NEAR, placed, room);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
if (found !== null) {
|
|
591
|
+
left = found.left;
|
|
592
|
+
} else {
|
|
593
|
+
// Over the line's end, as far right as nothing else stands in the way.
|
|
594
|
+
let width = -BETWEEN;
|
|
595
|
+
for (const each of line.items) {
|
|
596
|
+
width += each.small + BETWEEN;
|
|
597
|
+
}
|
|
598
|
+
let limit = room - EDGE;
|
|
599
|
+
const right = Math.max(contentRight(line.block), line.words);
|
|
600
|
+
for (let x = right + 1; x < limit; x += STEP) {
|
|
601
|
+
if (!clearAt(x, line.middle, line.block)) {
|
|
602
|
+
limit = x - NEAR;
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
left = Math.max(EDGE, Math.min(room - EDGE, limit) - width);
|
|
606
|
+
}
|
|
607
|
+
for (const each of line.items) {
|
|
608
|
+
each.compact = compact;
|
|
609
|
+
each.left = left;
|
|
610
|
+
each.top = line.top;
|
|
611
|
+
const width = compact ? each.small : each.width;
|
|
612
|
+
placed.push({ left, top: line.top, width });
|
|
613
|
+
left += width + BETWEEN;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
};
|
|
617
|
+
// An element's marker: on its top right corner, inside the frame's width, beside another.
|
|
618
|
+
const placeCorner = (each, placed, room) => {
|
|
619
|
+
let left = each.at.right - each.width / 2;
|
|
620
|
+
let top = each.at.top - MARKER_HEIGHT / 2;
|
|
621
|
+
if (top < 2) {
|
|
622
|
+
top = Math.min(2, each.at.bottom - MARKER_HEIGHT);
|
|
623
|
+
}
|
|
624
|
+
for (let tries = 0; tries < 20; tries += 1) {
|
|
625
|
+
const hit = overlapping(placed, left, top, each.width);
|
|
626
|
+
if (!hit) {
|
|
627
|
+
break;
|
|
628
|
+
}
|
|
629
|
+
left = hit.left + hit.width + BETWEEN;
|
|
630
|
+
}
|
|
631
|
+
each.left = Math.max(EDGE, Math.min(left, room - each.width - EDGE));
|
|
632
|
+
each.top = top;
|
|
633
|
+
placed.push({ left: each.left, top, width: each.width });
|
|
634
|
+
};
|
|
635
|
+
// The marker's node, kept across draws for its mark, where it was placed.
|
|
636
|
+
const markerOf = (each) => {
|
|
637
|
+
const mark = each.mark;
|
|
638
|
+
const label = each.label;
|
|
639
|
+
const kept = markerNodes.get(mark.id);
|
|
640
|
+
let marker = kept ? kept.element : null;
|
|
641
|
+
if (marker === null) {
|
|
642
|
+
marker = createElement.call(document, "div");
|
|
643
|
+
// A listener of its own makes it a thing to tap: WebKit makes a tap a click only on such
|
|
644
|
+
// an element (P105, measured: the window's listener isn't enough). It never runs (the
|
|
645
|
+
// window's first listener stops every click on the layer); the click is told there.
|
|
646
|
+
callOf(listenOn, marker, ["click", tapTarget]);
|
|
647
|
+
}
|
|
648
|
+
marker.className = "marker" + (each.compact ? " compact" : "") + (mark.id === active ? " active" : "") + (mode.elements ? " inert" : "");
|
|
649
|
+
marker.style.cssText = "left:" + each.left + "px;top:" + each.top + "px";
|
|
650
|
+
markers.push({ id: mark.id, element: marker });
|
|
651
|
+
const form = (each.compact ? "compact:" : "full:") + label;
|
|
652
|
+
if (kept && kept.form === form) {
|
|
653
|
+
return marker;
|
|
654
|
+
}
|
|
655
|
+
markerNodes.set(mark.id, { element: marker, form });
|
|
656
|
+
marker.replaceChildren();
|
|
657
|
+
if (!each.compact || label === "") {
|
|
658
|
+
const glyph = createElementNS.call(document, SVG, "svg");
|
|
659
|
+
glyph.setAttribute("viewBox", "0 0 16 16");
|
|
660
|
+
glyph.setAttribute("aria-hidden", "true");
|
|
661
|
+
const path = createElementNS.call(document, SVG, "path");
|
|
662
|
+
path.setAttribute("fill", "currentColor");
|
|
663
|
+
path.setAttribute("d", "M3 2h10a2 2 0 0 1 2 2v6a2 2 0 0 1-2 2H8l-4 3v-3H3a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2z");
|
|
664
|
+
glyph.appendChild(path);
|
|
665
|
+
marker.appendChild(glyph);
|
|
666
|
+
}
|
|
667
|
+
if (label !== "") {
|
|
668
|
+
const number = createElement.call(document, "span");
|
|
669
|
+
number.textContent = label;
|
|
670
|
+
marker.appendChild(number);
|
|
671
|
+
}
|
|
672
|
+
return marker;
|
|
673
|
+
};
|
|
674
|
+
const tapTarget = () => {};
|
|
365
675
|
let active = null;
|
|
676
|
+
// The mark whose marker the pointer is over: its words are lit as the active mark's are, so
|
|
677
|
+
// a marker in the gutter shows which words it is for (P105).
|
|
678
|
+
let hot = null;
|
|
366
679
|
let flashing = null;
|
|
367
680
|
let flashUntil = 0;
|
|
681
|
+
// The layer's parts: its style, the marks, the markers, the outline while picking. A draw
|
|
682
|
+
// replaces the marks and the outline, and leaves each marker that stays where it is (P105): a
|
|
683
|
+
// marker taken out of the page and put back mid-press loses its click (WebKit, measured once
|
|
684
|
+
// the pointer's arrival on a marker lit its mark and redrew).
|
|
685
|
+
let boxes = null;
|
|
686
|
+
const boxesOf = (root) => {
|
|
687
|
+
if (boxes === null || boxes.style.parentNode !== root || boxes.marks.parentNode !== root || boxes.markers.parentNode !== root || boxes.hover.parentNode !== root) {
|
|
688
|
+
const style = createElement.call(document, "style");
|
|
689
|
+
if (nonce !== "") {
|
|
690
|
+
style.nonce = nonce;
|
|
691
|
+
}
|
|
692
|
+
boxes = { style, marks: createElement.call(document, "div"), markers: createElement.call(document, "div"), hover: createElement.call(document, "div") };
|
|
693
|
+
root.replaceChildren(boxes.style, boxes.marks, boxes.markers, boxes.hover);
|
|
694
|
+
}
|
|
695
|
+
if (boxes.style.textContent !== STYLE) {
|
|
696
|
+
boxes.style.textContent = STYLE;
|
|
697
|
+
}
|
|
698
|
+
return boxes;
|
|
699
|
+
};
|
|
368
700
|
const draw = () => {
|
|
369
701
|
if (marks.length === 0 && hover === null && layer === null) {
|
|
370
702
|
return;
|
|
371
703
|
}
|
|
372
704
|
const root = ensureLayer();
|
|
705
|
+
const into = boxesOf(root);
|
|
373
706
|
const parts = [];
|
|
374
|
-
const style = createElement.call(document, "style");
|
|
375
|
-
if (nonce !== "") {
|
|
376
|
-
style.nonce = nonce;
|
|
377
|
-
}
|
|
378
|
-
style.textContent = STYLE;
|
|
379
|
-
parts.push(style);
|
|
380
707
|
const flashOn = Date.now() < flashUntil;
|
|
708
|
+
// What is measured first, then drawn: no node of the layer changes while the page is read.
|
|
709
|
+
const corners = [];
|
|
710
|
+
const wanted = [];
|
|
711
|
+
markers = [];
|
|
381
712
|
for (const mark of marks) {
|
|
382
713
|
if (mark.found === null) {
|
|
383
714
|
continue;
|
|
@@ -388,11 +719,57 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
388
719
|
continue;
|
|
389
720
|
}
|
|
390
721
|
const each = createElement.call(document, "div");
|
|
391
|
-
each.className = "mark " + (mark.found.range ? "text" : "element") + (mark.id === active ? " active" : "") + (flashOn && mark.id === flashing ? " flash" : "");
|
|
722
|
+
each.className = "mark " + (mark.found.range ? "text" : "element") + (mark.count === null ? "" : " quiet") + (mark.id === active || mark.id === hot ? " active" : "") + (flashOn && mark.id === flashing ? " flash" : "");
|
|
392
723
|
each.style.cssText = "left:" + rect.left + "px;top:" + rect.top + "px;width:" + rect.width + "px;height:" + rect.height + "px";
|
|
393
724
|
parts.push(each);
|
|
394
725
|
}
|
|
726
|
+
if (mark.count !== null) {
|
|
727
|
+
const at = lastBox(rects);
|
|
728
|
+
if (at !== null) {
|
|
729
|
+
const label = labelOf(mark.count);
|
|
730
|
+
const each = { mark, order: corners.length + wanted.length, label, width: markerWidth(label), small: compactWidth(label), compact: false, at, left: 0, top: 0 };
|
|
731
|
+
if (mark.found.range) {
|
|
732
|
+
// Kept while it still holds the words (P105): a change elsewhere doesn't move them.
|
|
733
|
+
if (mark.block === undefined || !mark.block.isConnected || !containsOf.call(mark.block, mark.found.range.endContainer)) {
|
|
734
|
+
mark.block = blockOf(mark.found.range);
|
|
735
|
+
}
|
|
736
|
+
each.range = mark.found.range;
|
|
737
|
+
each.block = mark.block;
|
|
738
|
+
wanted.push(each);
|
|
739
|
+
} else {
|
|
740
|
+
corners.push(each);
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
}
|
|
395
744
|
}
|
|
745
|
+
// Markers go over every mark: elements' on their corners first, then the lines'.
|
|
746
|
+
const room = document.documentElement.clientWidth || window.innerWidth;
|
|
747
|
+
const placed = [];
|
|
748
|
+
for (const each of corners) {
|
|
749
|
+
placeCorner(each, placed, room);
|
|
750
|
+
}
|
|
751
|
+
// The layer's own boxes out of the way while places are hit-tested (P105): each test
|
|
752
|
+
// otherwise walks every mark and marker the last draw left (0.13 ms each, 300 of them).
|
|
753
|
+
if (wanted.length > 0) {
|
|
754
|
+
into.marks.style.display = "none";
|
|
755
|
+
into.markers.style.display = "none";
|
|
756
|
+
try {
|
|
757
|
+
placeLines(wanted, placed, room);
|
|
758
|
+
} finally {
|
|
759
|
+
into.marks.style.display = "";
|
|
760
|
+
into.markers.style.display = "";
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
const badges = [];
|
|
764
|
+
for (const each of corners.concat(wanted).sort((a, b) => a.order - b.order)) {
|
|
765
|
+
badges.push(markerOf(each));
|
|
766
|
+
}
|
|
767
|
+
for (const id of Array.from(markerNodes.keys())) {
|
|
768
|
+
if (!markers.some((each) => each.id === id)) {
|
|
769
|
+
markerNodes.delete(id);
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
const outlines = [];
|
|
396
773
|
if (hover !== null && hover.isConnected) {
|
|
397
774
|
const rect = elementRect.call(hover);
|
|
398
775
|
const outline = createElement.call(document, "div");
|
|
@@ -402,9 +779,20 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
402
779
|
tag.className = "tag";
|
|
403
780
|
tag.textContent = describe(hover).slice(0, 80);
|
|
404
781
|
tag.style.cssText = "left:" + Math.max(4, rect.left) + "px;top:" + (rect.top >= 24 ? rect.top - 22 : Math.max(4, rect.bottom + 4)) + "px";
|
|
405
|
-
|
|
782
|
+
outlines.push(outline, tag);
|
|
783
|
+
}
|
|
784
|
+
into.marks.replaceChildren(...parts);
|
|
785
|
+
for (const child of Array.from(into.markers.childNodes)) {
|
|
786
|
+
if (badges.indexOf(child) === -1) {
|
|
787
|
+
into.markers.removeChild(child);
|
|
788
|
+
}
|
|
406
789
|
}
|
|
407
|
-
|
|
790
|
+
for (const badge of badges) {
|
|
791
|
+
if (badge.parentNode !== into.markers) {
|
|
792
|
+
into.markers.appendChild(badge);
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
into.hover.replaceChildren(...outlines);
|
|
408
796
|
};
|
|
409
797
|
let drawing = false;
|
|
410
798
|
const redraw = () => {
|
|
@@ -418,8 +806,28 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
418
806
|
};
|
|
419
807
|
|
|
420
808
|
const resultOf = (mark) => (mark.found === null ? { id: mark.id, state: "orphaned" } : { id: mark.id, state: "attached", rect: box(rectOf(mark.found)) });
|
|
421
|
-
//
|
|
422
|
-
const
|
|
809
|
+
// Two boxes more than 0.5 px apart on any side (or one of them unknown).
|
|
810
|
+
const apart = (a, b) => !a || !b || Math.abs(a.x - b.x) > 0.5 || Math.abs(a.y - b.y) > 0.5 || Math.abs(a.width - b.width) > 0.5 || Math.abs(a.height - b.height) > 0.5;
|
|
811
|
+
// What the host was last told of each mark (mark.told), so a settle tells only what moved.
|
|
812
|
+
const moved = (mark, result) => {
|
|
813
|
+
const told = mark.told;
|
|
814
|
+
if (!told || told.state !== result.state) {
|
|
815
|
+
return true;
|
|
816
|
+
}
|
|
817
|
+
return result.state === "attached" && apart(told.rect, result.rect);
|
|
818
|
+
};
|
|
819
|
+
const report = (pairs) => {
|
|
820
|
+
if (pairs.length > 0) {
|
|
821
|
+
for (const pair of pairs) {
|
|
822
|
+
pair.mark.told = pair.result;
|
|
823
|
+
}
|
|
824
|
+
tell(M.anchored, { results: pairs.map((pair) => pair.result) });
|
|
825
|
+
}
|
|
826
|
+
};
|
|
827
|
+
const resultsOf = (only) => marks.map((mark) => ({ mark, result: resultOf(mark) })).filter((pair) => !only || moved(pair.mark, pair.result));
|
|
828
|
+
// Finds every mark again; tells the host every result (after show, or when a state changed),
|
|
829
|
+
// only the marks that moved (once the content is quiet), or nothing.
|
|
830
|
+
const anchorAll = (all, quiet) => {
|
|
423
831
|
const index = textIndex();
|
|
424
832
|
let changed = false;
|
|
425
833
|
for (const mark of marks) {
|
|
@@ -437,7 +845,9 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
437
845
|
mark.state = state;
|
|
438
846
|
}
|
|
439
847
|
if (all || changed) {
|
|
440
|
-
|
|
848
|
+
report(resultsOf(false));
|
|
849
|
+
} else if (quiet) {
|
|
850
|
+
follow();
|
|
441
851
|
}
|
|
442
852
|
draw();
|
|
443
853
|
};
|
|
@@ -469,18 +879,25 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
469
879
|
deadline = now + 1000;
|
|
470
880
|
}
|
|
471
881
|
clearTimeout(anchorTimer);
|
|
882
|
+
// A run that waited its whole pause found the content quiet; one cut short by the deadline
|
|
883
|
+
// didn't, and tells only states (what moved is told once it is quiet).
|
|
884
|
+
const wait = Math.max(0, Math.min(150, deadline - now));
|
|
472
885
|
anchorTimer = setTimeout(() => {
|
|
473
886
|
anchorTimer = 0;
|
|
474
887
|
deadline = 0;
|
|
475
888
|
const every = reportAll;
|
|
476
889
|
reportAll = false;
|
|
477
|
-
anchorAll(every);
|
|
890
|
+
anchorAll(every, wait === 150);
|
|
891
|
+
if (wait !== 150) {
|
|
892
|
+
// Cut short by the deadline: what moved is told once the content has been quiet.
|
|
893
|
+
settle();
|
|
894
|
+
}
|
|
478
895
|
if (pendingFocus !== null) {
|
|
479
896
|
const id = pendingFocus;
|
|
480
897
|
pendingFocus = null;
|
|
481
898
|
focus(id);
|
|
482
899
|
}
|
|
483
|
-
},
|
|
900
|
+
}, wait);
|
|
484
901
|
};
|
|
485
902
|
new MutationObserver((records) => {
|
|
486
903
|
if (marks.length === 0) {
|
|
@@ -496,6 +913,66 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
496
913
|
}).observe(document, { childList: true, subtree: true, characterData: true });
|
|
497
914
|
window.addEventListener("resize", redraw);
|
|
498
915
|
window.addEventListener("scroll", redraw, true);
|
|
916
|
+
// Follow-ups. Once a scroll, a resize or a run of changes has settled: the marks that moved
|
|
917
|
+
// since the host was last told, in one report (P104), while any mark has a marker (a host
|
|
918
|
+
// without markers hears the reports it heard before); and once a scroll has settled, the
|
|
919
|
+
// person's selection, if its box moved since it was told (P105). Each only what moved more
|
|
920
|
+
// than 0.5 px, and all of them on one clock, at most one report a second (the latest): they
|
|
921
|
+
// share the bridge's 120 a minute with the person's own selections and clicks. While marks
|
|
922
|
+
// are being found again, that run tells them instead.
|
|
923
|
+
const FOLLOW_EVERY = 1000;
|
|
924
|
+
let settleTimer = 0;
|
|
925
|
+
let followTimer = 0;
|
|
926
|
+
let followed = 0;
|
|
927
|
+
// A scroll moved the page since the selection was last told: it is followed only then.
|
|
928
|
+
let selectionScrolled = false;
|
|
929
|
+
const counted = () => marks.some((mark) => mark.count !== null);
|
|
930
|
+
const sendFollow = () => {
|
|
931
|
+
let sent = 0;
|
|
932
|
+
if (marks.length > 0 && !settling() && counted()) {
|
|
933
|
+
const pairs = resultsOf(true);
|
|
934
|
+
if (pairs.length > 0) {
|
|
935
|
+
report(pairs);
|
|
936
|
+
sent += 1;
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
if (selectionScrolled) {
|
|
940
|
+
selectionScrolled = false;
|
|
941
|
+
if (reportSelection(true)) {
|
|
942
|
+
sent += 1;
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
if (sent > 0) {
|
|
946
|
+
// Each report takes its own second of the clock: two at once leave the next one empty.
|
|
947
|
+
followed = Date.now() + (sent - 1) * FOLLOW_EVERY;
|
|
948
|
+
}
|
|
949
|
+
};
|
|
950
|
+
const follow = () => {
|
|
951
|
+
if (followTimer !== 0 || !(selectionScrolled || counted())) {
|
|
952
|
+
return;
|
|
953
|
+
}
|
|
954
|
+
const wait = followed + FOLLOW_EVERY - Date.now();
|
|
955
|
+
if (wait <= 0) {
|
|
956
|
+
sendFollow();
|
|
957
|
+
return;
|
|
958
|
+
}
|
|
959
|
+
followTimer = setTimeout(() => {
|
|
960
|
+
followTimer = 0;
|
|
961
|
+
sendFollow();
|
|
962
|
+
}, wait);
|
|
963
|
+
};
|
|
964
|
+
const settle = () => {
|
|
965
|
+
if (marks.length === 0 && !selectionScrolled) {
|
|
966
|
+
return;
|
|
967
|
+
}
|
|
968
|
+
clearTimeout(settleTimer);
|
|
969
|
+
settleTimer = setTimeout(() => {
|
|
970
|
+
settleTimer = 0;
|
|
971
|
+
follow();
|
|
972
|
+
}, 150);
|
|
973
|
+
};
|
|
974
|
+
window.addEventListener("resize", settle);
|
|
975
|
+
window.addEventListener("scroll", settle, true);
|
|
499
976
|
|
|
500
977
|
const focus = (id) => {
|
|
501
978
|
const mark = marks.filter((each) => each.id === id)[0];
|
|
@@ -519,9 +996,92 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
519
996
|
setTimeout(redraw, 1850);
|
|
520
997
|
}
|
|
521
998
|
draw();
|
|
522
|
-
|
|
999
|
+
report([{ mark, result: resultOf(mark) }]);
|
|
523
1000
|
};
|
|
524
1001
|
|
|
1002
|
+
// A marker's click (P104). The layer takes no pointer but on its markers, so a press whose
|
|
1003
|
+
// target is the layer is on a marker: the content's handlers never see it (this listener
|
|
1004
|
+
// runs before any of theirs), nor does what lies under it, and a click is reported. No tab
|
|
1005
|
+
// stop, by choice: it would join the page's focus order, which the content controls, and the
|
|
1006
|
+
// layer is hidden from assistive technology; the host's own list of marks is the keyboard way.
|
|
1007
|
+
//
|
|
1008
|
+
// A press is read and stopped through the platform's own functions and getters, kept now
|
|
1009
|
+
// (P105), as the navigation guard reads its events (P099): content that later patches
|
|
1010
|
+
// Event's, MouseEvent's or DOMRect's prototypes (or Function's call) neither hears a press
|
|
1011
|
+
// through this listener nor makes it misread one. Narrowing only: content can still cover
|
|
1012
|
+
// the layer, reach its nodes through other built-ins, and tell that a press happened.
|
|
1013
|
+
const stopPress = Event.prototype.stopImmediatePropagation;
|
|
1014
|
+
// The platform's preventDefault, kept before a tab's shell locks its own over it (P099).
|
|
1015
|
+
const cancelPress = preventNavigation;
|
|
1016
|
+
const targetOf = getterOf(Event.prototype, "target");
|
|
1017
|
+
const typeOf = getterOf(Event.prototype, "type");
|
|
1018
|
+
const buttonOf = getterOf(MouseEvent.prototype, "button");
|
|
1019
|
+
const clientXOf = getterOf(MouseEvent.prototype, "clientX");
|
|
1020
|
+
const clientYOf = getterOf(MouseEvent.prototype, "clientY");
|
|
1021
|
+
const leftOf = getterOf(DOMRectReadOnly.prototype, "left");
|
|
1022
|
+
const topOf = getterOf(DOMRectReadOnly.prototype, "top");
|
|
1023
|
+
const widthOf = getterOf(DOMRectReadOnly.prototype, "width");
|
|
1024
|
+
const heightOf = getterOf(DOMRectReadOnly.prototype, "height");
|
|
1025
|
+
const markerBox = (element) => {
|
|
1026
|
+
const rect = callOf(elementRect, element, []);
|
|
1027
|
+
return { x: callOf(leftOf, rect, []), y: callOf(topOf, rect, []), width: callOf(widthOf, rect, []), height: callOf(heightOf, rect, []) };
|
|
1028
|
+
};
|
|
1029
|
+
const markerAt = (x, y) => {
|
|
1030
|
+
for (let i = markers.length - 1; i >= 0; i -= 1) {
|
|
1031
|
+
const rect = markerBox(markers[i].element);
|
|
1032
|
+
if (x >= rect.x && x <= rect.x + rect.width && y >= rect.y && y <= rect.y + rect.height) {
|
|
1033
|
+
return markers[i];
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
return null;
|
|
1037
|
+
};
|
|
1038
|
+
const isShown = (id) => {
|
|
1039
|
+
for (let i = 0; i < marks.length; i += 1) {
|
|
1040
|
+
if (marks[i].id === id) {
|
|
1041
|
+
return true;
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
return false;
|
|
1045
|
+
};
|
|
1046
|
+
const onMarker = (event) => {
|
|
1047
|
+
if (layer === null || callOf(targetOf, event, []) !== layer) {
|
|
1048
|
+
return;
|
|
1049
|
+
}
|
|
1050
|
+
callOf(stopPress, event, []);
|
|
1051
|
+
const type = callOf(typeOf, event, []);
|
|
1052
|
+
// No focus moves, no selection changes, no menu; a touch still makes its click (on WebKit,
|
|
1053
|
+
// since the marker has a listener of its own, P105).
|
|
1054
|
+
if (CANCELLED[type] === 1) {
|
|
1055
|
+
callOf(cancelPress, event, []);
|
|
1056
|
+
}
|
|
1057
|
+
if (HOVERS[type] === 1 || HOVERS[type] === 0) {
|
|
1058
|
+
const hit = HOVERS[type] === 1 ? markerAt(callOf(clientXOf, event, []), callOf(clientYOf, event, [])) : null;
|
|
1059
|
+
const id = hit === null ? null : hit.id;
|
|
1060
|
+
if (id !== hot) {
|
|
1061
|
+
hot = id;
|
|
1062
|
+
redraw();
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
// isTrusted is the event's own, unforgeable.
|
|
1066
|
+
if (type === "click" && event.isTrusted && callOf(buttonOf, event, []) === 0) {
|
|
1067
|
+
const hit = markerAt(callOf(clientXOf, event, []), callOf(clientYOf, event, []));
|
|
1068
|
+
if (hit !== null && isShown(hit.id)) {
|
|
1069
|
+
tell(M.activated, { id: hit.id, rect: markerBox(hit.element) });
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1072
|
+
};
|
|
1073
|
+
// The pointer moving over a marker is stopped too. Narrowing only: the content still sees
|
|
1074
|
+
// the pointer leave its own element for the layer (relatedTarget), the layer's :hover and
|
|
1075
|
+
// :active, and the frame's user activation, so it can tell a marker was pressed, and
|
|
1076
|
+
// roughly where. Content that patches no built-in can't act on or cancel the click; one that
|
|
1077
|
+
// does can still cover the layer or reach its nodes (P105).
|
|
1078
|
+
const CANCELLED = { mousedown: 1, mouseup: 1, click: 1, dblclick: 1, auxclick: 1, contextmenu: 1 };
|
|
1079
|
+
// The pointer on a marker (1), or leaving the layer for the content (0).
|
|
1080
|
+
const HOVERS = { pointerover: 1, pointermove: 1, pointerout: 0, pointerleave: 0 };
|
|
1081
|
+
for (const type of ["pointerdown", "pointerup", "pointercancel", "mousedown", "mouseup", "click", "dblclick", "auxclick", "contextmenu", "touchstart", "touchend", "touchcancel", "pointerover", "pointerout", "pointerenter", "pointerleave", "pointermove", "mouseover", "mouseout", "mouseenter", "mouseleave", "mousemove"]) {
|
|
1082
|
+
window.addEventListener(type, onMarker, true);
|
|
1083
|
+
}
|
|
1084
|
+
|
|
525
1085
|
// Picking an element: what is under the pointer, never the page itself or the shell's layer.
|
|
526
1086
|
let cursor = null;
|
|
527
1087
|
const setCursor = (on) => {
|
|
@@ -541,11 +1101,12 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
541
1101
|
mode = { elements: false, text: false };
|
|
542
1102
|
hover = null;
|
|
543
1103
|
reported = null;
|
|
1104
|
+
reportedBox = null;
|
|
544
1105
|
setCursor(false);
|
|
545
1106
|
draw();
|
|
546
1107
|
};
|
|
547
|
-
// The content's own handlers never see the pointer while an element is being picked
|
|
548
|
-
// finger outlines what it is on as it goes down.
|
|
1108
|
+
// The content's own handlers never see the pointer while an element is being picked (content
|
|
1109
|
+
// that patches Event.prototype can: P105); a finger outlines what it is on as it goes down.
|
|
549
1110
|
let pressed = null;
|
|
550
1111
|
const swallow = (event) => {
|
|
551
1112
|
if (mode.elements) {
|
|
@@ -608,35 +1169,44 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
608
1169
|
}
|
|
609
1170
|
}, true);
|
|
610
1171
|
|
|
611
|
-
// Selecting text: each selection that settles, and its clearing;
|
|
1172
|
+
// Selecting text: each selection that settles, and its clearing; after a scroll, the same
|
|
1173
|
+
// selection again only if its box moved, on the follow-ups' clock (P105: re-sent after every
|
|
1174
|
+
// settle, an honest page's scrolling spent the budget its marker's click needed).
|
|
612
1175
|
let reported = null;
|
|
1176
|
+
let reportedBox = null;
|
|
613
1177
|
let selectionTimer = 0;
|
|
614
|
-
|
|
1178
|
+
// Whether it told the host anything.
|
|
1179
|
+
const reportSelection = (following) => {
|
|
615
1180
|
if (!mode.text) {
|
|
616
|
-
return;
|
|
1181
|
+
return false;
|
|
617
1182
|
}
|
|
618
1183
|
const selection = selectionOf.call(document);
|
|
619
1184
|
if (selection === null || selection.isCollapsed || selection.rangeCount === 0) {
|
|
620
1185
|
if (reported !== null) {
|
|
621
1186
|
reported = null;
|
|
1187
|
+
reportedBox = null;
|
|
622
1188
|
tell(M.unselected, {});
|
|
1189
|
+
return true;
|
|
623
1190
|
}
|
|
624
|
-
return;
|
|
1191
|
+
return false;
|
|
625
1192
|
}
|
|
626
1193
|
const range = selection.getRangeAt(0);
|
|
627
1194
|
if (!document.body || !document.body.contains(range.commonAncestorContainer)) {
|
|
628
|
-
return;
|
|
1195
|
+
return false;
|
|
629
1196
|
}
|
|
630
1197
|
const anchor = textAnchor(range, textIndex());
|
|
631
1198
|
if (anchor === null) {
|
|
632
|
-
return;
|
|
1199
|
+
return false;
|
|
633
1200
|
}
|
|
634
1201
|
const key = anchor.selectors[1].start + ":" + anchor.selectors[1].end;
|
|
635
|
-
|
|
636
|
-
|
|
1202
|
+
const rect = box(rangeRect.call(range));
|
|
1203
|
+
if (key === reported && !(following && apart(reportedBox, rect))) {
|
|
1204
|
+
return false;
|
|
637
1205
|
}
|
|
638
1206
|
reported = key;
|
|
639
|
-
|
|
1207
|
+
reportedBox = rect;
|
|
1208
|
+
tell(M.selected, { anchor, rect });
|
|
1209
|
+
return true;
|
|
640
1210
|
};
|
|
641
1211
|
document.addEventListener("selectionchange", () => {
|
|
642
1212
|
if (mode.text) {
|
|
@@ -644,11 +1214,10 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
644
1214
|
selectionTimer = setTimeout(() => reportSelection(false), 250);
|
|
645
1215
|
}
|
|
646
1216
|
});
|
|
647
|
-
let scrollTimer = 0;
|
|
648
1217
|
window.addEventListener("scroll", () => {
|
|
649
1218
|
if (reported !== null) {
|
|
650
|
-
|
|
651
|
-
|
|
1219
|
+
selectionScrolled = true;
|
|
1220
|
+
settle();
|
|
652
1221
|
}
|
|
653
1222
|
}, true);
|
|
654
1223
|
|
|
@@ -666,6 +1235,7 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
666
1235
|
mode = { elements: params.elements === true, text: params.text === true };
|
|
667
1236
|
hover = null;
|
|
668
1237
|
reported = null;
|
|
1238
|
+
reportedBox = null;
|
|
669
1239
|
setCursor(mode.elements);
|
|
670
1240
|
draw();
|
|
671
1241
|
reportSelection(false);
|
|
@@ -677,7 +1247,7 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
677
1247
|
marks = (Array.isArray(params.marks) ? params.marks : [])
|
|
678
1248
|
.slice(0, LIMIT.marks)
|
|
679
1249
|
.filter((mark) => mark && typeof mark.id === "string" && mark.anchor && Array.isArray(mark.anchor.selectors))
|
|
680
|
-
.map((mark) => ({ id: mark.id, anchor: mark.anchor, found: null, state: null }));
|
|
1250
|
+
.map((mark) => ({ id: mark.id, anchor: mark.anchor, count: typeof mark.count === "number" && mark.count >= 0 && mark.count <= LIMIT.count && Math.floor(mark.count) === mark.count ? mark.count : null, found: null, state: null, told: null }));
|
|
681
1251
|
if (typeof params.active === "string") {
|
|
682
1252
|
active = params.active;
|
|
683
1253
|
}
|
|
@@ -693,6 +1263,7 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
|
|
|
693
1263
|
pendingFocus = null;
|
|
694
1264
|
marks = [];
|
|
695
1265
|
active = null;
|
|
1266
|
+
hot = null;
|
|
696
1267
|
draw();
|
|
697
1268
|
break;
|
|
698
1269
|
default:
|