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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:expandtab:shiftwidth=2:tabstop=2:
*/
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsTextEquivUtils.h"
#include "LocalAccessible.h"
#include "LocalAccessible-inl.h"
#include "AccIterator.h"
#include "nsCoreUtils.h"
#include "mozilla/dom/Text.h"
#include "nsIContentInlines.h"
using namespace mozilla;
using namespace mozilla::a11y;
/**
* The accessible for which we are computing a text equivalent. It is useful
* for bailing out during recursive text computation, or for special cases
* like step f. of the ARIA implementation guide.
*/
static const Accessible* sInitiatorAcc = nullptr;
////////////////////////////////////////////////////////////////////////////////
// nsTextEquivUtils. Public.
nsresult nsTextEquivUtils::GetNameFromSubtree(
const LocalAccessible* aAccessible, nsAString& aName) {
aName.Truncate();
if (sInitiatorAcc) return NS_OK;
sInitiatorAcc = aAccessible;
if (GetRoleRule(aAccessible->Role()) == eNameFromSubtreeRule) {
// XXX: is it necessary to care the accessible is not a document?
if (aAccessible->IsContent()) {
nsAutoString name;
AppendFromAccessibleChildren(aAccessible, &name);
name.CompressWhitespace();
if (!nsCoreUtils::IsWhitespaceString(name)) aName = name;
}
}
sInitiatorAcc = nullptr;
return NS_OK;
}
nsresult nsTextEquivUtils::GetTextEquivFromIDRefs(
const LocalAccessible* aAccessible, nsAtom* aIDRefsAttr,
nsAString& aTextEquiv) {
aTextEquiv.Truncate();
nsIContent* content = aAccessible->GetContent();
if (!content) return NS_OK;
nsIContent* refContent = nullptr;
IDRefsIterator iter(aAccessible->Document(), content, aIDRefsAttr);
while ((refContent = iter.NextElem())) {
if (!aTextEquiv.IsEmpty()) aTextEquiv += ' ';
nsresult rv =
AppendTextEquivFromContent(aAccessible, refContent, &aTextEquiv);
NS_ENSURE_SUCCESS(rv, rv);
}
return NS_OK;
}
nsresult nsTextEquivUtils::AppendTextEquivFromContent(
const LocalAccessible* aInitiatorAcc, nsIContent* aContent,
nsAString* aString) {
// Prevent recursion which can cause infinite loops.
if (sInitiatorAcc) return NS_OK;
sInitiatorAcc = aInitiatorAcc;
// If the given content is not visible or isn't accessible then go down
// through the DOM subtree otherwise go down through accessible subtree and
// calculate the flat string.
nsIFrame* frame = aContent->GetPrimaryFrame();
bool isVisible = frame && frame->StyleVisibility()->IsVisible();
nsresult rv = NS_ERROR_FAILURE;
bool goThroughDOMSubtree = true;
if (isVisible) {
LocalAccessible* accessible =
aInitiatorAcc->Document()->GetAccessible(aContent);
if (accessible) {
rv = AppendFromAccessible(accessible, aString);
goThroughDOMSubtree = false;
}
}
if (goThroughDOMSubtree) rv = AppendFromDOMNode(aContent, aString);
sInitiatorAcc = nullptr;
return rv;
}
nsresult nsTextEquivUtils::AppendTextEquivFromTextContent(nsIContent* aContent,
nsAString* aString) {
if (aContent->IsText()) {
if (aContent->TextLength() > 0) {
nsIFrame* frame = aContent->GetPrimaryFrame();
if (frame) {
nsIFrame::RenderedText text = frame->GetRenderedText(
0, UINT32_MAX, nsIFrame::TextOffsetType::OffsetsInContentText,
nsIFrame::TrailingWhitespace::DontTrim);
aString->Append(text.mString);
} else {
// If aContent is an object that is display: none, we have no a frame.
aContent->GetAsText()->AppendTextTo(*aString);
}
}
return NS_OK;
}
if (aContent->IsHTMLElement() &&
aContent->NodeInfo()->Equals(nsGkAtoms::br)) {
aString->AppendLiteral("\r\n");
return NS_OK;
}
return NS_OK_NO_NAME_CLAUSE_HANDLED;
}
nsresult nsTextEquivUtils::AppendFromDOMChildren(nsIContent* aContent,
nsAString* aString) {
for (nsIContent* childContent = aContent->GetFirstChild(); childContent;
childContent = childContent->GetNextSibling()) {
nsresult rv = AppendFromDOMNode(childContent, aString);
NS_ENSURE_SUCCESS(rv, rv);
}
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
// nsTextEquivUtils. Private.
nsresult nsTextEquivUtils::AppendFromAccessibleChildren(
const Accessible* aAccessible, nsAString* aString) {
nsresult rv = NS_OK_NO_NAME_CLAUSE_HANDLED;
uint32_t childCount = aAccessible->ChildCount();
for (uint32_t childIdx = 0; childIdx < childCount; childIdx++) {
Accessible* child = aAccessible->ChildAt(childIdx);
rv = AppendFromAccessible(child, aString);
NS_ENSURE_SUCCESS(rv, rv);
}
return rv;
}
nsresult nsTextEquivUtils::AppendFromAccessible(Accessible* aAccessible,
nsAString* aString) {
// XXX: is it necessary to care the accessible is not a document?
bool isHTMLBlock = false;
if (aAccessible->IsLocal() && aAccessible->AsLocal()->IsContent()) {
nsIContent* content = aAccessible->AsLocal()->GetContent();
nsresult rv = AppendTextEquivFromTextContent(content, aString);
if (rv != NS_OK_NO_NAME_CLAUSE_HANDLED) return rv;
if (!content->IsText()) {
nsIFrame* frame = content->GetPrimaryFrame();
if (frame) {
// If this is a block level frame (as opposed to span level), we need to
// add spaces around that block's text, so we don't get words jammed
// together in final name.
const nsStyleDisplay* display = frame->StyleDisplay();
if (display->IsBlockOutsideStyle() ||
display->mDisplay == StyleDisplay::TableCell) {
isHTMLBlock = true;
if (!aString->IsEmpty()) {
aString->Append(char16_t(' '));
}
}
}
}
}
bool isEmptyTextEquiv = true;
// If the name is from tooltip then append it to result string in the end
// (see h. step of name computation guide).
nsAutoString text;
if (aAccessible->Name(text) != eNameFromTooltip) {
isEmptyTextEquiv = !AppendString(aString, text);
}
// Implementation of f. step.
nsresult rv = AppendFromValue(aAccessible, aString);
NS_ENSURE_SUCCESS(rv, rv);
if (rv != NS_OK_NO_NAME_CLAUSE_HANDLED) isEmptyTextEquiv = false;
// Implementation of g) step of text equivalent computation guide. Go down
// into subtree if accessible allows "text equivalent from subtree rule" or
// it's not root and not control.
if (isEmptyTextEquiv) {
if (ShouldIncludeInSubtreeCalculation(aAccessible)) {
rv = AppendFromAccessibleChildren(aAccessible, aString);
NS_ENSURE_SUCCESS(rv, rv);
if (rv != NS_OK_NO_NAME_CLAUSE_HANDLED) isEmptyTextEquiv = false;
}
}
// Implementation of h. step
if (isEmptyTextEquiv && !text.IsEmpty()) {
AppendString(aString, text);
if (isHTMLBlock) {
aString->Append(char16_t(' '));
}
return NS_OK;
}
if (!isEmptyTextEquiv && isHTMLBlock) {
aString->Append(char16_t(' '));
}
return rv;
}
nsresult nsTextEquivUtils::AppendFromValue(Accessible* aAccessible,
nsAString* aString) {
if (GetRoleRule(aAccessible->Role()) != eNameFromValueRule) {
return NS_OK_NO_NAME_CLAUSE_HANDLED;
}
// Implementation of step f. of text equivalent computation. If the given
// accessible is not root accessible (the accessible the text equivalent is
// computed for in the end) then append accessible value. Otherwise append
// value if and only if the given accessible is in the middle of its parent.
nsAutoString text;
if (aAccessible != sInitiatorAcc) {
aAccessible->Value(text);
return AppendString(aString, text) ? NS_OK : NS_OK_NO_NAME_CLAUSE_HANDLED;
}
// XXX: is it necessary to care the accessible is not a document?
if (aAccessible->IsDoc()) return NS_ERROR_UNEXPECTED;
for (Accessible* next = aAccessible->NextSibling(); next;
next = next->NextSibling()) {
if (!IsWhitespaceLeaf(next)) {
for (Accessible* prev = aAccessible->PrevSibling(); prev;
prev = prev->PrevSibling()) {
if (!IsWhitespaceLeaf(prev)) {
aAccessible->Value(text);
return AppendString(aString, text) ? NS_OK
: NS_OK_NO_NAME_CLAUSE_HANDLED;
}
}
}
}
return NS_OK_NO_NAME_CLAUSE_HANDLED;
}
nsresult nsTextEquivUtils::AppendFromDOMNode(nsIContent* aContent,
nsAString* aString) {
nsresult rv = AppendTextEquivFromTextContent(aContent, aString);
NS_ENSURE_SUCCESS(rv, rv);
if (rv != NS_OK_NO_NAME_CLAUSE_HANDLED) return NS_OK;
if (aContent->IsXULElement()) {
nsAutoString textEquivalent;
if (aContent->NodeInfo()->Equals(nsGkAtoms::label, kNameSpaceID_XUL)) {
aContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::value,
textEquivalent);
} else {
aContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::label,
textEquivalent);
}
if (textEquivalent.IsEmpty()) {
aContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::tooltiptext,
textEquivalent);
}
AppendString(aString, textEquivalent);
}
return AppendFromDOMChildren(aContent, aString);
}
bool nsTextEquivUtils::AppendString(nsAString* aString,
const nsAString& aTextEquivalent) {
if (aTextEquivalent.IsEmpty()) return false;
// Insert spaces to insure that words from controls aren't jammed together.
if (!aString->IsEmpty() && !nsCoreUtils::IsWhitespace(aString->Last())) {
aString->Append(char16_t(' '));
}
aString->Append(aTextEquivalent);
if (!nsCoreUtils::IsWhitespace(aString->Last())) {
aString->Append(char16_t(' '));
}
return true;
}
uint32_t nsTextEquivUtils::GetRoleRule(role aRole) {
#define ROLE(geckoRole, stringRole, ariaRole, atkRole, macRole, macSubrole, \
msaaRole, ia2Role, androidClass, nameRule) \
case roles::geckoRole: \
return nameRule;
switch (aRole) {
#include "RoleMap.h"
default:
MOZ_CRASH("Unknown role.");
}
#undef ROLE
}
bool nsTextEquivUtils::ShouldIncludeInSubtreeCalculation(
Accessible* aAccessible) {
uint32_t nameRule = GetRoleRule(aAccessible->Role());
if (nameRule == eNameFromSubtreeRule) {
return true;
}
if (!(nameRule & eNameFromSubtreeIfReqRule)) {
return false;
}
if (aAccessible == sInitiatorAcc) {
// We're calculating the text equivalent for this accessible, but this
// accessible should only be included when calculating the text equivalent
// for something else.
return false;
}
// sInitiatorAcc can be null when, for example, LocalAccessible::Value calls
// GetTextEquivFromSubtree.
role initiatorRole = sInitiatorAcc ? sInitiatorAcc->Role() : roles::NOTHING;
if (initiatorRole == roles::OUTLINEITEM &&
aAccessible->Role() == roles::GROUPING) {
// Child treeitems are contained in a group. We don't want to include those
// in the parent treeitem's text equivalent.
return false;
}
return true;
}
bool nsTextEquivUtils::IsWhitespaceLeaf(Accessible* aAccessible) {
if (!aAccessible || !aAccessible->IsTextLeaf()) {
return false;
}
nsAutoString name;
aAccessible->Name(name);
return nsCoreUtils::IsWhitespaceString(name);
}
|