driverkit 0.0.0-stage → 0.0.1
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/CONTROLS.md +578 -0
- package/REACT.md +477 -0
- package/README.md +104 -2
- package/dist/agent/backend.d.ts +69 -0
- package/dist/agent/backend.d.ts.map +1 -0
- package/dist/agent/backend.js +130 -0
- package/dist/agent/backend.js.map +1 -0
- package/dist/agent/control.d.ts +148 -0
- package/dist/agent/control.d.ts.map +1 -0
- package/dist/agent/control.js +342 -0
- package/dist/agent/control.js.map +1 -0
- package/dist/agent/index.d.ts +9 -0
- package/dist/agent/index.d.ts.map +1 -0
- package/dist/agent/index.js +6 -0
- package/dist/agent/index.js.map +1 -0
- package/dist/agent/loop.d.ts +136 -0
- package/dist/agent/loop.d.ts.map +1 -0
- package/dist/agent/loop.js +824 -0
- package/dist/agent/loop.js.map +1 -0
- package/dist/agent/render.d.ts +173 -0
- package/dist/agent/render.d.ts.map +1 -0
- package/dist/agent/render.js +425 -0
- package/dist/agent/render.js.map +1 -0
- package/dist/agent/tools.d.ts +138 -0
- package/dist/agent/tools.d.ts.map +1 -0
- package/dist/agent/tools.js +622 -0
- package/dist/agent/tools.js.map +1 -0
- package/dist/agent/types.d.ts +566 -0
- package/dist/agent/types.d.ts.map +1 -0
- package/dist/agent/types.js +32 -0
- package/dist/agent/types.js.map +1 -0
- package/dist/browser/budget.d.ts +92 -0
- package/dist/browser/budget.d.ts.map +1 -0
- package/dist/browser/budget.js +151 -0
- package/dist/browser/budget.js.map +1 -0
- package/dist/browser/connect.d.ts +368 -0
- package/dist/browser/connect.d.ts.map +1 -0
- package/dist/browser/connect.js +1038 -0
- package/dist/browser/connect.js.map +1 -0
- package/dist/browser/elsewhere.d.ts +89 -0
- package/dist/browser/elsewhere.d.ts.map +1 -0
- package/dist/browser/elsewhere.js +91 -0
- package/dist/browser/elsewhere.js.map +1 -0
- package/dist/browser/frames.d.ts +85 -0
- package/dist/browser/frames.d.ts.map +1 -0
- package/dist/browser/frames.js +11 -0
- package/dist/browser/frames.js.map +1 -0
- package/dist/browser/index.d.ts +8 -0
- package/dist/browser/index.d.ts.map +1 -0
- package/dist/browser/index.js +9 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/check/check.d.ts +171 -0
- package/dist/check/check.d.ts.map +1 -0
- package/dist/check/check.js +666 -0
- package/dist/check/check.js.map +1 -0
- package/dist/check/index.d.ts +3 -0
- package/dist/check/index.d.ts.map +1 -0
- package/dist/check/index.js +2 -0
- package/dist/check/index.js.map +1 -0
- package/dist/check/types.d.ts +149 -0
- package/dist/check/types.d.ts.map +1 -0
- package/dist/check/types.js +29 -0
- package/dist/check/types.js.map +1 -0
- package/dist/controls/controls.d.ts +198 -0
- package/dist/controls/controls.d.ts.map +1 -0
- package/dist/controls/controls.js +742 -0
- package/dist/controls/controls.js.map +1 -0
- package/dist/controls/dom.d.ts +108 -0
- package/dist/controls/dom.d.ts.map +1 -0
- package/dist/controls/dom.js +56 -0
- package/dist/controls/dom.js.map +1 -0
- package/dist/controls/index.d.ts +26 -0
- package/dist/controls/index.d.ts.map +1 -0
- package/dist/controls/index.js +23 -0
- package/dist/controls/index.js.map +1 -0
- package/dist/controls/rows.d.ts +66 -0
- package/dist/controls/rows.d.ts.map +1 -0
- package/dist/controls/rows.js +127 -0
- package/dist/controls/rows.js.map +1 -0
- package/dist/core/index.d.ts +3 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +15 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/registry.d.ts +192 -0
- package/dist/core/registry.d.ts.map +1 -0
- package/dist/core/registry.js +2799 -0
- package/dist/core/registry.js.map +1 -0
- package/dist/core/types.d.ts +2468 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +19 -0
- package/dist/core/types.js.map +1 -0
- package/dist/otel/index.d.ts +122 -0
- package/dist/otel/index.d.ts.map +1 -0
- package/dist/otel/index.js +157 -0
- package/dist/otel/index.js.map +1 -0
- package/dist/react/index.d.ts +191 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +547 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/runtime.d.ts +95 -0
- package/dist/react/runtime.d.ts.map +1 -0
- package/dist/react/runtime.js +54 -0
- package/dist/react/runtime.js.map +1 -0
- package/dist/react/store.d.ts +333 -0
- package/dist/react/store.d.ts.map +1 -0
- package/dist/react/store.js +695 -0
- package/dist/react/store.js.map +1 -0
- package/dist/relay/auth.d.ts +244 -0
- package/dist/relay/auth.d.ts.map +1 -0
- package/dist/relay/auth.js +391 -0
- package/dist/relay/auth.js.map +1 -0
- package/dist/relay/frames.d.ts +141 -0
- package/dist/relay/frames.d.ts.map +1 -0
- package/dist/relay/frames.js +20 -0
- package/dist/relay/frames.js.map +1 -0
- package/dist/relay/index.d.ts +8 -0
- package/dist/relay/index.d.ts.map +1 -0
- package/dist/relay/index.js +4 -0
- package/dist/relay/index.js.map +1 -0
- package/dist/relay/relay.d.ts +277 -0
- package/dist/relay/relay.d.ts.map +1 -0
- package/dist/relay/relay.js +1026 -0
- package/dist/relay/relay.js.map +1 -0
- package/dist/relay/serve.d.ts +33 -0
- package/dist/relay/serve.d.ts.map +1 -0
- package/dist/relay/serve.js +86 -0
- package/dist/relay/serve.js.map +1 -0
- package/dist/widget/chat.d.ts +290 -0
- package/dist/widget/chat.d.ts.map +1 -0
- package/dist/widget/chat.js +486 -0
- package/dist/widget/chat.js.map +1 -0
- package/dist/widget/css.d.ts +49 -0
- package/dist/widget/css.d.ts.map +1 -0
- package/dist/widget/css.js +532 -0
- package/dist/widget/css.js.map +1 -0
- package/dist/widget/dom.d.ts +143 -0
- package/dist/widget/dom.d.ts.map +1 -0
- package/dist/widget/dom.js +27 -0
- package/dist/widget/dom.js.map +1 -0
- package/dist/widget/highlight.d.ts +64 -0
- package/dist/widget/highlight.d.ts.map +1 -0
- package/dist/widget/highlight.js +136 -0
- package/dist/widget/highlight.js.map +1 -0
- package/dist/widget/index.d.ts +9 -0
- package/dist/widget/index.d.ts.map +1 -0
- package/dist/widget/index.js +20 -0
- package/dist/widget/index.js.map +1 -0
- package/dist/widget/position.d.ts +166 -0
- package/dist/widget/position.d.ts.map +1 -0
- package/dist/widget/position.js +497 -0
- package/dist/widget/position.js.map +1 -0
- package/dist/widget/settings.d.ts +116 -0
- package/dist/widget/settings.d.ts.map +1 -0
- package/dist/widget/settings.js +117 -0
- package/dist/widget/settings.js.map +1 -0
- package/dist/widget/widget.d.ts +442 -0
- package/dist/widget/widget.d.ts.map +1 -0
- package/dist/widget/widget.js +2005 -0
- package/dist/widget/widget.js.map +1 -0
- package/package.json +144 -4
|
@@ -0,0 +1,742 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* driverkit/controls — the controls layer.
|
|
3
|
+
*
|
|
4
|
+
* ```ts
|
|
5
|
+
* const dk = controlsFromDom({ root: "#app", rows: "#contacts" });
|
|
6
|
+
*
|
|
7
|
+
* mount({
|
|
8
|
+
* app: "Contacts",
|
|
9
|
+
* verbs: [...myShortcuts, ...dk.verbs],
|
|
10
|
+
* visible: () => dk.withControls(myVisible()),
|
|
11
|
+
* });
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* ──────────────────────────────────────────────────────────────────────────
|
|
15
|
+
* WHY THERE IS A SECOND LAYER AT ALL.
|
|
16
|
+
*
|
|
17
|
+
* Everything else in driverkit is a shortcut: somebody writes
|
|
18
|
+
* `select-where(company, status?)` and an agent selects four hundred rows in
|
|
19
|
+
* one call, with a count in front of the commit. That is the layer worth
|
|
20
|
+
* having and it is not going anywhere.
|
|
21
|
+
*
|
|
22
|
+
* It is also, on its own, incomplete — and the incompleteness is structural
|
|
23
|
+
* rather than a gap somebody can close by writing more verbs. A real failure:
|
|
24
|
+
* a demo whose only selection verb was `select-where(company)`, asked to
|
|
25
|
+
* select one named contact. The model refused, correctly, because the only
|
|
26
|
+
* tool it had would have selected all four hundred and ten contacts at that
|
|
27
|
+
* company in order to get one of them. It did the right thing with the wrong
|
|
28
|
+
* vocabulary. You cannot enumerate your way out of that: somebody will always
|
|
29
|
+
* ask for the thing nobody listed.
|
|
30
|
+
*
|
|
31
|
+
* So this layer is complete BY CONSTRUCTION instead of by curation. One
|
|
32
|
+
* action, total coverage: whatever the person at the keyboard could press,
|
|
33
|
+
* tick or type into, an agent can too.
|
|
34
|
+
*
|
|
35
|
+
* controls one action, total coverage press a button, type in a field
|
|
36
|
+
* shortcuts one call, many rows select-where(...), accept-selected
|
|
37
|
+
*
|
|
38
|
+
* Neither replaces the other. A shortcut is how a thousand rows move in one
|
|
39
|
+
* call with one number in front of it; a control is how the nine hundred and
|
|
40
|
+
* ninety-ninth thing nobody listed gets done at all. They compose in one
|
|
41
|
+
* `mount()` and an agent cannot tell which layer a verb came from.
|
|
42
|
+
*
|
|
43
|
+
* WHAT THIS IS NOT.
|
|
44
|
+
*
|
|
45
|
+
* It is not selector automation wearing a hat. The agent never sees a
|
|
46
|
+
* selector, a path or a coordinate: it sees `accept` because somebody typed
|
|
47
|
+
* `data-dk-control="accept"` next to their own button, and that word is the
|
|
48
|
+
* only way in. Nothing here searches by label text, by role, by position or by
|
|
49
|
+
* similarity, and a name that is not marked on the page is a refusal and never
|
|
50
|
+
* a near miss — because a near miss is how an agent presses Delete when it
|
|
51
|
+
* meant Dismiss. `Verb.at` is the one selector in the library and it is
|
|
52
|
+
* presentational: it exists so a watching human sees an outline, it is never
|
|
53
|
+
* used to find anything, and nothing here reads it.
|
|
54
|
+
*
|
|
55
|
+
* WHERE THE TWO HALVES LIVE, which is the decision the rest follows from.
|
|
56
|
+
*
|
|
57
|
+
* static, cached, in the manifest THAT the app can press a control:
|
|
58
|
+
* three verbs, three argument lists.
|
|
59
|
+
* dynamic, uncached, in the snapshot WHICH controls are on this screen now:
|
|
60
|
+
* `Snapshot.controls`.
|
|
61
|
+
*
|
|
62
|
+
* Getting that backwards is the expensive mistake and it has an obvious
|
|
63
|
+
* shape — put the control names in a verb's `ArgSpec.oneOf`. It reads as a
|
|
64
|
+
* kindness (the model cannot then name a control that does not exist) and it
|
|
65
|
+
* drops a per-screen list into the prompt prefix, so every navigation busts
|
|
66
|
+
* the cache for that user and every user's prefix differs from every other's.
|
|
67
|
+
* The library's whole cost argument is that the static half is static. So the
|
|
68
|
+
* control names are a plain `string` argument, validated against the page at
|
|
69
|
+
* call time, and the list of them rides in the snapshot beside the rows.
|
|
70
|
+
* ──────────────────────────────────────────────────────────────────────────
|
|
71
|
+
*/
|
|
72
|
+
import { collapse, defaultEvent, resolveRoot } from "./dom.js";
|
|
73
|
+
import { rowHandles } from "./rows.js";
|
|
74
|
+
/** The slugs the control verbs refuse with. Declared on every one of them, so
|
|
75
|
+
* the list reaches the model in the CACHED half and a well-briefed agent
|
|
76
|
+
* avoids the call rather than discovering the answer by making it. */
|
|
77
|
+
const REFUSES = [
|
|
78
|
+
"no-such-control",
|
|
79
|
+
"no-such-row",
|
|
80
|
+
"needs-row",
|
|
81
|
+
"not-a-row-control",
|
|
82
|
+
"wrong-verb",
|
|
83
|
+
"disabled",
|
|
84
|
+
];
|
|
85
|
+
/** `<input>` types that are not text. Everything else on an `input`, including
|
|
86
|
+
* `date` and `number`, takes text and is a field. A checkbox is a control you
|
|
87
|
+
* press, which is why this list exists at all: typing into one is nonsense and
|
|
88
|
+
* pressing one is the commonest per-row action there is. */
|
|
89
|
+
/**
|
|
90
|
+
* How far up `data-dk-human-only` is looked for. See `humanOnly`.
|
|
91
|
+
*
|
|
92
|
+
* A number rather than an unbounded loop, because the only way `parentElement`
|
|
93
|
+
* can fail to terminate is a fake that points an element at itself — a bug
|
|
94
|
+
* worth surfacing as a wrong answer rather than as a page that stops
|
|
95
|
+
* responding. 64 is deeper than any real markup's control-to-`<body>` distance
|
|
96
|
+
* and the walk stops at the first element carrying the attribute anyway.
|
|
97
|
+
*/
|
|
98
|
+
const HUMAN_ONLY_DEPTH = 64;
|
|
99
|
+
const NOT_TEXT = new Set([
|
|
100
|
+
"checkbox",
|
|
101
|
+
"radio",
|
|
102
|
+
"button",
|
|
103
|
+
"submit",
|
|
104
|
+
"reset",
|
|
105
|
+
"file",
|
|
106
|
+
"image",
|
|
107
|
+
"color",
|
|
108
|
+
"range",
|
|
109
|
+
]);
|
|
110
|
+
/**
|
|
111
|
+
* Bind the controls layer over a DOM.
|
|
112
|
+
*
|
|
113
|
+
* Nothing is captured. Every call re-reads the page, for the reason
|
|
114
|
+
* `rows.ts` gives at length: any app worth adapting re-renders, and a node
|
|
115
|
+
* captured at mount is a node that has since been detached — pressing it does
|
|
116
|
+
* precisely nothing, with no error, and the verb reports success.
|
|
117
|
+
*/
|
|
118
|
+
export function controlsFromDom(config = {}) {
|
|
119
|
+
const attribute = config.attribute ?? "data-dk-control";
|
|
120
|
+
const kindAttribute = config.kindAttribute ?? "data-dk-kind";
|
|
121
|
+
const labelAttribute = config.labelAttribute ?? "data-dk-label";
|
|
122
|
+
const fieldAttribute = config.fieldAttribute ?? "data-dk-field";
|
|
123
|
+
const humanOnlyAttribute = config.humanOnlyAttribute ?? "data-dk-human-only";
|
|
124
|
+
const limit = config.limit ?? 40;
|
|
125
|
+
const refuseWhen = config.refuseWhen ?? disabledCheck;
|
|
126
|
+
const warn = warner(config.onWarn);
|
|
127
|
+
const names = {
|
|
128
|
+
press: config.names?.press ?? "press",
|
|
129
|
+
type: config.names?.type ?? "type-into",
|
|
130
|
+
commit: config.names?.commit ?? "commit-control",
|
|
131
|
+
};
|
|
132
|
+
// ── reading the page ──────────────────────────────────────────────────────
|
|
133
|
+
function root() {
|
|
134
|
+
return resolveRoot(config.root, config.document, "controlsFromDom");
|
|
135
|
+
}
|
|
136
|
+
function rowsOf() {
|
|
137
|
+
if (config.rows === undefined)
|
|
138
|
+
return [];
|
|
139
|
+
const spec = config.rows;
|
|
140
|
+
const shared = {
|
|
141
|
+
...(config.root === undefined ? {} : { root: config.root }),
|
|
142
|
+
...(config.document === undefined ? {} : { document: config.document }),
|
|
143
|
+
...(config.onWarn === undefined ? {} : { onWarn: config.onWarn }),
|
|
144
|
+
};
|
|
145
|
+
if (typeof spec === "string")
|
|
146
|
+
return rowHandles(spec, shared);
|
|
147
|
+
if (typeof spec === "object" && "container" in spec) {
|
|
148
|
+
const { container, ...rest } = spec;
|
|
149
|
+
return rowHandles(container, { ...shared, ...rest });
|
|
150
|
+
}
|
|
151
|
+
return rowHandles(spec, shared);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Every marked element, each told whether it is inside a row.
|
|
155
|
+
*
|
|
156
|
+
* The row test is a containment test and not an ancestor walk, and it stays
|
|
157
|
+
* one now that `ElementLike` has an optional `parentElement` for
|
|
158
|
+
* `humanOnly`: it asks "is this element inside one of the rows the snapshot
|
|
159
|
+
* is showing", which is exactly the question and tests against a known set,
|
|
160
|
+
* rather than "does it have a `tr` somewhere above it", which is a different
|
|
161
|
+
* question and the wrong answer in a nested table. The opt-out cannot be
|
|
162
|
+
* written that way — there is no set of marked wrappers to test against —
|
|
163
|
+
* which is why that one walks and this one does not.
|
|
164
|
+
*/
|
|
165
|
+
function scan() {
|
|
166
|
+
const r = root();
|
|
167
|
+
const inRow = new Map();
|
|
168
|
+
for (const row of rowsOf()) {
|
|
169
|
+
for (const el of row.el.querySelectorAll(`[${attribute}]`))
|
|
170
|
+
inRow.set(el, row.id);
|
|
171
|
+
// A row element may itself be the control — a clickable `<tr>`.
|
|
172
|
+
if (isMarked(row.el, attribute))
|
|
173
|
+
inRow.set(row.el, row.id);
|
|
174
|
+
}
|
|
175
|
+
const out = [];
|
|
176
|
+
const withheld = new Set();
|
|
177
|
+
for (const el of r.querySelectorAll(`[${attribute}]`)) {
|
|
178
|
+
const name = collapse(el.getAttribute(attribute));
|
|
179
|
+
if (!name)
|
|
180
|
+
continue;
|
|
181
|
+
// Dropped HERE, in the one function every other function in this file
|
|
182
|
+
// reads the page through, so the opt-out cannot be true of the
|
|
183
|
+
// enumeration and false of the three verbs. A control that is not in
|
|
184
|
+
// this list does not exist to the layer at all — which is the same
|
|
185
|
+
// answer an unmarked button gets, deliberately.
|
|
186
|
+
if (humanOnly(el)) {
|
|
187
|
+
// Said once, and only to the author. A control that vanished because
|
|
188
|
+
// of a mark on a `<dialog>` three levels up is the one case of this
|
|
189
|
+
// attribute that looks like a bug in the library, so the library says
|
|
190
|
+
// which it was.
|
|
191
|
+
if (humanOnlyAbove(el))
|
|
192
|
+
withheld.add(name);
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
out.push({ el, name, row: inRow.get(el) ?? null });
|
|
196
|
+
}
|
|
197
|
+
if (withheld.size) {
|
|
198
|
+
warn(`controls: ${withheld.size} control(s) are inside an element marked ` +
|
|
199
|
+
`${humanOnlyAttribute} and are not published: ` +
|
|
200
|
+
`${[...withheld].slice(0, 12).join(", ")}. That is what the mark means — an agent is ` +
|
|
201
|
+
`told nothing about them, exactly as if they were never marked as controls. Put ` +
|
|
202
|
+
`${humanOnlyAttribute}="false" on one to open it again.`);
|
|
203
|
+
}
|
|
204
|
+
return out;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Marked for a person only, on this element or on anything above it.
|
|
208
|
+
*
|
|
209
|
+
* Presence is the signal, because `data-dk-human-only` with no value is how
|
|
210
|
+
* an author writes it; the one exception is the literal `false`, so a
|
|
211
|
+
* templating language that always emits the attribute can still say no — and
|
|
212
|
+
* a `false` on the element itself wins over a mark further up, which is the
|
|
213
|
+
* only reading that lets an author open one control inside a closed region.
|
|
214
|
+
*
|
|
215
|
+
* THE ANCESTOR WALK, which this did not have. Marking a `<dialog>` used to
|
|
216
|
+
* mark nothing: every button inside the confirmation stayed enumerable and
|
|
217
|
+
* pressable, which is the opposite of what the author wrote. The mark that is
|
|
218
|
+
* natural to write — one attribute on the thing that is the gate — was the
|
|
219
|
+
* one that did not work, and the mark that worked had to be repeated on
|
|
220
|
+
* every Yes, every signature field and every 2FA box inside it. An opt-out
|
|
221
|
+
* whose correct use is the laborious one is an opt-out that will be got
|
|
222
|
+
* wrong.
|
|
223
|
+
*
|
|
224
|
+
* It walks OUT PAST `root`, not up to it. A human-only region that contains
|
|
225
|
+
* the root is a region an author marked and the enumerator is standing
|
|
226
|
+
* inside; stopping at the root would silently ignore it, and that is the
|
|
227
|
+
* direction in which being wrong costs something. The visible consequence is
|
|
228
|
+
* that a root inside a marked region enumerates nothing at all, which is
|
|
229
|
+
* loud, is what the markup says, and is warned about once.
|
|
230
|
+
*
|
|
231
|
+
* TWO THINGS IT DOES NOT SEE, both worth knowing before relying on it. A
|
|
232
|
+
* shadow boundary: `parentElement` is null on the top node of a shadow tree,
|
|
233
|
+
* so a mark on the HOST element does not reach controls inside it — mark
|
|
234
|
+
* something inside the tree. And an `ElementLike` that does not implement
|
|
235
|
+
* `parentElement` at all (a non-DOM renderer's adapter, an older fake) reads
|
|
236
|
+
* as having no ancestors, which is the element-only behaviour this had
|
|
237
|
+
* before and is the compatible answer rather than the safe one.
|
|
238
|
+
*
|
|
239
|
+
* `DEPTH` bounds the walk. A real DOM cannot cycle, and a hand-written
|
|
240
|
+
* `ElementLike` whose `parentElement` points at itself is a bug in a fake
|
|
241
|
+
* that should surface as a wrong answer rather than as a hung page.
|
|
242
|
+
*/
|
|
243
|
+
function humanOnly(el) {
|
|
244
|
+
let node = el;
|
|
245
|
+
for (let depth = 0; node && depth < HUMAN_ONLY_DEPTH; depth += 1) {
|
|
246
|
+
const value = node.getAttribute(humanOnlyAttribute);
|
|
247
|
+
if (value !== null && value !== undefined) {
|
|
248
|
+
return collapse(value).toLowerCase() !== "false";
|
|
249
|
+
}
|
|
250
|
+
node = node.parentElement;
|
|
251
|
+
}
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
/** Was this element's mark inherited from an ancestor? Only for the author's
|
|
255
|
+
* diagnostic: an agent is never told a control is being withheld, because a
|
|
256
|
+
* different answer for a withheld control is an oracle over which buttons
|
|
257
|
+
* are the interesting ones. */
|
|
258
|
+
function humanOnlyAbove(el) {
|
|
259
|
+
const own = el.getAttribute(humanOnlyAttribute);
|
|
260
|
+
return (own === null || own === undefined) && humanOnly(el);
|
|
261
|
+
}
|
|
262
|
+
function kindOf(name, el) {
|
|
263
|
+
const fromHook = config.kind?.(name, el);
|
|
264
|
+
if (fromHook)
|
|
265
|
+
return fromHook;
|
|
266
|
+
const declared = collapse(el.getAttribute(kindAttribute)).toLowerCase();
|
|
267
|
+
if (declared === "read" || declared === "describe" || declared === "commit")
|
|
268
|
+
return declared;
|
|
269
|
+
if (declared !== "") {
|
|
270
|
+
warn(`controls: the control \`${name}\` declares ${kindAttribute}="${declared}", which is not ` +
|
|
271
|
+
`read, describe or commit. Treating it as undeclared, which routes it through the ` +
|
|
272
|
+
`commit verb and asks the human every time.`);
|
|
273
|
+
}
|
|
274
|
+
return undefined;
|
|
275
|
+
}
|
|
276
|
+
function isField(el) {
|
|
277
|
+
const declared = collapse(el.getAttribute(fieldAttribute)).toLowerCase();
|
|
278
|
+
if (declared === "true")
|
|
279
|
+
return true;
|
|
280
|
+
if (declared === "false")
|
|
281
|
+
return false;
|
|
282
|
+
const tag = (el.tagName ?? "").toLowerCase();
|
|
283
|
+
if (tag === "textarea")
|
|
284
|
+
return true;
|
|
285
|
+
if (tag !== "input")
|
|
286
|
+
return false;
|
|
287
|
+
return !NOT_TEXT.has(collapse(el.getAttribute("type")).toLowerCase());
|
|
288
|
+
}
|
|
289
|
+
function labelOf(name, el) {
|
|
290
|
+
const explicit = collapse(el.getAttribute(labelAttribute));
|
|
291
|
+
if (explicit)
|
|
292
|
+
return clip(explicit);
|
|
293
|
+
const text = collapse(el.textContent);
|
|
294
|
+
if (text)
|
|
295
|
+
return clip(text);
|
|
296
|
+
const aria = collapse(el.getAttribute("aria-label"));
|
|
297
|
+
if (aria)
|
|
298
|
+
return clip(aria);
|
|
299
|
+
const title = collapse(el.getAttribute("title"));
|
|
300
|
+
if (title)
|
|
301
|
+
return clip(title);
|
|
302
|
+
const placeholder = collapse(el.getAttribute("placeholder"));
|
|
303
|
+
if (placeholder)
|
|
304
|
+
return clip(placeholder);
|
|
305
|
+
// No label is better than the name repeated as one: a model reading
|
|
306
|
+
// `accept (accept)` learns nothing and pays for the second word.
|
|
307
|
+
return undefined;
|
|
308
|
+
}
|
|
309
|
+
// ── the enumeration ───────────────────────────────────────────────────────
|
|
310
|
+
/**
|
|
311
|
+
* The controls on screen, one entry per NAME rather than per element.
|
|
312
|
+
*
|
|
313
|
+
* This is the field that keeps the snapshot small, and the brief it has to
|
|
314
|
+
* meet is concrete: twenty rows with six controls each must not be a hundred
|
|
315
|
+
* and twenty entries. So a control found inside a row is described once,
|
|
316
|
+
* marked `per: "row"`, and addressed by row id — which works because the
|
|
317
|
+
* markup that rendered it was one template, so the twenty copies are the same
|
|
318
|
+
* control twenty times over and differ in nothing an agent needs.
|
|
319
|
+
*
|
|
320
|
+
* `disabled` is the one property that genuinely varies between the copies
|
|
321
|
+
* (row 3's Delete greyed out, row 4's not). Collapsing cannot preserve that,
|
|
322
|
+
* so it claims `disabled` only when EVERY copy is disabled, and the call-time
|
|
323
|
+
* check refuses for the specific row. The snapshot is therefore not quite a
|
|
324
|
+
* complete account of a row control's availability, and the alternative —
|
|
325
|
+
* one entry per row per control — costs the cost argument. Said plainly in
|
|
326
|
+
* CONTROLS.md rather than hidden here.
|
|
327
|
+
*/
|
|
328
|
+
function enumerate() {
|
|
329
|
+
const byName = new Map();
|
|
330
|
+
const dupes = new Set();
|
|
331
|
+
const undeclared = [];
|
|
332
|
+
for (const { el, name, row } of scan()) {
|
|
333
|
+
const existing = byName.get(name);
|
|
334
|
+
const off = refuseWhen(el, name) !== null;
|
|
335
|
+
if (existing) {
|
|
336
|
+
existing.copies += 1;
|
|
337
|
+
if (off)
|
|
338
|
+
existing.disabled += 1;
|
|
339
|
+
// Two screen controls sharing a name is an authoring bug: the verb acts
|
|
340
|
+
// on whichever the page yields first, exactly like two rows sharing an
|
|
341
|
+
// id. Not renamed — a suffixed name would be a name no screen shows.
|
|
342
|
+
if (row === null || existing.s.per !== "row")
|
|
343
|
+
dupes.add(name);
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
const kind = kindOf(name, el);
|
|
347
|
+
if (kind === undefined)
|
|
348
|
+
undeclared.push(name);
|
|
349
|
+
const label = labelOf(name, el);
|
|
350
|
+
byName.set(name, {
|
|
351
|
+
s: {
|
|
352
|
+
name,
|
|
353
|
+
...(label === undefined ? {} : { label }),
|
|
354
|
+
...(kind === undefined ? {} : { kind }),
|
|
355
|
+
...(row === null ? {} : { per: "row" }),
|
|
356
|
+
...(isField(el) ? { field: true } : {}),
|
|
357
|
+
},
|
|
358
|
+
copies: 1,
|
|
359
|
+
disabled: off ? 1 : 0,
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
for (const name of dupes) {
|
|
363
|
+
warn(`controls: two controls are both named \`${name}\`. A verb naming it acts on whichever ` +
|
|
364
|
+
`the page yields first. Give them different ${attribute} values.`);
|
|
365
|
+
}
|
|
366
|
+
if (undeclared.length) {
|
|
367
|
+
// Worded as a decision the author owes, not as a nuisance they can
|
|
368
|
+
// clear. The line this replaces ended "and the question goes away",
|
|
369
|
+
// which is true and is also an instruction to type the cheapest value:
|
|
370
|
+
// `read` silences the gate on every one of these in one pass, and
|
|
371
|
+
// `read` on something that changes data is the one claim the library
|
|
372
|
+
// cannot catch and will act on. So the undeclared state is named as the
|
|
373
|
+
// SAFE one it is, and the declaration is named as a claim.
|
|
374
|
+
warn(`controls: ${undeclared.length} control(s) declare no ${kindAttribute}: ` +
|
|
375
|
+
`${undeclared.slice(0, 12).join(", ")}. Each is reachable only through ` +
|
|
376
|
+
`\`${names.commit}\`, which asks the person first — so this is safe, and it is the ` +
|
|
377
|
+
`state to leave them in until somebody has read the handler. ` +
|
|
378
|
+
`${kindAttribute}="read|describe|commit" is a CLAIM about what the control does, and ` +
|
|
379
|
+
`nothing here can check it: \`read\` on a button that changes data takes away the only ` +
|
|
380
|
+
`thing standing in front of an agent pressing it. Declare the ones you have read.`);
|
|
381
|
+
}
|
|
382
|
+
const all = [...byName.values()].map(({ s, copies, disabled }) => copies > 0 && disabled === copies ? { ...s, disabled: true } : s);
|
|
383
|
+
// Over the limit the list is cut rather than sent. A truncated list that
|
|
384
|
+
// says it is truncated costs the agent a paging step; a 200-entry list
|
|
385
|
+
// costs every turn of every session, in the half that is never cached.
|
|
386
|
+
if (all.length <= limit)
|
|
387
|
+
return { controls: all, hidden: 0 };
|
|
388
|
+
warn(`controls: ${all.length} controls on screen, over the limit of ${limit}, so ${all.length - limit} ` +
|
|
389
|
+
`are not listed. Narrow \`root\` to the part of the page an agent should drive, or raise \`limit\` ` +
|
|
390
|
+
`knowing it is paid for on every turn.`);
|
|
391
|
+
return { controls: all.slice(0, limit), hidden: all.length - limit };
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* The control list as one line of text.
|
|
395
|
+
*
|
|
396
|
+
* Transitional, and default-on because without it the layer does not work:
|
|
397
|
+
* `Snapshot.controls` is structured and new, and nothing that renders a
|
|
398
|
+
* snapshot for a model reads it yet. `note` is rendered by every renderer
|
|
399
|
+
* there is, so that is where the controls go until they do. Turn it off with
|
|
400
|
+
* `inNote: false` once your renderer prints `controls`, and the duplication —
|
|
401
|
+
* which is real, and in the uncached half — goes away.
|
|
402
|
+
*
|
|
403
|
+
* The grammar is terse on purpose and the legend is in the verbs'
|
|
404
|
+
* descriptions, which are cached. Bare name means the author declared it
|
|
405
|
+
* `read`; anything else is parenthesised.
|
|
406
|
+
*/
|
|
407
|
+
function render(controls, hidden) {
|
|
408
|
+
if (controls.length === 0)
|
|
409
|
+
return undefined;
|
|
410
|
+
const screen = controls.filter((c) => c.per !== "row");
|
|
411
|
+
const perRow = controls.filter((c) => c.per === "row");
|
|
412
|
+
const parts = [];
|
|
413
|
+
if (screen.length)
|
|
414
|
+
parts.push(`controls: ${screen.map(one).join(", ")}`);
|
|
415
|
+
if (perRow.length)
|
|
416
|
+
parts.push(`each row: ${perRow.map(one).join(", ")}`);
|
|
417
|
+
if (hidden)
|
|
418
|
+
parts.push(`${hidden} more control(s) on this screen are not listed`);
|
|
419
|
+
return parts.join("; ");
|
|
420
|
+
}
|
|
421
|
+
function one(c) {
|
|
422
|
+
const marks = [];
|
|
423
|
+
if (c.field)
|
|
424
|
+
marks.push("text");
|
|
425
|
+
if (c.kind === "describe" || c.kind === "commit")
|
|
426
|
+
marks.push(c.kind);
|
|
427
|
+
if (c.kind === undefined)
|
|
428
|
+
marks.push("undeclared");
|
|
429
|
+
if (c.disabled)
|
|
430
|
+
marks.push("disabled");
|
|
431
|
+
const label = c.label && c.label.toLowerCase() !== c.name.toLowerCase() ? ` “${c.label}”` : "";
|
|
432
|
+
return `${c.name}${label}${marks.length ? ` (${marks.join(", ")})` : ""}`;
|
|
433
|
+
}
|
|
434
|
+
// ── acting ────────────────────────────────────────────────────────────────
|
|
435
|
+
/**
|
|
436
|
+
* Which verb a control's declared kind routes to.
|
|
437
|
+
*
|
|
438
|
+
* The whole safety argument of this layer is in these three lines, so there
|
|
439
|
+
* is no cleverness in them. `read` and `describe` go to the safe door, where
|
|
440
|
+
* the mechanism then picks between pressing and typing; `commit` and
|
|
441
|
+
* **undeclared** go to the gated one, which takes both. The second half of
|
|
442
|
+
* that is the load-bearing choice and CONTROLS.md argues it at length: a
|
|
443
|
+
* control nobody declared is an unreviewed button, and the library cannot
|
|
444
|
+
* tell a Next Page from a Delete Account by looking at either.
|
|
445
|
+
*/
|
|
446
|
+
function routeOf(kind, field) {
|
|
447
|
+
if (kind === undefined || kind === "commit")
|
|
448
|
+
return names.commit;
|
|
449
|
+
return field ? names.type : names.press;
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Find the element a call named, or refuse.
|
|
453
|
+
*
|
|
454
|
+
* Resolved from the page at call time and never from the snapshot the model
|
|
455
|
+
* was shown. A control that has been re-rendered away since then is the
|
|
456
|
+
* ordinary case, not an exception — the person at the keyboard has been
|
|
457
|
+
* clicking the whole time the model was thinking — so it is a refusal with a
|
|
458
|
+
* list of what IS there, and never a click on whatever is nearest.
|
|
459
|
+
*/
|
|
460
|
+
function resolve(verb, control, row, ctx) {
|
|
461
|
+
const found = scan();
|
|
462
|
+
const matching = found.filter((f) => f.name === control);
|
|
463
|
+
if (matching.length === 0) {
|
|
464
|
+
const offered = [...new Set(found.map((f) => f.name))].sort();
|
|
465
|
+
return ctx.refuse("no-such-control", offered.length
|
|
466
|
+
? `this screen offers no control named \`${control}\`. It offers: ${offered.join(", ")}. ` +
|
|
467
|
+
`Read the screen's control list rather than remembering one — it changes as you navigate.`
|
|
468
|
+
: `this screen offers no controls at all. Nothing on it is marked with ${attribute}, so ` +
|
|
469
|
+
`there is nothing for \`${verb}\` to press here.`);
|
|
470
|
+
}
|
|
471
|
+
const wantsRow = matching.some((f) => f.row !== null);
|
|
472
|
+
if (row === undefined || row === "") {
|
|
473
|
+
if (wantsRow) {
|
|
474
|
+
const ids = [...new Set(rowsOf().map((r) => r.id))];
|
|
475
|
+
return ctx.refuse("needs-row", `\`${control}\` is on each row, so it needs a row id in \`row\`. Rows on screen: ` +
|
|
476
|
+
`${ids.slice(0, 8).join(", ")}${ids.length > 8 ? ", …" : ""}.`);
|
|
477
|
+
}
|
|
478
|
+
return describeEl(matching[0].el, control);
|
|
479
|
+
}
|
|
480
|
+
if (!wantsRow) {
|
|
481
|
+
return ctx.refuse("not-a-row-control", `\`${control}\` belongs to the screen, not to a row, so it cannot be pressed for row ` +
|
|
482
|
+
`\`${row}\`. Call it without \`row\`.`);
|
|
483
|
+
}
|
|
484
|
+
const onRow = matching.find((f) => f.row === row);
|
|
485
|
+
if (!onRow) {
|
|
486
|
+
const ids = rowsOf().map((r) => r.id);
|
|
487
|
+
if (!ids.includes(row)) {
|
|
488
|
+
return ctx.refuse("no-such-row", `no row \`${row}\` is on screen. Rows on screen: ${ids.slice(0, 8).join(", ")}` +
|
|
489
|
+
`${ids.length > 8 ? ", …" : ""}. The screen may have paged or filtered since you read ` +
|
|
490
|
+
`it; read it again. Quoting \`rev\` turns this into a \`stale\` refusal instead.`);
|
|
491
|
+
}
|
|
492
|
+
return ctx.refuse("no-such-control", `row \`${row}\` is on screen but offers no \`${control}\`. Row controls are not always ` +
|
|
493
|
+
`rendered on every row.`);
|
|
494
|
+
}
|
|
495
|
+
return describeEl(onRow.el, control);
|
|
496
|
+
}
|
|
497
|
+
function describeEl(el, control) {
|
|
498
|
+
return { el, kind: kindOf(control, el), field: isField(el) };
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* The sentence a routing refusal carries.
|
|
502
|
+
*
|
|
503
|
+
* Four cases, and each names the verb to use instead, because a refusal that
|
|
504
|
+
* only says no costs a model a turn and teaches it nothing. The undeclared
|
|
505
|
+
* one is phrased as a fact about the app rather than about the control: the
|
|
506
|
+
* model has not made a mistake, the markup is simply silent, and the next
|
|
507
|
+
* call it should make is the gated one.
|
|
508
|
+
*/
|
|
509
|
+
function whyWrongVerb(control, verb, route, resolved) {
|
|
510
|
+
if (resolved.kind === undefined) {
|
|
511
|
+
return (`the app has not declared what \`${control}\` does, so it is treated as though it changes ` +
|
|
512
|
+
`data. Use \`${route}\`, which asks the person first.`);
|
|
513
|
+
}
|
|
514
|
+
if (route === names.commit) {
|
|
515
|
+
return `\`${control}\` is declared \`${resolved.kind}\`. Use \`${route}\` for it.`;
|
|
516
|
+
}
|
|
517
|
+
if (resolved.field && verb === names.press) {
|
|
518
|
+
return (`\`${control}\` is a field, not a button. Use \`${names.type}\` and pass the text you ` +
|
|
519
|
+
`want in it.`);
|
|
520
|
+
}
|
|
521
|
+
if (!resolved.field && verb === names.type) {
|
|
522
|
+
return `\`${control}\` is a button, not a field. Use \`${names.press}\` for it.`;
|
|
523
|
+
}
|
|
524
|
+
return `\`${control}\` is declared \`${resolved.kind}\`. Use \`${route}\` for it.`;
|
|
525
|
+
}
|
|
526
|
+
/** The checks every control verb makes once it has an element, in the order
|
|
527
|
+
* that gives the most useful refusal: routing before availability, because a
|
|
528
|
+
* model told `disabled` about a control it should not have called through
|
|
529
|
+
* this verb at all will retry it here when the button enables. */
|
|
530
|
+
function gate(verb, control, hasText, resolved, ctx) {
|
|
531
|
+
const route = routeOf(resolved.kind, resolved.field);
|
|
532
|
+
if (route !== verb) {
|
|
533
|
+
return ctx.refuse("wrong-verb", whyWrongVerb(control, verb, route, resolved));
|
|
534
|
+
}
|
|
535
|
+
// Only the gated verb can be ambiguous about the mechanism, because it is
|
|
536
|
+
// the one verb that takes both kinds of control. The text and the control
|
|
537
|
+
// must agree: assigning text to a button is nonsense, and clicking a field
|
|
538
|
+
// because no text came with the call is the silent-nothing failure.
|
|
539
|
+
if (verb === names.commit) {
|
|
540
|
+
if (resolved.field && !hasText) {
|
|
541
|
+
return ctx.refuse("wrong-verb", `\`${control}\` is a field: it takes text. Pass \`text\`.`);
|
|
542
|
+
}
|
|
543
|
+
if (!resolved.field && hasText) {
|
|
544
|
+
return ctx.refuse("wrong-verb", `\`${control}\` is a button, not a field, so there is nowhere to put text. Call ` +
|
|
545
|
+
`\`${verb}\` again without \`text\`.`);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
const why = refuseWhen(resolved.el, control);
|
|
549
|
+
if (why) {
|
|
550
|
+
return ctx.refuse(why, `the \`${control}\` control is on screen but not actionable right now. Something has to ` +
|
|
551
|
+
`change before it will work — a selection, a required field, a permission.`);
|
|
552
|
+
}
|
|
553
|
+
return null;
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Put text into a field.
|
|
557
|
+
*
|
|
558
|
+
* Assigning `value` is not typing, and the difference is the failure this
|
|
559
|
+
* library cares most about. A framework learns about a keystroke from an
|
|
560
|
+
* event; a field whose `value` was assigned and nothing else looks right on
|
|
561
|
+
* screen, has told the app nothing, and is wiped by the next render — while
|
|
562
|
+
* the verb reports `ok`. So this fires `input` and `change`, and when it
|
|
563
|
+
* cannot (no `Event`, no `dispatchEvent`) it throws rather than doing the
|
|
564
|
+
* half of the job that looks like all of it.
|
|
565
|
+
*/
|
|
566
|
+
function putText(el, control, text) {
|
|
567
|
+
if (config.setValue) {
|
|
568
|
+
config.setValue(el, text);
|
|
569
|
+
return;
|
|
570
|
+
}
|
|
571
|
+
const Ev = config.Event ?? defaultEvent();
|
|
572
|
+
if (!Ev || typeof el.dispatchEvent !== "function") {
|
|
573
|
+
throw new Error(`driverkit: cannot type into \`${control}\`: no way to fire an input event ` +
|
|
574
|
+
`(${!Ev ? "no Event constructor" : "the element has no dispatchEvent"}). Assigning value ` +
|
|
575
|
+
`without an event would look typed and tell your app nothing. Pass options.Event, or ` +
|
|
576
|
+
`options.setValue to put text in your own way.`);
|
|
577
|
+
}
|
|
578
|
+
el.focus?.();
|
|
579
|
+
el.value = text;
|
|
580
|
+
el.dispatchEvent(new Ev("input", { bubbles: true }));
|
|
581
|
+
el.dispatchEvent(new Ev("change", { bubbles: true }));
|
|
582
|
+
}
|
|
583
|
+
// ── the verbs ─────────────────────────────────────────────────────────────
|
|
584
|
+
const CONTROL_ARG = {
|
|
585
|
+
type: "string",
|
|
586
|
+
describe: "the name of a control, exactly as the screen's control list spells it. Not a selector, " +
|
|
587
|
+
"not a label, not a guess: a name the screen does not list is refused rather than matched " +
|
|
588
|
+
"to the nearest thing.",
|
|
589
|
+
};
|
|
590
|
+
const ROW_ARG = {
|
|
591
|
+
type: "string",
|
|
592
|
+
describe: "the id of the row to act on, for a control the screen lists under `each row`. Use an id " +
|
|
593
|
+
"from the rows on screen. Omit it for a control that belongs to the screen itself.",
|
|
594
|
+
optional: true,
|
|
595
|
+
};
|
|
596
|
+
const LEGEND = "The screen lists the controls it is offering right now, under `controls` (the screen's own) " +
|
|
597
|
+
"and `each row` (one per row, named with `row`). A bare name is a control the app declared " +
|
|
598
|
+
"harmless; `(describe)` changes what is selected or filtered; `(commit)` changes data; " +
|
|
599
|
+
"`(undeclared)` means the app never said, so it is treated as though it changes data; " +
|
|
600
|
+
"`(text)` takes text; `(disabled)` is on screen but not actionable. The list changes as you " +
|
|
601
|
+
"navigate, so read it rather than remembering it.";
|
|
602
|
+
const press = {
|
|
603
|
+
name: names.press,
|
|
604
|
+
kind: "describe",
|
|
605
|
+
describe: "use one of the controls the screen is offering: press a button, tick a checkbox, switch " +
|
|
606
|
+
"tabs, follow a link. This is the verb for a control the app declared harmless — a bare name " +
|
|
607
|
+
`or \`(describe)\` in the list. A \`(commit)\` or \`(undeclared)\` control is refused here ` +
|
|
608
|
+
`and goes through \`${names.commit}\`; a \`(text)\` control through \`${names.type}\`. ` +
|
|
609
|
+
"It presses exactly one control and reports no count. " +
|
|
610
|
+
LEGEND,
|
|
611
|
+
refuses: [...REFUSES],
|
|
612
|
+
args: { control: CONTROL_ARG, row: ROW_ARG },
|
|
613
|
+
run: ({ control, row }, ctx) => {
|
|
614
|
+
const resolved = resolve(names.press, control, row, ctx);
|
|
615
|
+
if ("reason" in resolved)
|
|
616
|
+
return resolved;
|
|
617
|
+
const refused = gate(names.press, control, false, resolved, ctx);
|
|
618
|
+
if (refused)
|
|
619
|
+
return refused;
|
|
620
|
+
resolved.el.click();
|
|
621
|
+
},
|
|
622
|
+
};
|
|
623
|
+
const typeInto = {
|
|
624
|
+
name: names.type,
|
|
625
|
+
kind: "describe",
|
|
626
|
+
describe: "put text into one of the fields the screen is offering — a search box, a filter, a form " +
|
|
627
|
+
"input. Replaces whatever is in it; pass an empty string to clear it. This is the verb for " +
|
|
628
|
+
`a field the app declared harmless. A \`(commit)\` or \`(undeclared)\` field is refused ` +
|
|
629
|
+
`here and goes through \`${names.commit}\`. ` +
|
|
630
|
+
LEGEND,
|
|
631
|
+
refuses: [...REFUSES],
|
|
632
|
+
args: {
|
|
633
|
+
control: CONTROL_ARG,
|
|
634
|
+
text: {
|
|
635
|
+
type: "string",
|
|
636
|
+
describe: "the text to put in the field. An empty string clears it.",
|
|
637
|
+
},
|
|
638
|
+
row: ROW_ARG,
|
|
639
|
+
},
|
|
640
|
+
run: ({ control, text, row }, ctx) => {
|
|
641
|
+
const resolved = resolve(names.type, control, row, ctx);
|
|
642
|
+
if ("reason" in resolved)
|
|
643
|
+
return resolved;
|
|
644
|
+
const refused = gate(names.type, control, true, resolved, ctx);
|
|
645
|
+
if (refused)
|
|
646
|
+
return refused;
|
|
647
|
+
putText(resolved.el, control, text);
|
|
648
|
+
},
|
|
649
|
+
};
|
|
650
|
+
const commitControl = {
|
|
651
|
+
name: names.commit,
|
|
652
|
+
kind: "commit",
|
|
653
|
+
describe: "use a control that changes data — or one whose effect the app never declared, which is " +
|
|
654
|
+
"treated the same way. One call presses one control, so one irreversible action is one " +
|
|
655
|
+
"question to the person watching. Pass `text` for a field. If the control is listed as " +
|
|
656
|
+
`harmless or \`(describe)\`, this refuses and \`${names.press}\` or \`${names.type}\` is ` +
|
|
657
|
+
"the verb for it — routing a safe press through here would ask the person a question that " +
|
|
658
|
+
"did not need asking, and a gate that asks about everything is a gate nobody reads. " +
|
|
659
|
+
LEGEND,
|
|
660
|
+
refuses: [...REFUSES],
|
|
661
|
+
args: {
|
|
662
|
+
control: CONTROL_ARG,
|
|
663
|
+
text: {
|
|
664
|
+
type: "string",
|
|
665
|
+
describe: "the text to put in the field, when the control is one that takes text.",
|
|
666
|
+
optional: true,
|
|
667
|
+
},
|
|
668
|
+
row: ROW_ARG,
|
|
669
|
+
},
|
|
670
|
+
run: ({ control, text, row }, ctx) => {
|
|
671
|
+
const resolved = resolve(names.commit, control, row, ctx);
|
|
672
|
+
if ("reason" in resolved)
|
|
673
|
+
return resolved;
|
|
674
|
+
const hasText = text !== undefined && text !== null;
|
|
675
|
+
const refused = gate(names.commit, control, hasText, resolved, ctx);
|
|
676
|
+
if (refused)
|
|
677
|
+
return refused;
|
|
678
|
+
if (hasText)
|
|
679
|
+
putText(resolved.el, control, text);
|
|
680
|
+
else
|
|
681
|
+
resolved.el.click();
|
|
682
|
+
},
|
|
683
|
+
};
|
|
684
|
+
const controls = () => enumerate().controls;
|
|
685
|
+
const note = () => {
|
|
686
|
+
if (config.inNote === false)
|
|
687
|
+
return undefined;
|
|
688
|
+
const { controls: list, hidden } = enumerate();
|
|
689
|
+
return render(list, hidden);
|
|
690
|
+
};
|
|
691
|
+
return {
|
|
692
|
+
verbs: [press, typeInto, commitControl],
|
|
693
|
+
controls,
|
|
694
|
+
note,
|
|
695
|
+
withControls: (snapshot) => {
|
|
696
|
+
const { controls: list, hidden } = enumerate();
|
|
697
|
+
const line = config.inNote === false ? undefined : render(list, hidden);
|
|
698
|
+
return {
|
|
699
|
+
...snapshot,
|
|
700
|
+
...(list.length ? { controls: list } : {}),
|
|
701
|
+
...(line ? { note: snapshot.note ? `${snapshot.note}. ${line}` : line } : {}),
|
|
702
|
+
};
|
|
703
|
+
},
|
|
704
|
+
};
|
|
705
|
+
}
|
|
706
|
+
/** `disabled` and `aria-disabled` only, read off the author's own markup.
|
|
707
|
+
* Deliberately not a visibility or pointer-events check: those need
|
|
708
|
+
* layout, layout needs a real browser, and a heuristic that is right in
|
|
709
|
+
* Chrome and wrong in a test is worse than a rule the author can read off
|
|
710
|
+
* their own markup. */
|
|
711
|
+
function disabledCheck(el) {
|
|
712
|
+
if (el.getAttribute("disabled") !== null)
|
|
713
|
+
return "disabled";
|
|
714
|
+
if (collapse(el.getAttribute("aria-disabled")) === "true")
|
|
715
|
+
return "disabled";
|
|
716
|
+
return null;
|
|
717
|
+
}
|
|
718
|
+
/** Is this element itself marked? `querySelectorAll` never returns the node it
|
|
719
|
+
* was called on, so a clickable row has to be checked directly. */
|
|
720
|
+
function isMarked(el, attribute) {
|
|
721
|
+
return collapse(el.getAttribute(attribute)) !== "";
|
|
722
|
+
}
|
|
723
|
+
/** A label is for recognising a control, not for reading its contents. A cell
|
|
724
|
+
* with a paragraph in it would otherwise put the paragraph in every snapshot. */
|
|
725
|
+
function clip(text) {
|
|
726
|
+
return text.length <= 40 ? text : `${text.slice(0, 39)}…`;
|
|
727
|
+
}
|
|
728
|
+
const alreadySaid = new Set();
|
|
729
|
+
function warner(onWarn) {
|
|
730
|
+
if (onWarn)
|
|
731
|
+
return onWarn;
|
|
732
|
+
// Module-scoped: the enumeration runs on every verb call, so a per-binding
|
|
733
|
+
// set would repeat the same line for the life of the page.
|
|
734
|
+
const console = globalThis.console;
|
|
735
|
+
return (message) => {
|
|
736
|
+
if (alreadySaid.has(message) || !console?.warn)
|
|
737
|
+
return;
|
|
738
|
+
alreadySaid.add(message);
|
|
739
|
+
console.warn(`driverkit: ${message}`);
|
|
740
|
+
};
|
|
741
|
+
}
|
|
742
|
+
//# sourceMappingURL=controls.js.map
|