@botiverse/porthole 0.14.8 → 0.14.9
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/CHANGELOG.md +27 -0
- package/dist/bridge/bridge.d.ts +6 -0
- package/dist/bridge/bridge.js +5 -0
- package/dist/check/page-source.js +1 -1
- package/dist/config/capabilities.d.ts +16 -2
- package/dist/config/capabilities.js +8 -0
- package/dist/conformance/harness-source.js +1 -1
- package/dist/find/types.d.ts +83 -0
- package/dist/find/types.js +29 -0
- package/dist/find/validate.d.ts +17 -0
- package/dist/find/validate.js +82 -0
- package/dist/guide/guide.js +2 -1
- package/dist/host/create-host.js +214 -0
- package/dist/host/pass-on.js +3 -2
- package/dist/host/types.d.ts +51 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -0
- package/dist/react/frame.d.ts +43 -0
- package/dist/react/frame.js +107 -3
- package/dist/react/view.js +2 -0
- package/dist/runtime/contract.d.ts +1 -1
- package/dist/runtime/contract.js +1 -1
- package/dist/shell/annotate.js +2 -95
- package/dist/shell/document.d.ts +3 -3
- package/dist/shell/document.js +5 -3
- package/dist/shell/find.d.ts +82 -0
- package/dist/shell/find.js +725 -0
- package/dist/shell/runtime.js +4 -1
- package/dist/shell/text.d.ts +17 -0
- package/dist/shell/text.js +140 -0
- package/docs/ADOPTING.md +83 -0
- package/docs/ARCHITECTURE.md +10 -3
- package/docs/DECISIONS.md +219 -1
- package/docs/DESIGN.md +33 -0
- package/docs/THREAT_MODEL.md +48 -4
- package/docs/insertion-entry-points.md +17 -0
- package/package.json +1 -1
- package/src/bridge/bridge.ts +7 -0
- package/src/config/capabilities.ts +13 -1
- package/src/find/types.ts +86 -0
- package/src/find/validate.ts +101 -0
- package/src/guide/guide.ts +3 -1
- package/src/host/create-host.ts +230 -0
- package/src/host/pass-on.ts +3 -2
- package/src/host/types.ts +52 -0
- package/src/index.ts +12 -0
- package/src/react/frame.tsx +160 -1
- package/src/react/view.tsx +3 -0
- package/src/runtime/contract.ts +1 -1
- package/src/shell/annotate.ts +3 -95
- package/src/shell/document.ts +5 -3
- package/src/shell/find.ts +731 -0
- package/src/shell/runtime.ts +4 -1
- package/src/shell/text.ts +140 -0
package/src/shell/runtime.ts
CHANGED
|
@@ -1780,6 +1780,8 @@ export const SHELL_RUNTIME = String.raw`(() => {
|
|
|
1780
1780
|
|
|
1781
1781
|
__ANNOTATE__
|
|
1782
1782
|
|
|
1783
|
+
__FIND__
|
|
1784
|
+
|
|
1783
1785
|
if (plan.protocol !== "shell") {
|
|
1784
1786
|
// A view that speaks for itself.
|
|
1785
1787
|
if (typeof plan.document === "string") {
|
|
@@ -2213,7 +2215,8 @@ export const SHELL_RUNTIME = String.raw`(() => {
|
|
|
2213
2215
|
}
|
|
2214
2216
|
break;
|
|
2215
2217
|
default:
|
|
2216
|
-
|
|
2218
|
+
// Find's own request is answered by the find runtime (P131), where the config declares it.
|
|
2219
|
+
if (data.id !== undefined && !(data.method === "porthole/find.query" && declared("find"))) {
|
|
2217
2220
|
send({ id: data.id, error: { code: -32601, message: String(data.method) + " isn't offered here" } });
|
|
2218
2221
|
}
|
|
2219
2222
|
break;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The document's text as annotations (P053) and find in page (P131) read it, spliced into each of
|
|
3
|
+
* their runtimes (inside its own function, after it took `createTreeWalker` and `createRange` from
|
|
4
|
+
* the DOM before content ran): the body's text nodes in order, without scripts, styles, noscript
|
|
5
|
+
* and templates, as one string with each node's start (`textIndex()`); an offset into it as a
|
|
6
|
+
* boundary point (`pointAt`), a boundary point as an offset (`offsetOf`), a span as a range
|
|
7
|
+
* (`rangeAt`). One source, so a match find highlights and an anchor's `TextPositionSelector` count
|
|
8
|
+
* the same text. Plain ES2020, no template literals (it sits in one).
|
|
9
|
+
*
|
|
10
|
+
* Every built-in it calls is taken before content runs and called through the shell's `callOf`
|
|
11
|
+
* (P129): the walker's step, a node's type, name and text (the `nodeType`, `localName` and
|
|
12
|
+
* `nodeValue` getters), the body getter, a range's boundaries, and the map of nodes. Its
|
|
13
|
+
* lists are the shell's prototype-less ones (`listOf`, `pushTo`), and the names it skips a lookup
|
|
14
|
+
* with no prototype, so nothing content puts on `Array.prototype`, `Object.prototype`, `Map` or a
|
|
15
|
+
* DOM prototype takes part in what it reads.
|
|
16
|
+
*/
|
|
17
|
+
export const TEXT_INDEX = String.raw`// The document's text: the body's text nodes in order, without scripts, styles, templates.
|
|
18
|
+
const textNextNode = TreeWalker.prototype.nextNode;
|
|
19
|
+
const textTypeOf = getterOf(Node.prototype, "nodeType");
|
|
20
|
+
const textNameOf = getterOf(Element.prototype, "localName");
|
|
21
|
+
const textValueOf = getterOf(Node.prototype, "nodeValue");
|
|
22
|
+
const textBodyOf = getterOf(Document.prototype, "body");
|
|
23
|
+
const textSetStart = Range.prototype.setStart;
|
|
24
|
+
const textSetEnd = Range.prototype.setEnd;
|
|
25
|
+
const textCollapse = Range.prototype.collapse;
|
|
26
|
+
const textComparePoint = Range.prototype.comparePoint;
|
|
27
|
+
const TextMap = Map;
|
|
28
|
+
const textMapSet = Map.prototype.set;
|
|
29
|
+
const textMapGet = Map.prototype.get;
|
|
30
|
+
const textMapHas = Map.prototype.has;
|
|
31
|
+
const TEXT_SHOW = NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT;
|
|
32
|
+
const TEXT_ACCEPT = NodeFilter.FILTER_ACCEPT;
|
|
33
|
+
const TEXT_REJECT = NodeFilter.FILTER_REJECT;
|
|
34
|
+
const TEXT_SKIP = NodeFilter.FILTER_SKIP;
|
|
35
|
+
const SKIPPED = bare(null);
|
|
36
|
+
SKIPPED.script = 1;
|
|
37
|
+
SKIPPED.style = 1;
|
|
38
|
+
SKIPPED.noscript = 1;
|
|
39
|
+
SKIPPED.template = 1;
|
|
40
|
+
// A text node's text, read once through the kept getter; anything but a string is none.
|
|
41
|
+
const textOf = (node) => {
|
|
42
|
+
const value = textValueOf === null ? node.nodeValue : callOf(textValueOf, node, []);
|
|
43
|
+
return typeof value === "string" ? value : "";
|
|
44
|
+
};
|
|
45
|
+
const textKind = (node) => (textTypeOf === null ? node.nodeType : callOf(textTypeOf, node, []));
|
|
46
|
+
const textLesser = (a, b) => (a < b ? a : b);
|
|
47
|
+
const skippedElement = (node) => SKIPPED[textNameOf === null ? node.localName : callOf(textNameOf, node, [])] === 1;
|
|
48
|
+
const textIndex = () => {
|
|
49
|
+
const nodes = listOf();
|
|
50
|
+
const starts = listOf();
|
|
51
|
+
const at = new TextMap();
|
|
52
|
+
let text = "";
|
|
53
|
+
const body = textBodyOf === null ? document.body : callOf(textBodyOf, document, []);
|
|
54
|
+
if (body) {
|
|
55
|
+
const walker = callOf(createTreeWalker, document, [body, TEXT_SHOW, {
|
|
56
|
+
acceptNode: (node) => (textKind(node) === 1 ? (skippedElement(node) ? TEXT_REJECT : TEXT_SKIP) : TEXT_ACCEPT),
|
|
57
|
+
}]);
|
|
58
|
+
for (let node = callOf(textNextNode, walker, []); node !== null; node = callOf(textNextNode, walker, [])) {
|
|
59
|
+
callOf(textMapSet, at, [node, nodes.length]);
|
|
60
|
+
pushTo(nodes, node);
|
|
61
|
+
pushTo(starts, text.length);
|
|
62
|
+
text += textOf(node);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return { nodes, starts, at, text };
|
|
66
|
+
};
|
|
67
|
+
// A boundary point as an offset into the text: in a text node, where it is; elsewhere, the
|
|
68
|
+
// first text at or after it.
|
|
69
|
+
const offsetOf = (index, container, offset) => {
|
|
70
|
+
if (textKind(container) === 3 && callOf(textMapHas, index.at, [container])) {
|
|
71
|
+
return index.starts[callOf(textMapGet, index.at, [container])] + textLesser(offset, textOf(container).length);
|
|
72
|
+
}
|
|
73
|
+
const point = callOf(createRange, document, []);
|
|
74
|
+
try {
|
|
75
|
+
callOf(textSetStart, point, [container, offset]);
|
|
76
|
+
} catch (_) {
|
|
77
|
+
return index.text.length;
|
|
78
|
+
}
|
|
79
|
+
callOf(textCollapse, point, [true]);
|
|
80
|
+
let low = 0;
|
|
81
|
+
let high = index.nodes.length;
|
|
82
|
+
while (low < high) {
|
|
83
|
+
const middle = (low + high) >> 1;
|
|
84
|
+
if (callOf(textComparePoint, point, [index.nodes[middle], 0]) >= 0) {
|
|
85
|
+
high = middle;
|
|
86
|
+
} else {
|
|
87
|
+
low = middle + 1;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return low < index.nodes.length ? index.starts[low] : index.text.length;
|
|
91
|
+
};
|
|
92
|
+
const pointAt = (index, at, end) => {
|
|
93
|
+
let low = 0;
|
|
94
|
+
let high = index.nodes.length - 1;
|
|
95
|
+
let found = -1;
|
|
96
|
+
while (low <= high) {
|
|
97
|
+
const middle = (low + high) >> 1;
|
|
98
|
+
if (index.starts[middle] <= at) {
|
|
99
|
+
found = middle;
|
|
100
|
+
low = middle + 1;
|
|
101
|
+
} else {
|
|
102
|
+
high = middle - 1;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (found === -1) {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
let node = index.nodes[found];
|
|
109
|
+
let offset = at - index.starts[found];
|
|
110
|
+
if (end) {
|
|
111
|
+
// An end at a node's very start belongs to the text before it.
|
|
112
|
+
while (offset === 0 && found > 0) {
|
|
113
|
+
found -= 1;
|
|
114
|
+
node = index.nodes[found];
|
|
115
|
+
offset = textOf(node).length;
|
|
116
|
+
}
|
|
117
|
+
} else {
|
|
118
|
+
while (offset >= textOf(node).length && found + 1 < index.nodes.length) {
|
|
119
|
+
found += 1;
|
|
120
|
+
node = index.nodes[found];
|
|
121
|
+
offset = 0;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return { node, offset: textLesser(offset, textOf(node).length) };
|
|
125
|
+
};
|
|
126
|
+
const rangeAt = (index, start, end) => {
|
|
127
|
+
const from = pointAt(index, start, false);
|
|
128
|
+
const to = pointAt(index, end, true);
|
|
129
|
+
if (from === null || to === null) {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
const range = callOf(createRange, document, []);
|
|
133
|
+
try {
|
|
134
|
+
callOf(textSetStart, range, [from.node, from.offset]);
|
|
135
|
+
callOf(textSetEnd, range, [to.node, to.offset]);
|
|
136
|
+
} catch (_) {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
return range;
|
|
140
|
+
};`;
|