162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297 | def flatten_axtree(
AX_tree,
extra_properties: dict | None = None,
with_visible: bool = False,
with_clickable: bool = False,
with_center_coords: bool = False,
with_bounding_box_coords: bool = False,
with_som: bool = False,
filter_visible_only: bool = True,
filter_with_bid_only: bool = False,
filter_som_only: bool = False,
coord_decimals: int = 0,
ignored_roles=IGNORED_ROLES,
ignored_properties=IGNORED_AXTREE_PROPERTIES,
ignore_navigation: bool = False,
hide_bid_if_invisible: bool = False,
hide_all_children: bool = False,
nodes_with_bid: list[str] = NODES_WITH_BID,
) -> str:
"""Formats the accessibility tree into a string text"""
if ignore_navigation:
ignored_roles += NAVIGATION_ROLES
extra_properties = extra_properties or {}
node_id_to_idx = {}
for idx, node in enumerate(AX_tree["nodes"]):
node_id_to_idx[node["nodeId"]] = idx
def dfs(node_idx: int, depth: int, parent_node_filtered: bool) -> str:
tree_str = ""
node = AX_tree["nodes"][node_idx]
indent = " " * depth
skip_node = False
filter_node = False
node_role = node["role"]["value"]
if node_role in ignored_roles:
return tree_str
elif "name" not in node:
skip_node = True
pass
else:
node_name = node["name"]["value"]
if "value" in node and "value" in node["value"]:
node_value = node["value"]["value"]
else:
node_value = None
attributes = []
bid = node.get("browsergym_id", None)
for property in node.get("properties", []):
if "value" not in property:
continue
if "value" not in property["value"]:
continue
prop_name = property["name"]
prop_value = property["value"]["value"]
if prop_name == "browsergym_id":
bid = prop_value
elif prop_name in ignored_properties:
continue
elif prop_name in ("required", "focused", "atomic"):
if prop_value:
attributes.append(prop_name)
else:
attributes.append(f"{prop_name}={repr(prop_value)}")
if node_role == "generic" and not attributes:
skip_node = True
elif node_role != "StaticText":
filter_node, extra_attributes_to_print = _process_bid(
bid,
extra_properties=extra_properties,
with_visible=with_visible,
with_clickable=with_clickable,
with_center_coords=with_center_coords,
with_bounding_box_coords=with_bounding_box_coords,
with_som=with_som,
filter_visible_only=filter_visible_only,
filter_with_bid_only=filter_with_bid_only,
filter_som_only=filter_som_only,
coord_decimals=coord_decimals,
)
# if either is True, skip the node
skip_node = skip_node or filter_node or (hide_all_children and parent_node_filtered)
# insert extra attributes before regular attributes
attributes = extra_attributes_to_print + attributes
# actually print the node string
if not skip_node:
if node_role == "paragraph":
node_str = ""
elif node_role == "StaticText":
node_str = node_name.strip()
else:
node_repr = node_name.strip()
if node_repr and node_role != "checkbox":
node_str = f"{node_role} {node_repr}"
else:
node_str = "-" if node_role == "listitem" else node_role
if (
not (
bid is None
or (hide_bid_if_invisible and extra_properties.get(bid, {}).get("visibility", 0) < 0.5)
)
and node_role in nodes_with_bid
):
node_str = f"BID:{bid} " + node_str
if node_value is not None:
node_str += f' value={repr(node["value"]["value"])}'
if attributes:
node_str += ", ".join([""] + attributes)
if "'Advertisement'" in node_str:
return tree_str
tree_str += f"{indent}{node_str}"
for child_node_id in node["childIds"]:
if child_node_id not in node_id_to_idx or child_node_id == node["nodeId"]:
continue
# mark this to save some tokens
child_depth = depth if skip_node else (depth + 1)
child_str = dfs(node_id_to_idx[child_node_id], child_depth, parent_node_filtered=filter_node or skip_node)
if child_str and node_role != "link":
if tree_str:
tree_str += "\n"
tree_str += child_str
return tree_str
return dfs(0, 0, False)
|