@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.
Files changed (54) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/dist/bridge/bridge.d.ts +6 -0
  3. package/dist/bridge/bridge.js +5 -0
  4. package/dist/check/page-source.js +1 -1
  5. package/dist/config/capabilities.d.ts +16 -2
  6. package/dist/config/capabilities.js +8 -0
  7. package/dist/conformance/harness-source.js +1 -1
  8. package/dist/find/types.d.ts +83 -0
  9. package/dist/find/types.js +29 -0
  10. package/dist/find/validate.d.ts +17 -0
  11. package/dist/find/validate.js +82 -0
  12. package/dist/guide/guide.js +2 -1
  13. package/dist/host/create-host.js +214 -0
  14. package/dist/host/pass-on.js +3 -2
  15. package/dist/host/types.d.ts +51 -0
  16. package/dist/index.d.ts +3 -1
  17. package/dist/index.js +3 -0
  18. package/dist/react/frame.d.ts +43 -0
  19. package/dist/react/frame.js +107 -3
  20. package/dist/react/view.js +2 -0
  21. package/dist/runtime/contract.d.ts +1 -1
  22. package/dist/runtime/contract.js +1 -1
  23. package/dist/shell/annotate.js +2 -95
  24. package/dist/shell/document.d.ts +3 -3
  25. package/dist/shell/document.js +5 -3
  26. package/dist/shell/find.d.ts +82 -0
  27. package/dist/shell/find.js +725 -0
  28. package/dist/shell/runtime.js +4 -1
  29. package/dist/shell/text.d.ts +17 -0
  30. package/dist/shell/text.js +140 -0
  31. package/docs/ADOPTING.md +83 -0
  32. package/docs/ARCHITECTURE.md +10 -3
  33. package/docs/DECISIONS.md +219 -1
  34. package/docs/DESIGN.md +33 -0
  35. package/docs/THREAT_MODEL.md +48 -4
  36. package/docs/insertion-entry-points.md +17 -0
  37. package/package.json +1 -1
  38. package/src/bridge/bridge.ts +7 -0
  39. package/src/config/capabilities.ts +13 -1
  40. package/src/find/types.ts +86 -0
  41. package/src/find/validate.ts +101 -0
  42. package/src/guide/guide.ts +3 -1
  43. package/src/host/create-host.ts +230 -0
  44. package/src/host/pass-on.ts +3 -2
  45. package/src/host/types.ts +52 -0
  46. package/src/index.ts +12 -0
  47. package/src/react/frame.tsx +160 -1
  48. package/src/react/view.tsx +3 -0
  49. package/src/runtime/contract.ts +1 -1
  50. package/src/shell/annotate.ts +3 -95
  51. package/src/shell/document.ts +5 -3
  52. package/src/shell/find.ts +731 -0
  53. package/src/shell/runtime.ts +4 -1
  54. package/src/shell/text.ts +140 -0
@@ -253,6 +253,9 @@ function InstanceView(props: PortholeViewProps): ReactElement | null {
253
253
  ref,
254
254
  (): PortholeFrameHandle => ({
255
255
  clearSelection: () => shown.current?.clearSelection(),
256
+ find: (query, options) =>
257
+ shown.current?.find(query, options) ?? Promise.resolve("unsupported"),
258
+ clearFind: () => shown.current?.clearFind(),
256
259
  }),
257
260
  [],
258
261
  );
@@ -171,7 +171,7 @@ export const RUNTIME_HOST_FUNCTIONS: readonly CapabilityName[] = [
171
171
 
172
172
  /**
173
173
  * Where a host function sits in the runtime: its capability and the methods it serves. Null for
174
- * the shell's own (`resize`, `reportError`, `annotate`), which content never calls by name.
174
+ * the shell's own (`resize`, `reportError`, `annotate`, `find`), which content never calls by name.
175
175
  */
176
176
  export function familyOf(
177
177
  serves: CapabilityName,
@@ -1,3 +1,5 @@
1
+ import { TEXT_INDEX } from "./text.ts";
2
+
1
3
  /**
2
4
  * Which marker a press on the shell's layer is for (P119), spliced into the runtime: the index of
3
5
  * `count` boxes (`boxAt(i)`, the markers' own boxes in the viewport, the last drawn on top), or -1.
@@ -107,101 +109,7 @@ export const ANNOTATE_RUNTIME = String.raw`if (declared("annotate")) {
107
109
  const squash = (text) => String(text).replace(/\s+/g, " ").trim();
108
110
  const box = (rect) => ({ x: rect.left, y: rect.top, width: rect.width, height: rect.height });
109
111
 
110
- // The document's text: the body's text nodes in order, without scripts, styles, templates.
111
- const SKIPPED = { script: 1, style: 1, noscript: 1, template: 1 };
112
- const textIndex = () => {
113
- const nodes = [];
114
- const starts = [];
115
- const at = new Map();
116
- let text = "";
117
- const body = document.body;
118
- if (body) {
119
- const walker = createTreeWalker.call(document, body, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT, {
120
- acceptNode: (node) => (node.nodeType === 1 ? (SKIPPED[node.localName] ? NodeFilter.FILTER_REJECT : NodeFilter.FILTER_SKIP) : NodeFilter.FILTER_ACCEPT),
121
- });
122
- for (let node = walker.nextNode(); node !== null; node = walker.nextNode()) {
123
- at.set(node, nodes.length);
124
- nodes.push(node);
125
- starts.push(text.length);
126
- text += node.nodeValue;
127
- }
128
- }
129
- return { nodes, starts, at, text };
130
- };
131
- // A boundary point as an offset into the text: in a text node, where it is; elsewhere, the
132
- // first text at or after it.
133
- const offsetOf = (index, container, offset) => {
134
- if (container.nodeType === 3 && index.at.has(container)) {
135
- return index.starts[index.at.get(container)] + Math.min(offset, container.nodeValue.length);
136
- }
137
- const point = createRange.call(document);
138
- try {
139
- point.setStart(container, offset);
140
- } catch (_) {
141
- return index.text.length;
142
- }
143
- point.collapse(true);
144
- let low = 0;
145
- let high = index.nodes.length;
146
- while (low < high) {
147
- const middle = (low + high) >> 1;
148
- if (point.comparePoint(index.nodes[middle], 0) >= 0) {
149
- high = middle;
150
- } else {
151
- low = middle + 1;
152
- }
153
- }
154
- return low < index.nodes.length ? index.starts[low] : index.text.length;
155
- };
156
- const pointAt = (index, at, end) => {
157
- let low = 0;
158
- let high = index.nodes.length - 1;
159
- let found = -1;
160
- while (low <= high) {
161
- const middle = (low + high) >> 1;
162
- if (index.starts[middle] <= at) {
163
- found = middle;
164
- low = middle + 1;
165
- } else {
166
- high = middle - 1;
167
- }
168
- }
169
- if (found === -1) {
170
- return null;
171
- }
172
- let node = index.nodes[found];
173
- let offset = at - index.starts[found];
174
- if (end) {
175
- // An end at a node's very start belongs to the text before it.
176
- while (offset === 0 && found > 0) {
177
- found -= 1;
178
- node = index.nodes[found];
179
- offset = node.nodeValue.length;
180
- }
181
- } else {
182
- while (offset >= node.nodeValue.length && found + 1 < index.nodes.length) {
183
- found += 1;
184
- node = index.nodes[found];
185
- offset = 0;
186
- }
187
- }
188
- return { node, offset: Math.min(offset, node.nodeValue.length) };
189
- };
190
- const rangeAt = (index, start, end) => {
191
- const from = pointAt(index, start, false);
192
- const to = pointAt(index, end, true);
193
- if (from === null || to === null) {
194
- return null;
195
- }
196
- const range = createRange.call(document);
197
- try {
198
- range.setStart(from.node, from.offset);
199
- range.setEnd(to.node, to.offset);
200
- } catch (_) {
201
- return null;
202
- }
203
- return range;
204
- };
112
+ ${TEXT_INDEX}
205
113
  const spanOf = (index, node) => {
206
114
  const range = createRange.call(document);
207
115
  range.selectNodeContents(node);
@@ -1,5 +1,6 @@
1
1
  import { hostOriginProblem } from "../isolation/served.ts";
2
2
  import { ANNOTATE_RUNTIME } from "./annotate.ts";
3
+ import { FIND_RUNTIME } from "./find.ts";
3
4
  import { LEGACY_GLOBALS_RUNTIME } from "./legacy.ts";
4
5
  import { SHELL_RUNTIME } from "./runtime.ts";
5
6
  import { USE_RUNTIME } from "./use.ts";
@@ -133,14 +134,15 @@ body {
133
134
  * A given `document` travels in the runtime's plan, as data.
134
135
  */
135
136
  /**
136
- * The shell's runtime for a plan, before its plan goes in: annotations where declared, 0.6's
137
- * globals (the legacy path, P085) for what the config declares, and the capability runtime where
138
- * the config has one.
137
+ * The shell's runtime for a plan, before its plan goes in: annotations and find in page (P131)
138
+ * where declared, 0.6's globals (the legacy path, P085) for what the config declares, and the
139
+ * capability runtime where the config has one.
139
140
  */
140
141
  export function composedRuntime(plan: ShellPlan): string {
141
142
  return SHELL_RUNTIME.replace("__ANNOTATE__", () =>
142
143
  plan.capabilities.annotate === undefined ? "" : ANNOTATE_RUNTIME,
143
144
  )
145
+ .replace("__FIND__", () => (plan.capabilities.find === undefined ? "" : FIND_RUNTIME))
144
146
  .replace("__LEGACY__", () => LEGACY_GLOBALS_RUNTIME)
145
147
  .replace("__RUNTIME__", () => (plan.runtime === undefined ? "" : USE_RUNTIME));
146
148
  }