shapewall 0.0.0-stage → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +139 -0
- package/Dockerfile.agent +45 -0
- package/LICENSE +202 -0
- package/NOTICE +12 -0
- package/README.md +651 -2
- package/THIRD_PARTY.md +165 -0
- package/bin/shapewall.js +29 -0
- package/dist/agent/package-lock.json +8384 -0
- package/dist/client/assets/BoardChat-D5SFd8HE.js +28 -0
- package/dist/client/assets/BuzzDialog-HWXBy52W.js +1 -0
- package/dist/client/assets/ChecksPanel-DTdImWoD.js +1 -0
- package/dist/client/assets/History-CEv6b-av.js +1 -0
- package/dist/client/assets/LocalSettings-C4vriEVQ.js +1 -0
- package/dist/client/assets/Management-DCfhcEUY.js +1 -0
- package/dist/client/assets/QuickConnect-DQGgUsl1.js +1 -0
- package/dist/client/assets/ShareDialog-C_plIto0.js +2 -0
- package/dist/client/assets/app-update-dialog-2hPz_N9G.js +1 -0
- package/dist/client/assets/circle-x-BZMPi8Zf.js +1 -0
- package/dist/client/assets/index-ByQRKVit.css +2 -0
- package/dist/client/assets/index-DweQ8Qtu.js +29 -0
- package/dist/client/assets/refresh-cw-CRow0Nbq.js +1 -0
- package/dist/client/assets/runtime-Bft6x_6E.js +4 -0
- package/dist/client/assets/square-A8EnIYLR.js +1 -0
- package/dist/client/assets/tts-dist-DYlBQmwG.js +18 -0
- package/dist/client/assets/tts-en-g2p-BPUtBFGG.js +1 -0
- package/dist/client/assets/tts-ort-wasm-simd-threaded-qh98TCA_.wasm +0 -0
- package/dist/client/assets/tts-ort.wasm.bundle.min-rEkcs_ow.js +12 -0
- package/dist/client/assets/tts-rolldown-runtime-D8NudN84.js +1 -0
- package/dist/client/assets/tts-spacy-en-tokenizer-6SNMl--A.json +1 -0
- package/dist/client/assets/tts-tts.worker-Bzm6aWbv.js +1 -0
- package/dist/client/assets/tts-us_gold-DcjeNMH1.json +92589 -0
- package/dist/client/assets/tts-us_silver-C964mEB5.json +93363 -0
- package/dist/client/assets/ui-CLbws0rx.js +41 -0
- package/dist/client/build-info.json +1 -0
- package/dist/client/favicon.svg +7 -0
- package/dist/client/icons/apple-touch-icon.png +0 -0
- package/dist/client/icons/icon-192.png +0 -0
- package/dist/client/icons/icon-512.png +0 -0
- package/dist/client/icons/maskable-512.png +0 -0
- package/dist/client/index.html +25 -0
- package/dist/client/manifest.webmanifest +15 -0
- package/dist/client/sw.js +53 -0
- package/dist/client/theme-boot.js +27 -0
- package/dist/console/assets/LocalAccountsApp-BQCcmj3x.js +1 -0
- package/dist/console/assets/LocalConsole-Bb2FUDs7.js +1 -0
- package/dist/console/assets/app-update-dialog-DIeYepLZ.js +1 -0
- package/dist/console/assets/console-C7uJLLOn.js +51 -0
- package/dist/console/assets/console-R8IBY3Am.css +2 -0
- package/dist/console/build-info.json +1 -0
- package/dist/console/console.html +21 -0
- package/dist/console/favicon.svg +7 -0
- package/dist/console/icons/apple-touch-icon.png +0 -0
- package/dist/console/icons/icon-192.png +0 -0
- package/dist/console/icons/icon-512.png +0 -0
- package/dist/console/icons/maskable-512.png +0 -0
- package/dist/console/manifest.webmanifest +15 -0
- package/dist/console/theme-boot.js +27 -0
- package/docs/agent-hardening.md +342 -0
- package/docs/agent-recipes.md +187 -0
- package/docs/agent-workflow.md +320 -0
- package/docs/agents.md +894 -0
- package/docs/architecture.md +242 -0
- package/docs/buzz.md +140 -0
- package/docs/capture-warnings.md +57 -0
- package/docs/config.md +489 -0
- package/docs/deployment.md +289 -0
- package/docs/design-system.md +162 -0
- package/docs/github.md +107 -0
- package/docs/hosted.md +764 -0
- package/docs/images/board-overview.png +0 -0
- package/docs/images/journeys.png +0 -0
- package/docs/images/live-comment.png +0 -0
- package/docs/images/phone-comment.png +0 -0
- package/docs/images/phone-overview.png +0 -0
- package/docs/images/variant.png +0 -0
- package/docs/interaction-audits.md +40 -0
- package/docs/journeys.md +137 -0
- package/docs/local-accounts.md +80 -0
- package/docs/notifications.md +237 -0
- package/docs/page-checks.md +69 -0
- package/docs/performance.md +174 -0
- package/docs/starter-agent.md +325 -0
- package/docs/updates.md +87 -0
- package/examples/audits/interaction.audit.json +17 -0
- package/package.json +132 -4
- package/src/accounts/avatar.js +121 -0
- package/src/accounts/local.js +464 -0
- package/src/agent/acp/command.js +204 -0
- package/src/agent/acp/detect.js +75 -0
- package/src/agent/acp/sandbox.js +160 -0
- package/src/agent/agent-health.js +75 -0
- package/src/agent/backends/acp.js +1297 -0
- package/src/agent/backends/index.js +61 -0
- package/src/agent/backends/openai.js +308 -0
- package/src/agent/composer.js +221 -0
- package/src/agent/config.js +338 -0
- package/src/agent/context.js +191 -0
- package/src/agent/control.js +122 -0
- package/src/agent/errors.js +17 -0
- package/src/agent/feedback-loop.js +137 -0
- package/src/agent/manage.js +94 -0
- package/src/agent/next-steps.js +51 -0
- package/src/agent/owner-mcp.js +38 -0
- package/src/agent/plain-text.js +52 -0
- package/src/agent/ratings-core.js +213 -0
- package/src/agent/ratings.js +100 -0
- package/src/agent/reply-text.js +67 -0
- package/src/agent/routes.js +1065 -0
- package/src/agent/run.js +326 -0
- package/src/agent/settings-routes.js +704 -0
- package/src/agent/showable.js +23 -0
- package/src/agent/starter/approvals.js +181 -0
- package/src/agent/starter/build-image.js +90 -0
- package/src/agent/starter/cli.js +163 -0
- package/src/agent/starter/disk.js +53 -0
- package/src/agent/starter/doctor.js +180 -0
- package/src/agent/starter/egress-proxy.js +351 -0
- package/src/agent/starter/gate.js +90 -0
- package/src/agent/starter/loop.js +114 -0
- package/src/agent/starter/model-relay.js +289 -0
- package/src/agent/starter/pause.js +14 -0
- package/src/agent/starter/probe.js +272 -0
- package/src/agent/starter/prompt.js +41 -0
- package/src/agent/starter/push-guard.js +146 -0
- package/src/agent/starter/rpc.js +164 -0
- package/src/agent/starter/runtime.js +287 -0
- package/src/agent/starter/sandbox-main.js +121 -0
- package/src/agent/starter/settings.js +290 -0
- package/src/agent/starter/source.js +69 -0
- package/src/agent/starter/supervisor.js +755 -0
- package/src/agent/starter/sweep.js +46 -0
- package/src/agent/starter/tools.js +524 -0
- package/src/agent/starter/watch.js +84 -0
- package/src/agent/starter/worktree.js +409 -0
- package/src/agent/text-diff.js +210 -0
- package/src/agent/thread-history.js +223 -0
- package/src/agent/threads.js +399 -0
- package/src/agent/tools.js +200 -0
- package/src/agent/turn-changes.js +424 -0
- package/src/agent/undo-state.js +26 -0
- package/src/agent/user-settings.js +308 -0
- package/src/auth/record.js +375 -0
- package/src/broken-links.js +164 -0
- package/src/buzz/client.js +75 -0
- package/src/buzz/config.js +151 -0
- package/src/buzz/environment.js +13 -0
- package/src/buzz/gateway.js +192 -0
- package/src/buzz/routes.js +61 -0
- package/src/buzz/signer-child.js +205 -0
- package/src/buzz/store.js +89 -0
- package/src/buzz/transport.js +60 -0
- package/src/buzz/verify.js +140 -0
- package/src/capture/actions.js +159 -0
- package/src/capture/capture.js +776 -0
- package/src/capture/dom-names.js +189 -0
- package/src/capture/element-map.js +509 -0
- package/src/capture/live.js +827 -0
- package/src/capture/media-scheme.js +317 -0
- package/src/capture/network-summary.js +86 -0
- package/src/capture/pacing.js +34 -0
- package/src/capture/requests.js +167 -0
- package/src/capture/sanitize.js +752 -0
- package/src/checks/accessibility.js +134 -0
- package/src/checks/budgets.js +58 -0
- package/src/checks/cli.js +55 -0
- package/src/checks/comparison.js +113 -0
- package/src/checks/environments.js +32 -0
- package/src/checks/flow-config.js +82 -0
- package/src/checks/flow-origin.js +53 -0
- package/src/checks/flows.js +398 -0
- package/src/checks/lighthouse-auth.js +145 -0
- package/src/checks/lighthouse-findings.js +130 -0
- package/src/checks/lighthouse.js +395 -0
- package/src/checks/robots.js +85 -0
- package/src/checks/routes.js +52 -0
- package/src/checks/run-control.js +259 -0
- package/src/checks/runner.js +724 -0
- package/src/checks/schema.js +133 -0
- package/src/checks/seo-analysis.js +443 -0
- package/src/checks/seo-probe.js +190 -0
- package/src/checks/seo-snapshot.js +99 -0
- package/src/checks/store.js +214 -0
- package/src/checks/transport.js +47 -0
- package/src/cli/args.js +319 -0
- package/src/cli/capture.js +79 -0
- package/src/cli/commands.js +92 -0
- package/src/cli/comments.js +84 -0
- package/src/cli/help.js +243 -0
- package/src/cli/init.js +214 -0
- package/src/cli/misc.js +64 -0
- package/src/cli/notify.js +87 -0
- package/src/cli/open.js +22 -0
- package/src/cli/serve.js +274 -0
- package/src/cli/variant.js +82 -0
- package/src/cli/watch.js +70 -0
- package/src/cli.js +130 -0
- package/src/comment-fields.js +76 -0
- package/src/comments-cli.js +208 -0
- package/src/config-reload.js +100 -0
- package/src/config.js +1061 -0
- package/src/devices-cli.js +21 -0
- package/src/diff.js +95 -0
- package/src/discover.js +284 -0
- package/src/doctor.js +295 -0
- package/src/event-log.js +268 -0
- package/src/events-cli.js +102 -0
- package/src/events.js +113 -0
- package/src/export.js +119 -0
- package/src/feedback.js +246 -0
- package/src/github.js +551 -0
- package/src/hosted/LICENSE.md +105 -0
- package/src/hosted/access-cli.js +78 -0
- package/src/hosted/admin.js +682 -0
- package/src/hosted/agent-approvals.js +163 -0
- package/src/hosted/agent-policy.js +189 -0
- package/src/hosted/app.js +510 -0
- package/src/hosted/auth-routes.js +540 -0
- package/src/hosted/auth.js +536 -0
- package/src/hosted/backup.js +483 -0
- package/src/hosted/board-context.js +35 -0
- package/src/hosted/board-routes.js +486 -0
- package/src/hosted/board-view.js +94 -0
- package/src/hosted/clean-worker.js +24 -0
- package/src/hosted/cleaner.js +89 -0
- package/src/hosted/comments.js +355 -0
- package/src/hosted/console-routes.js +401 -0
- package/src/hosted/cookies.js +18 -0
- package/src/hosted/crypto.js +80 -0
- package/src/hosted/db.js +496 -0
- package/src/hosted/device-routes.js +175 -0
- package/src/hosted/device.js +233 -0
- package/src/hosted/events.js +63 -0
- package/src/hosted/guards.js +172 -0
- package/src/hosted/guest-view.js +65 -0
- package/src/hosted/http.js +39 -0
- package/src/hosted/import.js +300 -0
- package/src/hosted/mail.js +73 -0
- package/src/hosted/mcp-http.js +71 -0
- package/src/hosted/membership.js +114 -0
- package/src/hosted/operator.js +478 -0
- package/src/hosted/ops-cli.js +345 -0
- package/src/hosted/pages.js +146 -0
- package/src/hosted/permissions.js +113 -0
- package/src/hosted/presence.js +372 -0
- package/src/hosted/projects.js +124 -0
- package/src/hosted/public.js +109 -0
- package/src/hosted/push.js +622 -0
- package/src/hosted/quotas.js +115 -0
- package/src/hosted/ratelimit.js +96 -0
- package/src/hosted/rebind.js +316 -0
- package/src/hosted/retention.js +128 -0
- package/src/hosted/server.js +260 -0
- package/src/hosted/setup-link.js +64 -0
- package/src/hosted/setup.js +323 -0
- package/src/hosted/shares.js +444 -0
- package/src/hosted/tailscale.js +204 -0
- package/src/hosted/tenancy.js +301 -0
- package/src/hosted/token-api.js +455 -0
- package/src/hosted/token-events.js +60 -0
- package/src/hosted/tokens.js +57 -0
- package/src/hosted/turnstile.js +49 -0
- package/src/hosted/usercontent.js +154 -0
- package/src/hosted/zip.js +155 -0
- package/src/invocation.js +58 -0
- package/src/journey-paths.js +282 -0
- package/src/journeys.js +189 -0
- package/src/links.js +106 -0
- package/src/local-projects.js +182 -0
- package/src/lock.js +193 -0
- package/src/mcp.js +417 -0
- package/src/mentions.js +151 -0
- package/src/net.js +15 -0
- package/src/notify/config.js +275 -0
- package/src/notify/index.js +301 -0
- package/src/notify/lease.js +73 -0
- package/src/notify/messages.js +212 -0
- package/src/notify/run.js +174 -0
- package/src/pixeldiff.js +49 -0
- package/src/png.js +143 -0
- package/src/reactions.js +111 -0
- package/src/remote/cli.js +133 -0
- package/src/remote/credentials.js +62 -0
- package/src/remote/ids.js +12 -0
- package/src/remote/login.js +155 -0
- package/src/remote/remote-mcp.js +237 -0
- package/src/remote/remote-watch.js +291 -0
- package/src/remote/remote.js +313 -0
- package/src/remote/share-state.js +78 -0
- package/src/remote/share.js +369 -0
- package/src/remote/terminal.js +13 -0
- package/src/serve-state.js +64 -0
- package/src/server/app.js +824 -0
- package/src/server/devices.js +186 -0
- package/src/server/local-console.js +28 -0
- package/src/server/local-user.js +59 -0
- package/src/server/pairing-routes.js +159 -0
- package/src/server/share-routes.js +89 -0
- package/src/server/tailscale.js +97 -0
- package/src/sitemap.js +431 -0
- package/src/store.js +983 -0
- package/src/tokens.js +11 -0
- package/src/update/routes.js +245 -0
- package/src/watch.js +403 -0
- package/src/which.js +58 -0
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
// Names an id must not take in live HTML (DOM clobbering): the methods of
|
|
2
|
+
// Document and HTMLFormElement (and what they inherit), and the document
|
|
3
|
+
// properties the board reads. A named element could otherwise shadow them for
|
|
4
|
+
// code that reads the frame's DOM. Generated from Chromium's prototypes.
|
|
5
|
+
export const CLOBBER_NAMES = new Set([
|
|
6
|
+
'URL',
|
|
7
|
+
'__defineGetter__',
|
|
8
|
+
'__defineSetter__',
|
|
9
|
+
'__lookupGetter__',
|
|
10
|
+
'__lookupSetter__',
|
|
11
|
+
'__proto__',
|
|
12
|
+
'action',
|
|
13
|
+
'activeElement',
|
|
14
|
+
'addEventListener',
|
|
15
|
+
'adoptNode',
|
|
16
|
+
'after',
|
|
17
|
+
'all',
|
|
18
|
+
'animate',
|
|
19
|
+
'append',
|
|
20
|
+
'appendChild',
|
|
21
|
+
'ariaNotify',
|
|
22
|
+
'attachInternals',
|
|
23
|
+
'attachShadow',
|
|
24
|
+
'baseURI',
|
|
25
|
+
'before',
|
|
26
|
+
'blur',
|
|
27
|
+
'body',
|
|
28
|
+
'captureEvents',
|
|
29
|
+
'caretPositionFromPoint',
|
|
30
|
+
'caretRangeFromPoint',
|
|
31
|
+
'checkValidity',
|
|
32
|
+
'checkVisibility',
|
|
33
|
+
'childNodes',
|
|
34
|
+
'children',
|
|
35
|
+
'clear',
|
|
36
|
+
'click',
|
|
37
|
+
'cloneNode',
|
|
38
|
+
'close',
|
|
39
|
+
'closest',
|
|
40
|
+
'compareDocumentPosition',
|
|
41
|
+
'computedStyleMap',
|
|
42
|
+
'constructor',
|
|
43
|
+
'contains',
|
|
44
|
+
'cookie',
|
|
45
|
+
'createAttribute',
|
|
46
|
+
'createAttributeNS',
|
|
47
|
+
'createCDATASection',
|
|
48
|
+
'createComment',
|
|
49
|
+
'createDocumentFragment',
|
|
50
|
+
'createElement',
|
|
51
|
+
'createElementNS',
|
|
52
|
+
'createEvent',
|
|
53
|
+
'createExpression',
|
|
54
|
+
'createNSResolver',
|
|
55
|
+
'createNodeIterator',
|
|
56
|
+
'createProcessingInstruction',
|
|
57
|
+
'createRange',
|
|
58
|
+
'createTextNode',
|
|
59
|
+
'createTreeWalker',
|
|
60
|
+
'defaultView',
|
|
61
|
+
'dispatchEvent',
|
|
62
|
+
'documentElement',
|
|
63
|
+
'domain',
|
|
64
|
+
'elementFromPoint',
|
|
65
|
+
'elements',
|
|
66
|
+
'elementsFromPoint',
|
|
67
|
+
'evaluate',
|
|
68
|
+
'execCommand',
|
|
69
|
+
'exitFullscreen',
|
|
70
|
+
'exitPictureInPicture',
|
|
71
|
+
'exitPointerLock',
|
|
72
|
+
'firstChild',
|
|
73
|
+
'focus',
|
|
74
|
+
'fonts',
|
|
75
|
+
'forms',
|
|
76
|
+
'getAnimations',
|
|
77
|
+
'getAttribute',
|
|
78
|
+
'getAttributeNS',
|
|
79
|
+
'getAttributeNames',
|
|
80
|
+
'getAttributeNode',
|
|
81
|
+
'getAttributeNodeNS',
|
|
82
|
+
'getBoundingClientRect',
|
|
83
|
+
'getClientRects',
|
|
84
|
+
'getElementById',
|
|
85
|
+
'getElementsByClassName',
|
|
86
|
+
'getElementsByName',
|
|
87
|
+
'getElementsByTagName',
|
|
88
|
+
'getElementsByTagNameNS',
|
|
89
|
+
'getHTML',
|
|
90
|
+
'getRootNode',
|
|
91
|
+
'getSelection',
|
|
92
|
+
'hasAttribute',
|
|
93
|
+
'hasAttributeNS',
|
|
94
|
+
'hasAttributes',
|
|
95
|
+
'hasChildNodes',
|
|
96
|
+
'hasFocus',
|
|
97
|
+
'hasOwnProperty',
|
|
98
|
+
'hasPointerCapture',
|
|
99
|
+
'hasStorageAccess',
|
|
100
|
+
'hasUnpartitionedCookieAccess',
|
|
101
|
+
'head',
|
|
102
|
+
'hidePopover',
|
|
103
|
+
'images',
|
|
104
|
+
'implementation',
|
|
105
|
+
'importNode',
|
|
106
|
+
'insertAdjacentElement',
|
|
107
|
+
'insertAdjacentHTML',
|
|
108
|
+
'insertAdjacentText',
|
|
109
|
+
'insertBefore',
|
|
110
|
+
'isDefaultNamespace',
|
|
111
|
+
'isEqualNode',
|
|
112
|
+
'isPrototypeOf',
|
|
113
|
+
'isSameNode',
|
|
114
|
+
'lastChild',
|
|
115
|
+
'length',
|
|
116
|
+
'links',
|
|
117
|
+
'location',
|
|
118
|
+
'lookupNamespaceURI',
|
|
119
|
+
'lookupPrefix',
|
|
120
|
+
'matches',
|
|
121
|
+
'method',
|
|
122
|
+
'moveBefore',
|
|
123
|
+
'nodeName',
|
|
124
|
+
'nodeType',
|
|
125
|
+
'normalize',
|
|
126
|
+
'open',
|
|
127
|
+
'ownerDocument',
|
|
128
|
+
'parentElement',
|
|
129
|
+
'parentNode',
|
|
130
|
+
'prepend',
|
|
131
|
+
'propertyIsEnumerable',
|
|
132
|
+
'prototype',
|
|
133
|
+
'pseudo',
|
|
134
|
+
'queryCommandEnabled',
|
|
135
|
+
'queryCommandIndeterm',
|
|
136
|
+
'queryCommandState',
|
|
137
|
+
'queryCommandSupported',
|
|
138
|
+
'queryCommandValue',
|
|
139
|
+
'querySelector',
|
|
140
|
+
'querySelectorAll',
|
|
141
|
+
'readyState',
|
|
142
|
+
'releaseEvents',
|
|
143
|
+
'releasePointerCapture',
|
|
144
|
+
'remove',
|
|
145
|
+
'removeAttribute',
|
|
146
|
+
'removeAttributeNS',
|
|
147
|
+
'removeAttributeNode',
|
|
148
|
+
'removeChild',
|
|
149
|
+
'removeEventListener',
|
|
150
|
+
'replaceChild',
|
|
151
|
+
'replaceChildren',
|
|
152
|
+
'replaceWith',
|
|
153
|
+
'reportValidity',
|
|
154
|
+
'requestFullscreen',
|
|
155
|
+
'requestPointerLock',
|
|
156
|
+
'requestStorageAccess',
|
|
157
|
+
'requestSubmit',
|
|
158
|
+
'reset',
|
|
159
|
+
'scroll',
|
|
160
|
+
'scrollBy',
|
|
161
|
+
'scrollIntoView',
|
|
162
|
+
'scrollIntoViewIfNeeded',
|
|
163
|
+
'scrollTo',
|
|
164
|
+
'scrollingElement',
|
|
165
|
+
'setAttribute',
|
|
166
|
+
'setAttributeNS',
|
|
167
|
+
'setAttributeNode',
|
|
168
|
+
'setAttributeNodeNS',
|
|
169
|
+
'setHTML',
|
|
170
|
+
'setHTMLUnsafe',
|
|
171
|
+
'setPointerCapture',
|
|
172
|
+
'showPopover',
|
|
173
|
+
'startViewTransition',
|
|
174
|
+
'styleSheets',
|
|
175
|
+
'submit',
|
|
176
|
+
'toLocaleString',
|
|
177
|
+
'toString',
|
|
178
|
+
'toggleAttribute',
|
|
179
|
+
'togglePopover',
|
|
180
|
+
'valueOf',
|
|
181
|
+
'webkitCancelFullScreen',
|
|
182
|
+
'webkitExitFullscreen',
|
|
183
|
+
'webkitMatchesSelector',
|
|
184
|
+
'webkitRequestFullScreen',
|
|
185
|
+
'webkitRequestFullscreen',
|
|
186
|
+
'when',
|
|
187
|
+
'write',
|
|
188
|
+
'writeln',
|
|
189
|
+
])
|
|
@@ -0,0 +1,509 @@
|
|
|
1
|
+
// Element maps. At capture, after the live HTML is written, the same
|
|
2
|
+
// document is rendered once more in a plain (non-mobile) page at the
|
|
3
|
+
// capture's native size, which is exactly how the board's frame renders it,
|
|
4
|
+
// and every element in that frame is written to
|
|
5
|
+
// <page>.<viewport>.elements.json: where it is, what it says, how a selector
|
|
6
|
+
// reaches it, and which element a click on it means.
|
|
7
|
+
//
|
|
8
|
+
// The board then never reads the live frame's DOM: it hit-tests clicks on the
|
|
9
|
+
// map, places pins from it and re-resolves anchors on it after a recapture,
|
|
10
|
+
// so the frame can run with an opaque origin (sandbox="" without
|
|
11
|
+
// allow-same-origin). This file has no Node or DOM imports: the capture
|
|
12
|
+
// (Node), the hosted push validation (Node) and the board (browser) share it.
|
|
13
|
+
//
|
|
14
|
+
// Map format, version 1:
|
|
15
|
+
// { v: 1, width, height, elements: [element, ...], truncated?: true }
|
|
16
|
+
// Elements are in document order, ancestors before descendants, starting at
|
|
17
|
+
// <body>. Each one:
|
|
18
|
+
// t tag name (lower case) n its :nth-of-type() index
|
|
19
|
+
// p parent's index (-1 for <body>) b [left, top, width, height] in CSS px
|
|
20
|
+
// h its selector step when a unique #id / data-testid / aria-label hook exists
|
|
21
|
+
// i, d, r its id, data-testid, aria-label (for matching stored selectors)
|
|
22
|
+
// x its text (whitespace collapsed, first 80 characters); l: 1 when longer
|
|
23
|
+
// z hit order (higher paints on top); absent when a click never lands on it
|
|
24
|
+
// c its box clipped by scrolling/hidden ancestors, when that differs from b
|
|
25
|
+
// a the element a click on it means (nearest link, button, ...); -1: none
|
|
26
|
+
|
|
27
|
+
export const ELEMENT_MAP_VERSION = 1
|
|
28
|
+
export const ELEMENT_MAP_LIMITS = {
|
|
29
|
+
elements: 8000,
|
|
30
|
+
bytes: 3 * 1024 * 1024,
|
|
31
|
+
text: 80,
|
|
32
|
+
attr: 200,
|
|
33
|
+
selector: 600,
|
|
34
|
+
size: 10000,
|
|
35
|
+
}
|
|
36
|
+
// Taps land on the element people mean: a button rather than its inner span.
|
|
37
|
+
export const PREFERRED =
|
|
38
|
+
'a, button, input, select, textarea, label, summary, [role="button"], [role="link"], [role="tab"], [role="menuitem"], img'
|
|
39
|
+
|
|
40
|
+
// The only selectors an anchor may hold. Steps are `#id` (CSS-escaped),
|
|
41
|
+
// `tag[data-testid="..."]`, `tag[aria-label="..."]` or `tag:nth-of-type(n)`,
|
|
42
|
+
// joined by ` > `. The server (src/store.js) checks the same grammar.
|
|
43
|
+
const ID_BODY = String.raw`(?:[A-Za-z0-9_\u0080-\uffff-]|\\[0-9a-fA-F]{6} ?|\\[0-9a-fA-F]{1,5}(?![0-9a-fA-F]) ?|\\[^0-9a-fA-F\n\r\f])+`
|
|
44
|
+
const ATTR_VALUE = String.raw`(?:[^"\\\n]|\\.){0,200}`
|
|
45
|
+
const ANCHOR_STEP = String.raw`(?:#${ID_BODY}|[a-z][a-z0-9-]{0,40}(?:\[(?:data-testid|aria-label)="${ATTR_VALUE}"\]|:nth-of-type\([1-9][0-9]{0,4}\)))`
|
|
46
|
+
export const ANCHOR_SELECTOR_RE = new RegExp(`^${ANCHOR_STEP}(?: > ${ANCHOR_STEP}){0,79}$`)
|
|
47
|
+
const ONE_STEP_RE = new RegExp(`^${ANCHOR_STEP}$`)
|
|
48
|
+
const TAG_RE = /^[a-z][a-z0-9-]{0,40}$/
|
|
49
|
+
|
|
50
|
+
// ---- In the page --------------------------------------------------------------
|
|
51
|
+
// Runs inside the rendered live document (sent as source text, so it must be
|
|
52
|
+
// self-contained). Mirrors client/src/board/anchors.js, which is what the board
|
|
53
|
+
// did on the frame's DOM before maps: the same text, the same selector steps,
|
|
54
|
+
// the same "nearest interactive ancestor".
|
|
55
|
+
export function buildElementMap(limits) {
|
|
56
|
+
const doc = document
|
|
57
|
+
const root = doc.documentElement
|
|
58
|
+
const body = doc.body
|
|
59
|
+
const width = Math.round(innerWidth)
|
|
60
|
+
const height = Math.round(innerHeight)
|
|
61
|
+
if (!body) return { v: 1, width, height, elements: [] }
|
|
62
|
+
const textOf = (element) => {
|
|
63
|
+
const text =
|
|
64
|
+
element.innerText ||
|
|
65
|
+
element.textContent ||
|
|
66
|
+
element.getAttribute('aria-label') ||
|
|
67
|
+
element.getAttribute('alt') ||
|
|
68
|
+
element.getAttribute('placeholder') ||
|
|
69
|
+
''
|
|
70
|
+
return String(text).replace(/\s+/g, ' ').trim()
|
|
71
|
+
}
|
|
72
|
+
const escapeAttr = (value) => String(value).replace(/["\\]/g, '\\$&')
|
|
73
|
+
const unique = (selector) => {
|
|
74
|
+
try {
|
|
75
|
+
return doc.querySelectorAll(selector).length === 1
|
|
76
|
+
} catch (error) {
|
|
77
|
+
return false
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const round = (value) => Math.round(value * 10) / 10
|
|
81
|
+
const sized = (rect) => Boolean(rect && rect.width && rect.height)
|
|
82
|
+
|
|
83
|
+
// Elements in the frame, plus their ancestors (selectors walk through them).
|
|
84
|
+
const nodes = [body, ...Array.from(body.querySelectorAll('*'))]
|
|
85
|
+
const rects = new Map()
|
|
86
|
+
const keep = new Set()
|
|
87
|
+
for (const element of nodes) {
|
|
88
|
+
const rect = element.getBoundingClientRect()
|
|
89
|
+
rects.set(element, rect)
|
|
90
|
+
if (!sized(rect) || rect.right <= 0 || rect.bottom <= 0 || rect.left >= width || rect.top >= height) continue
|
|
91
|
+
for (let node = element; node && node !== root && !keep.has(node); node = node.parentElement) keep.add(node)
|
|
92
|
+
}
|
|
93
|
+
// Document order keeps ancestors first, so a cut keeps every parent.
|
|
94
|
+
const all = nodes.filter((element) => keep.has(element))
|
|
95
|
+
const list = all.slice(0, limits.elements)
|
|
96
|
+
const index = new Map(list.map((element, position) => [element, position]))
|
|
97
|
+
const styles = new Map()
|
|
98
|
+
const style = (element) => {
|
|
99
|
+
let value = styles.get(element)
|
|
100
|
+
if (!value) {
|
|
101
|
+
value = getComputedStyle(element)
|
|
102
|
+
styles.set(element, value)
|
|
103
|
+
}
|
|
104
|
+
return value
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Paint order, approximated: the root-level z-index of the outermost
|
|
108
|
+
// positioned ancestor, then positioned above in-flow, then document order.
|
|
109
|
+
const layer = (element) => {
|
|
110
|
+
let z = 0
|
|
111
|
+
let positioned = 0
|
|
112
|
+
for (let node = element; node && node !== root; node = node.parentElement) {
|
|
113
|
+
const value = style(node)
|
|
114
|
+
if (value.position !== 'static') {
|
|
115
|
+
positioned = 1
|
|
116
|
+
const zIndex = parseInt(value.zIndex, 10)
|
|
117
|
+
if (Number.isFinite(zIndex)) z = zIndex
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return [z, positioned]
|
|
121
|
+
}
|
|
122
|
+
// A click only lands on the part of an element its clipping ancestors show.
|
|
123
|
+
const clipOf = (element, rect) => {
|
|
124
|
+
let left = rect.left
|
|
125
|
+
let top = rect.top
|
|
126
|
+
let right = rect.right
|
|
127
|
+
let bottom = rect.bottom
|
|
128
|
+
if (style(element).position === 'fixed') return null
|
|
129
|
+
for (let node = element.parentElement; node && node !== root; node = node.parentElement) {
|
|
130
|
+
const value = style(node)
|
|
131
|
+
if (value.overflowX !== 'visible' || value.overflowY !== 'visible') {
|
|
132
|
+
const box = rects.get(node) || node.getBoundingClientRect()
|
|
133
|
+
left = Math.max(left, box.left)
|
|
134
|
+
top = Math.max(top, box.top)
|
|
135
|
+
right = Math.min(right, box.right)
|
|
136
|
+
bottom = Math.min(bottom, box.bottom)
|
|
137
|
+
}
|
|
138
|
+
if (value.position === 'fixed') break
|
|
139
|
+
}
|
|
140
|
+
if (left === rect.left && top === rect.top && right === rect.right && bottom === rect.bottom) return null
|
|
141
|
+
return [round(left), round(top), round(Math.max(0, right - left)), round(Math.max(0, bottom - top))]
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const elements = []
|
|
145
|
+
const hittable = []
|
|
146
|
+
list.forEach((element, position) => {
|
|
147
|
+
const tag = element.tagName.toLowerCase()
|
|
148
|
+
const rect = rects.get(element)
|
|
149
|
+
let nth = 1
|
|
150
|
+
let sibling = element
|
|
151
|
+
while ((sibling = sibling.previousElementSibling)) if (sibling.tagName === element.tagName) nth += 1
|
|
152
|
+
const item = {
|
|
153
|
+
t: tag,
|
|
154
|
+
n: nth,
|
|
155
|
+
p: element === body ? -1 : index.get(element.parentElement),
|
|
156
|
+
b: [round(rect.left), round(rect.top), round(rect.width), round(rect.height)],
|
|
157
|
+
}
|
|
158
|
+
const id = element.id
|
|
159
|
+
const testId = element.getAttribute('data-testid')
|
|
160
|
+
const label = element.getAttribute('aria-label')
|
|
161
|
+
if (id && unique(`#${CSS.escape(id)}`)) item.h = `#${CSS.escape(id)}`
|
|
162
|
+
else if (testId && unique(`${tag}[data-testid="${escapeAttr(testId)}"]`)) item.h = `${tag}[data-testid="${escapeAttr(testId)}"]`
|
|
163
|
+
else if (label && label.length <= 80 && unique(`${tag}[aria-label="${escapeAttr(label)}"]`))
|
|
164
|
+
item.h = `${tag}[aria-label="${escapeAttr(label)}"]`
|
|
165
|
+
if (id && id.length <= limits.attr) item.i = id
|
|
166
|
+
if (testId && testId.length <= limits.attr) item.d = testId
|
|
167
|
+
if (label && label.length <= limits.attr) item.r = label
|
|
168
|
+
const text = textOf(element)
|
|
169
|
+
if (text) {
|
|
170
|
+
item.x = text.slice(0, limits.text)
|
|
171
|
+
if (text.length > limits.text) item.l = 1
|
|
172
|
+
}
|
|
173
|
+
const value = style(element)
|
|
174
|
+
if (element !== body && sized(rect) && value.visibility === 'visible' && value.pointerEvents !== 'none') {
|
|
175
|
+
hittable.push({ position, key: layer(element) })
|
|
176
|
+
const clip = clipOf(element, rect)
|
|
177
|
+
if (clip) item.c = clip
|
|
178
|
+
const target = element.closest(limits.preferred) || element
|
|
179
|
+
if (target !== element) item.a = index.has(target) && sized(rects.get(target)) ? index.get(target) : -1
|
|
180
|
+
}
|
|
181
|
+
elements.push(item)
|
|
182
|
+
})
|
|
183
|
+
hittable.sort((a, b) => a.key[0] - b.key[0] || a.key[1] - b.key[1] || a.position - b.position)
|
|
184
|
+
hittable.forEach((entry, rank) => {
|
|
185
|
+
elements[entry.position].z = rank
|
|
186
|
+
})
|
|
187
|
+
const map = { v: 1, width, height, elements }
|
|
188
|
+
if (all.length > list.length) map.truncated = true
|
|
189
|
+
return map
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export const ELEMENT_MAP_EXPRESSION = `(${buildElementMap.toString()})(${JSON.stringify({
|
|
193
|
+
elements: ELEMENT_MAP_LIMITS.elements,
|
|
194
|
+
text: ELEMENT_MAP_LIMITS.text,
|
|
195
|
+
attr: ELEMENT_MAP_LIMITS.attr,
|
|
196
|
+
preferred: PREFERRED,
|
|
197
|
+
})})`
|
|
198
|
+
|
|
199
|
+
// ---- Validation -----------------------------------------------------------------
|
|
200
|
+
// A map is data from the capture, or from a push (untrusted): rebuilt field by
|
|
201
|
+
// field, anything unexpected refused. Returns the clean map or throws.
|
|
202
|
+
|
|
203
|
+
export class ElementMapError extends Error {}
|
|
204
|
+
|
|
205
|
+
// eslint-disable-next-line no-control-regex
|
|
206
|
+
const CONTROL_RE = /[\u0000-\u001f\u007f]/
|
|
207
|
+
const COORD_MAX = 100000
|
|
208
|
+
|
|
209
|
+
function box(value, where) {
|
|
210
|
+
if (!Array.isArray(value) || value.length !== 4) throw new ElementMapError(`${where}: a box is four numbers`)
|
|
211
|
+
const out = value.map((number) => {
|
|
212
|
+
if (typeof number !== 'number' || !Number.isFinite(number) || Math.abs(number) > COORD_MAX)
|
|
213
|
+
throw new ElementMapError(`${where}: a box is four numbers`)
|
|
214
|
+
return Math.round(number * 10) / 10
|
|
215
|
+
})
|
|
216
|
+
if (out[2] < 0 || out[3] < 0) throw new ElementMapError(`${where}: a box has no negative size`)
|
|
217
|
+
return out
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function plain(value, max, where) {
|
|
221
|
+
if (typeof value !== 'string' || !value || value.length > max || CONTROL_RE.test(value))
|
|
222
|
+
throw new ElementMapError(`${where}: up to ${max} printable characters`)
|
|
223
|
+
return value
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export function cleanElementMap(raw) {
|
|
227
|
+
const limits = ELEMENT_MAP_LIMITS
|
|
228
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) throw new ElementMapError('an element map is an object')
|
|
229
|
+
if (raw.v !== ELEMENT_MAP_VERSION) throw new ElementMapError('unknown element map version')
|
|
230
|
+
const size = (value, name) => {
|
|
231
|
+
if (!Number.isInteger(value) || value < 1 || value > limits.size) throw new ElementMapError(`${name}: 1 to ${limits.size}`)
|
|
232
|
+
return value
|
|
233
|
+
}
|
|
234
|
+
const width = size(raw.width, 'width')
|
|
235
|
+
const height = size(raw.height, 'height')
|
|
236
|
+
if (!Array.isArray(raw.elements) || raw.elements.length > limits.elements)
|
|
237
|
+
throw new ElementMapError(`elements: a list of at most ${limits.elements}`)
|
|
238
|
+
const count = raw.elements.length
|
|
239
|
+
const ranks = new Set()
|
|
240
|
+
const elements = raw.elements.map((item, position) => {
|
|
241
|
+
const where = `elements[${position}]`
|
|
242
|
+
if (!item || typeof item !== 'object' || Array.isArray(item)) throw new ElementMapError(`${where}: an object`)
|
|
243
|
+
if (typeof item.t !== 'string' || !TAG_RE.test(item.t)) throw new ElementMapError(`${where}.t: a tag name`)
|
|
244
|
+
if (!Number.isInteger(item.n) || item.n < 1 || item.n > 99999) throw new ElementMapError(`${where}.n: 1 to 99999`)
|
|
245
|
+
// Parents come first; only the first element (<body>) has none.
|
|
246
|
+
if (!Number.isInteger(item.p) || item.p >= position || item.p < -1 || (item.p === -1) !== (position === 0))
|
|
247
|
+
throw new ElementMapError(`${where}.p: an earlier element`)
|
|
248
|
+
const clean = { t: item.t, n: item.n, p: item.p, b: box(item.b, `${where}.b`) }
|
|
249
|
+
if (item.h !== undefined) {
|
|
250
|
+
if (typeof item.h !== 'string' || item.h.length > limits.selector || !ONE_STEP_RE.test(item.h))
|
|
251
|
+
throw new ElementMapError(`${where}.h: one selector step`)
|
|
252
|
+
clean.h = item.h
|
|
253
|
+
}
|
|
254
|
+
for (const key of ['i', 'd', 'r']) if (item[key] !== undefined) clean[key] = plain(item[key], limits.attr, `${where}.${key}`)
|
|
255
|
+
if (item.x !== undefined) clean.x = plain(item.x, limits.text, `${where}.x`)
|
|
256
|
+
if (item.l !== undefined) {
|
|
257
|
+
if (item.l !== 1 || clean.x === undefined) throw new ElementMapError(`${where}.l: 1 with a text`)
|
|
258
|
+
clean.l = 1
|
|
259
|
+
}
|
|
260
|
+
if (item.z !== undefined) {
|
|
261
|
+
if (!Number.isInteger(item.z) || item.z < 0 || item.z >= count || ranks.has(item.z))
|
|
262
|
+
throw new ElementMapError(`${where}.z: a distinct rank`)
|
|
263
|
+
ranks.add(item.z)
|
|
264
|
+
clean.z = item.z
|
|
265
|
+
}
|
|
266
|
+
if (item.c !== undefined) clean.c = box(item.c, `${where}.c`)
|
|
267
|
+
if (item.a !== undefined) {
|
|
268
|
+
if (!Number.isInteger(item.a) || item.a < -1 || item.a >= count) throw new ElementMapError(`${where}.a: an element or -1`)
|
|
269
|
+
clean.a = item.a
|
|
270
|
+
}
|
|
271
|
+
return clean
|
|
272
|
+
})
|
|
273
|
+
const map = { v: ELEMENT_MAP_VERSION, width, height, elements }
|
|
274
|
+
if (raw.truncated === true) map.truncated = true
|
|
275
|
+
return map
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// From the bytes of a file (a push): JSON, size-capped, then cleanElementMap.
|
|
279
|
+
export function parseElementMap(text) {
|
|
280
|
+
if (typeof text !== 'string' || text.length > ELEMENT_MAP_LIMITS.bytes) throw new ElementMapError('the element map is too large')
|
|
281
|
+
let raw
|
|
282
|
+
try {
|
|
283
|
+
raw = JSON.parse(text)
|
|
284
|
+
} catch {
|
|
285
|
+
throw new ElementMapError('the element map is not JSON')
|
|
286
|
+
}
|
|
287
|
+
return cleanElementMap(raw)
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// ---- On the board -------------------------------------------------------------------
|
|
291
|
+
|
|
292
|
+
const clip = (value, max) => (value.length > max ? `${value.slice(0, max - 3)}...` : value)
|
|
293
|
+
|
|
294
|
+
// The selector a comment on element `position` stores: the same steps as
|
|
295
|
+
// anchors.js selectorFor() on the DOM, from the closest stable hook down.
|
|
296
|
+
export function selectorOf(map, position) {
|
|
297
|
+
const parts = []
|
|
298
|
+
let node = position
|
|
299
|
+
let guard = 0
|
|
300
|
+
while (node >= 0 && guard < 10000) {
|
|
301
|
+
const item = map.elements[node]
|
|
302
|
+
if (!item) break
|
|
303
|
+
if (item.h) {
|
|
304
|
+
parts.unshift(item.h)
|
|
305
|
+
break
|
|
306
|
+
}
|
|
307
|
+
parts.unshift(`${item.t}:nth-of-type(${item.n})`)
|
|
308
|
+
node = item.p
|
|
309
|
+
guard += 1
|
|
310
|
+
}
|
|
311
|
+
let selector = parts.join(' > ')
|
|
312
|
+
while (selector.length > ELEMENT_MAP_LIMITS.selector && parts.length > 1) {
|
|
313
|
+
parts.shift()
|
|
314
|
+
selector = parts.join(' > ')
|
|
315
|
+
}
|
|
316
|
+
return selector.length > ELEMENT_MAP_LIMITS.selector ? '' : selector
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// The element a click at page coordinates (x, y) means, or -1 on the page
|
|
320
|
+
// background: the topmost element under it, then its nearest interactive
|
|
321
|
+
// ancestor (as elementAt() did on the DOM).
|
|
322
|
+
export function hitTest(map, x, y) {
|
|
323
|
+
let best = -1
|
|
324
|
+
let bestRank = -1
|
|
325
|
+
const elements = map?.elements || []
|
|
326
|
+
for (let position = 0; position < elements.length; position += 1) {
|
|
327
|
+
const item = elements[position]
|
|
328
|
+
if (item.z === undefined || item.z <= bestRank) continue
|
|
329
|
+
const [left, top, width, height] = item.c || item.b
|
|
330
|
+
if (x >= left && x < left + width && y >= top && y < top + height) {
|
|
331
|
+
best = position
|
|
332
|
+
bestRank = item.z
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
if (best < 0) return -1
|
|
336
|
+
const target = elements[best].a
|
|
337
|
+
return target === undefined ? best : target
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// A short label for the element a comment would pin to (as
|
|
341
|
+
// anchors.js describeElement): the last selector step and its text, and
|
|
342
|
+
// `large` when it covers a fifth of the page or more.
|
|
343
|
+
export function describeAt(map, position, native) {
|
|
344
|
+
const item = map.elements[position]
|
|
345
|
+
const selector = selectorOf(map, position)
|
|
346
|
+
const step = selector ? selector.split(' > ').pop() : item.t
|
|
347
|
+
const text = item.x || ''
|
|
348
|
+
const [left, top, width, height] = item.b
|
|
349
|
+
const area = native?.width && native?.height ? (width * height) / (native.width * native.height) : 0
|
|
350
|
+
return {
|
|
351
|
+
label: `${clip(step, 48)}${text ? ` "${clip(text, 28)}"` : ''}`,
|
|
352
|
+
large: area > 0.2,
|
|
353
|
+
rect: { left, top, width, height },
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// A click at page coordinates as an anchor, or null (as anchors.js anchorAt).
|
|
358
|
+
export function anchorFromMap(map, x, y, native = null) {
|
|
359
|
+
const position = hitTest(map, x, y)
|
|
360
|
+
if (position < 0) return null
|
|
361
|
+
const item = map.elements[position]
|
|
362
|
+
const [left, top, width, height] = item.b
|
|
363
|
+
if (!width || !height) return null
|
|
364
|
+
const selector = selectorOf(map, position)
|
|
365
|
+
if (!selector || !ANCHOR_SELECTOR_RE.test(selector)) return null
|
|
366
|
+
const clamp = (value) => Math.max(0, Math.min(1, value))
|
|
367
|
+
const round = (value) => Math.round(clamp(value) * 10000) / 10000
|
|
368
|
+
return {
|
|
369
|
+
selector,
|
|
370
|
+
text: item.x || '',
|
|
371
|
+
dx: round((x - left) / width),
|
|
372
|
+
dy: round((y - top) / height),
|
|
373
|
+
...(native?.width && native?.height ? { w: round(width / native.width), h: round(height / native.height) } : {}),
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// CSS escapes in an identifier or a quoted string: \41 (hex, one optional
|
|
378
|
+
// space after it) or \x (that character).
|
|
379
|
+
function cssUnescape(value) {
|
|
380
|
+
return value.replace(/\\(?:([0-9a-fA-F]{1,6}) ?|([^\n\r\f]))/g, (match, hex, char) => {
|
|
381
|
+
if (hex === undefined) return char
|
|
382
|
+
const code = parseInt(hex, 16)
|
|
383
|
+
return code === 0 || code > 0x10ffff || (code >= 0xd800 && code <= 0xdfff) ? '\ufffd' : String.fromCodePoint(code)
|
|
384
|
+
})
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const STEP_RES = {
|
|
388
|
+
id: new RegExp(`#(${ID_BODY})`, 'y'),
|
|
389
|
+
tag: /([a-z][a-z0-9-]{0,40})/y,
|
|
390
|
+
attr: new RegExp(`\\[(data-testid|aria-label)="(${ATTR_VALUE})"\\]`, 'y'),
|
|
391
|
+
nth: /:nth-of-type\(([1-9][0-9]{0,4})\)/y,
|
|
392
|
+
join: / > /y,
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// A stored selector as steps, or null when it is not in the anchor grammar.
|
|
396
|
+
export function parseSelector(selector) {
|
|
397
|
+
if (typeof selector !== 'string' || !ANCHOR_SELECTOR_RE.test(selector)) return null
|
|
398
|
+
const steps = []
|
|
399
|
+
let at = 0
|
|
400
|
+
const take = (re) => {
|
|
401
|
+
re.lastIndex = at
|
|
402
|
+
const match = re.exec(selector)
|
|
403
|
+
if (match) at = re.lastIndex
|
|
404
|
+
return match
|
|
405
|
+
}
|
|
406
|
+
while (at < selector.length) {
|
|
407
|
+
if (steps.length && !take(STEP_RES.join)) return null
|
|
408
|
+
let match = take(STEP_RES.id)
|
|
409
|
+
if (match) {
|
|
410
|
+
steps.push({ id: cssUnescape(match[1]) })
|
|
411
|
+
continue
|
|
412
|
+
}
|
|
413
|
+
match = take(STEP_RES.tag)
|
|
414
|
+
if (!match) return null
|
|
415
|
+
const tag = match[1]
|
|
416
|
+
match = take(STEP_RES.attr)
|
|
417
|
+
if (match) {
|
|
418
|
+
steps.push({ tag, [match[1] === 'data-testid' ? 'testId' : 'label']: cssUnescape(match[2]) })
|
|
419
|
+
continue
|
|
420
|
+
}
|
|
421
|
+
match = take(STEP_RES.nth)
|
|
422
|
+
if (!match) return null
|
|
423
|
+
steps.push({ tag, nth: Number(match[1]) })
|
|
424
|
+
}
|
|
425
|
+
return steps
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
function stepMatches(item, step) {
|
|
429
|
+
if (step.id !== undefined) return item.i === step.id
|
|
430
|
+
if (item.t !== step.tag) return false
|
|
431
|
+
if (step.testId !== undefined) return item.d === step.testId
|
|
432
|
+
if (step.label !== undefined) return item.r === step.label
|
|
433
|
+
return item.n === step.nth
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// The first element (document order) a stored selector names, or -1: the
|
|
437
|
+
// map's answer to querySelector() for the anchor grammar (child combinators).
|
|
438
|
+
export function matchSelector(map, selector) {
|
|
439
|
+
const steps = parseSelector(selector)
|
|
440
|
+
if (!steps?.length) return -1
|
|
441
|
+
const elements = map?.elements || []
|
|
442
|
+
const last = steps[steps.length - 1]
|
|
443
|
+
for (let position = 0; position < elements.length; position += 1) {
|
|
444
|
+
if (!stepMatches(elements[position], last)) continue
|
|
445
|
+
let node = elements[position].p
|
|
446
|
+
let ok = true
|
|
447
|
+
for (let step = steps.length - 2; step >= 0; step -= 1) {
|
|
448
|
+
if (node < 0 || !stepMatches(elements[node], steps[step])) {
|
|
449
|
+
ok = false
|
|
450
|
+
break
|
|
451
|
+
}
|
|
452
|
+
node = elements[node].p
|
|
453
|
+
}
|
|
454
|
+
if (ok) return position
|
|
455
|
+
}
|
|
456
|
+
return -1
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
function lastTag(selector) {
|
|
460
|
+
const last = String(selector).split('>').pop().trim()
|
|
461
|
+
const match = /^([a-z][a-z0-9-]*)/i.exec(last)
|
|
462
|
+
return match ? match[1].toLowerCase() : null
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
// Finds the anchored element on the map (as anchors.js resolveAnchor did on
|
|
466
|
+
// the DOM): by selector, checked against the saved text, then by its text
|
|
467
|
+
// alone among elements of the same tag. { x, y, box } in percent of the
|
|
468
|
+
// native page, or null when it is gone.
|
|
469
|
+
export function resolveOnMap(map, anchor, native) {
|
|
470
|
+
if (!map?.elements?.length || !anchor?.selector || !native || !ANCHOR_SELECTOR_RE.test(anchor.selector)) return null
|
|
471
|
+
const wanted = String(anchor.text || '')
|
|
472
|
+
const elements = map.elements
|
|
473
|
+
let found = matchSelector(map, anchor.selector)
|
|
474
|
+
// The map keeps the first 80 characters: a match on those is a match on
|
|
475
|
+
// the whole text (the stored anchor text is at most 80 characters).
|
|
476
|
+
if (found >= 0 && wanted && !(elements[found].x || '').startsWith(wanted)) found = -1
|
|
477
|
+
const tag = lastTag(anchor.selector)
|
|
478
|
+
if (found < 0 && wanted && tag) {
|
|
479
|
+
let bestArea = Infinity
|
|
480
|
+
for (let position = 0; position < elements.length; position += 1) {
|
|
481
|
+
const item = elements[position]
|
|
482
|
+
if (item.t !== tag || item.l || item.x !== wanted) continue
|
|
483
|
+
const area = item.b[2] * item.b[3]
|
|
484
|
+
if (area > 0 && area < bestArea) {
|
|
485
|
+
found = position
|
|
486
|
+
bestArea = area
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
if (found < 0) return null
|
|
491
|
+
const [left, top, width, height] = elements[found].b
|
|
492
|
+
if (!width && !height) return null
|
|
493
|
+
const percent = (value, size) => Math.max(0, Math.min(100, (value / size) * 100))
|
|
494
|
+
const dx = Number.isFinite(anchor.dx) ? anchor.dx : 0.5
|
|
495
|
+
const dy = Number.isFinite(anchor.dy) ? anchor.dy : 0.5
|
|
496
|
+
return {
|
|
497
|
+
x: percent(left + dx * width, native.width),
|
|
498
|
+
y: percent(top + dy * height, native.height),
|
|
499
|
+
box: {
|
|
500
|
+
left: percent(left, native.width),
|
|
501
|
+
top: percent(top, native.height),
|
|
502
|
+
right: percent(left + width, native.width),
|
|
503
|
+
bottom: percent(top + height, native.height),
|
|
504
|
+
},
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
// The file name of the map that goes with a live HTML file.
|
|
509
|
+
export const elementMapName = (liveFile) => String(liveFile).replace(/\.html$/, '.elements.json')
|