-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathAccessibleNameComputation.cs
More file actions
193 lines (162 loc) · 5.29 KB
/
AccessibleNameComputation.cs
File metadata and controls
193 lines (162 loc) · 5.29 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
using AngleSharp.Dom;
namespace Bunit;
/// <summary>
/// Provides methods to compute the accessible name of an element.
/// </summary>
/// <remarks>
/// This is a simplified implementation of the Accessible Name and Description Computation algorithm.
/// See https://www.w3.org/TR/accname-1.1/ for the full specification.
/// </remarks>
[SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "Using lowercase for comparison with lowercase constant values.")]
internal static class AccessibleNameComputation
{
/// <summary>
/// Computes the accessible name of an element.
/// </summary>
public static string? GetAccessibleName(IElement element)
{
return GetNameFromAriaLabelledBy(element)
?? GetNameFromAriaLabel(element)
?? GetNameFromAssociatedLabelOrNativeContent(element)
?? GetNameFromTitleAttribute(element)
?? GetNameFromPlaceholder(element);
}
private static string? GetNameFromAriaLabelledBy(IElement element)
{
var labelledBy = element.GetAttribute("aria-labelledby");
if (string.IsNullOrWhiteSpace(labelledBy))
return null;
return GetTextFromReferencedElements(element, labelledBy);
}
private static string? GetNameFromAriaLabel(IElement element)
{
var ariaLabel = element.GetAttribute("aria-label");
return string.IsNullOrWhiteSpace(ariaLabel) ? null : ariaLabel;
}
private static string? GetNameFromAssociatedLabelOrNativeContent(IElement element)
{
var tagName = element.TagName.ToUpperInvariant();
if (tagName is "INPUT" or "SELECT" or "TEXTAREA")
{
return GetNameFromLinkedLabel(element)
?? GetNameFromInputButtonValue(element);
}
if (tagName == "IMG")
{
return GetNameFromAltAttribute(element);
}
if (tagName is "BUTTON" or "A" or "H1" or "H2" or "H3" or "H4" or "H5" or "H6")
{
return GetNonEmptyTextContent(element);
}
return null;
}
private static string? GetNameFromTitleAttribute(IElement element)
{
var title = element.GetAttribute("title");
return string.IsNullOrWhiteSpace(title) ? null : title;
}
private static string? GetNameFromPlaceholder(IElement element)
{
var tagName = element.TagName.ToUpperInvariant();
if (tagName is not ("INPUT" or "TEXTAREA"))
return null;
var placeholder = element.GetAttribute("placeholder");
return string.IsNullOrWhiteSpace(placeholder) ? null : placeholder;
}
private static string? GetTextFromReferencedElements(IElement element, string spaceDelimitedIds)
{
var ids = spaceDelimitedIds.Split(' ', StringSplitOptions.RemoveEmptyEntries);
var texts = new List<string>();
var root = GetRootElement(element);
foreach (var id in ids)
{
var referencedElement = element.Owner?.GetElementById(id) ?? root?.QuerySelector($"#{id}");
if (referencedElement == null)
continue;
var text = referencedElement.TextContent.Trim();
if (!string.IsNullOrWhiteSpace(text))
texts.Add(text);
}
return texts.Count > 0 ? string.Join(" ", texts) : null;
}
private static IElement? GetRootElement(IElement element)
{
var current = element;
while (current.ParentElement != null)
{
current = current.ParentElement;
}
return current;
}
private static string? GetNameFromLinkedLabel(IElement element)
{
var id = element.GetAttribute("id");
if (!string.IsNullOrWhiteSpace(id))
{
var linkedLabel = FindLabelWithForAttribute(element, id);
if (linkedLabel != null)
{
return linkedLabel.TextContent.Trim();
}
}
var wrappingLabel = element.Closest("label");
if (wrappingLabel != null)
{
return GetTextContentExcludingElement(wrappingLabel, element);
}
return null;
}
private static IElement? FindLabelWithForAttribute(IElement element, string id)
{
var label = element.Owner?.QuerySelector($"label[for='{id}']");
if (label != null)
return label;
var root = GetRootElement(element);
return root?.QuerySelector($"label[for='{id}']");
}
private static string? GetNameFromInputButtonValue(IElement element)
{
if (!element.TagName.Equals("INPUT", StringComparison.OrdinalIgnoreCase))
return null;
var inputType = element.GetAttribute("type")?.ToLowerInvariant();
if (inputType is not ("button" or "submit" or "reset"))
return null;
var value = element.GetAttribute("value");
return string.IsNullOrWhiteSpace(value) ? null : value;
}
private static string? GetNameFromAltAttribute(IElement element)
{
var alt = element.GetAttribute("alt");
return string.IsNullOrWhiteSpace(alt) ? null : alt;
}
private static string? GetNonEmptyTextContent(IElement element)
{
var textContent = element.TextContent.Trim();
return string.IsNullOrWhiteSpace(textContent) ? null : textContent;
}
private static string GetTextContentExcludingElement(IElement container, IElement excludeElement)
{
var texts = new List<string>();
CollectTextNodesExcluding(container, excludeElement, texts);
return string.Join(" ", texts).Trim();
}
private static void CollectTextNodesExcluding(INode node, IElement excludeElement, List<string> texts)
{
foreach (var child in node.ChildNodes)
{
if (child == excludeElement)
continue;
if (child.NodeType == NodeType.Text)
{
var text = child.TextContent.Trim();
if (!string.IsNullOrWhiteSpace(text))
texts.Add(text);
}
else if (child.NodeType == NodeType.Element)
{
CollectTextNodesExcluding(child, excludeElement, texts);
}
}
}
}