-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy patharia_snapshot_filter.js
More file actions
112 lines (107 loc) · 3.77 KB
/
aria_snapshot_filter.js
File metadata and controls
112 lines (107 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// LICENSE_CODE ZON
'use strict'; /*jslint node:true es9:true*/
export class Aria_snapshot_filter {
static INTERACTIVE_ROLES = new Set([
'button', 'link', 'textbox', 'searchbox', 'combobox', 'checkbox',
'radio', 'switch', 'slider', 'tab', 'menuitem', 'option',
]);
static parse_playwright_snapshot(snapshot_text){
const lines = snapshot_text.split('\n');
const elements = [];
for (const line of lines)
{
const trimmed = line.trim();
if (!trimmed || !trimmed.startsWith('-'))
continue;
const ref_match = trimmed.match(/\[ref=([^\]]+)\]/);
if (!ref_match)
continue;
const ref = ref_match[1];
const role_match = trimmed.match(/^-\s+([a-zA-Z]+)/);
if (!role_match)
continue;
const role = role_match[1];
if (!this.INTERACTIVE_ROLES.has(role))
continue;
const name_match = trimmed.match(/"([^"]*)"/);
const name = name_match ? name_match[1] : '';
let url = null;
const next_line_index = lines.indexOf(line)+1;
if (next_line_index<lines.length)
{
const next_line = lines[next_line_index];
const url_match = next_line.match(/\/url:\s*(.+)/);
if (url_match)
url = url_match[1].trim().replace(/^["']|["']$/g, '');
}
elements.push({ref, role, name, url});
}
return elements;
}
static format_compact(elements){
const lines = [];
for (const el of elements)
{
const parts = [`[${el.ref}]`, el.role];
if (el.name && el.name.length>0)
{
const name = el.name.length>60 ?
el.name.substring(0, 57)+'...' : el.name;
parts.push(`"${name}"`);
}
if (el.url && el.url.length>0 && !el.url.startsWith('#'))
{
let url = el.url;
if (url.length>50)
url = url.substring(0, 47)+'...';
parts.push(`-> ${url}`);
}
lines.push(parts.join(' '));
}
return lines.join('\n');
}
static filter_snapshot(snapshot_text){
if (typeof snapshot_text!=='string')
{
try {
return typeof snapshot_text==='object'
? JSON.stringify(snapshot_text, null, 2)
: String(snapshot_text);
} catch(e){
return String(snapshot_text);
}
}
try {
const elements = this.parse_playwright_snapshot(snapshot_text);
if (elements.length==0)
return 'No interactive elements found';
return this.format_compact(elements);
} catch(e){
return `Error filtering snapshot: ${e.message}\n${e.stack}`;
}
}
static format_dom_elements(elements){
if (!elements || elements.length==0)
return null;
const lines = [];
for (const el of elements)
{
const parts = [`[${el.ref}]`, el.role || 'unknown'];
if (el.name && el.name.length>0)
{
const name = el.name.length>60 ?
el.name.substring(0, 57)+'...' : el.name;
parts.push(`"${name}"`);
}
if (el.url && el.url.length>0 && !el.url.startsWith('#'))
{
let url = el.url;
if (url.length>50)
url = url.substring(0, 47)+'...';
parts.push(`-> ${url}`);
}
lines.push(parts.join(' '));
}
return lines.join('\n');
}
}