barebrowse 0.24.1 → 0.26.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/src/aria.js CHANGED
@@ -5,6 +5,8 @@
5
5
  * and formats it as readable YAML-like text, similar to Playwright's ariaSnapshot.
6
6
  */
7
7
 
8
+ import { urlsText } from './links.js';
9
+
8
10
  /**
9
11
  * Format a nested ARIA tree as readable text output.
10
12
  *
@@ -37,6 +39,9 @@ export function formatTree(node, depth = 0) {
37
39
  line += ` "${node.name}"`;
38
40
  }
39
41
 
42
+ // Link URLs (opt-in links: true): right after the name, before props and ref
43
+ line += urlsText(node.urls);
44
+
40
45
  // Notable properties that agents care about
41
46
  const props = node.properties || {};
42
47
  const propParts = [];
@@ -166,6 +166,7 @@ const AX_FN = function reconstructAX(base, REF_ATTR) {
166
166
  const exp = el.getAttribute('aria-expanded');
167
167
  if (exp !== null) p.expanded = exp === 'true';
168
168
  if (role === 'heading') p.level = Number(el.getAttribute('aria-level')) || Number(el.tagName[1]) || 2;
169
+ if (role === 'link' && el.href) p.url = el.href; // browser-serialized absolute URL (CDP's `url` property)
169
170
  if (el.getAttribute('aria-selected') === 'true') p.selected = true;
170
171
  if (el.required || el.getAttribute('aria-required') === 'true') p.required = true;
171
172
  if ((role === 'textbox' || role === 'searchbox' || role === 'combobox' || role === 'spinbutton') && el.value) {
package/src/bareagent.js CHANGED
@@ -62,11 +62,12 @@ export function createBrowseTools(opts = {}) {
62
62
  type: 'object',
63
63
  properties: {
64
64
  url: { type: 'string', description: 'URL to browse' },
65
- pruneMode: { type: 'string', enum: ['act', 'read'], description: '"act" (default) for interactive elements only; "read" for paragraphs and long text (articles/docs).' },
65
+ pruneMode: { type: 'string', enum: ['act', 'read', 'data'], description: '"act" (default) for interactive elements only; "read" for paragraphs and long text (articles/docs); "data": one line per record — tables with headers, repeated items grouped; for data pages, not articles. Output starts with a legend line; read it as: table rows `row "<label>": <column>: <value> · …`, groups `<label>: a, b, c`, and a title line followed by detail lines is one block up to the next title line. Main content only: use "act" for menus and page controls.' },
66
+ links: { type: 'boolean', description: 'Show each link\'s absolute URL after its name: link "name" <url> [ref=N]. Default off.' },
66
67
  },
67
68
  required: ['url'],
68
69
  },
69
- execute: async ({ url, pruneMode }) => await browse(url, pruneMode ? { ...opts, pruneMode } : opts),
70
+ execute: async ({ url, pruneMode, links }) => await browse(url, pruneMode || links ? { ...opts, pruneMode, links } : opts),
70
71
  },
71
72
  {
72
73
  name: 'goto',
@@ -86,12 +87,13 @@ export function createBrowseTools(opts = {}) {
86
87
  parameters: {
87
88
  type: 'object',
88
89
  properties: {
89
- pruneMode: { type: 'string', enum: ['act', 'read'], description: '"act" (default) for interactive elements only; "read" for paragraphs and long text (articles/docs).' },
90
+ pruneMode: { type: 'string', enum: ['act', 'read', 'data'], description: '"act" (default) for interactive elements only; "read" for paragraphs and long text (articles/docs); "data": one line per record — tables with headers, repeated items grouped; for data pages, not articles. Output starts with a legend line; read it as: table rows `row "<label>": <column>: <value> · …`, groups `<label>: a, b, c`, and a title line followed by detail lines is one block up to the next title line. Main content only: use "act" for menus and page controls.' },
91
+ links: { type: 'boolean', description: 'Show each link\'s absolute URL after its name: link "name" <url> [ref=N]. Default off.' },
90
92
  },
91
93
  },
92
- execute: async (/** @type {{ pruneMode?: string }} */ { pruneMode } = {}) => {
94
+ execute: async (/** @type {{ pruneMode?: string, links?: boolean }} */ { pruneMode, links } = {}) => {
93
95
  const page = await getPage();
94
- return await page.snapshot(pruneMode ? { mode: pruneMode } : undefined);
96
+ return await page.snapshot(pruneMode || links ? { mode: pruneMode, links } : undefined);
95
97
  },
96
98
  },
97
99
  {
@@ -16,7 +16,7 @@
16
16
  * makes "descendant of dialog" a plain subtree walk — no parentMap needed.
17
17
  */
18
18
 
19
- import { ACCEPT_PATTERNS, DIALOG_ROLES, CONSENT_DIALOG_HINTS } from './consent-patterns.js';
19
+ import { ACCEPT_PATTERNS, DIALOG_ACCEPT_PATTERNS, DIALOG_ROLES, CONSENT_DIALOG_HINTS } from './consent-patterns.js';
20
20
 
21
21
  /** Roles whose text confirms a container is a consent dialog. */
22
22
  const CONSENT_TEXT_ROLES = new Set(['heading', 'StaticText', 'generic']);
@@ -44,7 +44,7 @@ function hasConsentContent(dialog) {
44
44
  * ACCEPT_PATTERNS priority (most specific first).
45
45
  */
46
46
  function findAcceptButton(dialog) {
47
- for (const pattern of ACCEPT_PATTERNS) {
47
+ for (const pattern of DIALOG_ACCEPT_PATTERNS) {
48
48
  for (const node of walk(dialog)) {
49
49
  if (node.role === 'button' && node.name && pattern.test(node.name)) return node;
50
50
  }
@@ -127,6 +127,16 @@ export const ACCEPT_PATTERNS = [
127
127
  /^ok$/i,
128
128
  ];
129
129
 
130
+ // Accept labels that are only safe INSIDE an already-detected consent dialog
131
+ // (e.g. Google Funding Choices' bare "Consent" button). Never used by the
132
+ // page-wide fallback, where a lone "Consent" could be any unrelated control.
133
+ export const DIALOG_ONLY_ACCEPT_PATTERNS = [
134
+ /^consent$/i,
135
+ ];
136
+
137
+ // Priority-ordered list the dialog-scoped search walks, built once.
138
+ export const DIALOG_ACCEPT_PATTERNS = [...ACCEPT_PATTERNS, ...DIALOG_ONLY_ACCEPT_PATTERNS];
139
+
130
140
  // Roles that indicate a consent dialog container.
131
141
  export const DIALOG_ROLES = new Set(['dialog', 'alertdialog']);
132
142
 
package/src/consent.js CHANGED
@@ -10,7 +10,7 @@
10
10
  * tree walk + click.
11
11
  */
12
12
 
13
- import { ACCEPT_PATTERNS, DIALOG_ROLES, CONSENT_DIALOG_HINTS } from './consent-patterns.js';
13
+ import { ACCEPT_PATTERNS, DIALOG_ACCEPT_PATTERNS, DIALOG_ROLES, CONSENT_DIALOG_HINTS } from './consent-patterns.js';
14
14
 
15
15
  /**
16
16
  * Click a node via JavaScript .click() instead of mouse events.
@@ -134,7 +134,7 @@ function hasConsentContent(dialogId, nodes, nodeMap, parentMap) {
134
134
  * Find the best "accept" button inside a dialog subtree.
135
135
  */
136
136
  function findAcceptButton(dialogId, nodes, nodeMap, parentMap) {
137
- for (const pattern of ACCEPT_PATTERNS) {
137
+ for (const pattern of DIALOG_ACCEPT_PATTERNS) {
138
138
  for (const node of nodes) {
139
139
  if (node.role?.value !== 'button') continue;
140
140
  const name = node.name?.value || '';
package/src/daemon.js CHANGED
@@ -13,6 +13,7 @@ import { join, resolve } from 'node:path';
13
13
  import { connect } from './index.js';
14
14
  import { formatReadable } from './readable.js';
15
15
  import { cleanOld } from './output-clean.js';
16
+ import { isHttpError } from './navigation.js';
16
17
 
17
18
  /** Owner-only file write helper — daemon artifacts can hold authenticated content. */
18
19
  function writeFilePrivate(path, data) {
@@ -313,13 +314,13 @@ export async function runDaemon(opts, outputDir, initialUrl) {
313
314
  // Command handlers
314
315
  const handlers = {
315
316
  async goto({ url, timeout }) {
316
- await page.goto(url, timeout || 30000);
317
- return { ok: true };
317
+ const { status } = await page.goto(url, timeout || 30000);
318
+ return isHttpError(status) ? { ok: true, status } : { ok: true };
318
319
  },
319
320
 
320
- async snapshot({ mode }) {
321
+ async snapshot({ mode, links }) {
321
322
  const pruneMode = mode || defaultPruneMode;
322
- const text = await page.snapshot({ mode: pruneMode });
323
+ const text = await page.snapshot({ mode: pruneMode, links });
323
324
  const ts = new Date().toISOString().replace(/[:.]/g, '-');
324
325
  const file = join(absDir, `page-${ts}.yml`);
325
326
  writeFilePrivate(file, text);
package/src/data.js ADDED
@@ -0,0 +1,315 @@
1
+ /**
2
+ * data.js — "data" snapshot mode: one line per record.
3
+ *
4
+ * For pages that are data, not prose (tables, listings, feeds, search results).
5
+ * Takes the RAW tree from buildTree() (not a pruned one: prune.js collapse()
6
+ * destroys table structure) and returns text. Never touches prune.js/aria.js.
7
+ *
8
+ * Rules, applied in this order:
9
+ * Clean pass (N1-N6) — per node, bottom-up
10
+ * N1 drop rendering noise (InlineTextBox, LineBreak, ListMarker, images)
11
+ * N2 drop StaticText with no letter/digit (whitespace, punctuation)
12
+ * N3 StaticText keeps only its text (no role word on output)
13
+ * N4 drop children whose text is already inside a content-derived name
14
+ * (unless the child is, or holds, something interactive)
15
+ * N5 unwrap unnamed wrappers; all-text wrappers become one text leaf
16
+ * N6 drop empty/nameless leaves that say nothing and can't be targeted
17
+ * (a name of only icon-font glyphs counts as no name)
18
+ * Repeated siblings (R1-R3)
19
+ * R1 a run of >=3 same-shape leaves collapses to "label: a, b, c"
20
+ * R2 a run of 2 collapses too when a >=3 run of the same shape and
21
+ * lead-in exists in the same sibling list ("weak pair")
22
+ * R3 label = lead-in leaves back to a heading/interactive anchor, else the
23
+ * nearest preceding heading, else the nearest named container
24
+ * Tables (T1-T4)
25
+ * T1 header rows (columnheader-only) become the column names
26
+ * T2 data row -> `row "<row header>": Header: value · Header: value`
27
+ * T3 header/row width mismatch (CDP exposes no colspan): drop the
28
+ * positional mapping, print `columns:` once, rows stay header-less
29
+ * T4 one value filling most cells of a wide row is stated once
30
+ *
31
+ * Scope: the main landmark if there is one, else the whole page.
32
+ * Refs: `[ref=N]` on interactive nodes only (same ids as act/read, so
33
+ * click/type resolve through the same refMap).
34
+ */
35
+
36
+ import { urlsText } from './links.js';
37
+
38
+ const SKIP = new Set(['InlineTextBox', 'LineBreak', 'ListMarker', 'image', 'img']);
39
+ const CELLS = new Set(['cell', 'gridcell', 'columnheader', 'rowheader']);
40
+ const TABLES = new Set(['table', 'grid', 'treegrid']);
41
+ const INTERACTIVE = new Set(['button', 'link', 'textbox', 'searchbox', 'checkbox', 'radio', 'combobox', 'listbox', 'menuitem', 'menuitemcheckbox', 'menuitemradio', 'option', 'slider', 'spinbutton', 'switch', 'tab', 'treeitem']);
42
+ // Roles whose accessible name is computed from content (ARIA name-from-content):
43
+ // a child whose text is already inside the name is redundant.
44
+ const NFC = new Set(['link', 'button', 'heading', 'generic', 'cell', 'gridcell', 'columnheader', 'rowheader', 'listitem', 'tab', 'menuitem', 'menuitemcheckbox', 'menuitemradio', 'option', 'treeitem', 'checkbox', 'radio', 'switch', 'StaticText', 'Abbr', 'strong', 'emphasis', 'LabelText', 'caption', 'term', 'definition', 'paragraph', 'LayoutTableCell', 'LayoutTableRow']);
45
+ // Unnamed wrappers that carry no semantics of their own.
46
+ const WRAP = new Set(['generic', 'none', 'group', 'presentation', 'paragraph', 'sectionheader', 'rowgroup', 'LayoutTable', 'LayoutTableRow', 'LayoutTableCell', 'list', 'Section', 'div', 'document']);
47
+ const MIN_RUN = 3;
48
+
49
+ /** First line of every data-mode snapshot: tells an agent with no docs how to read it. */
50
+ export const LEGEND = '# data mode: one line per record. Tables: row "<label>": <column>: <value> · … Groups: <label>: a, b, c. A title line followed by detail lines is one block, up to the next title line. Main content only; use mode "act" for menus and page controls.';
51
+
52
+ /** Appended to the legend only when links are on. */
53
+ export const LINKS_NOTE = ' Links: "name" <url>.';
54
+
55
+ // A name made only of private-use code points (icon-font glyphs, unreadable to an
56
+ // agent) is no name. Glyphs mixed into real text are left as they are.
57
+ const norm = (s) => (/^[\p{Co}\s]*$/u.test(s || '') ? '' : s.replace(/\s+/g, ' ').trim());
58
+ const allUrls = (n) => [...(n.urls || []), ...(n.children || []).flatMap(allUrls)];
59
+ const hasInteractive = (n) => INTERACTIVE.has(n.role) || (n.children || []).some(hasInteractive);
60
+ const isLeaf = (n) => n.text === undefined && !n.children.length && n.name;
61
+ const showRef = (n) => !!n.ref && INTERACTIVE.has(n.role);
62
+ // ` <url>` for a node's own links (already unique per node). A link line always
63
+ // carries its own URL: nothing is deduped across lines, items or cells.
64
+ const urlPart = (n) => urlsText(n.urls);
65
+ const withRef = (n) => (showRef(n) ? `${n.name}${urlPart(n)} [ref=${n.ref}]` : n.name);
66
+ // Leaves carrying state (checked, level, value...) never merge into a run.
67
+ const plain = (n) => !(n.props.checked !== undefined || n.props.expanded !== undefined || n.props.selected || n.props.level || n.props.required || (n.props.value !== undefined && n.props.value !== ''));
68
+ // Run key: leaves with the same key are "the same kind of item". `g` = the
69
+ // multi-child wrapper they came from, `sig` = raw child roles (+ '*' when the
70
+ // name overrides the content, e.g. aria-label).
71
+ const kindOf = (n) => (isLeaf(n) && n.role !== 'StaticText' && n.role !== 'heading' && plain(n) && !TABLES.has(n.role) && !CELLS.has(n.role))
72
+ ? n.role + (n.props.disabled ? '!' : '') + '|' + (n.g ?? '') + '|' + n.sig
73
+ : null;
74
+
75
+ // Flat text of a cleaned node; with refs, interactive nodes get their [ref=N].
76
+ function inline(n, refs) {
77
+ if (n.text !== undefined) return n.text;
78
+ const sub = n.children.length ? n.children.map((c) => inline(c, refs)).filter(Boolean).join(' ') : n.name;
79
+ const v = (n.name && !n.children.length) || INTERACTIVE.has(n.role) && n.name && refs ? n.name : sub;
80
+ const out = n.children.length && n.name && n.role !== 'StaticText' && !sub ? n.name : v;
81
+ return refs && n.ref && INTERACTIVE.has(n.role) ? `${out}${urlPart(n)} [ref=${n.ref}]` : out;
82
+ }
83
+
84
+ /** A data-mode result without its legend line, for size stats ('' stays ''). */
85
+ export const stripLegend = (s) => (s.startsWith(LEGEND) ? s.slice(s.indexOf('\n') + 1) : s);
86
+
87
+ /**
88
+ * Render a raw ARIA tree in data mode.
89
+ *
90
+ * @param {object} root - Root node from buildTree() (CDP vocabulary)
91
+ * @param {{ links?: boolean }} [opts] - links: the tree's link nodes carry `urls` (annotateLinks); adds the legend note
92
+ * @returns {string} LEGEND line, then one line per record, YAML-ish like formatTree(); '' when there are no records
93
+ */
94
+ export function dataSnapshot(root, opts = {}) {
95
+ if (!root) return '';
96
+ let gid = 0;
97
+ const rawKids = (n) => n.children.flatMap((c) => (c.ignored ? rawKids(c) : [c]));
98
+
99
+ // ---------- Clean pass (N1-N6) ----------
100
+ function clean(n) {
101
+ if (n.ignored) return n.children.flatMap(clean);
102
+ if (SKIP.has(n.role)) return []; // N1
103
+ if (n.role === 'StaticText') {
104
+ const t = norm(n.name);
105
+ return t && /[\p{L}\p{N}]/u.test(t) ? [{ role: 'StaticText', name: t, children: [], props: {} }] : []; // N2
106
+ }
107
+ let kids = n.children.flatMap(clean);
108
+ const name = norm(n.name);
109
+ const childText = kids.map((k) => inline(k, false)).filter(Boolean).join(' ');
110
+ const sig = rawKids(n).map((c) => c.role).join(',') + (name && childText && childText !== name ? '*' : '');
111
+ const props = n.properties || {};
112
+ // N4: a name that is just the concatenation of interactive children is hidden
113
+ // (children stay, they carry the refs); otherwise redundant children go.
114
+ const nameRedundant = !!(name && NFC.has(n.role) && kids.length && !INTERACTIVE.has(n.role)
115
+ && kids.every((k) => { const t = inline(k, false); return t && name.includes(t); }) && kids.some(hasInteractive));
116
+ if (name && NFC.has(n.role) && !nameRedundant) {
117
+ kids = kids.filter((c) => { const t = inline(c, false); return !(t && !hasInteractive(c) && name.includes(t)); });
118
+ }
119
+ const nm = nameRedundant ? '' : name;
120
+ if (!nm && WRAP.has(n.role)) { // N5
121
+ if (kids.length === 0) return [];
122
+ if (n.role === 'generic' && kids.every((k) => k.role === 'StaticText')) {
123
+ return [{ role: 'generic', name: kids.map((k) => k.name).join(' '), children: [], props, ref: n.nodeId, sig }];
124
+ }
125
+ if (kids.length === 1) return kids;
126
+ const id = ++gid; // multi-child wrapper: promote, tagged so runs don't merge across wrappers
127
+ kids.forEach((k) => { if (k.g === undefined) k.g = id; });
128
+ return kids;
129
+ }
130
+ if (!nm && kids.length === 0 && (n.role === 'link' || n.role === 'button')) return []; // N6
131
+ if (!nm && kids.length === 0 && !INTERACTIVE.has(n.role) && !CELLS.has(n.role) && n.role !== 'row') return [];
132
+ if (n.role === 'listitem' && !nm && kids.length === 1) return kids;
133
+ return [{ role: n.role, name: nm, children: kids, props, ref: n.nodeId, sig, urls: n.urls }];
134
+ }
135
+
136
+ // ---------- Transforms, bottom-up with the ancestor chain for labels ----------
137
+ function xform(n, chain) {
138
+ if (n.text !== undefined) return n;
139
+ if (TABLES.has(n.role)) return xformTable(n, chain);
140
+ const newKids = n.children.map((k, i) => xform(k, [...chain, { sibs: n.children, i, node: n }]));
141
+ return { ...n, children: collapseRuns({ ...n, children: newKids }, chain) };
142
+ }
143
+ function dedupe(kids) {
144
+ const out = [];
145
+ for (const k of kids) {
146
+ const p = out[out.length - 1];
147
+ if (p && isLeaf(p) && isLeaf(k) && p.role === k.role && p.name === k.name && (p.urls || [])[0] === (k.urls || [])[0]) continue;
148
+ out.push(k);
149
+ }
150
+ return out;
151
+ }
152
+
153
+ // R3: label for the run starting at kids[i]. Returns {label, consume} (consume =
154
+ // preceding siblings absorbed into the label), null for no label, undefined for
155
+ // "don't collapse" (a content-named container already says it).
156
+ function labelFor(parent, kids, i, chain) {
157
+ // lead-in: preceding text leaves back to an anchor (heading / interactive leaf)
158
+ const lead = []; let k = i - 1, found = false;
159
+ while (k >= 0 && lead.length < 3) {
160
+ const s = kids[k];
161
+ if (s.text !== undefined || s.children.length) break;
162
+ if (s.g !== kids[i].g) break;
163
+ if (s.role === 'heading' || INTERACTIVE.has(s.role)) {
164
+ if (!lead.length && s.role === kids[i].role) break; // an adjacent same-role item is a peer, not a title
165
+ lead.unshift(withRef(s)); found = true; break;
166
+ }
167
+ lead.unshift(s.name); k--;
168
+ }
169
+ if (found) {
170
+ let extra = 0; const a = kids[k];
171
+ while (k - 1 - extra >= 0 && isLeaf(kids[k - 1 - extra]) && kids[k - 1 - extra].role === a.role && kids[k - 1 - extra].name === a.name) extra++; // image-link twin of the anchor
172
+ return { label: [...new Set(lead)].join(' · '), consume: lead.length + extra };
173
+ }
174
+ // else: nearest preceding heading at this level, then the level's named container, then climb
175
+ const up = chain.slice().reverse(); let sibs = kids, idx = i, node = parent, m = 0;
176
+ for (;;) {
177
+ for (let j = idx - 1; j >= 0; j--) if (sibs[j].role === 'heading' && sibs[j].name) return { label: sibs[j].name, consume: 0 };
178
+ if (node.name && node.role !== 'root') return NFC.has(node.role) ? undefined : { label: node.name, consume: 0 };
179
+ if (m >= up.length) return null;
180
+ ({ sibs, i: idx, node } = up[m++]);
181
+ }
182
+ }
183
+ // Roles of the lead-in leaves back to the anchor; null if there is no anchor.
184
+ function leadShape(kids, i) {
185
+ const shape = [];
186
+ for (let k = i - 1; k >= 0 && shape.length < 3; k--) {
187
+ const s = kids[k];
188
+ if (s.text !== undefined || s.children.length || s.g !== kids[i].g) return null;
189
+ if (!shape.length && s.role === kids[i].role) return null;
190
+ shape.unshift(s.role + (s.props.disabled ? '!' : ''));
191
+ if (s.role === 'heading' || INTERACTIVE.has(s.role)) return shape.join('+');
192
+ }
193
+ return null;
194
+ }
195
+
196
+ // R1-R3
197
+ function collapseRuns(parent, chain) {
198
+ const kids = parent.children;
199
+ const out = [];
200
+ // R2 evidence: kinds that form a >=MIN_RUN run (with a lead-in shape) in this sibling list
201
+ const strong = new Set();
202
+ for (let i = 0; i < kids.length;) {
203
+ const kd = kindOf(kids[i]); let j = i;
204
+ if (kd) while (j + 1 < kids.length && kindOf(kids[j + 1]) === kd) j++;
205
+ if (kd && j - i + 1 >= MIN_RUN) { const sh = leadShape(kids, i); if (sh) strong.add(kd + '#' + sh); }
206
+ i = j + 1;
207
+ }
208
+ for (let i = 0; i < kids.length;) {
209
+ const kd = kindOf(kids[i]);
210
+ let j = i;
211
+ if (kd) while (j + 1 < kids.length && kindOf(kids[j + 1]) === kd) j++;
212
+ const len = j - i + 1;
213
+ const weak = kd && len === 2 && strong.has(kd + '#' + leadShape(kids, i));
214
+ if (kd && (len >= MIN_RUN || weak)) {
215
+ const items = kids.slice(i, j + 1);
216
+ const lf = labelFor(parent, kids, i, chain);
217
+ if (lf === undefined) { out.push(...items); i = j + 1; continue; }
218
+ if (lf && lf.consume) out.splice(out.length - lf.consume, lf.consume); // lead-in leaves now live in the label
219
+ const dis = items[0].props.disabled ? ' (disabled)' : '';
220
+ const body = items.map(withRef).join(', ');
221
+ out.push({ role: '_line', name: '', children: [], props: {}, text: lf ? `${lf.label}${dis}: ${body}` : body });
222
+ i = j + 1;
223
+ } else { out.push(kids[i]); i++; }
224
+ }
225
+ return dedupe(out);
226
+ }
227
+
228
+ // ---------- Tables (T1-T4) ----------
229
+ function xformTable(t, chain) {
230
+ const rows = [];
231
+ (function walk(n) { for (const c of n.children) { if (c.role === 'row' || c.role === 'LayoutTableRow') rows.push(c); else if (c.text === undefined) walk(c); } })(t);
232
+ const cellsOf = (r) => r.children.filter((c) => CELLS.has(c.role) || c.role === 'LayoutTableCell');
233
+ const isHeaderRow = (r) => { const cs = cellsOf(r); return cs.some((c) => c.role === 'columnheader') && !cs.some((c) => (c.role === 'cell' || c.role === 'gridcell') && (c.name || c.children.length)); };
234
+ const xcell = (c) => xform(c, [...chain, { sibs: [t], i: 0, node: t }]);
235
+ const out = [];
236
+ for (const c of t.children.filter((c) => !rows.includes(c) && c.role !== 'rowgroup')) out.push(xform(c, [...chain, { sibs: t.children, i: 0, node: t }]));
237
+ // T1: leading header rows (multi-row headers are merged per column)
238
+ const hdrRows = []; let hdrDone = false;
239
+ for (const r of rows) { if (!hdrDone && isHeaderRow(r)) hdrRows.push(r); else if (hdrRows.length) hdrDone = true; }
240
+ let headers = null;
241
+ if (hdrRows.length) {
242
+ const w = Math.max(...hdrRows.map((r) => cellsOf(r).length));
243
+ headers = Array.from({ length: w }, (_, i) => [...new Set(hdrRows.map((r) => { const c = cellsOf(r)[i]; return c ? inline(xcell(c), false) : ''; }).filter(Boolean))].join(' / '));
244
+ }
245
+ const dataRows = rows.filter((r) => !hdrRows.includes(r));
246
+ // T3: positional mapping only when every row is exactly as wide as the header
247
+ const useHdr = headers && dataRows.every((r) => cellsOf(r).length === headers.length) ? headers : null;
248
+ if (headers && !useHdr) out.push({ role: '_line', children: [], props: {}, text: `columns: ${headers.filter(Boolean).join(' · ')} [header/row width mismatch: positional mapping unsafe]` });
249
+ const firstRef = (n) => (showRef(n) ? n.ref : (n.children || []).map(firstRef).find(Boolean));
250
+ for (const r of dataRows) {
251
+ // T2
252
+ const cs = cellsOf(r);
253
+ const labelCells = cs.filter((c) => c.role === 'rowheader' || c.role === 'columnheader');
254
+ const lcx = labelCells.map(xcell);
255
+ const label = lcx.map((c) => inline(c, false)).filter(Boolean).join(' / ');
256
+ const labelUrls = urlsText([...new Set(lcx.flatMap(allUrls))]);
257
+ const labelRef = lcx.map(firstRef).find(Boolean);
258
+ const vals = [];
259
+ cs.forEach((c, idx) => {
260
+ if (labelCells.includes(c)) return;
261
+ const v = inline(xcell(c), true);
262
+ if (v) vals.push({ h: useHdr ? useHdr[idx] : '', v });
263
+ });
264
+ // T4: modal-value compression for wide rows
265
+ let tail = '';
266
+ if (vals.length >= 6) {
267
+ const cnt = new Map(); vals.forEach((x) => cnt.set(x.v, (cnt.get(x.v) || 0) + 1));
268
+ const [mv, mc] = [...cnt.entries()].sort((a, b) => b[1] - a[1])[0];
269
+ if (mc >= 4 && mc >= vals.length * 0.3) {
270
+ tail = ` (other ${mc} columns: ${mv})`;
271
+ for (let i = vals.length - 1; i >= 0; i--) if (vals[i].v === mv) vals.splice(i, 1);
272
+ }
273
+ }
274
+ if (!vals.length && !label) continue; // header-only / empty rows
275
+ const body = vals.map((x) => (x.h ? `${x.h}: ` : '') + x.v).join(' · ');
276
+ out.push({ role: '_line', children: [], props: {}, text: `row${label ? ` "${label}"` : ''}${labelUrls}${labelRef ? ` [ref=${labelRef}]` : ''}${tail}${body ? ': ' + body : ''}` });
277
+ }
278
+ return { ...t, children: out };
279
+ }
280
+
281
+ // ---------- Run ----------
282
+ let tops = clean(root);
283
+ if (tops.length === 1 && (tops[0].role === 'RootWebArea' || tops[0].role === 'WebArea')) tops = tops[0].children;
284
+ const find = (ns, role) => { for (const n of ns) { if (n.role === role) return n; const f = find(n.children, role); if (f) return f; } return null; };
285
+ const main = find(tops, 'main');
286
+ if (main && main.children.length) tops = [main];
287
+ const res = xform({ role: 'root', name: '', children: tops, props: {} }, []);
288
+
289
+ const lines = [];
290
+ function emit(c, d) {
291
+ const ind = ' '.repeat(d);
292
+ if (c.text !== undefined) {
293
+ lines.push(`${ind}- ${c.text}`);
294
+ return;
295
+ }
296
+ if (isLeaf(c) && (c.role === 'StaticText' || c.role === 'generic')) { lines.push(`${ind}- ${c.name}`); return; }
297
+ let l = `${ind}- ${c.role}`;
298
+ if (c.name) l += ` "${c.name}"`;
299
+ l += urlPart(c);
300
+ const p = c.props, pp = [];
301
+ if (p.checked !== undefined) pp.push(`checked=${p.checked}`);
302
+ if (p.disabled) pp.push('disabled');
303
+ if (p.expanded !== undefined) pp.push(`expanded=${p.expanded}`);
304
+ if (p.level) pp.push(`level=${p.level}`);
305
+ if (p.selected) pp.push('selected');
306
+ if (p.required && !CELLS.has(c.role)) pp.push('required');
307
+ if (p.value !== undefined && p.value !== '') pp.push(`value="${p.value}"`);
308
+ if (pp.length) l += ` [${pp.join(', ')}]`;
309
+ if (showRef(c)) l += ` [ref=${c.ref}]`;
310
+ lines.push(l);
311
+ for (const k of c.children) emit(k, d + 1);
312
+ }
313
+ for (const c of res.children) emit(c, 0);
314
+ return lines.length ? [LEGEND + (opts.links ? LINKS_NOTE : ''), ...lines].join('\n') : '';
315
+ }