@botiverse/porthole 0.14.7 → 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 +74 -0
- package/dist/bridge/bridge.d.ts +6 -0
- package/dist/bridge/bridge.js +5 -0
- package/dist/check/files.js +2 -0
- package/dist/check/lint.js +82 -3
- package/dist/check/page-source.js +1 -1
- package/dist/compile/policy.d.ts +17 -6
- package/dist/compile/policy.js +22 -7
- package/dist/config/capabilities.d.ts +16 -2
- package/dist/config/capabilities.js +8 -0
- package/dist/config/schema.d.ts +13 -0
- package/dist/config/validate.js +15 -0
- package/dist/conformance/harness-source.js +1 -1
- package/dist/conformance/kit.d.ts +5 -0
- package/dist/conformance/kit.js +21 -0
- package/dist/conformance/limits.js +10 -5
- package/dist/conformance/probes.js +139 -28
- package/dist/conformance/served.js +25 -4
- 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/guide/limits.js +8 -1
- package/dist/host/create-host.js +249 -4
- package/dist/host/pass-on.js +3 -2
- package/dist/host/types.d.ts +51 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.js +4 -1
- package/dist/presets/presets.d.ts +6 -0
- package/dist/presets/presets.js +6 -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/server/files.js +4 -3
- package/dist/server/server.js +4 -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/plan.d.ts +5 -0
- package/dist/shell/plan.js +2 -0
- package/dist/shell/runtime.d.ts +30 -1
- package/dist/shell/runtime.js +789 -105
- package/dist/shell/text.d.ts +17 -0
- package/dist/shell/text.js +140 -0
- package/docs/ADOPTING.md +103 -0
- package/docs/ARCHITECTURE.md +10 -3
- package/docs/DECISIONS.md +513 -1
- package/docs/DESIGN.md +40 -4
- package/docs/THREAT_MODEL.md +75 -6
- package/docs/insertion-entry-points.md +112 -0
- package/package.json +1 -1
- package/src/bridge/bridge.ts +7 -0
- package/src/check/files.ts +2 -0
- package/src/check/lint.ts +97 -8
- package/src/compile/policy.ts +23 -6
- package/src/config/capabilities.ts +13 -1
- package/src/config/schema.ts +13 -0
- package/src/config/validate.ts +16 -0
- package/src/conformance/kit.ts +22 -0
- package/src/conformance/limits.ts +10 -5
- package/src/conformance/probes.ts +149 -28
- package/src/conformance/served.ts +24 -4
- package/src/find/types.ts +86 -0
- package/src/find/validate.ts +101 -0
- package/src/guide/guide.ts +3 -1
- package/src/guide/limits.ts +9 -1
- package/src/host/create-host.ts +267 -4
- package/src/host/pass-on.ts +3 -2
- package/src/host/types.ts +52 -0
- package/src/index.ts +13 -0
- package/src/presets/presets.ts +15 -0
- package/src/react/frame.tsx +160 -1
- package/src/react/view.tsx +3 -0
- package/src/runtime/contract.ts +1 -1
- package/src/server/files.ts +8 -2
- package/src/server/server.ts +4 -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/plan.ts +7 -0
- package/src/shell/runtime.ts +792 -105
- package/src/shell/text.ts +140 -0
package/src/react/frame.tsx
CHANGED
|
@@ -19,6 +19,8 @@ import {
|
|
|
19
19
|
type Ref,
|
|
20
20
|
} from "react";
|
|
21
21
|
import type { AnnotateMode, AnnotationMark, AnnotationReport } from "../annotate/types.ts";
|
|
22
|
+
import type { FindOptions, FindResult } from "../find/types.ts";
|
|
23
|
+
import { blankQuery, checkFindQuery } from "../find/validate.ts";
|
|
22
24
|
import type {
|
|
23
25
|
CallContext,
|
|
24
26
|
CapabilityHandler,
|
|
@@ -495,6 +497,68 @@ export interface PortholeFrameHandle {
|
|
|
495
497
|
* `"annotations"` (the core's call throws; this never does).
|
|
496
498
|
*/
|
|
497
499
|
clearSelection(): void;
|
|
500
|
+
/**
|
|
501
|
+
* Finds `query` in the frame and highlights it (`find.query`, P131): where the matches stand,
|
|
502
|
+
* or `"unsupported"` (a config without `find`, an older shell, no answer in time, a later
|
|
503
|
+
* query superseded it, or no display running now). The query is kept for the content the frame
|
|
504
|
+
* shows now only: a display made later for the same content (the frame neared the screen again)
|
|
505
|
+
* finds it again and tells `onFindUpdated`; other content (another `content` that doesn't
|
|
506
|
+
* continue it, `document`, `instance`, `config`, `declaration` or `viewer`, a new `generation`)
|
|
507
|
+
* forgets it, and nothing is found there until `find` is called again (clear your find bar
|
|
508
|
+
* then). A query too long, or options that aren't these: `onHostError`, kind `"find"`, and
|
|
509
|
+
* `"unsupported"` (never a rejection). An empty query clears.
|
|
510
|
+
*/
|
|
511
|
+
find(query: string, options?: FindOptions): Promise<FindResult | "unsupported">;
|
|
512
|
+
/** Removes the highlights and forgets the query (`find.clear`): the host's find bar closed. */
|
|
513
|
+
clearFind(): void;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* The query a frame's handle last found (P131), and what the frame showed when it was asked: a
|
|
518
|
+
* display made after it finds it again only where the frame still shows that (`sameFindScope`).
|
|
519
|
+
*/
|
|
520
|
+
type FindShown = {
|
|
521
|
+
readonly query: string;
|
|
522
|
+
readonly caseSensitive: boolean;
|
|
523
|
+
readonly scope: FindScope;
|
|
524
|
+
} | null;
|
|
525
|
+
|
|
526
|
+
/** What a frame shows, as its handle's query holds it (P131). */
|
|
527
|
+
export interface FindScope {
|
|
528
|
+
/** The config, instance (its version too), declaration, viewer and generation, by value. */
|
|
529
|
+
readonly shows: string;
|
|
530
|
+
readonly document: string | undefined;
|
|
531
|
+
readonly content: string | undefined;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/** What the frame shows with these props, for its handle's query (P131). */
|
|
535
|
+
export function findScopeOf(props: PortholeFrameProps): FindScope {
|
|
536
|
+
return {
|
|
537
|
+
shows: JSON.stringify([
|
|
538
|
+
props.config,
|
|
539
|
+
props.instance ?? null,
|
|
540
|
+
props.declaration ?? null,
|
|
541
|
+
props.viewer ?? null,
|
|
542
|
+
props.generation ?? 0,
|
|
543
|
+
]),
|
|
544
|
+
document: props.document,
|
|
545
|
+
content: props.content,
|
|
546
|
+
};
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* Whether a frame showing `now` still shows what a query was asked of (`asked`, P131): the same
|
|
551
|
+
* config, instance, declaration, viewer, generation and document, and the content as it was or
|
|
552
|
+
* continued (more of a streamed page; asked before any content: whatever came). Otherwise the
|
|
553
|
+
* query was the last page's, and a new one is never searched with it.
|
|
554
|
+
*/
|
|
555
|
+
export function sameFindScope(asked: FindScope, now: FindScope): boolean {
|
|
556
|
+
return (
|
|
557
|
+
asked.shows === now.shows &&
|
|
558
|
+
asked.document === now.document &&
|
|
559
|
+
(asked.content === undefined ||
|
|
560
|
+
(now.content !== undefined && now.content.startsWith(asked.content)))
|
|
561
|
+
);
|
|
498
562
|
}
|
|
499
563
|
|
|
500
564
|
export interface PortholeFrameProps {
|
|
@@ -641,6 +705,18 @@ export interface PortholeFrameProps {
|
|
|
641
705
|
* (`activated`). The content's word: UI hints, never permissions.
|
|
642
706
|
*/
|
|
643
707
|
readonly onAnnotation?: (report: AnnotationReport) => void;
|
|
708
|
+
/**
|
|
709
|
+
* The person pressed Cmd+F or Ctrl+F in the frame (`findRequested`, P131): open the host's find
|
|
710
|
+
* bar. Given, it makes the frame's shortcut the host's (`find.takeShortcut(true)`): the
|
|
711
|
+
* browser's own find doesn't open there. The content's word (it can say it, or keep the key):
|
|
712
|
+
* open the bar, nothing more. Where the config declares `find`.
|
|
713
|
+
*/
|
|
714
|
+
readonly onFindRequested?: () => void;
|
|
715
|
+
/**
|
|
716
|
+
* The matches of the query shown changed without the host asking (`findUpdated`, P131): the
|
|
717
|
+
* content changed, or a display made later found the handle's query again.
|
|
718
|
+
*/
|
|
719
|
+
readonly onFindUpdated?: (result: FindResult) => void;
|
|
644
720
|
/** Draws the placeholder instead of the default one. */
|
|
645
721
|
readonly renderPlaceholder?: (props: PlaceholderProps) => ReactNode;
|
|
646
722
|
readonly className?: string;
|
|
@@ -714,6 +790,18 @@ export function PortholeFrame(props: PortholeFrameProps): ReactElement {
|
|
|
714
790
|
useLayoutEffect(() => {
|
|
715
791
|
own.current = props;
|
|
716
792
|
});
|
|
793
|
+
// The query the handle last found (P131): a display made after it, for the same content, finds
|
|
794
|
+
// it again.
|
|
795
|
+
const findShown = useRef<FindShown>(null);
|
|
796
|
+
// The query kept for what a display shows (`scope`), else forgotten: null.
|
|
797
|
+
const findFor = useCallback((scope: FindScope): FindShown => {
|
|
798
|
+
const kept = findShown.current;
|
|
799
|
+
if (kept !== null && !sameFindScope(kept.scope, scope)) {
|
|
800
|
+
findShown.current = null;
|
|
801
|
+
return null;
|
|
802
|
+
}
|
|
803
|
+
return kept;
|
|
804
|
+
}, []);
|
|
717
805
|
useImperativeHandle(
|
|
718
806
|
handleRef,
|
|
719
807
|
(): PortholeFrameHandle => ({
|
|
@@ -723,6 +811,32 @@ export function PortholeFrame(props: PortholeFrameProps): ReactElement {
|
|
|
723
811
|
attempt(() => now.annotations.clearSelection(), own.current);
|
|
724
812
|
}
|
|
725
813
|
},
|
|
814
|
+
find(query, options) {
|
|
815
|
+
try {
|
|
816
|
+
checkFindQuery(query, options);
|
|
817
|
+
} catch (error) {
|
|
818
|
+
hostFailed(own.current, error, "find");
|
|
819
|
+
return Promise.resolve("unsupported");
|
|
820
|
+
}
|
|
821
|
+
findShown.current = blankQuery(query)
|
|
822
|
+
? null
|
|
823
|
+
: {
|
|
824
|
+
query,
|
|
825
|
+
caseSensitive: options?.caseSensitive === true,
|
|
826
|
+
scope: findScopeOf(own.current),
|
|
827
|
+
};
|
|
828
|
+
const now = display.current;
|
|
829
|
+
return now === null
|
|
830
|
+
? Promise.resolve("unsupported")
|
|
831
|
+
: now.find.query(query, options).catch((error: unknown) => {
|
|
832
|
+
hostFailed(own.current, error, "find");
|
|
833
|
+
return "unsupported" as const;
|
|
834
|
+
});
|
|
835
|
+
},
|
|
836
|
+
clearFind() {
|
|
837
|
+
findShown.current = null;
|
|
838
|
+
display.current?.find.clear();
|
|
839
|
+
},
|
|
726
840
|
}),
|
|
727
841
|
[],
|
|
728
842
|
);
|
|
@@ -795,6 +909,7 @@ export function PortholeFrame(props: PortholeFrameProps): ReactElement {
|
|
|
795
909
|
frame={props}
|
|
796
910
|
page={page}
|
|
797
911
|
running={running}
|
|
912
|
+
findFor={findFor}
|
|
798
913
|
initialHeightPx={height}
|
|
799
914
|
placeholder={placeholder}
|
|
800
915
|
run={run}
|
|
@@ -811,6 +926,11 @@ interface MountProps {
|
|
|
811
926
|
readonly page: PageState;
|
|
812
927
|
/** Tells the frame which display runs now (null: none), for its handle. */
|
|
813
928
|
readonly running: (display: DisplayHandle | null) => void;
|
|
929
|
+
/**
|
|
930
|
+
* The query the frame's handle last found, where it was asked of what `scope` shows (P131): a
|
|
931
|
+
* new display for its content finds it again. Otherwise it is forgotten, and null.
|
|
932
|
+
*/
|
|
933
|
+
readonly findFor: (scope: FindScope) => FindShown;
|
|
814
934
|
readonly initialHeightPx: number;
|
|
815
935
|
readonly placeholder: (props: PlaceholderProps) => ReactNode;
|
|
816
936
|
readonly run: () => void;
|
|
@@ -823,6 +943,7 @@ function Mount({
|
|
|
823
943
|
frame,
|
|
824
944
|
page,
|
|
825
945
|
running,
|
|
946
|
+
findFor,
|
|
826
947
|
initialHeightPx,
|
|
827
948
|
placeholder,
|
|
828
949
|
run,
|
|
@@ -919,6 +1040,12 @@ function Mount({
|
|
|
919
1040
|
case "annotation":
|
|
920
1041
|
frame.onAnnotation?.(detail as DisplayEvents["annotation"]);
|
|
921
1042
|
return;
|
|
1043
|
+
case "findRequested":
|
|
1044
|
+
frame.onFindRequested?.();
|
|
1045
|
+
return;
|
|
1046
|
+
case "findUpdated":
|
|
1047
|
+
frame.onFindUpdated?.(detail as DisplayEvents["findUpdated"]);
|
|
1048
|
+
return;
|
|
922
1049
|
}
|
|
923
1050
|
},
|
|
924
1051
|
);
|
|
@@ -1045,6 +1172,8 @@ function Mount({
|
|
|
1045
1172
|
"focusTaken",
|
|
1046
1173
|
"outcome",
|
|
1047
1174
|
"annotation",
|
|
1175
|
+
"findRequested",
|
|
1176
|
+
"findUpdated",
|
|
1048
1177
|
"contentError",
|
|
1049
1178
|
"hostError",
|
|
1050
1179
|
];
|
|
@@ -1064,6 +1193,22 @@ function Mount({
|
|
|
1064
1193
|
following.current = wanted;
|
|
1065
1194
|
}, first);
|
|
1066
1195
|
}
|
|
1196
|
+
// Find (P131): the shortcut is the host's where it listens for it; the handle's query, found
|
|
1197
|
+
// again in this display where it shows what the query was asked of (its numbers told as an
|
|
1198
|
+
// update: nobody awaits them), else forgotten: another page is never searched with it.
|
|
1199
|
+
if (first.onFindRequested !== undefined) {
|
|
1200
|
+
display.find.takeShortcut(true);
|
|
1201
|
+
}
|
|
1202
|
+
const shownQuery = findFor(findScopeOf(first));
|
|
1203
|
+
if (shownQuery !== null) {
|
|
1204
|
+
void display.find
|
|
1205
|
+
.query(shownQuery.query, { caseSensitive: shownQuery.caseSensitive })
|
|
1206
|
+
.then((result) => {
|
|
1207
|
+
if (result !== "unsupported" && findFor(findScopeOf(latest())) === shownQuery) {
|
|
1208
|
+
latest().onFindUpdated?.(result);
|
|
1209
|
+
}
|
|
1210
|
+
});
|
|
1211
|
+
}
|
|
1067
1212
|
if (first.content !== undefined) {
|
|
1068
1213
|
display.setContent(first.content, { partial: first.partial === true });
|
|
1069
1214
|
}
|
|
@@ -1095,13 +1240,17 @@ function Mount({
|
|
|
1095
1240
|
locate,
|
|
1096
1241
|
nonce,
|
|
1097
1242
|
running,
|
|
1243
|
+
findFor,
|
|
1098
1244
|
]);
|
|
1099
1245
|
|
|
1100
1246
|
useEffect(() => {
|
|
1101
1247
|
if (content !== undefined) {
|
|
1102
1248
|
handle.current?.setContent(content, { partial: partial === true });
|
|
1103
1249
|
}
|
|
1104
|
-
|
|
1250
|
+
// Other content: the handle's query was the last one's, forgotten (the display forgot it too,
|
|
1251
|
+
// P131).
|
|
1252
|
+
findFor(findScopeOf(latest()));
|
|
1253
|
+
}, [content, partial, findFor]);
|
|
1105
1254
|
// Annotations follow their props (a new display gets them all when it is made, above).
|
|
1106
1255
|
const annotates = config.capabilities.annotate !== undefined;
|
|
1107
1256
|
const { annotating } = frame;
|
|
@@ -1170,6 +1319,16 @@ function Mount({
|
|
|
1170
1319
|
attempt(() => display.annotations.focus(id), latest());
|
|
1171
1320
|
}
|
|
1172
1321
|
}, [annotates, focusToken]);
|
|
1322
|
+
// The frame's shortcut is the host's while it listens for it (P131).
|
|
1323
|
+
const takesFind = frame.onFindRequested !== undefined;
|
|
1324
|
+
const [mountTakesFind] = useState(takesFind);
|
|
1325
|
+
const findFlipped = useRef(false);
|
|
1326
|
+
useEffect(() => {
|
|
1327
|
+
if (findFlipped.current || takesFind !== mountTakesFind) {
|
|
1328
|
+
findFlipped.current = true;
|
|
1329
|
+
handle.current?.find.takeShortcut(takesFind);
|
|
1330
|
+
}
|
|
1331
|
+
}, [takesFind, mountTakesFind]);
|
|
1173
1332
|
// A flip or a restyle after the display was made: its first scheme and values went with it.
|
|
1174
1333
|
const [mountScheme] = useState(scheme);
|
|
1175
1334
|
const flipped = useRef(false);
|
package/src/react/view.tsx
CHANGED
|
@@ -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
|
);
|
package/src/runtime/contract.ts
CHANGED
|
@@ -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,
|
package/src/server/files.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { CompiledConfig } from "../compile/compile.ts";
|
|
|
2
2
|
import {
|
|
3
3
|
assetCspDirectives,
|
|
4
4
|
fileSetCspDirectives,
|
|
5
|
+
fileSetDirectives,
|
|
5
6
|
INERT_FILE_CSP,
|
|
6
7
|
servedDocumentCsp,
|
|
7
8
|
servedPermissionsPolicy,
|
|
@@ -268,10 +269,15 @@ export async function serveFile(serving: FileServing): Promise<Response> {
|
|
|
268
269
|
serving.compiled.config.capabilities.assets !== undefined
|
|
269
270
|
? "same-origin"
|
|
270
271
|
: "no-referrer";
|
|
271
|
-
// Its version's files (P082),
|
|
272
|
+
// Its version's files (P082), frames of them where the config opts in (P128), and its
|
|
273
|
+
// artifact's assets where the config serves them (P089).
|
|
272
274
|
const directives = assetCspDirectives(
|
|
273
275
|
serving.compiled.config,
|
|
274
|
-
fileSetCspDirectives(
|
|
276
|
+
fileSetCspDirectives(
|
|
277
|
+
serving.compiled.cspDirectives,
|
|
278
|
+
serving.source,
|
|
279
|
+
fileSetDirectives(serving.compiled.config),
|
|
280
|
+
),
|
|
275
281
|
serving.origin,
|
|
276
282
|
);
|
|
277
283
|
// A page and a script (a worker's policy is its script's) get the page's CSP, a page with its
|
package/src/server/server.ts
CHANGED
|
@@ -1303,6 +1303,9 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
1303
1303
|
const ancestors = nested ? nestedAncestors(access.origin, frameAncestors) : frameAncestors;
|
|
1304
1304
|
if ("files" in served) {
|
|
1305
1305
|
if (at !== undefined) {
|
|
1306
|
+
// A page's own frames (P128): its files below the base are framed by its own pages too,
|
|
1307
|
+
// on this origin (`frame-ancestors` names every ancestor). Never another origin's.
|
|
1308
|
+
const own = served.compiled.config.nestedFrames === "own";
|
|
1306
1309
|
return serveFile({
|
|
1307
1310
|
request,
|
|
1308
1311
|
set: served.files,
|
|
@@ -1314,7 +1317,7 @@ export function createServer(options: ServerOptions): PortholeServer {
|
|
|
1314
1317
|
source: at.source,
|
|
1315
1318
|
compiled: served.compiled,
|
|
1316
1319
|
hostOrigins: options.hostOrigins,
|
|
1317
|
-
frameAncestors: ancestors,
|
|
1320
|
+
frameAncestors: own ? `${access.origin} ${ancestors}` : ancestors,
|
|
1318
1321
|
...versionOf(access.instance),
|
|
1319
1322
|
});
|
|
1320
1323
|
}
|
package/src/shell/annotate.ts
CHANGED
|
@@ -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
|
-
|
|
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);
|
package/src/shell/document.ts
CHANGED
|
@@ -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
|
|
137
|
-
* globals (the legacy path, P085) for what the config declares, and the
|
|
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
|
}
|