@botiverse/porthole 0.8.0 → 0.10.0
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/dist/annotate/types.d.ts +42 -4
- package/dist/annotate/types.js +6 -1
- package/dist/annotate/validate.d.ts +16 -1
- package/dist/annotate/validate.js +33 -3
- package/dist/bridge/bridge.js +1 -51
- package/dist/bridge/unseen.d.ts +18 -0
- package/dist/bridge/unseen.js +71 -0
- package/dist/check/page-source.js +1 -1
- package/dist/conformance/behavior.js +107 -68
- package/dist/conformance/clipboard.js +16 -14
- package/dist/conformance/harness-source.js +1 -1
- package/dist/conformance/limits.js +117 -64
- package/dist/conformance/probes.js +66 -49
- package/dist/conformance/served.js +66 -63
- package/dist/host/create-host.js +75 -3
- package/dist/platform/types.d.ts +12 -1
- package/dist/platform/web.js +8 -0
- package/dist/react/frame.d.ts +6 -2
- package/dist/shell/annotate.d.ts +10 -3
- package/dist/shell/annotate.js +610 -39
- package/docs/ADOPTING.md +76 -0
- package/docs/ARCHITECTURE.md +2 -1
- package/docs/DECISIONS.md +516 -1
- package/docs/DESIGN.md +178 -20
- package/docs/THREAT_MODEL.md +92 -9
- package/package.json +1 -1
- package/src/annotate/types.ts +39 -5
- package/src/annotate/validate.ts +39 -3
- package/src/bridge/bridge.ts +1 -60
- package/src/bridge/unseen.ts +82 -0
- package/src/conformance/behavior.ts +221 -69
- package/src/conformance/clipboard.ts +44 -14
- package/src/conformance/limits.ts +169 -76
- package/src/conformance/probes.ts +97 -49
- package/src/conformance/served.ts +112 -63
- package/src/host/create-host.ts +75 -3
- package/src/platform/types.ts +9 -1
- package/src/platform/web.ts +10 -0
- package/src/react/frame.tsx +6 -2
- package/src/shell/annotate.ts +610 -39
package/dist/annotate/types.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Annotations (P053): a person points at part of a display (an element, or text they select) to
|
|
3
|
-
* comment on it, and the host marks the anchors already made
|
|
3
|
+
* comment on it, and the host marks the anchors already made, each with a clickable marker and a
|
|
4
|
+
* number where it gives a count (P104). The host drives the shell with
|
|
4
5
|
* Porthole's own messages (`porthole/annotate.*`); the shell answers with anchors and where they
|
|
5
6
|
* are. An anchor is a set of W3C Web Annotation selectors, so it can be found again after the
|
|
6
7
|
* content changes; finding it is best-effort, and says so (`orphaned`).
|
|
@@ -40,7 +41,13 @@ export interface AnnotationAnchor {
|
|
|
40
41
|
/** A short description for the host to show ("h2#revenue Revenue grew 12%"); the content's. */
|
|
41
42
|
readonly label: string;
|
|
42
43
|
}
|
|
43
|
-
/**
|
|
44
|
+
/**
|
|
45
|
+
* A box in the frame's viewport, CSS px: add the frame element's position to place it on the page.
|
|
46
|
+
* The page's word: the host clamps each it reports to the frame's viewport (P104), so one wholly
|
|
47
|
+
* outside is a box of no size on the nearest edge. A positioning hint only. It is in the frame's
|
|
48
|
+
* own layout px: a host that scales or transforms the frame maps it through that transform too
|
|
49
|
+
* (P105).
|
|
50
|
+
*/
|
|
44
51
|
export interface AnnotationRect {
|
|
45
52
|
readonly x: number;
|
|
46
53
|
readonly y: number;
|
|
@@ -54,10 +61,21 @@ export interface AnnotateMode {
|
|
|
54
61
|
/** Text, by selecting it: each settled selection is reported, and its clearing. */
|
|
55
62
|
readonly text?: boolean;
|
|
56
63
|
}
|
|
57
|
-
/**
|
|
64
|
+
/**
|
|
65
|
+
* An anchor the host marks in the frame, under its own id. Everything here is sent into the
|
|
66
|
+
* frame, so the content can read it: give ids that say nothing (never a person's name or the
|
|
67
|
+
* comment's words).
|
|
68
|
+
*/
|
|
58
69
|
export interface AnnotationMark {
|
|
59
70
|
readonly id: string;
|
|
60
71
|
readonly anchor: AnnotationAnchor;
|
|
72
|
+
/**
|
|
73
|
+
* A marker (P104): with a count, the shell also draws a small bubble with this number (0 to
|
|
74
|
+
* `ANNOTATION_LIMITS.count`; above 99 it reads "99+", 0 draws the bubble alone) at the end of a
|
|
75
|
+
* text anchor and the top right of an element. The person can click it: the frame reports
|
|
76
|
+
* `activated`. The content can read the number; give one only where the page may know it.
|
|
77
|
+
*/
|
|
78
|
+
readonly count?: number;
|
|
61
79
|
}
|
|
62
80
|
/** Whether a mark's anchor was found in the content as it is now. */
|
|
63
81
|
export type AnchorState = "attached" | "orphaned";
|
|
@@ -83,10 +101,26 @@ export type AnnotationReport =
|
|
|
83
101
|
| {
|
|
84
102
|
readonly type: "cancelled";
|
|
85
103
|
}
|
|
86
|
-
/**
|
|
104
|
+
/**
|
|
105
|
+
* Where the marked anchors are now: every mark after `show`, the focused one after `focus`,
|
|
106
|
+
* every mark when the content changed one's state, and, while any mark has a `count`, once a
|
|
107
|
+
* scroll, a resize or a change of the content has settled, only the marks that moved since the
|
|
108
|
+
* host was last told, at most once a second (P104).
|
|
109
|
+
*/
|
|
87
110
|
| {
|
|
88
111
|
readonly type: "anchored";
|
|
89
112
|
readonly results: readonly AnchorResult[];
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* The person clicked the marker of a mark the host shows with a `count` (P104); `rect` is the
|
|
116
|
+
* marker's box, to open the host's own UI beside it. The content can fake it for any mark the
|
|
117
|
+
* host shows with a count, and can tell a real one happened: open the host's own view of that
|
|
118
|
+
* mark, never act on it.
|
|
119
|
+
*/
|
|
120
|
+
| {
|
|
121
|
+
readonly type: "activated";
|
|
122
|
+
readonly id: string;
|
|
123
|
+
readonly rect: AnnotationRect;
|
|
90
124
|
};
|
|
91
125
|
/** Porthole's own messages for annotations (own-prefixed, P002). */
|
|
92
126
|
export declare const ANNOTATE_METHODS: {
|
|
@@ -105,6 +139,8 @@ export declare const ANNOTATE_METHODS: {
|
|
|
105
139
|
readonly unselected: "porthole/annotate.unselected";
|
|
106
140
|
readonly cancelled: "porthole/annotate.cancelled";
|
|
107
141
|
readonly anchored: "porthole/annotate.anchored";
|
|
142
|
+
/** Frame → host: a mark's marker was clicked (P104). */
|
|
143
|
+
readonly activated: "porthole/annotate.activated";
|
|
108
144
|
};
|
|
109
145
|
/** The most of each part of an anchor or report the host takes. */
|
|
110
146
|
export declare const ANNOTATION_LIMITS: {
|
|
@@ -117,4 +153,6 @@ export declare const ANNOTATION_LIMITS: {
|
|
|
117
153
|
readonly id: 128;
|
|
118
154
|
readonly position: 50000000;
|
|
119
155
|
readonly coordinate: 10000000;
|
|
156
|
+
/** The largest `count` a mark's marker takes (P104). */
|
|
157
|
+
readonly count: 999;
|
|
120
158
|
};
|
package/dist/annotate/types.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Annotations (P053): a person points at part of a display (an element, or text they select) to
|
|
3
|
-
* comment on it, and the host marks the anchors already made
|
|
3
|
+
* comment on it, and the host marks the anchors already made, each with a clickable marker and a
|
|
4
|
+
* number where it gives a count (P104). The host drives the shell with
|
|
4
5
|
* Porthole's own messages (`porthole/annotate.*`); the shell answers with anchors and where they
|
|
5
6
|
* are. An anchor is a set of W3C Web Annotation selectors, so it can be found again after the
|
|
6
7
|
* content changes; finding it is best-effort, and says so (`orphaned`).
|
|
@@ -27,6 +28,8 @@ export const ANNOTATE_METHODS = {
|
|
|
27
28
|
unselected: "porthole/annotate.unselected",
|
|
28
29
|
cancelled: "porthole/annotate.cancelled",
|
|
29
30
|
anchored: "porthole/annotate.anchored",
|
|
31
|
+
/** Frame → host: a mark's marker was clicked (P104). */
|
|
32
|
+
activated: "porthole/annotate.activated",
|
|
30
33
|
};
|
|
31
34
|
/** The most of each part of an anchor or report the host takes. */
|
|
32
35
|
export const ANNOTATION_LIMITS = {
|
|
@@ -39,4 +42,6 @@ export const ANNOTATION_LIMITS = {
|
|
|
39
42
|
id: 128,
|
|
40
43
|
position: 50_000_000,
|
|
41
44
|
coordinate: 10_000_000,
|
|
45
|
+
/** The largest `count` a mark's marker takes (P104). */
|
|
46
|
+
count: 999,
|
|
42
47
|
};
|
|
@@ -1,8 +1,23 @@
|
|
|
1
1
|
import { type AnnotateMode, type AnnotationAnchor, type AnnotationMark, type AnnotationRect, type AnnotationReport } from "./types.ts";
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* An anchor, rebuilt from its known fields; null if any part is malformed or too large. Its label
|
|
4
|
+
* is text for the person, so what they couldn't see is taken out of it (P105: a wordless element's
|
|
5
|
+
* `alt`, `aria-label` or `title`, which the page never shows, can carry bidi controls or zero-width
|
|
6
|
+
* characters); its selectors' words are kept as they are, to be found again.
|
|
7
|
+
*/
|
|
3
8
|
export declare function parseAnchor(value: unknown): AnnotationAnchor | null;
|
|
4
9
|
/** A box in the frame's viewport; null unless every number is finite and sizes aren't negative. */
|
|
5
10
|
export declare function parseRect(value: unknown): AnnotationRect | null;
|
|
11
|
+
/**
|
|
12
|
+
* A box the frame reported, clamped to the frame's viewport (`width` by `height`, CSS px, P104):
|
|
13
|
+
* each side is held within it, so what overlaps the viewport keeps the part inside, and what lies
|
|
14
|
+
* wholly outside becomes a box of no size on the nearest edge. Where the viewport isn't known, a
|
|
15
|
+
* box of no size at the frame's corner. The page still chooses any point inside its own frame.
|
|
16
|
+
*/
|
|
17
|
+
export declare function clampRect(rect: AnnotationRect, viewport: {
|
|
18
|
+
readonly width: number;
|
|
19
|
+
readonly height: number;
|
|
20
|
+
} | null | undefined): AnnotationRect;
|
|
6
21
|
/** A report from the frame, checked; null if malformed (it is then dropped). */
|
|
7
22
|
export declare function parseReport(value: unknown): AnnotationReport | null;
|
|
8
23
|
/** What the host may start with: elements, text, or both (default both). */
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ANNOTATION_LIMITS as LIMITS, } from "./types.js";
|
|
2
|
+
import { withoutUnseen } from "../bridge/unseen.js";
|
|
2
3
|
/**
|
|
3
4
|
* The host's checks on what a frame says about annotations (P053), and on anchors a host stores
|
|
4
5
|
* and sends back. Each rebuilds what it accepts from known fields only, within `ANNOTATION_LIMITS`,
|
|
@@ -40,7 +41,12 @@ function selector(value) {
|
|
|
40
41
|
return null;
|
|
41
42
|
}
|
|
42
43
|
}
|
|
43
|
-
/**
|
|
44
|
+
/**
|
|
45
|
+
* An anchor, rebuilt from its known fields; null if any part is malformed or too large. Its label
|
|
46
|
+
* is text for the person, so what they couldn't see is taken out of it (P105: a wordless element's
|
|
47
|
+
* `alt`, `aria-label` or `title`, which the page never shows, can carry bidi controls or zero-width
|
|
48
|
+
* characters); its selectors' words are kept as they are, to be found again.
|
|
49
|
+
*/
|
|
44
50
|
export function parseAnchor(value) {
|
|
45
51
|
if (!isRecord(value) || (value.kind !== "element" && value.kind !== "text")) {
|
|
46
52
|
return null;
|
|
@@ -65,7 +71,7 @@ export function parseAnchor(value) {
|
|
|
65
71
|
if (value.kind === "text" ? !quote : !css && !quote) {
|
|
66
72
|
return null;
|
|
67
73
|
}
|
|
68
|
-
return { kind: value.kind, selectors, label: value.label };
|
|
74
|
+
return { kind: value.kind, selectors, label: withoutUnseen(value.label) };
|
|
69
75
|
}
|
|
70
76
|
const coordinate = (value) => typeof value === "number" && Number.isFinite(value) && Math.abs(value) <= LIMITS.coordinate;
|
|
71
77
|
/** A box in the frame's viewport; null unless every number is finite and sizes aren't negative. */
|
|
@@ -82,6 +88,23 @@ export function parseRect(value) {
|
|
|
82
88
|
}
|
|
83
89
|
return { x, y, width, height };
|
|
84
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* A box the frame reported, clamped to the frame's viewport (`width` by `height`, CSS px, P104):
|
|
93
|
+
* each side is held within it, so what overlaps the viewport keeps the part inside, and what lies
|
|
94
|
+
* wholly outside becomes a box of no size on the nearest edge. Where the viewport isn't known, a
|
|
95
|
+
* box of no size at the frame's corner. The page still chooses any point inside its own frame.
|
|
96
|
+
*/
|
|
97
|
+
export function clampRect(rect, viewport) {
|
|
98
|
+
const known = viewport !== null && typeof viewport === "object";
|
|
99
|
+
const width = known && Number.isFinite(viewport.width) ? viewport.width : 0;
|
|
100
|
+
const height = known && Number.isFinite(viewport.height) ? viewport.height : 0;
|
|
101
|
+
const within = (value, most) => Math.min(Math.max(value, 0), Math.max(0, most));
|
|
102
|
+
const left = within(rect.x, width);
|
|
103
|
+
const top = within(rect.y, height);
|
|
104
|
+
const right = within(rect.x + rect.width, width);
|
|
105
|
+
const bottom = within(rect.y + rect.height, height);
|
|
106
|
+
return { x: left, y: top, width: right - left, height: bottom - top };
|
|
107
|
+
}
|
|
85
108
|
const markId = (value) => text(value, LIMITS.id, 1);
|
|
86
109
|
function results(value) {
|
|
87
110
|
if (!Array.isArray(value) || value.length > LIMITS.marks) {
|
|
@@ -126,6 +149,10 @@ export function parseReport(value) {
|
|
|
126
149
|
const checked = results(value.results);
|
|
127
150
|
return checked === null ? null : { type: "anchored", results: checked };
|
|
128
151
|
}
|
|
152
|
+
case "activated": {
|
|
153
|
+
const rect = parseRect(value.rect);
|
|
154
|
+
return !markId(value.id) || rect === null ? null : { type: "activated", id: value.id, rect };
|
|
155
|
+
}
|
|
129
156
|
default:
|
|
130
157
|
return null;
|
|
131
158
|
}
|
|
@@ -150,7 +177,10 @@ export function checkMarks(marks) {
|
|
|
150
177
|
if (!markId(mark?.id) || seen.has(mark.id) || anchor === null) {
|
|
151
178
|
throw new TypeError(`annotations.show: mark ${index} needs a unique id (1 to ${LIMITS.id} characters) and a valid anchor`);
|
|
152
179
|
}
|
|
180
|
+
if (mark.count !== undefined && !count(mark.count, LIMITS.count)) {
|
|
181
|
+
throw new TypeError(`annotations.show: mark ${index}'s count is a whole number from 0 to ${LIMITS.count}`);
|
|
182
|
+
}
|
|
153
183
|
seen.add(mark.id);
|
|
154
|
-
return { id: mark.id, anchor };
|
|
184
|
+
return { id: mark.id, anchor, ...(mark.count === undefined ? {} : { count: mark.count }) };
|
|
155
185
|
});
|
|
156
186
|
}
|
package/dist/bridge/bridge.js
CHANGED
|
@@ -3,6 +3,7 @@ import { parseReport } from "../annotate/validate.js";
|
|
|
3
3
|
import { callGates, effectiveGates } from "../compile/policy.js";
|
|
4
4
|
import { boundedText } from "./bounded-text.js";
|
|
5
5
|
import { isJsonData } from "./json-data.js";
|
|
6
|
+
import { unseen } from "./unseen.js";
|
|
6
7
|
import { blockedNavigationMessage, cutText, withoutSealedAddresses } from "./navigation.js";
|
|
7
8
|
import { RUNTIME_HOST_FUNCTIONS, familyOf, methodGates, methodLevel, methodOf, methodsOfCall, assetCodeRuns, rosterOf, runtimeCapability, } from "../runtime/contract.js";
|
|
8
9
|
import { declaredMatch, } from "../runtime/declaration.js";
|
|
@@ -45,57 +46,6 @@ function resourceUri(args) {
|
|
|
45
46
|
}
|
|
46
47
|
/** The most text blocks one model context update holds. */
|
|
47
48
|
const MAX_CONTEXT_BLOCKS = 100;
|
|
48
|
-
/**
|
|
49
|
-
* What a person can't see but a model reads (the review of P100): format characters (zero-width
|
|
50
|
-
* ones, bidi controls, the tags U+E0000–E007F, a soft hyphen, a byte-order mark), variation
|
|
51
|
-
* selectors (U+FE00–FE0F, U+E0100–E01EF), private-use characters, lone surrogates, every
|
|
52
|
-
* default-ignorable code point (the re-check: U+034F, Mongolian's free variation selectors, the
|
|
53
|
-
* Hangul fillers, Khmer's inherent vowels), and controls other than a new line and a tab. An update with any is refused, never escaped: an escaped one
|
|
54
|
-
* reads to the model differently from what the person was shown.
|
|
55
|
-
*/
|
|
56
|
-
const UNSEEN = /[\p{Cf}\p{Co}\p{Cs}\p{Default_Ignorable_Code_Point}\u{FE00}-\u{FE0F}\u{E0100}-\u{E01EF}]|[^\P{Cc}\n\t]/u;
|
|
57
|
-
/**
|
|
58
|
-
* The parts of real emoji that draw as nothing alone, allowed only where an emoji has them (the
|
|
59
|
-
* re-review of P100): U+FE0E or U+FE0F directly after a pictograph, or after a keycap base
|
|
60
|
-
* (`[0-9#*]`) that U+20E3 follows; U+200D only between two pictographs, a selector or a skin tone
|
|
61
|
-
* after the first. Each carries at most one choice beside a visible emoji (a selector there or
|
|
62
|
-
* not, a joiner there or not), never letters a model would read. Tag characters stay refused:
|
|
63
|
-
* subdivision flags too.
|
|
64
|
-
*/
|
|
65
|
-
const EMOJI_PARTS = "(?<=\\p{Extended_Pictographic})[\\uFE0E\\uFE0F]|(?<=[0-9#*])[\\uFE0E\\uFE0F](?=\\u20E3)|(?<=\\p{Extended_Pictographic}[\\uFE0F\\p{Emoji_Modifier}]?)\\u200D(?=\\p{Extended_Pictographic})";
|
|
66
|
-
/**
|
|
67
|
-
* The scripts whose letters U+200C and U+200D shape (Unicode's notes on the joiners: the cursive
|
|
68
|
-
* scripts, where they break or make a join, and the Indic ones and Myanmar, where they choose a
|
|
69
|
-
* conjunct, a half form or a ligature), by Script_Extensions.
|
|
70
|
-
*/
|
|
71
|
-
const SHAPING_SCRIPTS = [
|
|
72
|
-
"Arabic",
|
|
73
|
-
"Syriac",
|
|
74
|
-
"Nko",
|
|
75
|
-
"Mongolian",
|
|
76
|
-
"Devanagari",
|
|
77
|
-
"Bengali",
|
|
78
|
-
"Gurmukhi",
|
|
79
|
-
"Gujarati",
|
|
80
|
-
"Oriya",
|
|
81
|
-
"Tamil",
|
|
82
|
-
"Telugu",
|
|
83
|
-
"Kannada",
|
|
84
|
-
"Malayalam",
|
|
85
|
-
"Sinhala",
|
|
86
|
-
"Myanmar",
|
|
87
|
-
];
|
|
88
|
-
const SHAPED = `[${SHAPING_SCRIPTS.map((name) => `\\p{scx=${name}}`).join("")}]`;
|
|
89
|
-
/**
|
|
90
|
-
* A joiner or non-joiner between two letters or marks of those scripts (Persian's میخواهم, a
|
|
91
|
-
* Hindi half form): the shape it chooses is what the person sees. One at a letter boundary, never
|
|
92
|
-
* doubled, first or last, or beside a letter of another script.
|
|
93
|
-
*/
|
|
94
|
-
const SHAPING_JOINERS = `(?<=[\\p{L}\\p{M}])(?<=${SHAPED})[\\u200C\\u200D](?=[\\p{L}\\p{M}])(?=${SHAPED})`;
|
|
95
|
-
/** What may be invisible alone and is allowed where it is seen: one pass, on the text as sent. */
|
|
96
|
-
const SEEN_IN_PLACE = new RegExp(`${EMOJI_PARTS}|${SHAPING_JOINERS}`, "gu");
|
|
97
|
-
/** Whether `text` holds what a person can't see, the parts of emoji and shaped letters aside. */
|
|
98
|
-
const unseen = (text) => UNSEEN.test(text.replace(SEEN_IN_PLACE, ""));
|
|
99
49
|
/** Whether any string in `value`, a key or a value, holds what can't be seen (bounded JSON data). */
|
|
100
50
|
function holdsUnseen(value) {
|
|
101
51
|
if (typeof value === "string") {
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a person can't see in text that a model or a host's UI reads (P100; P105 for labels): one
|
|
3
|
+
* definition, for the bridge's check of a model context update and the annotation labels' strip.
|
|
4
|
+
*/
|
|
5
|
+
/** Whether `text` holds what a person can't see, the parts of emoji and shaped letters aside. */
|
|
6
|
+
export declare const unseen: (text: string) => boolean;
|
|
7
|
+
/**
|
|
8
|
+
* `text` without anything a person can't see (P105), for text Porthole only shows, never matches,
|
|
9
|
+
* as a wordless element's label is (an `alt`, `aria-label` or `title` the person never sees on the
|
|
10
|
+
* page): every format character (bidi controls and the zero-width ones among them), every
|
|
11
|
+
* default-ignorable code point, variation selector, private-use character and lone surrogate, and
|
|
12
|
+
* every control but a new line and a tab. No exceptions: not the parts of emoji, nor the joiners
|
|
13
|
+
* between shaped letters that `unseen` lets by, whose surroundings a page can fake (the review of
|
|
14
|
+
* P105: a Mongolian variation selector beside each joiner carried 48 of them through, a payload
|
|
15
|
+
* of six bytes). An emoji may lose its joiners (a family shows as its faces). Nothing `unseen`
|
|
16
|
+
* refuses is left.
|
|
17
|
+
*/
|
|
18
|
+
export declare function withoutUnseen(text: string): string;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a person can't see in text that a model or a host's UI reads (P100; P105 for labels): one
|
|
3
|
+
* definition, for the bridge's check of a model context update and the annotation labels' strip.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* What a person can't see but a model reads (the review of P100): format characters (zero-width
|
|
7
|
+
* ones, bidi controls, the tags U+E0000–E007F, a soft hyphen, a byte-order mark), variation
|
|
8
|
+
* selectors (U+FE00–FE0F, U+E0100–E01EF), private-use characters, lone surrogates, every
|
|
9
|
+
* default-ignorable code point (the re-check: U+034F, Mongolian's free variation selectors, the
|
|
10
|
+
* Hangul fillers, Khmer's inherent vowels), and controls other than a new line and a tab. An update with any is refused, never escaped: an escaped one
|
|
11
|
+
* reads to the model differently from what the person was shown.
|
|
12
|
+
*/
|
|
13
|
+
const UNSEEN = /[\p{Cf}\p{Co}\p{Cs}\p{Default_Ignorable_Code_Point}\u{FE00}-\u{FE0F}\u{E0100}-\u{E01EF}]|[^\P{Cc}\n\t]/u;
|
|
14
|
+
/**
|
|
15
|
+
* The parts of real emoji that draw as nothing alone, allowed only where an emoji has them (the
|
|
16
|
+
* re-review of P100): U+FE0E or U+FE0F directly after a pictograph, or after a keycap base
|
|
17
|
+
* (`[0-9#*]`) that U+20E3 follows; U+200D only between two pictographs, a selector or a skin tone
|
|
18
|
+
* after the first. Each carries at most one choice beside a visible emoji (a selector there or
|
|
19
|
+
* not, a joiner there or not), never letters a model would read. Tag characters stay refused:
|
|
20
|
+
* subdivision flags too.
|
|
21
|
+
*/
|
|
22
|
+
const EMOJI_PARTS = "(?<=\\p{Extended_Pictographic})[\\uFE0E\\uFE0F]|(?<=[0-9#*])[\\uFE0E\\uFE0F](?=\\u20E3)|(?<=\\p{Extended_Pictographic}[\\uFE0F\\p{Emoji_Modifier}]?)\\u200D(?=\\p{Extended_Pictographic})";
|
|
23
|
+
/**
|
|
24
|
+
* The scripts whose letters U+200C and U+200D shape (Unicode's notes on the joiners: the cursive
|
|
25
|
+
* scripts, where they break or make a join, and the Indic ones and Myanmar, where they choose a
|
|
26
|
+
* conjunct, a half form or a ligature), by Script_Extensions.
|
|
27
|
+
*/
|
|
28
|
+
const SHAPING_SCRIPTS = [
|
|
29
|
+
"Arabic",
|
|
30
|
+
"Syriac",
|
|
31
|
+
"Nko",
|
|
32
|
+
"Mongolian",
|
|
33
|
+
"Devanagari",
|
|
34
|
+
"Bengali",
|
|
35
|
+
"Gurmukhi",
|
|
36
|
+
"Gujarati",
|
|
37
|
+
"Oriya",
|
|
38
|
+
"Tamil",
|
|
39
|
+
"Telugu",
|
|
40
|
+
"Kannada",
|
|
41
|
+
"Malayalam",
|
|
42
|
+
"Sinhala",
|
|
43
|
+
"Myanmar",
|
|
44
|
+
];
|
|
45
|
+
const SHAPED = `[${SHAPING_SCRIPTS.map((name) => `\\p{scx=${name}}`).join("")}]`;
|
|
46
|
+
/**
|
|
47
|
+
* A joiner or non-joiner between two letters or marks of those scripts (Persian's میخواهم, a
|
|
48
|
+
* Hindi half form): the shape it chooses is what the person sees. One at a letter boundary, never
|
|
49
|
+
* doubled, first or last, or beside a letter of another script.
|
|
50
|
+
*/
|
|
51
|
+
const SHAPING_JOINERS = `(?<=[\\p{L}\\p{M}])(?<=${SHAPED})[\\u200C\\u200D](?=[\\p{L}\\p{M}])(?=${SHAPED})`;
|
|
52
|
+
/** What may be invisible alone and is allowed where it is seen: one pass, on the text as sent. */
|
|
53
|
+
const SEEN_IN_PLACE = new RegExp(`${EMOJI_PARTS}|${SHAPING_JOINERS}`, "gu");
|
|
54
|
+
/** Whether `text` holds what a person can't see, the parts of emoji and shaped letters aside. */
|
|
55
|
+
export const unseen = (text) => UNSEEN.test(text.replace(SEEN_IN_PLACE, ""));
|
|
56
|
+
/** Every code point `UNSEEN` names, each on its own. */
|
|
57
|
+
const UNSEEN_EACH = new RegExp(UNSEEN.source, "gu");
|
|
58
|
+
/**
|
|
59
|
+
* `text` without anything a person can't see (P105), for text Porthole only shows, never matches,
|
|
60
|
+
* as a wordless element's label is (an `alt`, `aria-label` or `title` the person never sees on the
|
|
61
|
+
* page): every format character (bidi controls and the zero-width ones among them), every
|
|
62
|
+
* default-ignorable code point, variation selector, private-use character and lone surrogate, and
|
|
63
|
+
* every control but a new line and a tab. No exceptions: not the parts of emoji, nor the joiners
|
|
64
|
+
* between shaped letters that `unseen` lets by, whose surroundings a page can fake (the review of
|
|
65
|
+
* P105: a Mongolian variation selector beside each joiner carried 48 of them through, a payload
|
|
66
|
+
* of six bytes). An emoji may lose its joiners (a family shows as its faces). Nothing `unseen`
|
|
67
|
+
* refuses is left.
|
|
68
|
+
*/
|
|
69
|
+
export function withoutUnseen(text) {
|
|
70
|
+
return text.replace(UNSEEN_EACH, "");
|
|
71
|
+
}
|