screw-bugs 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +906 -2
- package/bin/screw-bugs.js +29 -0
- package/dist/browser-playwright/annotate.d.ts +9 -0
- package/dist/browser-playwright/annotate.js +86 -0
- package/dist/browser-playwright/capture.d.ts +232 -0
- package/dist/browser-playwright/capture.js +1049 -0
- package/dist/browser-playwright/frames.d.ts +77 -0
- package/dist/browser-playwright/frames.js +191 -0
- package/dist/browser-playwright/index.d.ts +2 -0
- package/dist/browser-playwright/index.js +2 -0
- package/dist/browser-playwright/input.d.ts +44 -0
- package/dist/browser-playwright/input.js +116 -0
- package/dist/browser-playwright/launch.d.ts +21 -0
- package/dist/browser-playwright/launch.js +86 -0
- package/dist/browser-playwright/page.d.ts +387 -0
- package/dist/browser-playwright/page.js +1930 -0
- package/dist/build-info.json +5 -0
- package/dist/cli/app.d.ts +19 -0
- package/dist/cli/app.js +70 -0
- package/dist/cli/build-info.d.ts +52 -0
- package/dist/cli/build-info.js +82 -0
- package/dist/cli/commands.d.ts +14 -0
- package/dist/cli/commands.js +308 -0
- package/dist/cli/config.d.ts +16 -0
- package/dist/cli/config.js +71 -0
- package/dist/cli/doctor.d.ts +19 -0
- package/dist/cli/doctor.js +113 -0
- package/dist/cli/environment.d.ts +20 -0
- package/dist/cli/environment.js +34 -0
- package/dist/cli/exit-codes.d.ts +29 -0
- package/dist/cli/exit-codes.js +28 -0
- package/dist/cli/failure.d.ts +20 -0
- package/dist/cli/failure.js +15 -0
- package/dist/cli/index.d.ts +6 -0
- package/dist/cli/index.js +6 -0
- package/dist/cli/install-method.d.ts +96 -0
- package/dist/cli/install-method.js +148 -0
- package/dist/cli/main.d.ts +1 -0
- package/dist/cli/main.js +10 -0
- package/dist/cli/mcp.d.ts +30 -0
- package/dist/cli/mcp.js +309 -0
- package/dist/cli/node-version.d.ts +3 -0
- package/dist/cli/node-version.js +22 -0
- package/dist/cli/operations.d.ts +127 -0
- package/dist/cli/operations.js +337 -0
- package/dist/cli/package-info.d.ts +22 -0
- package/dist/cli/package-info.js +25 -0
- package/dist/cli/report-server.d.ts +170 -0
- package/dist/cli/report-server.js +387 -0
- package/dist/cli/signals.d.ts +37 -0
- package/dist/cli/signals.js +61 -0
- package/dist/cli/stamp-build.d.ts +1 -0
- package/dist/cli/stamp-build.js +9 -0
- package/dist/cli/update-check.d.ts +140 -0
- package/dist/cli/update-check.js +316 -0
- package/dist/cli/uploads.d.ts +16 -0
- package/dist/cli/uploads.js +80 -0
- package/dist/core/css.d.ts +95 -0
- package/dist/core/css.js +436 -0
- package/dist/core/engine.d.ts +65 -0
- package/dist/core/engine.js +436 -0
- package/dist/core/errors.d.ts +66 -0
- package/dist/core/errors.js +30 -0
- package/dist/core/evidence.d.ts +14 -0
- package/dist/core/evidence.js +127 -0
- package/dist/core/index.d.ts +12 -0
- package/dist/core/index.js +12 -0
- package/dist/core/labels.d.ts +47 -0
- package/dist/core/labels.js +199 -0
- package/dist/core/liveness.d.ts +45 -0
- package/dist/core/liveness.js +64 -0
- package/dist/core/marks.d.ts +63 -0
- package/dist/core/marks.js +410 -0
- package/dist/core/services.d.ts +444 -0
- package/dist/core/services.js +22 -0
- package/dist/core/steps.d.ts +96 -0
- package/dist/core/steps.js +656 -0
- package/dist/core/uploads.d.ts +49 -0
- package/dist/core/uploads.js +133 -0
- package/dist/core/video.d.ts +16 -0
- package/dist/core/video.js +101 -0
- package/dist/core/viewport.d.ts +28 -0
- package/dist/core/viewport.js +50 -0
- package/dist/report/html.d.ts +37 -0
- package/dist/report/html.js +762 -0
- package/dist/report/index.d.ts +5 -0
- package/dist/report/index.js +5 -0
- package/dist/report/markup.d.ts +27 -0
- package/dist/report/markup.js +75 -0
- package/dist/report/pr-summary.d.ts +11 -0
- package/dist/report/pr-summary.js +110 -0
- package/dist/report/sha256.d.ts +9 -0
- package/dist/report/sha256.js +97 -0
- package/dist/report/styles.d.ts +1 -0
- package/dist/report/styles.js +328 -0
- package/dist/report/view.d.ts +141 -0
- package/dist/report/view.js +368 -0
- package/dist/report/walkthrough.d.ts +75 -0
- package/dist/report/walkthrough.js +458 -0
- package/dist/schema/config.d.ts +7 -0
- package/dist/schema/config.js +6 -0
- package/dist/schema/doctor.d.ts +46 -0
- package/dist/schema/doctor.js +23 -0
- package/dist/schema/evidence.d.ts +994 -0
- package/dist/schema/evidence.js +521 -0
- package/dist/schema/findings.d.ts +159 -0
- package/dist/schema/findings.js +80 -0
- package/dist/schema/index.d.ts +7 -0
- package/dist/schema/index.js +7 -0
- package/dist/schema/job.d.ts +1724 -0
- package/dist/schema/job.js +505 -0
- package/dist/schema/ledger.d.ts +951 -0
- package/dist/schema/ledger.js +193 -0
- package/dist/schema/status.d.ts +7 -0
- package/dist/schema/status.js +11 -0
- package/dist/store-local/accounts.d.ts +7 -0
- package/dist/store-local/accounts.js +9 -0
- package/dist/store-local/index.d.ts +3 -0
- package/dist/store-local/index.js +3 -0
- package/dist/store-local/paths.d.ts +10 -0
- package/dist/store-local/paths.js +17 -0
- package/dist/store-local/storage.d.ts +20 -0
- package/dist/store-local/storage.js +170 -0
- package/package.json +73 -4
|
@@ -0,0 +1,1049 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Functions that run inside the page under test, through Playwright's `evaluate`. Playwright sends
|
|
3
|
+
* their source to the page, so each one is self-contained: no imports, no helpers from outside.
|
|
4
|
+
* The project is built without DOM types; the page's globals are reached through `globalThis`.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* The elements a highlight may name, as they are now: interactive elements, headings, labelled
|
|
8
|
+
* fields and elements with a role, test ID or ARIA label first, then blocks of visible text, at
|
|
9
|
+
* most `max`, in page order. Boxes are in CSS pixels: from the top left of the page for a
|
|
10
|
+
* full-page screenshot, of the viewport otherwise. Also the device pixel ratio and scroll offsets.
|
|
11
|
+
* When it found more than it kept, or the page was too big to look at whole, `truncated` says so,
|
|
12
|
+
* with how many elements not kept share each key (as `candidateKeys` makes them) of a kept one.
|
|
13
|
+
* Also the visual viewport (`visual`: its scale and offset in the layout), as `VisualViewport`
|
|
14
|
+
* says, which boxes in the layout show through. Each element's `content` is where its visible text and images are, as in `contentOf`. An
|
|
15
|
+
* element that is there but not seen (see `seen`) is kept with `visible: false`. In a frame,
|
|
16
|
+
* `opacity` is that of its `<iframe>` with what is around it (see `frameElementInfo`), so an
|
|
17
|
+
* element is seen through it. Images get their alternative text and source, links their absolute
|
|
18
|
+
* address, each cut to `urlMax` characters (a `data:` URL to its type). For CSS selectors, each
|
|
19
|
+
* element gets at most `attrsMax` of its attributes (as `Attributes` says which), and the
|
|
20
|
+
* elements that hold kept ones are saved too, at most `nodesMax`, each with the one it is in.
|
|
21
|
+
*/
|
|
22
|
+
export const captureState = (options) => {
|
|
23
|
+
const g = globalThis;
|
|
24
|
+
const doc = g.document;
|
|
25
|
+
const unread = new Set(['script', 'style', 'noscript', 'template', 'svg', 'iframe']);
|
|
26
|
+
/*
|
|
27
|
+
* Visible text the way it reads: text in separate boxes (blocks, flex and grid items, inline
|
|
28
|
+
* blocks, line breaks) kept apart by a space, as innerText would not for inline blocks. Stops
|
|
29
|
+
* once it has `max` characters. The same walk as in `nearText`; each function must stand alone.
|
|
30
|
+
*/
|
|
31
|
+
const textOf = (root, max) => {
|
|
32
|
+
const parts = [];
|
|
33
|
+
let length = 0;
|
|
34
|
+
const walk = (node, shown) => {
|
|
35
|
+
if (length > max)
|
|
36
|
+
return;
|
|
37
|
+
if (node.nodeType === 3) {
|
|
38
|
+
if (shown) {
|
|
39
|
+
parts.push(node.data);
|
|
40
|
+
length += node.data.length;
|
|
41
|
+
}
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (node.nodeType !== 1 || unread.has(node.localName))
|
|
45
|
+
return;
|
|
46
|
+
if (node.localName === 'br') {
|
|
47
|
+
parts.push(' ');
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const style = g.getComputedStyle(node);
|
|
51
|
+
const contents = style.display === 'contents';
|
|
52
|
+
if (!contents && (node.checkVisibility ? !node.checkVisibility() : style.display === 'none'))
|
|
53
|
+
return;
|
|
54
|
+
const apart = style.display !== 'inline' && !contents;
|
|
55
|
+
if (apart)
|
|
56
|
+
parts.push(' ');
|
|
57
|
+
for (const child of Array.from(node.childNodes))
|
|
58
|
+
walk(child, style.visibility === 'visible');
|
|
59
|
+
if (apart)
|
|
60
|
+
parts.push(' ');
|
|
61
|
+
};
|
|
62
|
+
walk(root, true);
|
|
63
|
+
return parts.join('').replace(/\s+/g, ' ').trim();
|
|
64
|
+
};
|
|
65
|
+
const clean = (value) => {
|
|
66
|
+
const flat = typeof value === 'string' ? value.replace(/\s+/g, ' ').trim() : '';
|
|
67
|
+
return flat ? flat.slice(0, options.textMax) : undefined;
|
|
68
|
+
};
|
|
69
|
+
const inputRoles = {
|
|
70
|
+
checkbox: 'checkbox',
|
|
71
|
+
radio: 'radio',
|
|
72
|
+
button: 'button',
|
|
73
|
+
submit: 'button',
|
|
74
|
+
reset: 'button',
|
|
75
|
+
image: 'button',
|
|
76
|
+
range: 'slider',
|
|
77
|
+
number: 'spinbutton',
|
|
78
|
+
search: 'searchbox',
|
|
79
|
+
};
|
|
80
|
+
const tagRoles = {
|
|
81
|
+
button: 'button',
|
|
82
|
+
summary: 'button',
|
|
83
|
+
h1: 'heading',
|
|
84
|
+
h2: 'heading',
|
|
85
|
+
h3: 'heading',
|
|
86
|
+
h4: 'heading',
|
|
87
|
+
h5: 'heading',
|
|
88
|
+
h6: 'heading',
|
|
89
|
+
textarea: 'textbox',
|
|
90
|
+
nav: 'navigation',
|
|
91
|
+
main: 'main',
|
|
92
|
+
dialog: 'dialog',
|
|
93
|
+
li: 'listitem',
|
|
94
|
+
ul: 'list',
|
|
95
|
+
ol: 'list',
|
|
96
|
+
table: 'table',
|
|
97
|
+
form: 'form',
|
|
98
|
+
p: 'paragraph',
|
|
99
|
+
};
|
|
100
|
+
const roleOf = (el, tag) => {
|
|
101
|
+
const explicit = clean(el.getAttribute('role'))?.split(' ')[0];
|
|
102
|
+
if (explicit)
|
|
103
|
+
return explicit;
|
|
104
|
+
if (tag === 'a' || tag === 'area')
|
|
105
|
+
return el.hasAttribute('href') ? 'link' : undefined;
|
|
106
|
+
if (tag === 'input') {
|
|
107
|
+
const type = String(el.getAttribute('type') ?? 'text').toLowerCase();
|
|
108
|
+
return type === 'hidden' ? undefined : (inputRoles[type] ?? 'textbox');
|
|
109
|
+
}
|
|
110
|
+
if (tag === 'select')
|
|
111
|
+
return el.multiple || el.size > 1 ? 'listbox' : 'combobox';
|
|
112
|
+
if (tag === 'img')
|
|
113
|
+
return el.getAttribute('alt') === '' ? undefined : 'img';
|
|
114
|
+
return tagRoles[tag];
|
|
115
|
+
};
|
|
116
|
+
const byIds = (ids) => typeof ids === 'string'
|
|
117
|
+
? clean(ids
|
|
118
|
+
.split(/\s+/)
|
|
119
|
+
.map((id) => {
|
|
120
|
+
const el = doc.getElementById(id);
|
|
121
|
+
// A label may be hidden from view and still name the element.
|
|
122
|
+
return el ? textOf(el, options.textMax) || String(el.textContent ?? '') : '';
|
|
123
|
+
})
|
|
124
|
+
.join(' '))
|
|
125
|
+
: undefined;
|
|
126
|
+
const isField = (tag) => tag === 'input' || tag === 'textarea' || tag === 'select';
|
|
127
|
+
const labelOf = (el, tag) => isField(tag)
|
|
128
|
+
? (clean(Array.from(el.labels ?? [])
|
|
129
|
+
.map((label) => textOf(label, options.textMax))
|
|
130
|
+
.join(' ')) ??
|
|
131
|
+
byIds(el.getAttribute('aria-labelledby')) ??
|
|
132
|
+
clean(el.getAttribute('aria-label')))
|
|
133
|
+
: undefined;
|
|
134
|
+
const nameOf = (el, tag, label) => clean(el.getAttribute('aria-label')) ??
|
|
135
|
+
byIds(el.getAttribute('aria-labelledby')) ??
|
|
136
|
+
(isField(tag)
|
|
137
|
+
? ['submit', 'button', 'reset'].includes(el.type)
|
|
138
|
+
? clean(el.value)
|
|
139
|
+
: (label ?? clean(el.getAttribute('placeholder')) ?? clean(el.getAttribute('title')))
|
|
140
|
+
: tag === 'img'
|
|
141
|
+
? clean(el.getAttribute('alt'))
|
|
142
|
+
: (clean(textOf(el, options.textMax)) ?? clean(el.getAttribute('title'))));
|
|
143
|
+
/*
|
|
144
|
+
* An image's alternative text: the `alt` of an `<img>`, `<area>` or image button, else for an
|
|
145
|
+
* element with role img or an `<svg>`, its ARIA label or its `<title>`. The same as in
|
|
146
|
+
* `mediaFacts`; each function must stand alone.
|
|
147
|
+
*/
|
|
148
|
+
const altOf = (el, tag, role) => {
|
|
149
|
+
if (tag === 'img' || tag === 'area' || (tag === 'input' && el.type === 'image'))
|
|
150
|
+
return clean(el.getAttribute('alt'));
|
|
151
|
+
if (role !== 'img' && tag !== 'svg')
|
|
152
|
+
return undefined;
|
|
153
|
+
const title = tag === 'svg' ? el.querySelector(':scope > title') : null;
|
|
154
|
+
return (clean(el.getAttribute('aria-label')) ??
|
|
155
|
+
byIds(el.getAttribute('aria-labelledby')) ??
|
|
156
|
+
clean(title?.textContent));
|
|
157
|
+
};
|
|
158
|
+
const shortUrl = (value) => {
|
|
159
|
+
const url = typeof value === 'string' ? value.trim() : '';
|
|
160
|
+
if (!url)
|
|
161
|
+
return undefined;
|
|
162
|
+
return url.startsWith('data:')
|
|
163
|
+
? `${url.slice(0, Math.min(url.indexOf(',') + 1 || 40, 60))}…`
|
|
164
|
+
: url.slice(0, options.urlMax);
|
|
165
|
+
};
|
|
166
|
+
const srcOf = (el, tag) => tag === 'img'
|
|
167
|
+
? shortUrl(el.currentSrc || el.src)
|
|
168
|
+
: tag === 'input'
|
|
169
|
+
? shortUrl(el.src)
|
|
170
|
+
: undefined;
|
|
171
|
+
const hrefOf = (el, tag) => (tag === 'a' || tag === 'area') && el.hasAttribute('href')
|
|
172
|
+
? shortUrl(String(el.href))
|
|
173
|
+
: undefined;
|
|
174
|
+
/** An `<svg>` is read only when it is an image with a name, such as a logo. */
|
|
175
|
+
const namedSvg = (el) => el.getAttribute('role') === 'img' ||
|
|
176
|
+
el.hasAttribute('aria-label') ||
|
|
177
|
+
el.hasAttribute('aria-labelledby');
|
|
178
|
+
/*
|
|
179
|
+
* What a selector may name of an element, as saved (the same as in `frameElementInfo`; each function must stand alone): its classes (at most 8), and its attributes
|
|
180
|
+
* other than its ID, classes and test ID (saved as such), each value cut to `attrValueMax`
|
|
181
|
+
* characters, at most `attrsMax`. Values never saved (`style`, `value`, event handlers…) and
|
|
182
|
+
* those past the limit are named in `omittedAttrs`, and what was cut or left out is said, so
|
|
183
|
+
* a selector that depends on it is never taken as matching or not.
|
|
184
|
+
*/
|
|
185
|
+
const unsaved = new Set(['style', 'value', 'srcset', 'sizes', 'd', 'points']);
|
|
186
|
+
const namedApart = new Set(['id', 'class', 'data-testid']);
|
|
187
|
+
const savedOf = (node) => {
|
|
188
|
+
const attrs = {};
|
|
189
|
+
const cutAttrs = [];
|
|
190
|
+
const omittedAttrs = [];
|
|
191
|
+
let kept = 0;
|
|
192
|
+
let moreAttrs = false;
|
|
193
|
+
for (const attribute of Array.from(node.attributes)) {
|
|
194
|
+
const name = String(attribute.name).toLowerCase();
|
|
195
|
+
if (namedApart.has(name))
|
|
196
|
+
continue;
|
|
197
|
+
const value = String(attribute.value);
|
|
198
|
+
if (unsaved.has(name) || name.startsWith('on') || kept >= options.attrsMax) {
|
|
199
|
+
if (omittedAttrs.length < options.attrsMax)
|
|
200
|
+
omittedAttrs.push(name);
|
|
201
|
+
else
|
|
202
|
+
moreAttrs = true;
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
attrs[name] = value.slice(0, options.attrValueMax);
|
|
206
|
+
if (value.length > options.attrValueMax)
|
|
207
|
+
cutAttrs.push(name);
|
|
208
|
+
kept++;
|
|
209
|
+
}
|
|
210
|
+
const all = Array.from(node.classList);
|
|
211
|
+
const classes = all.slice(0, 8);
|
|
212
|
+
return {
|
|
213
|
+
...(classes.length > 0 ? { classes } : {}),
|
|
214
|
+
...(all.length > classes.length ? { moreClasses: true } : {}),
|
|
215
|
+
...(kept > 0 ? { attrs } : {}),
|
|
216
|
+
...(cutAttrs.length > 0 ? { cutAttrs } : {}),
|
|
217
|
+
...(omittedAttrs.length > 0 ? { omittedAttrs } : {}),
|
|
218
|
+
...(moreAttrs ? { moreAttrs: true } : {}),
|
|
219
|
+
...(node.namespaceURI !== 'http://www.w3.org/1999/xhtml' ? { foreign: true } : {}),
|
|
220
|
+
};
|
|
221
|
+
};
|
|
222
|
+
const ownText = (el) => Array.from(el.childNodes).some((node) => node.nodeType === 3 && String(node.textContent).trim() !== '');
|
|
223
|
+
const skip = new Set(['script', 'style', 'noscript', 'template', 'br', 'svg', 'path', 'iframe']);
|
|
224
|
+
const visible = (el, rect) => rect.width > 0 &&
|
|
225
|
+
rect.height > 0 &&
|
|
226
|
+
(typeof el.checkVisibility === 'function'
|
|
227
|
+
? el.checkVisibility({ visibilityProperty: true })
|
|
228
|
+
: g.getComputedStyle(el).visibility !== 'hidden');
|
|
229
|
+
/*
|
|
230
|
+
* Where an element's visible text and images are, in CSS pixels of the viewport: the union of
|
|
231
|
+
* its text's and its images' and fields' boxes. A wide element's text may be centred,
|
|
232
|
+
* right-aligned or right-to-left. Undefined when it has none, or too much to look at quickly.
|
|
233
|
+
* The same as `contentOf`; each function must stand alone.
|
|
234
|
+
*/
|
|
235
|
+
const replaced = new Set([
|
|
236
|
+
'img',
|
|
237
|
+
'svg',
|
|
238
|
+
'video',
|
|
239
|
+
'canvas',
|
|
240
|
+
'picture',
|
|
241
|
+
'iframe',
|
|
242
|
+
'embed',
|
|
243
|
+
'object',
|
|
244
|
+
'input',
|
|
245
|
+
'select',
|
|
246
|
+
'textarea',
|
|
247
|
+
'meter',
|
|
248
|
+
'progress',
|
|
249
|
+
]);
|
|
250
|
+
const unseen = new Set(['script', 'style', 'noscript', 'template']);
|
|
251
|
+
const contentOf = (root) => {
|
|
252
|
+
let [left, top, right, bottom] = [Infinity, Infinity, -Infinity, -Infinity];
|
|
253
|
+
let budget = 200;
|
|
254
|
+
const add = (r) => {
|
|
255
|
+
if (r.width <= 0 && r.height <= 0)
|
|
256
|
+
return;
|
|
257
|
+
[left, top] = [Math.min(left, r.left), Math.min(top, r.top)];
|
|
258
|
+
[right, bottom] = [Math.max(right, r.right), Math.max(bottom, r.bottom)];
|
|
259
|
+
};
|
|
260
|
+
const range = doc.createRange();
|
|
261
|
+
const walk = (node) => {
|
|
262
|
+
if (--budget < 0)
|
|
263
|
+
return false;
|
|
264
|
+
if (node.nodeType === 3) {
|
|
265
|
+
if (String(node.data).trim() === '')
|
|
266
|
+
return true;
|
|
267
|
+
range.selectNodeContents(node);
|
|
268
|
+
add(range.getBoundingClientRect());
|
|
269
|
+
return true;
|
|
270
|
+
}
|
|
271
|
+
if (node.nodeType !== 1 || unseen.has(node.localName))
|
|
272
|
+
return true;
|
|
273
|
+
if (node !== root &&
|
|
274
|
+
!node.checkVisibility?.({ visibilityProperty: true }) &&
|
|
275
|
+
g.getComputedStyle(node).display !== 'contents')
|
|
276
|
+
return true;
|
|
277
|
+
if (node !== root && replaced.has(node.localName)) {
|
|
278
|
+
add(node.getBoundingClientRect());
|
|
279
|
+
return true;
|
|
280
|
+
}
|
|
281
|
+
for (const child of Array.from(node.childNodes))
|
|
282
|
+
if (!walk(child))
|
|
283
|
+
return false;
|
|
284
|
+
return true;
|
|
285
|
+
};
|
|
286
|
+
if (!walk(root) || right <= left || bottom <= top)
|
|
287
|
+
return undefined;
|
|
288
|
+
return { x: left, y: top, width: right - left, height: bottom - top };
|
|
289
|
+
};
|
|
290
|
+
/*
|
|
291
|
+
* Whether an element can be seen, beyond having a size and `visibility: visible` (elements
|
|
292
|
+
* without those are not kept at all): not almost transparent with its ancestors' opacity (as a
|
|
293
|
+
* section that fades in on scroll is until it does), and not cut away whole by an ancestor that
|
|
294
|
+
* clips its overflow. The page itself scrolls, so its root and body do not clip here. A frame's
|
|
295
|
+
* document is seen through its `<iframe>`: through its opacity, too.
|
|
296
|
+
*/
|
|
297
|
+
/*
|
|
298
|
+
* The element a node shows inside, as it renders (the composed tree): the slot it is assigned
|
|
299
|
+
* to, else its parent, else the host of the shadow root it is at the top of (or is). The same in every
|
|
300
|
+
* page function here; each must stand alone. Opacity and clipping follow it.
|
|
301
|
+
*/
|
|
302
|
+
const composedUp = (node) => node.assignedSlot ?? node.parentElement ?? node.parentNode?.host ?? node.host ?? null;
|
|
303
|
+
const opacities = new Map();
|
|
304
|
+
const opacityOf = (el) => {
|
|
305
|
+
if (!el || el === doc.documentElement)
|
|
306
|
+
return options.opacity ?? 1;
|
|
307
|
+
const known = opacities.get(el);
|
|
308
|
+
if (known !== undefined)
|
|
309
|
+
return known;
|
|
310
|
+
const own = Number.parseFloat(g.getComputedStyle(el).opacity);
|
|
311
|
+
const value = (Number.isFinite(own) ? own : 1) * opacityOf(composedUp(el));
|
|
312
|
+
opacities.set(el, value);
|
|
313
|
+
return value;
|
|
314
|
+
};
|
|
315
|
+
const clips = (style) => [style.overflowX, style.overflowY].some((o) => o !== 'visible');
|
|
316
|
+
const seen = (el, rect) => {
|
|
317
|
+
if (opacityOf(el) < 0.1)
|
|
318
|
+
return false;
|
|
319
|
+
for (let up = composedUp(el); up && up !== doc.body; up = composedUp(up)) {
|
|
320
|
+
if (!clips(g.getComputedStyle(up)))
|
|
321
|
+
continue;
|
|
322
|
+
const r = up.getBoundingClientRect();
|
|
323
|
+
if (rect.right <= r.left ||
|
|
324
|
+
rect.left >= r.right ||
|
|
325
|
+
rect.bottom <= r.top ||
|
|
326
|
+
rect.top >= r.bottom)
|
|
327
|
+
return false;
|
|
328
|
+
}
|
|
329
|
+
return true;
|
|
330
|
+
};
|
|
331
|
+
// Every element, those in open shadow roots too (after the page's own, root by root).
|
|
332
|
+
const all = [];
|
|
333
|
+
const roots = doc.body ? [doc.body] : [];
|
|
334
|
+
for (let i = 0; i < roots.length; i++)
|
|
335
|
+
for (const el of Array.from(roots[i].querySelectorAll('*'))) {
|
|
336
|
+
all.push(el);
|
|
337
|
+
if (el.shadowRoot)
|
|
338
|
+
roots.push(el.shadowRoot);
|
|
339
|
+
}
|
|
340
|
+
const found = [];
|
|
341
|
+
// A page with a huge DOM is looked at only as far as this; what is beyond is rarely on screen.
|
|
342
|
+
const scanMax = 8000;
|
|
343
|
+
for (let i = 0; i < Math.min(all.length, scanMax); i++) {
|
|
344
|
+
const el = all[i];
|
|
345
|
+
const tag = String(el.tagName).toLowerCase();
|
|
346
|
+
if (skip.has(tag) && !(tag === 'svg' && namedSvg(el)))
|
|
347
|
+
continue;
|
|
348
|
+
const role = roleOf(el, tag);
|
|
349
|
+
const first = role !== undefined ||
|
|
350
|
+
tag === 'label' ||
|
|
351
|
+
el.hasAttribute('data-testid') ||
|
|
352
|
+
el.hasAttribute('aria-label');
|
|
353
|
+
if (!first && !ownText(el))
|
|
354
|
+
continue;
|
|
355
|
+
const rect = el.getBoundingClientRect();
|
|
356
|
+
if (!visible(el, rect))
|
|
357
|
+
continue;
|
|
358
|
+
found.push({ el, tag, role, rect, first });
|
|
359
|
+
}
|
|
360
|
+
const firsts = found.filter((f) => f.first).length;
|
|
361
|
+
let blocks = Math.max(0, options.max - firsts);
|
|
362
|
+
let kept = found.filter((f) => f.first || blocks-- > 0);
|
|
363
|
+
if (kept.length > options.max)
|
|
364
|
+
kept = kept.slice(0, options.max);
|
|
365
|
+
const [dx, dy] = options.fullPage ? [g.scrollX, g.scrollY] : [0, 0];
|
|
366
|
+
const index = new Map();
|
|
367
|
+
kept.forEach((f, i) => index.set(f.el, i));
|
|
368
|
+
/** The elements that hold kept ones, outer ones first; each saved once. */
|
|
369
|
+
const nodes = [];
|
|
370
|
+
const nodeIndex = new Map();
|
|
371
|
+
const nodeOf = (el) => {
|
|
372
|
+
const chain = [];
|
|
373
|
+
let up = el;
|
|
374
|
+
for (; up && up.nodeType === 1 && !nodeIndex.has(up); up = up.parentElement)
|
|
375
|
+
chain.push(up);
|
|
376
|
+
let parent = up ? nodeIndex.get(up) : undefined;
|
|
377
|
+
for (const node of chain.reverse()) {
|
|
378
|
+
if (nodes.length >= options.nodesMax)
|
|
379
|
+
return undefined;
|
|
380
|
+
const testId = node.getAttribute('data-testid');
|
|
381
|
+
nodes.push({
|
|
382
|
+
tag: String(node.tagName).toLowerCase(),
|
|
383
|
+
...(node.id ? { id: String(node.id) } : {}),
|
|
384
|
+
...(testId !== null ? { testId: String(testId) } : {}),
|
|
385
|
+
...savedOf(node),
|
|
386
|
+
...(parent !== undefined ? { parent } : {}),
|
|
387
|
+
});
|
|
388
|
+
parent = nodes.length - 1;
|
|
389
|
+
nodeIndex.set(node, parent);
|
|
390
|
+
}
|
|
391
|
+
return parent;
|
|
392
|
+
};
|
|
393
|
+
/** What a highlight matches an element by. */
|
|
394
|
+
const describe = (el, tag, role) => {
|
|
395
|
+
const label = labelOf(el, tag);
|
|
396
|
+
const name = role === undefined ? undefined : nameOf(el, tag, label);
|
|
397
|
+
const text = isField(tag) ? undefined : clean(textOf(el, options.textMax));
|
|
398
|
+
const testId = el.getAttribute('data-testid');
|
|
399
|
+
const alt = altOf(el, tag, role);
|
|
400
|
+
return { label, name, text, alt, testId: testId === null ? undefined : String(testId) };
|
|
401
|
+
};
|
|
402
|
+
// The keys of `candidateKeys` in src/schema; the two must make the same strings.
|
|
403
|
+
const matchText = (text) => text.replace(/\s+/g, ' ').trim().toLowerCase();
|
|
404
|
+
const keysOf = (c) => [
|
|
405
|
+
...(c.role !== undefined ? [JSON.stringify(['role', matchText(c.role)])] : []),
|
|
406
|
+
...(c.role !== undefined && c.name !== undefined
|
|
407
|
+
? [JSON.stringify(['role', matchText(c.role), matchText(c.name)])]
|
|
408
|
+
: []),
|
|
409
|
+
...(c.text !== undefined ? [JSON.stringify(['text', matchText(c.text)])] : []),
|
|
410
|
+
...(c.label !== undefined ? [JSON.stringify(['label', matchText(c.label)])] : []),
|
|
411
|
+
...(c.alt !== undefined ? [JSON.stringify(['alt', matchText(c.alt)])] : []),
|
|
412
|
+
...(c.id !== undefined ? [JSON.stringify(['id', c.id])] : []),
|
|
413
|
+
...(c.testId !== undefined ? [JSON.stringify(['testId', c.testId])] : []),
|
|
414
|
+
];
|
|
415
|
+
const candidates = kept.map(({ el, tag, role, rect }) => {
|
|
416
|
+
let parent;
|
|
417
|
+
for (let up = el.parentElement; up && parent === undefined; up = up.parentElement)
|
|
418
|
+
parent = index.get(up);
|
|
419
|
+
const { label, name, text, alt, testId } = describe(el, tag, role);
|
|
420
|
+
const content = contentOf(el);
|
|
421
|
+
const src = srcOf(el, tag);
|
|
422
|
+
const href = hrefOf(el, tag);
|
|
423
|
+
const up = el.parentElement ? nodeOf(el.parentElement) : undefined;
|
|
424
|
+
return {
|
|
425
|
+
tag,
|
|
426
|
+
...(role !== undefined ? { role } : {}),
|
|
427
|
+
...(name !== undefined ? { name } : {}),
|
|
428
|
+
...(text !== undefined ? { text } : {}),
|
|
429
|
+
...(label !== undefined ? { label } : {}),
|
|
430
|
+
...(alt !== undefined ? { alt } : {}),
|
|
431
|
+
...(src !== undefined ? { src } : {}),
|
|
432
|
+
...(href !== undefined ? { href } : {}),
|
|
433
|
+
...(el.id ? { id: String(el.id) } : {}),
|
|
434
|
+
...(testId !== undefined ? { testId } : {}),
|
|
435
|
+
...savedOf(el),
|
|
436
|
+
...(up !== undefined ? { up } : {}),
|
|
437
|
+
// An element it is in could not be saved, so selectors cannot see all it is in.
|
|
438
|
+
...(up === undefined && el.parentElement ? { upCut: true } : {}),
|
|
439
|
+
box: { x: rect.left + dx, y: rect.top + dy, width: rect.width, height: rect.height },
|
|
440
|
+
...(content !== undefined
|
|
441
|
+
? { content: { ...content, x: content.x + dx, y: content.y + dy } }
|
|
442
|
+
: {}),
|
|
443
|
+
...(parent !== undefined ? { parent } : {}),
|
|
444
|
+
...(seen(el, rect) ? {} : { visible: false }),
|
|
445
|
+
};
|
|
446
|
+
});
|
|
447
|
+
const dropped = found.filter((f) => !index.has(f.el));
|
|
448
|
+
let truncated;
|
|
449
|
+
if (dropped.length > 0 || all.length > scanMax) {
|
|
450
|
+
const keys = new Set(candidates.flatMap(keysOf));
|
|
451
|
+
const unsaved = {};
|
|
452
|
+
const nested = new Set();
|
|
453
|
+
for (const { el, tag, role } of dropped) {
|
|
454
|
+
const id = el.id ? String(el.id) : undefined;
|
|
455
|
+
for (const key of keysOf({ role, id, ...describe(el, tag, role) }))
|
|
456
|
+
if (keys.has(key))
|
|
457
|
+
unsaved[key] = (unsaved[key] ?? 0) + 1;
|
|
458
|
+
if (role === undefined)
|
|
459
|
+
continue;
|
|
460
|
+
for (let up = el.parentElement; up; up = up.parentElement) {
|
|
461
|
+
const at = index.get(up);
|
|
462
|
+
const holder = at === undefined ? undefined : candidates[at];
|
|
463
|
+
if (at !== undefined &&
|
|
464
|
+
holder?.role !== undefined &&
|
|
465
|
+
matchText(holder.role) === matchText(role))
|
|
466
|
+
nested.add(at);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
truncated = {
|
|
470
|
+
found: found.length,
|
|
471
|
+
complete: all.length <= scanMax,
|
|
472
|
+
unsaved,
|
|
473
|
+
nested: [...nested].sort((a, b) => a - b),
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
/*
|
|
477
|
+
* The visual viewport the boxes above show in: on a phone, what of the layout the screen shows,
|
|
478
|
+
* and at what scale. Read last, in this same call, so it is of the same moment as they are.
|
|
479
|
+
* Measuring it brings style and layout up to date, as measuring the boxes does: the screenshot
|
|
480
|
+
* this is read for does that anyway (the boxes here, the frames' elements, and taking the image
|
|
481
|
+
* itself), so it changes nothing a page could notice that the screenshot does not.
|
|
482
|
+
*/
|
|
483
|
+
const view = g.visualViewport;
|
|
484
|
+
return {
|
|
485
|
+
scale: Number(g.devicePixelRatio) || 1,
|
|
486
|
+
scrollX: Number(g.scrollX) || 0,
|
|
487
|
+
scrollY: Number(g.scrollY) || 0,
|
|
488
|
+
candidates,
|
|
489
|
+
nodes,
|
|
490
|
+
...(truncated !== undefined ? { truncated } : {}),
|
|
491
|
+
...(view ? { visual: { scale: view.scale, x: view.offsetLeft, y: view.offsetTop } } : {}),
|
|
492
|
+
};
|
|
493
|
+
};
|
|
494
|
+
/**
|
|
495
|
+
* Scrolls through the whole page, a viewport at a time, and back to where it was, so that what a
|
|
496
|
+
* page shows only once it is scrolled to (sections that fade in, lazy images, counters) is there
|
|
497
|
+
* for a full-page screenshot. At most `steps` viewports, `stepMs` apart; then waits for images
|
|
498
|
+
* still loading while `budgetMs` from the start allows, and gives what came in, once it has
|
|
499
|
+
* scrolled back, `settleMs` to finish (such as a fade-in). Never takes longer than `budgetMs` and a frame or two.
|
|
500
|
+
* Smooth scrolling of the page is turned off meanwhile, so each step lands at once.
|
|
501
|
+
*/
|
|
502
|
+
export const revealPage = async (options) => {
|
|
503
|
+
const g = globalThis;
|
|
504
|
+
const doc = g.document;
|
|
505
|
+
const root = doc.scrollingElement ?? doc.documentElement;
|
|
506
|
+
if (!root)
|
|
507
|
+
return;
|
|
508
|
+
const start = g.performance.now();
|
|
509
|
+
const left = () => options.budgetMs - (g.performance.now() - start);
|
|
510
|
+
const sleep = (ms) => new Promise((resolve) => g.setTimeout(resolve, Math.max(0, Math.min(ms, left()))));
|
|
511
|
+
const frame = () => new Promise((resolve) => {
|
|
512
|
+
g.requestAnimationFrame(() => resolve());
|
|
513
|
+
g.setTimeout(resolve, 50);
|
|
514
|
+
});
|
|
515
|
+
const [x, y] = [g.scrollX, g.scrollY];
|
|
516
|
+
const smooth = root.style.scrollBehavior;
|
|
517
|
+
root.style.scrollBehavior = 'auto';
|
|
518
|
+
try {
|
|
519
|
+
const step = Math.max(1, g.innerHeight * 0.85);
|
|
520
|
+
for (let i = 1; i <= options.steps &&
|
|
521
|
+
left() > options.settleMs &&
|
|
522
|
+
g.scrollY + g.innerHeight < root.scrollHeight - 1; i++) {
|
|
523
|
+
g.scrollTo(x, Math.min(i * step, root.scrollHeight));
|
|
524
|
+
await frame();
|
|
525
|
+
await sleep(options.stepMs);
|
|
526
|
+
}
|
|
527
|
+
const images = Array.from(doc.images);
|
|
528
|
+
while (images.some((img) => !img.complete) && left() > options.settleMs)
|
|
529
|
+
await sleep(50);
|
|
530
|
+
}
|
|
531
|
+
finally {
|
|
532
|
+
g.scrollTo(x, y);
|
|
533
|
+
root.style.scrollBehavior = smooth;
|
|
534
|
+
}
|
|
535
|
+
await frame();
|
|
536
|
+
await sleep(options.settleMs);
|
|
537
|
+
};
|
|
538
|
+
/**
|
|
539
|
+
* The visible text around an element: its own, or that of the nearest of its first few ancestors
|
|
540
|
+
* with enough to read, on one line and cut to `max` characters. Text in separate boxes is kept
|
|
541
|
+
* apart by a space, as in `captureState`. For the start of the page's text, pass `body`.
|
|
542
|
+
*/
|
|
543
|
+
export const nearText = (element, max) => {
|
|
544
|
+
const g = globalThis;
|
|
545
|
+
const unread = new Set(['script', 'style', 'noscript', 'template', 'svg', 'iframe']);
|
|
546
|
+
const textOf = (root) => {
|
|
547
|
+
const parts = [];
|
|
548
|
+
let length = 0;
|
|
549
|
+
const walk = (node, shown) => {
|
|
550
|
+
if (length > max)
|
|
551
|
+
return;
|
|
552
|
+
if (node.nodeType === 3) {
|
|
553
|
+
if (shown) {
|
|
554
|
+
parts.push(node.data);
|
|
555
|
+
length += node.data.length;
|
|
556
|
+
}
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
if (node.nodeType !== 1 || unread.has(node.localName))
|
|
560
|
+
return;
|
|
561
|
+
if (node.localName === 'br') {
|
|
562
|
+
parts.push(' ');
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
565
|
+
const style = g.getComputedStyle(node);
|
|
566
|
+
const contents = style.display === 'contents';
|
|
567
|
+
if (!contents && (node.checkVisibility ? !node.checkVisibility() : style.display === 'none'))
|
|
568
|
+
return;
|
|
569
|
+
const apart = style.display !== 'inline' && !contents;
|
|
570
|
+
if (apart)
|
|
571
|
+
parts.push(' ');
|
|
572
|
+
for (const child of Array.from(node.childNodes))
|
|
573
|
+
walk(child, style.visibility === 'visible');
|
|
574
|
+
if (apart)
|
|
575
|
+
parts.push(' ');
|
|
576
|
+
};
|
|
577
|
+
walk(root, true);
|
|
578
|
+
return parts.join('').replace(/\s+/g, ' ').trim();
|
|
579
|
+
};
|
|
580
|
+
let node = element;
|
|
581
|
+
let text = textOf(node);
|
|
582
|
+
for (let up = 0; up < 4 && node?.parentElement && text.length < 40; up++) {
|
|
583
|
+
node = node.parentElement;
|
|
584
|
+
text = textOf(node);
|
|
585
|
+
}
|
|
586
|
+
return text.slice(0, max) || undefined;
|
|
587
|
+
};
|
|
588
|
+
/**
|
|
589
|
+
* Where an element's visible text and images are, relative to the top left of its own box, in CSS
|
|
590
|
+
* pixels: the union of its text's and its images' and fields' boxes, as `captureState` saves
|
|
591
|
+
* them. Undefined when it has none, or too much to look at quickly.
|
|
592
|
+
*/
|
|
593
|
+
export const contentOf = (element) => {
|
|
594
|
+
const g = globalThis;
|
|
595
|
+
const doc = g.document;
|
|
596
|
+
const root = element;
|
|
597
|
+
const unseen = new Set(['script', 'style', 'noscript', 'template']);
|
|
598
|
+
const replaced = new Set([
|
|
599
|
+
'img',
|
|
600
|
+
'svg',
|
|
601
|
+
'video',
|
|
602
|
+
'canvas',
|
|
603
|
+
'picture',
|
|
604
|
+
'iframe',
|
|
605
|
+
'embed',
|
|
606
|
+
'object',
|
|
607
|
+
'input',
|
|
608
|
+
'select',
|
|
609
|
+
'textarea',
|
|
610
|
+
'meter',
|
|
611
|
+
'progress',
|
|
612
|
+
]);
|
|
613
|
+
let [left, top, right, bottom] = [Infinity, Infinity, -Infinity, -Infinity];
|
|
614
|
+
let budget = 200;
|
|
615
|
+
const add = (r) => {
|
|
616
|
+
if (r.width <= 0 && r.height <= 0)
|
|
617
|
+
return;
|
|
618
|
+
[left, top] = [Math.min(left, r.left), Math.min(top, r.top)];
|
|
619
|
+
[right, bottom] = [Math.max(right, r.right), Math.max(bottom, r.bottom)];
|
|
620
|
+
};
|
|
621
|
+
const range = doc.createRange();
|
|
622
|
+
const walk = (node) => {
|
|
623
|
+
if (--budget < 0)
|
|
624
|
+
return false;
|
|
625
|
+
if (node.nodeType === 3) {
|
|
626
|
+
if (String(node.data).trim() === '')
|
|
627
|
+
return true;
|
|
628
|
+
range.selectNodeContents(node);
|
|
629
|
+
add(range.getBoundingClientRect());
|
|
630
|
+
return true;
|
|
631
|
+
}
|
|
632
|
+
if (node.nodeType !== 1 || unseen.has(node.localName))
|
|
633
|
+
return true;
|
|
634
|
+
if (node !== root &&
|
|
635
|
+
!node.checkVisibility?.({ visibilityProperty: true }) &&
|
|
636
|
+
g.getComputedStyle(node).display !== 'contents')
|
|
637
|
+
return true;
|
|
638
|
+
if (node !== root && replaced.has(node.localName)) {
|
|
639
|
+
add(node.getBoundingClientRect());
|
|
640
|
+
return true;
|
|
641
|
+
}
|
|
642
|
+
for (const child of Array.from(node.childNodes))
|
|
643
|
+
if (!walk(child))
|
|
644
|
+
return false;
|
|
645
|
+
return true;
|
|
646
|
+
};
|
|
647
|
+
if (!walk(root) || right <= left || bottom <= top)
|
|
648
|
+
return undefined;
|
|
649
|
+
const own = root.getBoundingClientRect();
|
|
650
|
+
return { x: left - own.left, y: top - own.top, width: right - left, height: bottom - top };
|
|
651
|
+
};
|
|
652
|
+
/**
|
|
653
|
+
* The images and links visible in the document, in page order, at most `max` of each: images with
|
|
654
|
+
* their alternative text (as `captureState` reads it), source and size in CSS pixels, links with
|
|
655
|
+
* their text and absolute address. Texts are cut to `textMax` characters, addresses to `urlMax`
|
|
656
|
+
* (a `data:` URL to its type).
|
|
657
|
+
*/
|
|
658
|
+
export const mediaFacts = (options) => {
|
|
659
|
+
const g = globalThis;
|
|
660
|
+
const doc = g.document;
|
|
661
|
+
const clean = (value) => {
|
|
662
|
+
const flat = typeof value === 'string' ? value.replace(/\s+/g, ' ').trim() : '';
|
|
663
|
+
return flat ? flat.slice(0, options.textMax) : undefined;
|
|
664
|
+
};
|
|
665
|
+
const byIds = (ids) => typeof ids === 'string'
|
|
666
|
+
? clean(ids
|
|
667
|
+
.split(/\s+/)
|
|
668
|
+
.map((id) => String(doc.getElementById(id)?.textContent ?? ''))
|
|
669
|
+
.join(' '))
|
|
670
|
+
: undefined;
|
|
671
|
+
const shortUrl = (value) => {
|
|
672
|
+
const url = typeof value === 'string' ? value.trim() : '';
|
|
673
|
+
if (!url)
|
|
674
|
+
return undefined;
|
|
675
|
+
return url.startsWith('data:')
|
|
676
|
+
? `${url.slice(0, Math.min(url.indexOf(',') + 1 || 40, 60))}…`
|
|
677
|
+
: url.slice(0, options.urlMax);
|
|
678
|
+
};
|
|
679
|
+
const shown = (el) => {
|
|
680
|
+
const rect = el.getBoundingClientRect();
|
|
681
|
+
return (rect.width > 0 &&
|
|
682
|
+
rect.height > 0 &&
|
|
683
|
+
(typeof el.checkVisibility === 'function'
|
|
684
|
+
? el.checkVisibility({ visibilityProperty: true })
|
|
685
|
+
: g.getComputedStyle(el).visibility !== 'hidden') &&
|
|
686
|
+
rect);
|
|
687
|
+
};
|
|
688
|
+
const images = [];
|
|
689
|
+
const links = [];
|
|
690
|
+
// Those in open shadow roots too, after the page's own.
|
|
691
|
+
const wanted = 'img, input[type=image], svg, [role=img], a[href], area[href]';
|
|
692
|
+
const all = [];
|
|
693
|
+
const roots = doc.body ? [doc.body] : [];
|
|
694
|
+
for (let i = 0; i < roots.length; i++)
|
|
695
|
+
for (const el of Array.from(roots[i].querySelectorAll('*'))) {
|
|
696
|
+
if (el.matches(wanted))
|
|
697
|
+
all.push(el);
|
|
698
|
+
if (el.shadowRoot)
|
|
699
|
+
roots.push(el.shadowRoot);
|
|
700
|
+
}
|
|
701
|
+
for (let i = 0; i < Math.min(all.length, 4000); i++) {
|
|
702
|
+
if (images.length >= options.max && links.length >= options.max)
|
|
703
|
+
break;
|
|
704
|
+
const el = all[i];
|
|
705
|
+
const tag = String(el.tagName).toLowerCase();
|
|
706
|
+
if ((tag === 'a' || tag === 'area') && links.length < options.max) {
|
|
707
|
+
const rect = shown(el);
|
|
708
|
+
const href = shortUrl(String(el.href));
|
|
709
|
+
if (!rect || href === undefined)
|
|
710
|
+
continue;
|
|
711
|
+
const text = clean(el.getAttribute('aria-label')) ??
|
|
712
|
+
clean(el.innerText) ??
|
|
713
|
+
clean(el.querySelector('img[alt]')?.getAttribute('alt'));
|
|
714
|
+
links.push({ ...(text !== undefined ? { text } : {}), href });
|
|
715
|
+
continue;
|
|
716
|
+
}
|
|
717
|
+
if (tag === 'a' || tag === 'area' || images.length >= options.max)
|
|
718
|
+
continue;
|
|
719
|
+
const role = el.getAttribute('role');
|
|
720
|
+
if (tag === 'svg' && role !== 'img' && !el.hasAttribute('aria-label'))
|
|
721
|
+
continue;
|
|
722
|
+
// Decorative images, with an empty alt, are no one's content.
|
|
723
|
+
if (tag === 'img' && el.getAttribute('alt') === '')
|
|
724
|
+
continue;
|
|
725
|
+
const rect = shown(el);
|
|
726
|
+
if (!rect)
|
|
727
|
+
continue;
|
|
728
|
+
const title = tag === 'svg' ? el.querySelector(':scope > title') : null;
|
|
729
|
+
const alt = tag === 'img' || tag === 'input'
|
|
730
|
+
? clean(el.getAttribute('alt'))
|
|
731
|
+
: (clean(el.getAttribute('aria-label')) ??
|
|
732
|
+
byIds(el.getAttribute('aria-labelledby')) ??
|
|
733
|
+
clean(title?.textContent));
|
|
734
|
+
const src = tag === 'img'
|
|
735
|
+
? shortUrl(el.currentSrc || el.src)
|
|
736
|
+
: tag === 'input'
|
|
737
|
+
? shortUrl(el.src)
|
|
738
|
+
: undefined;
|
|
739
|
+
images.push({
|
|
740
|
+
...(alt !== undefined ? { alt } : {}),
|
|
741
|
+
...(src !== undefined ? { src } : {}),
|
|
742
|
+
width: Math.round(rect.width),
|
|
743
|
+
height: Math.round(rect.height),
|
|
744
|
+
});
|
|
745
|
+
}
|
|
746
|
+
return { images, links };
|
|
747
|
+
};
|
|
748
|
+
/**
|
|
749
|
+
* Whether `root` (an element, or a document's `<html>`) shows anything yet: visible text, or a
|
|
750
|
+
* visible image, video, canvas or field, itself included, in open shadow roots too. An `<iframe>`
|
|
751
|
+
* does not count: what it shows is its own document's. Seen as `findText` sees, with `opacity`
|
|
752
|
+
* that of the `<iframe>` its document is in. Before it shows something, an absence of text proves
|
|
753
|
+
* nothing.
|
|
754
|
+
*/
|
|
755
|
+
export const hasContent = (root, options) => {
|
|
756
|
+
const g = globalThis;
|
|
757
|
+
const top = root;
|
|
758
|
+
const doc = top.ownerDocument;
|
|
759
|
+
/*
|
|
760
|
+
* The element a node shows inside, as it renders (the composed tree): the slot it is assigned
|
|
761
|
+
* to, else its parent, else the host of the shadow root it is at the top of (or is). The same in every
|
|
762
|
+
* page function here; each must stand alone. Opacity and clipping follow it.
|
|
763
|
+
*/
|
|
764
|
+
const up = (node) => node.assignedSlot ?? node.parentElement ?? node.parentNode?.host ?? node.host ?? null;
|
|
765
|
+
const opacities = new Map();
|
|
766
|
+
const opacityOf = (el) => {
|
|
767
|
+
if (!el || el === doc.documentElement)
|
|
768
|
+
return options.opacity;
|
|
769
|
+
const known = opacities.get(el);
|
|
770
|
+
if (known !== undefined)
|
|
771
|
+
return known;
|
|
772
|
+
const own = Number.parseFloat(g.getComputedStyle(el).opacity);
|
|
773
|
+
const value = (Number.isFinite(own) ? own : 1) * opacityOf(up(el));
|
|
774
|
+
opacities.set(el, value);
|
|
775
|
+
return value;
|
|
776
|
+
};
|
|
777
|
+
const seen = (el) => {
|
|
778
|
+
const rect = el.getBoundingClientRect();
|
|
779
|
+
return (rect.width > 0 &&
|
|
780
|
+
rect.height > 0 &&
|
|
781
|
+
(typeof el.checkVisibility === 'function'
|
|
782
|
+
? el.checkVisibility({ visibilityProperty: true })
|
|
783
|
+
: g.getComputedStyle(el).visibility !== 'hidden') &&
|
|
784
|
+
opacityOf(el) >= 0.1);
|
|
785
|
+
};
|
|
786
|
+
const media = 'img, svg, video, canvas, picture, input, select, textarea, [role=img]';
|
|
787
|
+
if (top.matches?.(media) && seen(top))
|
|
788
|
+
return true;
|
|
789
|
+
// The root and every open shadow root inside it, each read on its own.
|
|
790
|
+
const scopes = [top];
|
|
791
|
+
let checked = 0;
|
|
792
|
+
for (let i = 0; i < scopes.length && checked < 20_000; i++) {
|
|
793
|
+
const scope = scopes[i];
|
|
794
|
+
if (scope.shadowRoot)
|
|
795
|
+
scopes.push(scope.shadowRoot);
|
|
796
|
+
const walker = doc.createTreeWalker(scope, 5);
|
|
797
|
+
for (let node = walker.nextNode(); node && checked < 20_000; node = walker.nextNode()) {
|
|
798
|
+
checked++;
|
|
799
|
+
if (node.nodeType === 1) {
|
|
800
|
+
if (node.shadowRoot)
|
|
801
|
+
scopes.push(node.shadowRoot);
|
|
802
|
+
if (node.matches(media) && seen(node))
|
|
803
|
+
return true;
|
|
804
|
+
}
|
|
805
|
+
else if (String(node.data).trim() !== '') {
|
|
806
|
+
// Seen through what it shows in, its slot included.
|
|
807
|
+
const holder = up(node);
|
|
808
|
+
if (holder && seen(holder))
|
|
809
|
+
return true;
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
return false;
|
|
814
|
+
};
|
|
815
|
+
/**
|
|
816
|
+
* Looks for `text` (case-insensitive, whitespace collapsed, as `getByText` matches) in what `root`
|
|
817
|
+
* shows, `root` itself included: its visible text, the alt text of its visible images (and the
|
|
818
|
+
* ARIA label or `<title>` of those with role img, such as an `<svg>` logo), and the ARIA labels of
|
|
819
|
+
* its visible elements. Open shadow roots are read as they render: a host shows its shadow root,
|
|
820
|
+
* a `<slot>` what is assigned to it. Visible as `captureState` judges it: with a size, not hidden,
|
|
821
|
+
* and not almost transparent with what is around it (`opacity` is that of the `<iframe>` the
|
|
822
|
+
* document is in). Text in frames inside it is not looked at here. When the document is bigger
|
|
823
|
+
* than it reads (`nodesMax` nodes of text, `labelsMax` labelled elements), it says so rather than
|
|
824
|
+
* "absent". `root` may be a shadow root itself: a closed one, which only CDP can hand over.
|
|
825
|
+
*/
|
|
826
|
+
export const findText = (root, options) => {
|
|
827
|
+
const g = globalThis;
|
|
828
|
+
const top = root;
|
|
829
|
+
const doc = top.ownerDocument;
|
|
830
|
+
const norm = (value) => String(value ?? '')
|
|
831
|
+
.replace(/\s+/g, ' ')
|
|
832
|
+
.trim()
|
|
833
|
+
.toLowerCase();
|
|
834
|
+
const want = norm(options.text);
|
|
835
|
+
if (want === '')
|
|
836
|
+
return { _tag: 'absent' };
|
|
837
|
+
const unread = new Set(['script', 'style', 'noscript', 'template', 'svg', 'iframe']);
|
|
838
|
+
/*
|
|
839
|
+
* The element a node shows inside, as it renders (the composed tree): the slot it is assigned
|
|
840
|
+
* to, else its parent, else the host of the shadow root it is at the top of (or is). The same in every
|
|
841
|
+
* page function here; each must stand alone. Opacity and clipping follow it.
|
|
842
|
+
*/
|
|
843
|
+
const up = (node) => node.assignedSlot ?? node.parentElement ?? node.parentNode?.host ?? node.host ?? null;
|
|
844
|
+
const opacities = new Map();
|
|
845
|
+
const opacityOf = (el) => {
|
|
846
|
+
if (!el || el === doc.documentElement)
|
|
847
|
+
return options.opacity;
|
|
848
|
+
const known = opacities.get(el);
|
|
849
|
+
if (known !== undefined)
|
|
850
|
+
return known;
|
|
851
|
+
const own = Number.parseFloat(g.getComputedStyle(el).opacity);
|
|
852
|
+
const value = (Number.isFinite(own) ? own : 1) * opacityOf(up(el));
|
|
853
|
+
opacities.set(el, value);
|
|
854
|
+
return value;
|
|
855
|
+
};
|
|
856
|
+
const SEEN = 0.1;
|
|
857
|
+
const visible = (el) => {
|
|
858
|
+
const rect = el.getBoundingClientRect();
|
|
859
|
+
return (rect.width > 0 &&
|
|
860
|
+
rect.height > 0 &&
|
|
861
|
+
(typeof el.checkVisibility === 'function'
|
|
862
|
+
? el.checkVisibility({ visibilityProperty: true })
|
|
863
|
+
: g.getComputedStyle(el).visibility !== 'hidden') &&
|
|
864
|
+
opacityOf(el) >= SEEN);
|
|
865
|
+
};
|
|
866
|
+
/** The part of `value` around where `want` is in it, at most `max` characters. */
|
|
867
|
+
const around = (value) => {
|
|
868
|
+
const flat = value.replace(/\s+/g, ' ').trim();
|
|
869
|
+
if (flat.length <= options.max)
|
|
870
|
+
return flat;
|
|
871
|
+
const at = Math.max(0, flat.toLowerCase().indexOf(want));
|
|
872
|
+
const start = Math.max(0, Math.min(at - 15, flat.length - options.max + 1));
|
|
873
|
+
const cut = flat.slice(start, start + options.max - 1);
|
|
874
|
+
return `${start > 0 ? '…' : ''}${cut}${start + options.max - 1 < flat.length ? '…' : ''}`;
|
|
875
|
+
};
|
|
876
|
+
/** The nearest element around `el` (itself included) a person would name the place by. */
|
|
877
|
+
const landmarks = new Set([
|
|
878
|
+
'header',
|
|
879
|
+
'footer',
|
|
880
|
+
'nav',
|
|
881
|
+
'main',
|
|
882
|
+
'aside',
|
|
883
|
+
'section',
|
|
884
|
+
'form',
|
|
885
|
+
'dialog',
|
|
886
|
+
]);
|
|
887
|
+
const ident = /^-?[A-Za-z_][\w-]*$/;
|
|
888
|
+
const nearOf = (el) => {
|
|
889
|
+
for (let at = el, depth = 0; at && depth < 8; at = up(at), depth++) {
|
|
890
|
+
const tag = String(at.tagName).toLowerCase();
|
|
891
|
+
if (tag === 'body' || tag === 'html')
|
|
892
|
+
return undefined;
|
|
893
|
+
if (at.id && ident.test(at.id))
|
|
894
|
+
return `#${at.id}`;
|
|
895
|
+
const testId = at.getAttribute('data-testid');
|
|
896
|
+
if (testId)
|
|
897
|
+
return `[data-testid="${String(testId).slice(0, 40)}"]`;
|
|
898
|
+
const name = Array.from(at.classList).find((c) => ident.test(c) && c.length <= 30);
|
|
899
|
+
if (name !== undefined)
|
|
900
|
+
return `.${name}`;
|
|
901
|
+
if (landmarks.has(tag))
|
|
902
|
+
return tag;
|
|
903
|
+
}
|
|
904
|
+
return undefined;
|
|
905
|
+
};
|
|
906
|
+
/** The nodes an element shows, as it renders: its open shadow root's, a slot's assigned ones. */
|
|
907
|
+
const shownChildren = (node) => {
|
|
908
|
+
if (node.shadowRoot)
|
|
909
|
+
return node.shadowRoot.childNodes;
|
|
910
|
+
if (node.localName === 'slot' && typeof node.assignedNodes === 'function') {
|
|
911
|
+
const assigned = node.assignedNodes({ flatten: true });
|
|
912
|
+
return assigned.length > 0 ? assigned : node.childNodes;
|
|
913
|
+
}
|
|
914
|
+
return node.childNodes;
|
|
915
|
+
};
|
|
916
|
+
// Visible text, read the way it reads (as in `captureState`), each character with its node.
|
|
917
|
+
const chars = [];
|
|
918
|
+
const owners = [];
|
|
919
|
+
const push = (value, owner) => {
|
|
920
|
+
for (const ch of value) {
|
|
921
|
+
if (/\s/.test(ch)) {
|
|
922
|
+
if (chars.length > 0 && chars[chars.length - 1] !== ' ') {
|
|
923
|
+
chars.push(' ');
|
|
924
|
+
owners.push(owner);
|
|
925
|
+
}
|
|
926
|
+
continue;
|
|
927
|
+
}
|
|
928
|
+
for (const lower of ch.toLowerCase()) {
|
|
929
|
+
chars.push(lower);
|
|
930
|
+
owners.push(owner);
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
};
|
|
934
|
+
let budget = options.nodesMax;
|
|
935
|
+
const walk = (node, shown, opacity) => {
|
|
936
|
+
if (--budget < 0)
|
|
937
|
+
return;
|
|
938
|
+
if (node.nodeType === 3) {
|
|
939
|
+
if (shown && opacity >= SEEN)
|
|
940
|
+
push(String(node.data), node);
|
|
941
|
+
return;
|
|
942
|
+
}
|
|
943
|
+
// A shadow root handed in itself (a closed one, see `watchText`): what it holds, as its host.
|
|
944
|
+
if (node.nodeType === 11) {
|
|
945
|
+
for (const child of Array.from(node.childNodes))
|
|
946
|
+
walk(child, shown, opacity);
|
|
947
|
+
return;
|
|
948
|
+
}
|
|
949
|
+
if (node.nodeType !== 1 || unread.has(node.localName))
|
|
950
|
+
return;
|
|
951
|
+
if (node.localName === 'br')
|
|
952
|
+
return push(' ', node);
|
|
953
|
+
const style = g.getComputedStyle(node);
|
|
954
|
+
const contents = style.display === 'contents';
|
|
955
|
+
if (!contents && (node.checkVisibility ? !node.checkVisibility() : style.display === 'none'))
|
|
956
|
+
return;
|
|
957
|
+
const own = Number.parseFloat(style.opacity);
|
|
958
|
+
const through = opacity * (Number.isFinite(own) ? own : 1);
|
|
959
|
+
// Opacity only falls further down: nothing inside is seen either.
|
|
960
|
+
if (through < SEEN)
|
|
961
|
+
return;
|
|
962
|
+
const apart = style.display !== 'inline' && !contents;
|
|
963
|
+
if (apart)
|
|
964
|
+
push(' ', node);
|
|
965
|
+
for (const child of Array.from(shownChildren(node)))
|
|
966
|
+
walk(child, style.visibility === 'visible', through);
|
|
967
|
+
if (apart)
|
|
968
|
+
push(' ', node);
|
|
969
|
+
};
|
|
970
|
+
const start = top.localName === 'html' ? (doc.body ?? top) : top;
|
|
971
|
+
// A shadow root shows as its host does: text right inside it is hidden when the host is.
|
|
972
|
+
const host = start.nodeType === 11 ? start.host : undefined;
|
|
973
|
+
walk(start, host ? g.getComputedStyle(host).visibility === 'visible' : true, opacityOf(up(start)));
|
|
974
|
+
const at = chars.join('').indexOf(want);
|
|
975
|
+
if (at >= 0) {
|
|
976
|
+
const owner = owners[at];
|
|
977
|
+
const first = owner.nodeType === 1 ? owner : up(owner);
|
|
978
|
+
// Up to the element whose own text holds all of it, when it runs across several.
|
|
979
|
+
let holder = first;
|
|
980
|
+
for (let el = first, step = 0; el && step < 6; el = up(el), step++)
|
|
981
|
+
if (norm(el.innerText).includes(want)) {
|
|
982
|
+
holder = el;
|
|
983
|
+
break;
|
|
984
|
+
}
|
|
985
|
+
const text = String(holder?.innerText ?? '');
|
|
986
|
+
const value = around(norm(text).includes(want) ? text : options.text);
|
|
987
|
+
const near = holder ? nearOf(holder) : undefined;
|
|
988
|
+
return { _tag: 'found', as: 'text', value, ...(near !== undefined ? { near } : {}) };
|
|
989
|
+
}
|
|
990
|
+
if (budget < 0)
|
|
991
|
+
return {
|
|
992
|
+
_tag: 'incomplete',
|
|
993
|
+
reason: `it holds more than ${options.nodesMax} elements and text nodes, more than are read`,
|
|
994
|
+
};
|
|
995
|
+
// Alt text and labels, which hold no text of their own; the root's own, and in shadow roots.
|
|
996
|
+
const byIds = (el, ids) => {
|
|
997
|
+
if (typeof ids !== 'string')
|
|
998
|
+
return '';
|
|
999
|
+
const where = el.getRootNode?.() ?? doc;
|
|
1000
|
+
return ids
|
|
1001
|
+
.split(/\s+/)
|
|
1002
|
+
.map((id) => String(
|
|
1003
|
+
// Called on what holds it, so it keeps its receiver: a shadow root, else the document.
|
|
1004
|
+
((typeof where.getElementById === 'function' ? where.getElementById(id) : null) ??
|
|
1005
|
+
doc.getElementById(id))?.textContent ?? ''))
|
|
1006
|
+
.join(' ');
|
|
1007
|
+
};
|
|
1008
|
+
const selector = 'img[alt], area[alt], input[type=image][alt], [aria-label], [aria-labelledby], svg';
|
|
1009
|
+
const labelled = top.matches?.(selector) ? [top] : [];
|
|
1010
|
+
// The root, then every open shadow root found in what was read so far.
|
|
1011
|
+
const roots = [top, ...(top.shadowRoot ? [top.shadowRoot] : [])];
|
|
1012
|
+
for (let i = 0; i < roots.length; i++) {
|
|
1013
|
+
for (const el of Array.from(roots[i].querySelectorAll('*'))) {
|
|
1014
|
+
if (el.shadowRoot)
|
|
1015
|
+
roots.push(el.shadowRoot);
|
|
1016
|
+
if (el.matches(selector))
|
|
1017
|
+
labelled.push(el);
|
|
1018
|
+
}
|
|
1019
|
+
if (labelled.length > options.labelsMax)
|
|
1020
|
+
return {
|
|
1021
|
+
_tag: 'incomplete',
|
|
1022
|
+
reason: `it holds more than the ${options.labelsMax} images and labelled elements that are read`,
|
|
1023
|
+
};
|
|
1024
|
+
}
|
|
1025
|
+
for (const el of labelled) {
|
|
1026
|
+
const tag = String(el.localName);
|
|
1027
|
+
const image = tag === 'img' ||
|
|
1028
|
+
tag === 'area' ||
|
|
1029
|
+
tag === 'input' ||
|
|
1030
|
+
tag === 'svg' ||
|
|
1031
|
+
el.getAttribute('role') === 'img';
|
|
1032
|
+
const title = tag === 'svg' ? el.querySelector(':scope > title') : null;
|
|
1033
|
+
const value = tag === 'img' || tag === 'area' || tag === 'input'
|
|
1034
|
+
? String(el.getAttribute('alt') ?? '')
|
|
1035
|
+
: String(el.getAttribute('aria-label') ?? '') ||
|
|
1036
|
+
byIds(el, el.getAttribute('aria-labelledby')) ||
|
|
1037
|
+
String(title?.textContent ?? '');
|
|
1038
|
+
if (!norm(value).includes(want) || !visible(el))
|
|
1039
|
+
continue;
|
|
1040
|
+
const near = nearOf(up(el) ?? el);
|
|
1041
|
+
return {
|
|
1042
|
+
_tag: 'found',
|
|
1043
|
+
as: image ? 'alt' : 'label',
|
|
1044
|
+
value: around(value),
|
|
1045
|
+
...(near !== undefined ? { near } : {}),
|
|
1046
|
+
};
|
|
1047
|
+
}
|
|
1048
|
+
return { _tag: 'absent' };
|
|
1049
|
+
};
|