Repository navigation
Expand file tree
/
Copy patha11y.go
More file actions
126 lines (116 loc) · 3.32 KB
/
Copy patha11y.go
File metadata and controls
126 lines (116 loc) · 3.32 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package biloba
import (
"context"
"encoding/json"
"fmt"
"github.com/onsi/biloba/engine"
"strings"
"github.com/chromedp/cdproto/accessibility"
)
/*
A11yOutline() returns the page's accessibility tree as indented text: one line per node,
showing each node's ARIA role and accessible name (e.g. `button "Submit"`). Nodes that
are ignored for accessibility are omitted, while their meaningful descendants are kept - so
the result is the compact, role/name-oriented view of the page that a screen reader (and,
increasingly, a reasoning model) works from.
It complements [Biloba.Outline]: where Outline shows the raw DOM, A11yOutline elides
presentational noise and surfaces semantics. The output is capped at ~32 KB.
Read https://onsi.github.io/biloba/#accessibility-outline for details.
*/
func (b *Biloba) A11yOutline() string {
b.gt.Helper()
b.guardConfig("A11yOutline")
text, err := b.a11yOutline()
if err != nil {
b.gt.Fatalf("Failed to capture accessibility outline:\n%s", err.Error())
return ""
}
return capOutline(text)
}
func (b *Biloba) a11yOutline() (string, error) {
var nodes []*accessibility.Node
err := b.runEngine("capture the accessibility tree", func(ctx context.Context) error {
var err error
if b.frame != nil {
nodes, err = engine.AccessibilityTreeForFrameContext(ctx, b.frame.id)
} else {
nodes, err = engine.AccessibilityTreeContext(ctx)
}
return err
})
if err != nil {
return "", err
}
return renderA11yTree(nodes), nil
}
func renderA11yTree(nodes []*accessibility.Node) string {
if len(nodes) == 0 {
return ""
}
byID := make(map[accessibility.NodeID]*accessibility.Node, len(nodes))
for _, n := range nodes {
byID[n.NodeID] = n
}
// The root is the first node whose parent we don't have a record of (getFullAXTree
// returns the tree root first, but we don't rely on ordering).
var root *accessibility.Node
for _, n := range nodes {
if n.ParentID == "" || byID[n.ParentID] == nil {
root = n
break
}
}
if root == nil {
root = nodes[0]
}
out := &strings.Builder{}
var walk func(n *accessibility.Node, depth int)
walk = func(n *accessibility.Node, depth int) {
// Ignored nodes (and InlineTextBox nodes, which just mirror their StaticText parent)
// contribute no semantics, so skip the node itself but keep descending into its
// children at the same depth - this flattens away presentational wrappers.
nextDepth := depth
if !n.Ignored && axValueString(n.Role) != "InlineTextBox" {
fmt.Fprintf(out, "%s%s\n", strings.Repeat(" ", depth), a11yLine(n))
nextDepth = depth + 1
}
for _, childID := range n.ChildIDs {
if child := byID[childID]; child != nil {
walk(child, nextDepth)
}
}
}
walk(root, 0)
return out.String()
}
func a11yLine(n *accessibility.Node) string {
role := axValueString(n.Role)
if role == "" {
role = "none"
}
line := role
if name := axValueString(n.Name); name != "" {
line += fmt.Sprintf(" %q", name)
}
if value := axValueString(n.Value); value != "" {
line += fmt.Sprintf(" (value: %q)", value)
}
return line
}
func axValueString(v *accessibility.Value) string {
if v == nil || len(v.Value) == 0 {
return ""
}
var decoded any
if err := json.Unmarshal([]byte(v.Value), &decoded); err != nil {
return ""
}
switch d := decoded.(type) {
case nil:
return ""
case string:
return d
default:
return fmt.Sprint(d)
}
}