@botiverse/porthole 0.14.10 → 0.14.12

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.
@@ -32,14 +32,36 @@ export const MARKER_HIT = String.raw `(count, boxAt, x, y, touch) => {
32
32
  }
33
33
  return best;
34
34
  }`;
35
+ /**
36
+ * What a marker is called (P133, P134), to whoever can't see it, spliced into the runtime: what it
37
+ * is, and the words it is on, in quotes. In English, whatever the page's language: the shell has no
38
+ * words of the host's. `anchor.label` stays as the host stores it (it is the host's to show): an
39
+ * element's starts with its tag (`describe`, "p#intro.lead Words"), so an element's words are its
40
+ * quote, cut where the label's are and marked so, or for one with no text of its own (an image's
41
+ * `alt`) what follows the tag in the label. An element with neither has no words.
42
+ */
43
+ export const MARKER_NAME = String.raw `(mark) => {
44
+ const squash = (text) => String(text).replace(/\s+/g, " ").trim();
45
+ const anchor = mark.anchor;
46
+ const label = typeof anchor.label === "string" ? squash(anchor.label).slice(0, 120) : "";
47
+ let words = label;
48
+ if (anchor.kind === "element") {
49
+ const quote = anchor.selectors.filter((each) => each && each.type === "TextQuoteSelector")[0];
50
+ const said = quote && typeof quote.exact === "string" ? squash(quote.exact.slice(0, 300)) : "";
51
+ const space = label.indexOf(" ");
52
+ words = said.length > 40 ? said.slice(0, 40).trim() + "\u2026" : said !== "" ? said : space === -1 ? "" : label.slice(space + 1);
53
+ }
54
+ const what = mark.draft ? "Draft comment" : mark.count === 1 ? "1 comment" : mark.count > 1 ? mark.count + " comments" : "Comment";
55
+ return words === "" ? what : what + ' on "' + words + '"';
56
+ }`;
35
57
  /**
36
58
  * The shell's annotation runtime (P053), spliced into the shell's runtime (`__ANNOTATE__`) when
37
59
  * the config declares `annotate`, so it runs before any content does, with the DOM's own
38
60
  * functions as they were. Plain ES2020 like the rest of the shell; no template literals (it sits
39
61
  * in one).
40
62
  *
41
- * The host drives it (`porthole/annotate.start|stop|show|focus|clear|track|clearSelection`) and it
42
- * reports (`porthole/annotate.selected|unselected|cancelled|anchored|activated|tracked`):
63
+ * The host drives it (`porthole/annotate.start|stop|show|focus|clear|track|clearSelection|focusMark`)
64
+ * and it reports (`porthole/annotate.selected|unselected|cancelled|anchored|activated|tracked`):
43
65
  * - picking an element: the element under the pointer (or finger) is outlined with its name; a
44
66
  * click or tap picks it, and the content's own handlers never see that click (unless the content
45
67
  * patched the DOM's built-ins, P105); Escape cancels (while selecting text too, P112);
@@ -49,17 +71,22 @@ export const MARKER_HIT = String.raw `(count, boxAt, x, y, touch) => {
49
71
  * was, its text anywhere, its words with any spacing) and drawn over it, in a layer of the
50
72
  * shell's own (a closed shadow root), again whenever the content changes; what can't be found
51
73
  * is reported `orphaned`;
52
- * - markers (P104): a mark with a count gets a small bubble with the number beside its anchor's
53
- * last line (P105: in the line's gutter); a click on it is reported (`activated`), and the
54
- * content's own handlers never see it; a draft (P107) is drawn apart, a dashed line and a hollow
55
- * bubble with no number, and has a marker with or without a count;
74
+ * - markers (P104): a mark with a count gets a small button with the number, a bubble: a text
75
+ * anchor's at its paragraph's end (P133), a draft's (P107) beside its anchor's last line (P105:
76
+ * in the line's gutter), an element's on its corner; a click on it, or Enter or Space with the
77
+ * focus on it (P133), is reported (`activated`), and the content's own handlers never see it; a
78
+ * draft is drawn apart, a dashed line and a hollow bubble with no number, and has a marker with
79
+ * or without a count; the host's `focusMark` (P134) puts the focus on a marker that is there, and
80
+ * tells nothing back;
56
81
  * - following: while a mark has a marker, once a scroll, a resize or a change of the content
57
82
  * settles, the marks that moved since the host was last told are reported, at most once a
58
83
  * second, so the host's own UI beside a mark can follow;
59
84
  * - places in the document (P132): each result's box also in the document's coordinates
60
85
  * (`documentRect`), with how far the document was scrolled as it was read (`scroll`), and the
61
86
  * followed mark's (`tracked`) too, so a host places marks scrolled out of the frame, and keeps
62
- * them placed as the document scrolls, from one report and the followed mark's scroll.
87
+ * them placed as the document scrolls, from one report and the followed mark's scroll;
88
+ * - the last line (P133): where a selection or a mark's words end, in the document's coordinates
89
+ * too (`lastLineDocumentRect`), so a host places its UI after them.
63
90
  *
64
91
  * It shares the realm with the content, which can interfere with all of it: the host takes its
65
92
  * reports as UI hints only.
@@ -81,6 +108,7 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
81
108
  track: "porthole/annotate.track",
82
109
  tracked: "porthole/annotate.tracked",
83
110
  clearSelection: "porthole/annotate.clearSelection",
111
+ focusMark: "porthole/annotate.focusMark",
84
112
  };
85
113
  // The DOM's own functions, taken before content runs.
86
114
  const elementFromPoint = Document.prototype.elementFromPoint;
@@ -91,6 +119,7 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
91
119
  const createElementNS = Document.prototype.createElementNS;
92
120
  const selectionOf = Document.prototype.getSelection;
93
121
  const removeAllRanges = Selection.prototype.removeAllRanges;
122
+ const focusElement = HTMLElement.prototype.focus;
94
123
  const elementRect = Element.prototype.getBoundingClientRect;
95
124
  const rangeRect = Range.prototype.getBoundingClientRect;
96
125
  const rangeRects = Range.prototype.getClientRects;
@@ -321,6 +350,19 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
321
350
  };
322
351
  // A box in the viewport, in the document (P132): where it is as the document scrolls.
323
352
  const inDocument = (rect, scroll) => ({ x: rect.x + scroll.x, y: rect.y + scroll.y, width: rect.width, height: rect.height });
353
+ // Where what was found ends (P133), in the document as a documentRect is: its last line with
354
+ // words on it, or an element's whole box (it has no lines of its own). Null with nothing drawn.
355
+ const lastLineOf = (found, scroll) => {
356
+ const rect = found.range ? lastBox(rangeRects.call(found.range)) : elementRect.call(found.element);
357
+ return rect === null ? null : inDocument(box(rect), scroll);
358
+ };
359
+ // What a selection's report adds to its box (P133): its last line, with the scroll it was
360
+ // read at; neither where nothing of it is drawn.
361
+ const placeOf = (found) => {
362
+ const scroll = scrollOf();
363
+ const lastLineDocumentRect = lastLineOf(found, scroll);
364
+ return lastLineDocumentRect === null ? {} : { lastLineDocumentRect, scroll };
365
+ };
324
366
 
325
367
  // The shell's own layer, over the content: marks, the outline of what would be picked.
326
368
  let layer = null;
@@ -335,18 +377,22 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
335
377
  // marker hollow, a light bubble with a dashed edge and the glyph's outline, no number.
336
378
  // While the last pointer was a finger or a pen (P119), each marker takes the pointer in a
337
379
  // box of at least 44 by 44 px centred on its own (an invisible ::before; 100% is its padding
338
- // box, its border 1 px), so a tap a few px off still lands on the shell's layer.
339
- 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)}.mark.draft{box-sizing:border-box;background:rgba(250,204,21,.08);box-shadow:none;border-bottom:2px dashed rgba(202,138,4,.95)}.mark.draft.active{background:rgba(250,204,21,.16);box-shadow:0 0 0 2px rgba(234,179,8,.45);border-bottom-width:3px}.mark.element.draft{background:rgba(250,204,21,.04);border-bottom:0;outline-style:dashed}.mark.element.draft.active{background:rgba(250,204,21,.08);box-shadow:none;outline-style:dashed}.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.draft{border:1px dashed rgb(202,138,4);background:#fffdf5;color:#a16207;box-shadow:0 1px 2px rgba(0,0,0,.2)}.marker.draft:hover{background:#fef9e7}.marker.draft.active{background:#fef3c7;box-shadow:0 0 0 2px rgba(234,179,8,.55),0 1px 2px rgba(0,0,0,.2)}.marker.inert{pointer-events:none}.touch>.marker::before{content:'';position:absolute;left:50%;top:50%;width:max(100% + 2px,44px);height:max(100% + 2px,44px);transform:translate(-50%,-50%)}.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}";
380
+ // box, its border 1 px), so a tap a few px off still lands on the shell's layer. A marker is
381
+ // a real button (P133): what a browser gives one is taken off, a ring says it has the focus.
382
+ 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)}.mark.draft{box-sizing:border-box;background:rgba(250,204,21,.08);box-shadow:none;border-bottom:2px dashed rgba(202,138,4,.95)}.mark.draft.active{background:rgba(250,204,21,.16);box-shadow:0 0 0 2px rgba(234,179,8,.45);border-bottom-width:3px}.mark.element.draft{background:rgba(250,204,21,.04);border-bottom:0;outline-style:dashed}.mark.element.draft.active{background:rgba(250,204,21,.08);box-shadow:none;outline-style:dashed}.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;appearance:none;-webkit-appearance:none}.marker:focus-visible{outline:2px solid #2563eb;outline-offset:1px}.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.draft{border:1px dashed rgb(202,138,4);background:#fffdf5;color:#a16207;box-shadow:0 1px 2px rgba(0,0,0,.2)}.marker.draft:hover{background:#fef9e7}.marker.draft.active{background:#fef3c7;box-shadow:0 0 0 2px rgba(234,179,8,.55),0 1px 2px rgba(0,0,0,.2)}.marker.inert,[data-placing]>.marker{pointer-events:none}.touch>.marker::before{content:'';position:absolute;left:50%;top:50%;width:max(100% + 2px,44px);height:max(100% + 2px,44px);transform:translate(-50%,-50%)}.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}";
340
383
  // Inline and important: the content's own style sheets can't hide, shrink or move it by
341
384
  // accident. Set again at each draw, since its scripts can rewrite it (narrowing only: the
342
- // content can still cover the layer, or move it with its ancestors, P104).
343
- 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";
385
+ // content can still cover the layer, or move it with its ancestors, P104). Clipped, where an
386
+ // engine has it, not hidden: hidden is a box a focused marker out of view would scroll (P133).
387
+ 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;overflow:clip!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";
344
388
  const ensureLayer = () => {
345
389
  if (layer === null) {
346
390
  layer = createElement.call(document, "div");
347
- layer.setAttribute("aria-hidden", "true");
348
391
  layer.setAttribute("data-porthole-annotations", "");
349
392
  shadow = attachShadow.call(layer, { mode: "closed" });
393
+ // Within the shadow root: a focus moving from one marker to the next never leaves it.
394
+ callOf(listenOn, shadow, ["focusin", onMarkerFocus]);
395
+ callOf(listenOn, shadow, ["focusout", onMarkerFocus]);
350
396
  }
351
397
  if (layer.style.cssText !== LAYER_STYLE) {
352
398
  layer.style.cssText = LAYER_STYLE;
@@ -378,6 +424,7 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
378
424
  const markerWidth = (label) => Math.max(MARKER_HEIGHT, 23 + (label === "" ? 0 : 3 + label.length * 7));
379
425
  // Compact (P105): the number alone, or the glyph alone for none, with less padding.
380
426
  const compactWidth = (label) => (label === "" ? 21 : Math.max(MARKER_HEIGHT, 10 + label.length * 7));
427
+ const nameOf = ${MARKER_NAME};
381
428
  // A rect's last line with words on it: its last box with a width, else any with a size.
382
429
  const lastBox = (rects) => {
383
430
  let any = null;
@@ -401,7 +448,9 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
401
448
  // in a row of them), just past the line's last word. Markers on one line of one block go
402
449
  // together, in the order their words end. Where the bubble with its glyph fits in none of
403
450
  // these, the number alone does (compact); where that fits in none either (a full line on a
404
- // narrow screen), it goes as far right as it can, over the line's end.
451
+ // narrow screen), it goes as far right as it can, over the line's end. A posted comment's
452
+ // marker (P133) is placed the same way on its paragraph's last line instead, where nothing
453
+ // follows its words, and first tries just past that line's last word.
405
454
  const computedOf = window.getComputedStyle;
406
455
  const elementsAt = Document.prototype.elementsFromPoint;
407
456
  const containsOf = Node.prototype.contains;
@@ -461,6 +510,52 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
461
510
  }
462
511
  return end;
463
512
  };
513
+ // The end of the paragraph an anchor ends in (P133): a range over the block's last words
514
+ // that are drawn, from the anchor's end on, as far as a block inside it (a nested list, say:
515
+ // its words are their own paragraph), 256 text nodes at most. Null if none follow the anchor.
516
+ const inBlock = (node, block) => {
517
+ for (let at = node.parentElement; at !== null && at !== block; at = at.parentElement) {
518
+ if (!INLINE.test(computedOf.call(window, at).display)) {
519
+ return false;
520
+ }
521
+ }
522
+ return true;
523
+ };
524
+ const paragraphEnd = (range, block) => {
525
+ const start = range.endContainer;
526
+ if (start.nodeType !== 3) {
527
+ return null;
528
+ }
529
+ const walker = createTreeWalker.call(document, block, NodeFilter.SHOW_TEXT);
530
+ walker.currentNode = start;
531
+ let end = null;
532
+ let node = start;
533
+ for (let pieces = 0; node !== null && pieces < 256; pieces += 1) {
534
+ if (node !== start && !inBlock(node, block)) {
535
+ break;
536
+ }
537
+ const words = node.nodeValue;
538
+ const first = node === start ? range.endOffset : 0;
539
+ let to = words.length;
540
+ while (to > first && /\s/.test(words.charAt(to - 1))) {
541
+ to -= 1;
542
+ }
543
+ if (to > first) {
544
+ const piece = createRange.call(document);
545
+ try {
546
+ piece.setStart(node, Math.max(first, to - 2));
547
+ piece.setEnd(node, to);
548
+ } catch (_) {
549
+ return end;
550
+ }
551
+ if (lastBox(rangeRects.call(piece)) !== null) {
552
+ end = piece;
553
+ }
554
+ }
555
+ node = walker.nextNode();
556
+ }
557
+ return end;
558
+ };
464
559
  // Nothing of the content's at a point but a box that holds the block (its background); a
465
560
  // point out of view hides nothing.
466
561
  const clearAt = (x, y, block) => {
@@ -513,7 +608,11 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
513
608
  return null;
514
609
  };
515
610
  const gutter = right + gap;
516
- let left = fits(gutter, false);
611
+ // A paragraph's end (P133): just past its last word, where the line leaves the room.
612
+ let left = line.ending ? fits(line.words + gap, false) : null;
613
+ if (left === null) {
614
+ left = fits(gutter, false);
615
+ }
517
616
  if (left === null && gutter > most) {
518
617
  left = fits(most, true);
519
618
  }
@@ -525,7 +624,8 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
525
624
  const placeLines = (wanted, placed, room) => {
526
625
  // One line of one block: markers whose last lines share a middle.
527
626
  const lines = [];
528
- wanted.sort((a, b) => a.at.top - b.at.top || a.at.right - b.at.right);
627
+ // In the order their words end, those at one paragraph's end too.
628
+ wanted.sort((a, b) => a.at.top - b.at.top || a.at.right - b.at.right || a.words.top - b.words.top || a.words.right - b.words.right);
529
629
  for (const each of wanted) {
530
630
  const middle = each.at.top + each.at.height / 2;
531
631
  let line = null;
@@ -535,10 +635,11 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
535
635
  }
536
636
  }
537
637
  if (line === null) {
538
- line = { block: each.block, middle, height: each.at.height, bottom: each.at.bottom, range: each.range, at: each.at, items: [] };
638
+ line = { block: each.block, middle, height: each.at.height, bottom: each.at.bottom, range: each.range, at: each.at, ending: false, items: [] };
539
639
  lines.push(line);
540
640
  }
541
641
  line.items.push(each);
642
+ line.ending = line.ending || each.ending;
542
643
  if (each.at.right > line.at.right) {
543
644
  line.at = each.at;
544
645
  line.range = each.range;
@@ -617,20 +718,24 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
617
718
  const kept = markerNodes.get(mark.id);
618
719
  let marker = kept ? kept.element : null;
619
720
  if (marker === null) {
620
- marker = createElement.call(document, "div");
621
- // A listener of its own makes it a thing to tap: WebKit makes a tap a click only on such
622
- // an element (P105, measured: the window's listener isn't enough). It never runs (the
623
- // window's first listener stops every click on the layer); the click is told there.
624
- callOf(listenOn, marker, ["click", tapTarget]);
625
- }
721
+ // A real button (P133): the keyboard reaches it, Enter and Space click it, assistive
722
+ // technology names it, and WebKit makes a tap a click on it (P105).
723
+ marker = createElement.call(document, "button");
724
+ marker.setAttribute("type", "button");
725
+ }
726
+ // Stepped aside while elements are picked, from the keyboard too. Set, not left to the
727
+ // button: Safari's Tab skips a button without one, unless the person asked otherwise.
728
+ marker.tabIndex = mode.elements ? -1 : 0;
626
729
  marker.className = "marker" + (mark.draft ? " draft" : "") + (each.compact ? " compact" : "") + (mark.id === active ? " active" : "") + (mode.elements ? " inert" : "");
627
730
  marker.style.cssText = "left:" + each.left + "px;top:" + each.top + "px";
628
731
  markers.push({ id: mark.id, element: marker });
629
- const form = (mark.draft ? "draft:" : "") + (each.compact ? "compact:" : "full:") + label;
732
+ const name = nameOf(mark);
733
+ const form = (mark.draft ? "draft:" : "") + (each.compact ? "compact:" : "full:") + label + ":" + name;
630
734
  if (kept && kept.form === form) {
631
735
  return marker;
632
736
  }
633
737
  markerNodes.set(mark.id, { element: marker, form });
738
+ marker.setAttribute("aria-label", name);
634
739
  marker.replaceChildren();
635
740
  if (!each.compact || label === "") {
636
741
  const glyph = createElementNS.call(document, SVG, "svg");
@@ -657,10 +762,9 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
657
762
  }
658
763
  return marker;
659
764
  };
660
- const tapTarget = () => {};
661
765
  let active = null;
662
- // The mark whose marker the pointer is over: its words are lit as the active mark's are, so
663
- // a marker in the gutter shows which words it is for (P105).
766
+ // The mark whose marker the pointer is over, or the focus is on (P133): its words are lit as
767
+ // the active mark's are, so a marker in the gutter shows which words it is for (P105).
664
768
  let hot = null;
665
769
  let flashing = null;
666
770
  let flashUntil = 0;
@@ -676,6 +780,10 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
676
780
  style.nonce = nonce;
677
781
  }
678
782
  boxes = { style, marks: createElement.call(document, "div"), markers: createElement.call(document, "div"), hover: createElement.call(document, "div") };
783
+ // Only the markers are for assistive technology (P133): the rest draws what the page
784
+ // already says.
785
+ boxes.marks.setAttribute("aria-hidden", "true");
786
+ boxes.hover.setAttribute("aria-hidden", "true");
679
787
  root.replaceChildren(boxes.style, boxes.marks, boxes.markers, boxes.hover);
680
788
  }
681
789
  if (boxes.style.textContent !== STYLE) {
@@ -697,6 +805,7 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
697
805
  // What is measured first, then drawn: no node of the layer changes while the page is read.
698
806
  const corners = [];
699
807
  const wanted = [];
808
+ const focused = focusedMarker() !== null;
700
809
  markers = [];
701
810
  for (const mark of marks) {
702
811
  if (mark.found === null) {
@@ -717,14 +826,28 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
717
826
  if (at !== null) {
718
827
  // A draft's marker has no number (P107).
719
828
  const label = mark.draft ? "" : labelOf(mark.count);
720
- const each = { mark, order: corners.length + wanted.length, label, width: markerWidth(label), small: compactWidth(label), compact: false, at, left: 0, top: 0 };
829
+ const each = { mark, label, width: markerWidth(label), small: compactWidth(label), compact: false, at, words: at, left: 0, top: 0, ending: false };
721
830
  if (mark.found.range) {
722
831
  // Kept while it still holds the words (P105): a change elsewhere doesn't move them.
723
832
  if (mark.block === undefined || !mark.block.isConnected || !containsOf.call(mark.block, mark.found.range.endContainer)) {
724
833
  mark.block = blockOf(mark.found.range);
834
+ mark.end = undefined;
725
835
  }
726
836
  each.range = mark.found.range;
727
837
  each.block = mark.block;
838
+ // A posted comment's, at its paragraph's end (P133); read once for what was found,
839
+ // since a block's words can be long. A draft's stays by the words being written on.
840
+ if (!mark.draft) {
841
+ each.ending = true;
842
+ if (mark.end === undefined) {
843
+ mark.end = paragraphEnd(mark.found.range, mark.block);
844
+ }
845
+ const last = mark.end === null ? null : lastBox(rangeRects.call(mark.end));
846
+ if (last !== null) {
847
+ each.at = last;
848
+ each.range = mark.end;
849
+ }
850
+ }
728
851
  wanted.push(each);
729
852
  } else {
730
853
  corners.push(each);
@@ -740,18 +863,25 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
740
863
  }
741
864
  // The layer's own boxes out of the way while places are hit-tested (P105): each test
742
865
  // otherwise walks every mark and marker the last draw left (0.13 ms each, 300 of them).
866
+ // A marker with the focus would lose it out of the display (P133): while the person is on
867
+ // one, the markers only stop taking the pointer, which the tests skip but still walk.
743
868
  if (wanted.length > 0) {
744
869
  into.marks.style.display = "none";
745
- into.markers.style.display = "none";
870
+ if (focused) {
871
+ into.markers.setAttribute("data-placing", "");
872
+ } else {
873
+ into.markers.style.display = "none";
874
+ }
746
875
  try {
747
876
  placeLines(wanted, placed, room);
748
877
  } finally {
749
878
  into.marks.style.display = "";
750
879
  into.markers.style.display = "";
880
+ into.markers.removeAttribute("data-placing");
751
881
  }
752
882
  }
753
883
  const badges = [];
754
- for (const each of corners.concat(wanted).sort((a, b) => a.order - b.order)) {
884
+ for (const each of corners.concat(wanted).sort((a, b) => a.mark.reading - b.mark.reading)) {
755
885
  badges.push(markerOf(each));
756
886
  }
757
887
  for (const id of Array.from(markerNodes.keys())) {
@@ -777,11 +907,14 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
777
907
  into.markers.removeChild(child);
778
908
  }
779
909
  }
780
- for (const badge of badges) {
781
- if (badge.parentNode !== into.markers) {
782
- into.markers.appendChild(badge);
910
+ // In reading order, the Tab key's (P133). A marker that stays in its place isn't touched
911
+ // (a press spans a redraw, above), and none moves while one has the focus: it would lose it.
912
+ badges.forEach((badge, at) => {
913
+ const there = into.markers.childNodes[at] || null;
914
+ if (badge.parentNode !== into.markers || (!focused && there !== badge)) {
915
+ into.markers.insertBefore(badge, focused ? null : there);
783
916
  }
784
- }
917
+ });
785
918
  into.hover.replaceChildren(...outlines);
786
919
  };
787
920
  let drawing = false;
@@ -817,7 +950,8 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
817
950
  // Its box moves as the document scrolls, inside the frame or not: each one tells the
818
951
  // host the scroll, for every mark it places by its place in the document (P132).
819
952
  const scroll = scrollOf();
820
- tell(M.tracked, { id: tracking, rect, documentRect: inDocument(rect, scroll), scroll });
953
+ const lastLineDocumentRect = lastLineOf(mark.found, scroll);
954
+ tell(M.tracked, Object.assign({ id: tracking, rect, documentRect: inDocument(rect, scroll), scroll }, lastLineDocumentRect === null ? {} : { lastLineDocumentRect }));
821
955
  }
822
956
  }
823
957
  trackFrame = nextFrame(trackTick);
@@ -861,15 +995,16 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
861
995
  placed.forEach((each, at) => orders.set(each.mark, at));
862
996
  return orders;
863
997
  };
864
- // A mark's result: its box in the viewport, and in the document (P132) by scroll, the
865
- // report's one reading of it.
998
+ // A mark's result: its box in the viewport, and in the document (P132), its last line too
999
+ // (P133), by scroll, the report's one reading of it.
866
1000
  const resultOf = (mark, orders, scroll) => {
867
1001
  if (mark.found === null) {
868
1002
  return { id: mark.id, state: "orphaned" };
869
1003
  }
870
1004
  const order = (orders || ordersOf()).get(mark);
871
1005
  const rect = box(rectOf(mark.found));
872
- return Object.assign({ id: mark.id, state: "attached", rect, documentRect: inDocument(rect, scroll) }, order === undefined ? {} : { order });
1006
+ const lastLineDocumentRect = lastLineOf(mark.found, scroll);
1007
+ return Object.assign({ id: mark.id, state: "attached", rect, documentRect: inDocument(rect, scroll) }, lastLineDocumentRect === null ? {} : { lastLineDocumentRect }, order === undefined ? {} : { order });
873
1008
  };
874
1009
  // Two boxes more than 0.5 px apart on any side (or one of them unknown).
875
1010
  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;
@@ -911,8 +1046,15 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
911
1046
  changed = true;
912
1047
  }
913
1048
  mark.found = found;
1049
+ mark.end = undefined;
914
1050
  mark.state = state;
915
1051
  }
1052
+ // Where each starts, once for each finding: the markers' reading order (P133).
1053
+ const orders = ordersOf();
1054
+ for (const mark of marks) {
1055
+ const reading = orders.get(mark);
1056
+ mark.reading = reading === undefined ? marks.length : reading;
1057
+ }
916
1058
  if (all || changed) {
917
1059
  const scroll = scrollOf();
918
1060
  report(resultsOf(false, scroll), scroll);
@@ -929,6 +1071,7 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
929
1071
  // mark), nor before a run of changes settles; a focus asked for meanwhile waits for it.
930
1072
  let waitingForContent = false;
931
1073
  let pendingFocus = null;
1074
+ let pendingMarker = null;
932
1075
  const settling = () => anchorTimer !== 0 || waitingForContent || marks.some((mark) => mark.state === null);
933
1076
  const scheduleAnchor = (all) => {
934
1077
  if (all) {
@@ -967,6 +1110,11 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
967
1110
  pendingFocus = null;
968
1111
  focus(id);
969
1112
  }
1113
+ if (pendingMarker !== null) {
1114
+ const id = pendingMarker;
1115
+ pendingMarker = null;
1116
+ focusMark(id);
1117
+ }
970
1118
  }, wait);
971
1119
  };
972
1120
  new MutationObserver((records) => {
@@ -1071,11 +1219,33 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
1071
1219
  report([{ mark, result: resultOf(mark, null, scroll) }], scroll);
1072
1220
  };
1073
1221
 
1222
+ // The focus on a mark's marker, at the host's word (P134). Marks a show just replaced have no
1223
+ // markers yet, so it waits for them, as focus does; then a marker that isn't there, or has
1224
+ // stepped aside for picking, is nothing to do. The kept function, so a page that patched
1225
+ // focus isn't handed the marker from inside the closed root. The shell's focus listener
1226
+ // lights it and brings it into view; nothing is told.
1227
+ const focusMark = (id) => {
1228
+ if (settling()) {
1229
+ pendingMarker = id;
1230
+ return;
1231
+ }
1232
+ if (mode.elements || !isShown(id)) {
1233
+ return;
1234
+ }
1235
+ for (let i = 0; i < markers.length; i += 1) {
1236
+ if (markers[i].id === id) {
1237
+ callOf(focusElement, markers[i].element, [{ preventScroll: true }]);
1238
+ return;
1239
+ }
1240
+ }
1241
+ };
1242
+
1074
1243
  // A marker's click (P104). The layer takes no pointer but on its markers, so a press whose
1075
1244
  // target is the layer is on a marker: the content's handlers never see it (this listener
1076
- // runs before any of theirs), nor does what lies under it, and a click is reported. No tab
1077
- // stop, by choice: it would join the page's focus order, which the content controls, and the
1078
- // layer is hidden from assistive technology; the host's own list of marks is the keyboard way.
1245
+ // runs before any of theirs), nor does what lies under it, and a click is reported. A press
1246
+ // of the mouse or a finger moves no focus; the keyboard reaches a marker as any button
1247
+ // (P133, which reverses P104's no tab stop): it joins the page's focus order, which the
1248
+ // content controls, so the host's own list of marks stays the keyboard way it can rely on.
1079
1249
  //
1080
1250
  // A press is read and stopped through the platform's own functions and getters, kept now
1081
1251
  // (P105), as the navigation guard reads its events (P099): content that later patches
@@ -1088,6 +1258,11 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
1088
1258
  const targetOf = getterOf(Event.prototype, "target");
1089
1259
  const typeOf = getterOf(Event.prototype, "type");
1090
1260
  const buttonOf = getterOf(MouseEvent.prototype, "button");
1261
+ const detailOf = getterOf(UIEvent.prototype, "detail");
1262
+ const keyOf = getterOf(KeyboardEvent.prototype, "key");
1263
+ const repeatOf = getterOf(KeyboardEvent.prototype, "repeat");
1264
+ const activeOf = getterOf(ShadowRoot.prototype, "activeElement");
1265
+ const scrollWindow = window.scrollBy;
1091
1266
  const clientXOf = getterOf(MouseEvent.prototype, "clientX");
1092
1267
  const clientYOf = getterOf(MouseEvent.prototype, "clientY");
1093
1268
  const leftOf = getterOf(DOMRectReadOnly.prototype, "left");
@@ -1104,6 +1279,16 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
1104
1279
  const at = markerHit(markers.length, (i) => markerBox(markers[i].element), x, y, touch);
1105
1280
  return at < 0 ? null : markers[at];
1106
1281
  };
1282
+ const markerOfNode = (element) => {
1283
+ for (let i = 0; i < markers.length; i += 1) {
1284
+ if (markers[i].element === element) {
1285
+ return markers[i];
1286
+ }
1287
+ }
1288
+ return null;
1289
+ };
1290
+ // The marker with the focus (P133), or null.
1291
+ const focusedMarker = () => (shadow === null || activeOf === null ? null : markerOfNode(callOf(activeOf, shadow, [])));
1107
1292
  const isShown = (id) => {
1108
1293
  for (let i = 0; i < marks.length; i += 1) {
1109
1294
  if (marks[i].id === id) {
@@ -1133,12 +1318,57 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
1133
1318
  }
1134
1319
  // isTrusted is the event's own, unforgeable.
1135
1320
  if (type === "click" && event.isTrusted && callOf(buttonOf, event, []) === 0) {
1321
+ // From the keyboard or assistive technology (P133) a click has no count and no place:
1322
+ // it is the focused marker's. Not while elements are picked: the markers step aside.
1323
+ const keyed = callOf(detailOf, event, []) === 0 ? focusedMarker() : null;
1324
+ if (keyed !== null) {
1325
+ if (!mode.elements && isShown(keyed.id)) {
1326
+ tell(M.activated, { id: keyed.id, rect: markerBox(keyed.element) });
1327
+ }
1328
+ return;
1329
+ }
1136
1330
  const hit = markerAt(callOf(clientXOf, event, []), callOf(clientYOf, event, []));
1137
1331
  if (hit !== null && isShown(hit.id)) {
1138
1332
  tell(M.activated, { id: hit.id, rect: markerBox(hit.element) });
1139
1333
  }
1140
1334
  }
1141
1335
  };
1336
+ // The keys that click a focused marker (P133), each stopped as a press is, so the content
1337
+ // neither hears them nor cancels the click they make; it keeps every other key, Tab and
1338
+ // Escape too, as it would on a button of its own, and the host's find shortcut stays. A key
1339
+ // held down clicks once: each repeat would spend the bridge's rate on one press.
1340
+ const onMarkerKey = (event) => {
1341
+ if (layer === null || callOf(targetOf, event, []) !== layer) {
1342
+ return;
1343
+ }
1344
+ const key = callOf(keyOf, event, []);
1345
+ if (key === "Enter" || key === " ") {
1346
+ callOf(stopPress, event, []);
1347
+ if (callOf(repeatOf, event, [])) {
1348
+ callOf(cancelPress, event, []);
1349
+ }
1350
+ }
1351
+ };
1352
+ for (const type of ["keydown", "keypress", "keyup"]) {
1353
+ window.addEventListener(type, onMarkerKey, true);
1354
+ }
1355
+ // The focus on a marker (P133) lights its words, as the pointer does, and brings it into view:
1356
+ // a marker out of view is out of the layer's box, which never scrolls, so the document must.
1357
+ const onMarkerFocus = (event) => {
1358
+ const on = callOf(typeOf, event, []) === "focusin" ? markerOfNode(callOf(targetOf, event, [])) : null;
1359
+ const id = on === null ? null : on.id;
1360
+ if (id !== hot) {
1361
+ hot = id;
1362
+ redraw();
1363
+ }
1364
+ if (on !== null) {
1365
+ const rect = markerBox(on.element);
1366
+ const seen = document.documentElement.clientHeight || window.innerHeight;
1367
+ if (rect.y < 0 || rect.y + rect.height > seen) {
1368
+ callOf(scrollWindow, window, [0, rect.y - (seen - rect.height) / 2]);
1369
+ }
1370
+ }
1371
+ };
1142
1372
  // The pointer moving over a marker is stopped too. Narrowing only: the content still sees
1143
1373
  // the pointer leave its own element for the layer (relatedTarget), the layer's :hover and
1144
1374
  // :active, and the frame's user activation, so it can tell a marker was pressed, and
@@ -1258,7 +1488,7 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
1258
1488
  return;
1259
1489
  }
1260
1490
  swallowUntil = performance.now() + 2000;
1261
- tell(M.selected, { anchor, rect: box(elementRect.call(element)) });
1491
+ tell(M.selected, Object.assign({ anchor, rect: box(elementRect.call(element)) }, placeOf({ element })));
1262
1492
  }, HOLD_MS),
1263
1493
  };
1264
1494
  }, true);
@@ -1306,7 +1536,7 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
1306
1536
  hover = null;
1307
1537
  setCursor(false);
1308
1538
  draw();
1309
- tell(M.selected, { anchor, rect: box(elementRect.call(element)) });
1539
+ tell(M.selected, Object.assign({ anchor, rect: box(elementRect.call(element)) }, placeOf({ element })));
1310
1540
  }, true);
1311
1541
  window.addEventListener("keydown", (event) => {
1312
1542
  if (event.key !== "Escape") {
@@ -1365,7 +1595,7 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
1365
1595
  }
1366
1596
  reported = key;
1367
1597
  reportedBox = rect;
1368
- tell(M.selected, { anchor, rect });
1598
+ tell(M.selected, Object.assign({ anchor, rect }, placeOf({ range })));
1369
1599
  return true;
1370
1600
  };
1371
1601
  document.addEventListener("selectionchange", () => {
@@ -1444,9 +1674,13 @@ export const ANNOTATE_RUNTIME = String.raw `if (declared("annotate")) {
1444
1674
  }
1445
1675
  break;
1446
1676
  }
1677
+ case M.focusMark:
1678
+ focusMark(String(params.id));
1679
+ break;
1447
1680
  case M.clear:
1448
1681
  track(null);
1449
1682
  pendingFocus = null;
1683
+ pendingMarker = null;
1450
1684
  marks = [];
1451
1685
  active = null;
1452
1686
  hot = null;
@@ -42,7 +42,7 @@ export declare const FIND_LATEST: string;
42
42
  * important, so the content's own style sheets can't hide, shrink or move it by accident (its
43
43
  * scripts can, P104). The annotation runtime carries the same text (a test holds them equal).
44
44
  */
45
- export declare 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";
45
+ export declare 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;overflow:clip!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";
46
46
  /**
47
47
  * The shell's find in page (P131), spliced into the shell's runtime (`__FIND__`) when the config
48
48
  * declares `find`, so it runs before any content does, with the DOM's own functions as they were.
@@ -135,7 +135,7 @@ export const FIND_LATEST = String.raw `(later, time, quiet, run, superseded) =>
135
135
  * important, so the content's own style sheets can't hide, shrink or move it by accident (its
136
136
  * scripts can, P104). The annotation runtime carries the same text (a test holds them equal).
137
137
  */
138
- export 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";
138
+ export 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;overflow:clip!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";
139
139
  /**
140
140
  * The shell's find in page (P131), spliced into the shell's runtime (`__FIND__`) when the config
141
141
  * declares `find`, so it runs before any content does, with the DOM's own functions as they were.