@botiverse/porthole 0.15.1 → 0.15.3

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.
@@ -931,9 +931,14 @@ export const SHELL_RUNTIME = String.raw`(() => {
931
931
  const noRewrite = function () {
932
932
  throw new Error("document.open and document.write aren't available here: build the page with DOM methods");
933
933
  };
934
+ const unfixedBeforeRewrite = unfixed.length;
934
935
  for (const name of ["open", "write", "writeln"]) {
935
936
  lock(Document.prototype, name, noRewrite);
936
937
  }
938
+ // Whether all three are fixed in place. The press port rests on it (P148): document.open() takes
939
+ // every listener off the window, the runtime's among them, and a document opened and closed
940
+ // again loads again, so content that could call it would hear a port before the runtime did.
941
+ const rewriteFixed = unfixed.length === unfixedBeforeRewrite;
937
942
 
938
943
  // Some engines act on a hint the moment its element is connected or changed (WebKit opens a
939
944
  // preconnect then, outside the CSP), before the observer's microtask. So every way to connect a
@@ -1397,6 +1402,254 @@ export const SHELL_RUNTIME = String.raw`(() => {
1397
1402
  ]);
1398
1403
  }
1399
1404
 
1405
+ // A nested page's real presses, told to its shell (P148). The shell sees a press in the page only
1406
+ // as its own activation turning on, and a press within 5 s of the last gesture turns nothing on
1407
+ // (P111): to the host that reads as a button that does nothing. So the runtime, which runs before
1408
+ // any content and hears the browser's own events first, tells the shell each one, over a
1409
+ // MessagePort the shell sends at each load of the page's frame. What keeps that the runtime's
1410
+ // word and never the content's:
1411
+ // - the port: taken here, by the first listener there is (on window, capturing), from the first
1412
+ // message that is the browser's own (isTrusted), from the parent read now, that names the
1413
+ // method and carries exactly one port. The event goes no further. Every other event of that
1414
+ // name is stopped too, so content never hears one, and one that fails a check doesn't use up
1415
+ // the one port. The port is in this closure alone: no content holds it or can post on it. No
1416
+ // second port is taken: the shell's next one (it sends one at every load of the frame, and
1417
+ // WebKit fires a load for a navigation within the document) is answered on the first, that
1418
+ // this document is still here;
1419
+ // - the press: only the browser's own events (isTrusted), read through the platform's getters
1420
+ // kept now. A mouse on its way down; a finger or a pen on its way up (one that scrolls ends in
1421
+ // pointercancel: HTML's own rule for what activates); Enter or Space, not held down, on a
1422
+ // native control that isn't editable (a link takes Enter alone), by its tag and type, never
1423
+ // its role, which is the content's word; a click on such a control (an assistive technology's
1424
+ // activation is a click alone), but not the click a form gives its submit button when Enter is
1425
+ // pressed in another of its fields, which is typing, nor any click while a key is down;
1426
+ // - what is told: nothing but that there was one. No place, no target, no time.
1427
+ // After a press is told, what the page's calls say to its host (send, below) waits for the
1428
+ // shell's word that it has the press (it tells the host before it answers), so a call made in
1429
+ // the press's own handler never arrives ahead of it; a shell that doesn't answer in 250 ms
1430
+ // holds nothing back.
1431
+ // None of it is installed unless all of it can be (a built-in missing, or document.open, write
1432
+ // or writeln not fixed in place above): the page then tells no press, and the host has the
1433
+ // shell's activation alone, as before.
1434
+ let pressHeld = null;
1435
+ const pressParent = window.parent;
1436
+ if (plan.press === true && !hostless && pressParent !== window && rewriteFixed) {
1437
+ const messageProto = typeof MessageEvent === "function" ? MessageEvent.prototype : null;
1438
+ const messageSourceOf = getterOf(messageProto, "source");
1439
+ const messageDataOf = getterOf(messageProto, "data");
1440
+ const messagePortsOf = getterOf(messageProto, "ports");
1441
+ const stopNow = Event.prototype.stopImmediatePropagation;
1442
+ const portProto = typeof MessagePort === "function" ? MessagePort.prototype : null;
1443
+ const portPost = portProto === null ? null : portProto.postMessage;
1444
+ const portStart = portProto === null ? null : portProto.start;
1445
+ const eventTargetOf = getterOf(Event.prototype, "target");
1446
+ const pointerTypeOf = getterOf(typeof PointerEvent === "function" ? PointerEvent.prototype : null, "pointerType");
1447
+ const keyProto = typeof KeyboardEvent === "function" ? KeyboardEvent.prototype : null;
1448
+ const keyOf = getterOf(keyProto, "key");
1449
+ const repeatOf = getterOf(keyProto, "repeat");
1450
+ const editableOf = getterOf(HTMLElement.prototype, "isContentEditable");
1451
+ const designModeOf = getterOf(Document.prototype, "designMode");
1452
+ const activeOf = getterOf(Document.prototype, "activeElement");
1453
+ const controlTypeOf = getterOf(HTMLInputElement.prototype, "type");
1454
+ // Each control's form through its own getter: a field tied to a form by its form attribute is
1455
+ // not inside that form's element.
1456
+ const formGetters = bare(null);
1457
+ formGetters.button = getterOf(HTMLButtonElement.prototype, "form");
1458
+ formGetters.input = getterOf(HTMLInputElement.prototype, "form");
1459
+ formGetters.select = getterOf(HTMLSelectElement.prototype, "form");
1460
+ formGetters.textarea = getterOf(HTMLTextAreaElement.prototype, "form");
1461
+ const pressLater = setTimeout;
1462
+ const pressNoLater = clearTimeout;
1463
+ const pressListen = EventTarget.prototype.addEventListener;
1464
+ const all =
1465
+ messageSourceOf !== null && messageDataOf !== null && messagePortsOf !== null &&
1466
+ typeof stopNow === "function" && typeof portPost === "function" && typeof portStart === "function" &&
1467
+ eventTargetOf !== null && pointerTypeOf !== null && keyOf !== null && repeatOf !== null &&
1468
+ editableOf !== null && designModeOf !== null && activeOf !== null && controlTypeOf !== null &&
1469
+ formGetters.button !== null && formGetters.input !== null && formGetters.select !== null && formGetters.textarea !== null &&
1470
+ localNameOf !== null && namespaceOf !== null && nodeTypeOf !== null;
1471
+ if (all) {
1472
+ let port = null;
1473
+ // How many presses the shell hasn't answered yet, and the timer that stops waiting for it.
1474
+ let unanswered = 0;
1475
+ let giveUp = 0;
1476
+ const release = () => {
1477
+ unanswered = 0;
1478
+ callOf(pressNoLater, window, [giveUp]);
1479
+ const held = pressHeld;
1480
+ pressHeld = null;
1481
+ if (held !== null) {
1482
+ for (let i = 0; i < held.length; i += 1) {
1483
+ post(held[i]);
1484
+ }
1485
+ }
1486
+ };
1487
+ const tellPress = () => {
1488
+ if (port === null) {
1489
+ return;
1490
+ }
1491
+ try {
1492
+ callOf(portPost, port, [0]);
1493
+ } catch (_) {
1494
+ return;
1495
+ }
1496
+ unanswered += 1;
1497
+ if (pressHeld === null) {
1498
+ pressHeld = [];
1499
+ }
1500
+ callOf(pressNoLater, window, [giveUp]);
1501
+ giveUp = callOf(pressLater, window, [release, 250]);
1502
+ };
1503
+ callOf(pressListen, window, [
1504
+ "message",
1505
+ (event) => {
1506
+ let named = false;
1507
+ try {
1508
+ const data = callOf(messageDataOf, event, []);
1509
+ named = data !== null && typeof data === "object" && data.method === "porthole/shell/press-port";
1510
+ } catch (_) {}
1511
+ if (!named) {
1512
+ return;
1513
+ }
1514
+ callOf(stopNow, event, []);
1515
+ if (event.isTrusted !== true) {
1516
+ return;
1517
+ }
1518
+ try {
1519
+ const ports = callOf(messagePortsOf, event, []);
1520
+ if (callOf(messageSourceOf, event, []) !== pressParent || ports === null || ports.length !== 1) {
1521
+ return;
1522
+ }
1523
+ // Only the first is ever taken. A later one is the shell's for a load that left this
1524
+ // document where it was (WebKit's, for a navigation within it, P142): told, on the
1525
+ // port held, that this document is still here, so the shell goes on hearing that one.
1526
+ if (port !== null) {
1527
+ callOf(portPost, port, [1]);
1528
+ return;
1529
+ }
1530
+ const taken = ports[0];
1531
+ // The shell's answer to a press: it has it, and has told the host.
1532
+ callOf(pressListen, taken, [
1533
+ "message",
1534
+ () => {
1535
+ unanswered -= 1;
1536
+ if (unanswered <= 0) {
1537
+ release();
1538
+ }
1539
+ },
1540
+ ]);
1541
+ callOf(portStart, taken, []);
1542
+ port = taken;
1543
+ } catch (_) {}
1544
+ },
1545
+ true,
1546
+ ]);
1547
+ // A native control a key or a click presses: "both" (Enter and Space), "enter" (a link), or
1548
+ // "" for anything else, an editable one included. By the kept getters: the tag and the type,
1549
+ // never an attribute the content writes to say what it is.
1550
+ const controlOf = (node) => {
1551
+ if (typeOf(node) !== 1 || callOf(namespaceOf, node, []) !== XHTML) {
1552
+ return "";
1553
+ }
1554
+ const name = callOf(localNameOf, node, []);
1555
+ let kind = "";
1556
+ if (name === "button" || name === "summary") {
1557
+ kind = "both";
1558
+ } else if (name === "input") {
1559
+ const type = callOf(controlTypeOf, node, []);
1560
+ kind = type === "button" || type === "submit" || type === "reset" ? "both" : "";
1561
+ } else if (name === "a") {
1562
+ kind = callOf(hasAttributeIn, node, ["href"]) === true ? "enter" : "";
1563
+ }
1564
+ return kind !== "" && callOf(editableOf, node, []) !== true ? kind : "";
1565
+ };
1566
+ const formOf = (node) => {
1567
+ if (typeOf(node) !== 1 || callOf(namespaceOf, node, []) !== XHTML) {
1568
+ return null;
1569
+ }
1570
+ const getter = formGetters[callOf(localNameOf, node, [])];
1571
+ return typeof getter === "function" ? callOf(getter, node, []) : null;
1572
+ };
1573
+ // The browser's own event: nothing a page dispatches is a press.
1574
+ const real = (event) => event.isTrusted === true;
1575
+ // Whether a key is down: from the browser's own keydown to its keyup, or the next press of
1576
+ // a pointer. No click in that time is a press. The key's own press was judged as it went
1577
+ // down, where it went down: content that moves the focus to its button inside the keydown
1578
+ // of an Enter typed in a text field gets that button a click of the browser's own, and a
1579
+ // held key's repeats give a button one each. An assistive technology's click comes with no
1580
+ // key down.
1581
+ let keyDown = false;
1582
+ const pointed = (down) => (event) => {
1583
+ if (!real(event)) {
1584
+ return;
1585
+ }
1586
+ keyDown = false;
1587
+ try {
1588
+ if ((callOf(pointerTypeOf, event, []) === "mouse") === down) {
1589
+ tellPress();
1590
+ }
1591
+ } catch (_) {}
1592
+ };
1593
+ callOf(pressListen, window, ["pointerdown", pointed(true), true]);
1594
+ callOf(pressListen, window, ["pointerup", pointed(false), true]);
1595
+ callOf(pressListen, window, [
1596
+ "keyup",
1597
+ (event) => {
1598
+ if (real(event)) {
1599
+ keyDown = false;
1600
+ }
1601
+ },
1602
+ true,
1603
+ ]);
1604
+ callOf(pressListen, window, [
1605
+ "keydown",
1606
+ (event) => {
1607
+ if (!real(event)) {
1608
+ return;
1609
+ }
1610
+ keyDown = true;
1611
+ try {
1612
+ // A key held down goes on being the key it was when it went down, wherever the focus
1613
+ // has moved since: its repeats are no press.
1614
+ const key = callOf(keyOf, event, []);
1615
+ if ((key !== "Enter" && key !== " ") || callOf(repeatOf, event, []) === true || callOf(designModeOf, document, []) === "on") {
1616
+ return;
1617
+ }
1618
+ const kind = controlOf(callOf(eventTargetOf, event, []));
1619
+ if (kind === "both" || (kind === "enter" && key === "Enter")) {
1620
+ tellPress();
1621
+ }
1622
+ } catch (_) {}
1623
+ },
1624
+ true,
1625
+ ]);
1626
+ callOf(pressListen, window, [
1627
+ "click",
1628
+ (event) => {
1629
+ if (!real(event)) {
1630
+ return;
1631
+ }
1632
+ try {
1633
+ const target = callOf(eventTargetOf, event, []);
1634
+ if (keyDown || controlOf(target) === "") {
1635
+ return;
1636
+ }
1637
+ // A form's own click on its submit button, for Enter in another of its fields.
1638
+ const active = callOf(activeOf, document, []);
1639
+ if (active !== null && active !== target) {
1640
+ const form = formOf(target);
1641
+ if (form !== null && form === formOf(active)) {
1642
+ return;
1643
+ }
1644
+ }
1645
+ tellPress();
1646
+ } catch (_) {}
1647
+ },
1648
+ true,
1649
+ ]);
1650
+ }
1651
+ }
1652
+
1400
1653
  // The clipboard is the host's to write (P051): content can neither write nor read it. A copy
1401
1654
  // the person makes (their keys, their menu) copies their selection as the browser makes it: no
1402
1655
  // copy or cut listener of the content's runs, so none can swap what goes on the clipboard.
@@ -1615,8 +1868,14 @@ export const SHELL_RUNTIME = String.raw`(() => {
1615
1868
  window.parent.postMessage(Object.assign({ jsonrpc: "2.0" }, message), "*");
1616
1869
  }
1617
1870
  };
1871
+ // After a press was told to the shell (P148), what the shell's protocol says next (a call, an
1872
+ // answer) waits for the shell to have the press: posted by the release above, through post.
1618
1873
  const send = (message) => {
1619
1874
  if (plan.protocol === "shell") {
1875
+ if (pressHeld !== null) {
1876
+ pressHeld[pressHeld.length] = message;
1877
+ return;
1878
+ }
1620
1879
  post(message);
1621
1880
  }
1622
1881
  };