forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathax_computed_node_data.cc
519 lines (464 loc) · 19.8 KB
/
ax_computed_node_data.cc
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/accessibility/ax_computed_node_data.h"
#include "base/check_op.h"
#include "base/i18n/break_iterator.h"
#include "base/logging.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node.h"
#include "ui/accessibility/ax_node_position.h"
#include "ui/accessibility/ax_position.h"
#include "ui/accessibility/ax_range.h"
#include "ui/accessibility/ax_tree_manager.h"
#include "ui/accessibility/ax_tree_manager_map.h"
namespace ui {
AXComputedNodeData::AXComputedNodeData(const AXNode& node) : owner_(&node) {}
AXComputedNodeData::~AXComputedNodeData() = default;
int AXComputedNodeData::GetOrComputeUnignoredIndexInParent() const {
DCHECK(!owner_->IsIgnored())
<< "Ignored nodes cannot have an `unignored index in parent`.\n"
<< *owner_;
if (unignored_index_in_parent_)
return *unignored_index_in_parent_;
if (const AXNode* unignored_parent = SlowGetUnignoredParent()) {
DCHECK_NE(unignored_parent->id(), kInvalidAXNodeID)
<< "All nodes should have a valid ID.\n"
<< *owner_;
unignored_parent->GetComputedNodeData().ComputeUnignoredValues();
} else {
// This should be the root node and, by convention, we assign it an
// index-in-parent of 0.
unignored_index_in_parent_ = 0;
unignored_parent_id_ = kInvalidAXNodeID;
}
return *unignored_index_in_parent_;
}
AXNodeID AXComputedNodeData::GetOrComputeUnignoredParentID() const {
if (unignored_parent_id_)
return *unignored_parent_id_;
if (const AXNode* unignored_parent = SlowGetUnignoredParent()) {
DCHECK_NE(unignored_parent->id(), kInvalidAXNodeID)
<< "All nodes should have a valid ID.\n"
<< *owner_;
unignored_parent_id_ = unignored_parent->id();
} else {
// This should be the root node and, by convention, we assign it an
// index-in-parent of 0.
DCHECK(!owner_->GetParent())
<< "If `unignored_parent` is nullptr, then this should be the "
"rootnode, since in all trees the rootnode should be unignored.\n"
<< *owner_;
unignored_index_in_parent_ = 0;
unignored_parent_id_ = kInvalidAXNodeID;
}
return *unignored_parent_id_;
}
AXNode* AXComputedNodeData::GetOrComputeUnignoredParent() const {
DCHECK(owner_->tree())
<< "All nodes should be owned by an accessibility tree.\n"
<< *owner_;
return owner_->tree()->GetFromId(GetOrComputeUnignoredParentID());
}
int AXComputedNodeData::GetOrComputeUnignoredChildCount() const {
DCHECK(!owner_->IsIgnored())
<< "Ignored nodes cannot have an `unignored child count`.\n"
<< *owner_;
if (!unignored_child_count_)
ComputeUnignoredValues();
return *unignored_child_count_;
}
const std::vector<AXNodeID>& AXComputedNodeData::GetOrComputeUnignoredChildIDs()
const {
DCHECK(!owner_->IsIgnored())
<< "Ignored nodes cannot have `unignored child IDs`.\n"
<< *owner_;
if (!unignored_child_ids_)
ComputeUnignoredValues();
return *unignored_child_ids_;
}
bool AXComputedNodeData::GetOrComputeIsDescendantOfPlatformLeaf() const {
if (!is_descendant_of_leaf_)
ComputeIsDescendantOfPlatformLeaf();
return *is_descendant_of_leaf_;
}
bool AXComputedNodeData::HasOrCanComputeAttribute(
const ax::mojom::StringAttribute attribute) const {
if (owner_->data().HasStringAttribute(attribute))
return true;
switch (attribute) {
case ax::mojom::StringAttribute::kValue:
// The value attribute could be computed on the browser for content
// editables and ARIA text/search boxes.
return owner_->data().IsNonAtomicTextField();
default:
return false;
}
}
bool AXComputedNodeData::HasOrCanComputeAttribute(
const ax::mojom::IntListAttribute attribute) const {
if (owner_->data().HasIntListAttribute(attribute))
return true;
switch (attribute) {
case ax::mojom::IntListAttribute::kLineStarts:
case ax::mojom::IntListAttribute::kLineEnds:
case ax::mojom::IntListAttribute::kSentenceStarts:
case ax::mojom::IntListAttribute::kSentenceEnds:
case ax::mojom::IntListAttribute::kWordStarts:
case ax::mojom::IntListAttribute::kWordEnds:
return true;
default:
return false;
}
}
const std::string& AXComputedNodeData::GetOrComputeAttributeUTF8(
const ax::mojom::StringAttribute attribute) const {
if (owner_->data().HasStringAttribute(attribute))
return owner_->data().GetStringAttribute(attribute);
switch (attribute) {
case ax::mojom::StringAttribute::kValue:
if (owner_->data().IsNonAtomicTextField()) {
DCHECK(HasOrCanComputeAttribute(attribute))
<< "Code in `HasOrCanComputeAttribute` should be in sync with "
"'GetOrComputeAttributeUTF8`";
return GetOrComputeTextContentWithParagraphBreaksUTF8();
}
// If an atomic text field has no value attribute sent from the renderer,
// then it means that it is empty, since we do not compute the values of
// such controls on the browser. The same for all other controls, other
// than non-atomic text fields.
return base::EmptyString();
default:
// This is a special case: for performance reasons do not use
// `base::EmptyString()` in other places throughout the codebase.
return base::EmptyString();
}
}
std::u16string AXComputedNodeData::GetOrComputeAttributeUTF16(
const ax::mojom::StringAttribute attribute) const {
if (owner_->data().HasStringAttribute(attribute))
return owner_->data().GetString16Attribute(attribute);
switch (attribute) {
case ax::mojom::StringAttribute::kValue:
if (owner_->data().IsNonAtomicTextField()) {
DCHECK(HasOrCanComputeAttribute(attribute))
<< "Code in `HasOrCanComputeAttribute` should be in sync with "
"'GetOrComputeAttributeUTF16`";
return GetOrComputeTextContentWithParagraphBreaksUTF16();
}
// If an atomic text field has no value attribute sent from the renderer,
// then it means that it is empty, since we do not compute the values of
// such controls on the browser. The same for all other controls, other
// than non-atomic text fields.
return std::u16string();
default:
return std::u16string();
}
}
const std::vector<int32_t>& AXComputedNodeData::GetOrComputeAttribute(
const ax::mojom::IntListAttribute attribute) const {
if (owner_->data().HasIntListAttribute(attribute))
return owner_->data().GetIntListAttribute(attribute);
const std::vector<int32_t>* result = nullptr;
switch (attribute) {
case ax::mojom::IntListAttribute::kLineStarts:
ComputeLineOffsetsIfNeeded();
result = &(*line_starts_);
break;
case ax::mojom::IntListAttribute::kLineEnds:
ComputeLineOffsetsIfNeeded();
result = &(*line_ends_);
break;
case ax::mojom::IntListAttribute::kSentenceStarts:
ComputeSentenceOffsetsIfNeeded();
result = &(*sentence_starts_);
break;
case ax::mojom::IntListAttribute::kSentenceEnds:
ComputeSentenceOffsetsIfNeeded();
result = &(*sentence_ends_);
break;
case ax::mojom::IntListAttribute::kWordStarts:
ComputeWordOffsetsIfNeeded();
result = &(*word_starts_);
break;
case ax::mojom::IntListAttribute::kWordEnds:
ComputeWordOffsetsIfNeeded();
result = &(*word_ends_);
break;
default:
return owner_->data().GetIntListAttribute(
ax::mojom::IntListAttribute::kNone);
}
DCHECK(HasOrCanComputeAttribute(attribute))
<< "Code in `HasOrCanComputeAttribute` should be in sync with "
"'GetOrComputeAttribute`";
DCHECK(result);
return *result;
}
const std::string&
AXComputedNodeData::GetOrComputeTextContentWithParagraphBreaksUTF8() const {
if (!text_content_with_paragraph_breaks_utf8_) {
VLOG_IF(1, text_content_with_paragraph_breaks_utf16_)
<< "Only a single encoding of text content with paragraph breaks "
"should be cached.";
auto range =
AXRange<AXPosition<AXNodePosition, AXNode>>::RangeOfContents(*owner_);
text_content_with_paragraph_breaks_utf8_ = base::UTF16ToUTF8(
range.GetText(AXTextConcatenationBehavior::kWithParagraphBreaks,
AXEmbeddedObjectBehavior::kSuppressCharacter));
}
return *text_content_with_paragraph_breaks_utf8_;
}
const std::u16string&
AXComputedNodeData::GetOrComputeTextContentWithParagraphBreaksUTF16() const {
if (!text_content_with_paragraph_breaks_utf16_) {
VLOG_IF(1, text_content_with_paragraph_breaks_utf8_)
<< "Only a single encoding of text content with paragraph breaks "
"should be cached.";
auto range =
AXRange<AXPosition<AXNodePosition, AXNode>>::RangeOfContents(*owner_);
text_content_with_paragraph_breaks_utf16_ =
range.GetText(AXTextConcatenationBehavior::kWithParagraphBreaks,
AXEmbeddedObjectBehavior::kSuppressCharacter);
}
return *text_content_with_paragraph_breaks_utf16_;
}
const std::string& AXComputedNodeData::GetOrComputeTextContentUTF8() const {
if (!text_content_utf8_) {
VLOG_IF(1, text_content_utf16_)
<< "Only a single encoding of text content should be cached.";
text_content_utf8_ = ComputeTextContentUTF8();
}
return *text_content_utf8_;
}
const std::u16string& AXComputedNodeData::GetOrComputeTextContentUTF16() const {
if (!text_content_utf16_) {
VLOG_IF(1, text_content_utf8_)
<< "Only a single encoding of text content should be cached.";
text_content_utf16_ = ComputeTextContentUTF16();
}
return *text_content_utf16_;
}
int AXComputedNodeData::GetOrComputeTextContentLengthUTF8() const {
return static_cast<int>(GetOrComputeTextContentUTF8().length());
}
int AXComputedNodeData::GetOrComputeTextContentLengthUTF16() const {
return static_cast<int>(GetOrComputeTextContentUTF16().length());
}
void AXComputedNodeData::ComputeUnignoredValues(
AXNodeID unignored_parent_id,
int starting_index_in_parent) const {
DCHECK_GE(starting_index_in_parent, 0);
// Reset any previously computed values.
unignored_index_in_parent_ = absl::nullopt;
unignored_parent_id_ = absl::nullopt;
unignored_child_count_ = absl::nullopt;
unignored_child_ids_ = absl::nullopt;
AXNodeID unignored_parent_id_for_child = unignored_parent_id;
if (!owner_->IsIgnored())
unignored_parent_id_for_child = owner_->id();
int unignored_child_count = 0;
std::vector<AXNodeID> unignored_child_ids;
for (auto iter = owner_->AllChildrenBegin(); iter != owner_->AllChildrenEnd();
++iter) {
const AXComputedNodeData& computed_data = iter->GetComputedNodeData();
int new_index_in_parent = starting_index_in_parent + unignored_child_count;
if (iter->IsIgnored()) {
// Skip the ignored node and recursively look at its children.
computed_data.ComputeUnignoredValues(unignored_parent_id_for_child,
new_index_in_parent);
DCHECK(computed_data.unignored_child_count_);
unignored_child_count += *computed_data.unignored_child_count_;
DCHECK(computed_data.unignored_child_ids_);
unignored_child_ids.insert(unignored_child_ids.end(),
computed_data.unignored_child_ids_->begin(),
computed_data.unignored_child_ids_->end());
} else {
// Setting `unignored_index_in_parent_` and `unignored_parent_id_` is the
// responsibility of the parent node, since only the parent node can
// calculate these values. This is in contrast to `unignored_child_count_`
// and `unignored_child_ids_` that are only set if this method is called
// on the node itself.
computed_data.unignored_index_in_parent_ = new_index_in_parent;
if (unignored_parent_id_for_child != kInvalidAXNodeID)
computed_data.unignored_parent_id_ = unignored_parent_id_for_child;
++unignored_child_count;
unignored_child_ids.push_back(iter->id());
}
}
if (unignored_parent_id != kInvalidAXNodeID)
unignored_parent_id_ = unignored_parent_id;
// Ignored nodes store unignored child information in order to propagate it to
// their parents, but do not expose it directly. The latter is guarded via a
// DCHECK.
unignored_child_count_ = unignored_child_count;
unignored_child_ids_ = unignored_child_ids;
}
AXNode* AXComputedNodeData::SlowGetUnignoredParent() const {
AXNode* unignored_parent = owner_->GetParent();
while (unignored_parent && unignored_parent->IsIgnored())
unignored_parent = unignored_parent->GetParent();
return unignored_parent;
}
void AXComputedNodeData::ComputeIsDescendantOfPlatformLeaf() const {
is_descendant_of_leaf_ = false;
for (const AXNode* ancestor = GetOrComputeUnignoredParent(); ancestor;
ancestor =
ancestor->GetComputedNodeData().GetOrComputeUnignoredParent()) {
if (ancestor->GetComputedNodeData().is_descendant_of_leaf_.value_or(
false) ||
ancestor->IsLeaf()) {
is_descendant_of_leaf_ = true;
return;
}
}
}
void AXComputedNodeData::ComputeLineOffsetsIfNeeded() const {
if (line_starts_ || line_ends_) {
DCHECK_EQ(line_starts_->size(), line_ends_->size());
return; // Already cached.
}
line_starts_ = std::vector<int32_t>();
line_ends_ = std::vector<int32_t>();
const std::u16string& text_content = GetOrComputeTextContentUTF16();
if (text_content.empty())
return;
// TODO(nektar): Using the `base::i18n::BreakIterator` class is not enough. We
// also need to pass information from Blink as to which inline text boxes
// start a new line and deprecate next/previous_on_line.
base::i18n::BreakIterator iter(text_content,
base::i18n::BreakIterator::BREAK_NEWLINE);
if (!iter.Init())
return;
while (iter.Advance()) {
line_starts_->push_back(base::checked_cast<int32_t>(iter.prev()));
line_ends_->push_back(base::checked_cast<int32_t>(iter.pos()));
}
}
void AXComputedNodeData::ComputeSentenceOffsetsIfNeeded() const {
if (sentence_starts_ || sentence_ends_) {
DCHECK_EQ(sentence_starts_->size(), sentence_ends_->size());
return; // Already cached.
}
sentence_starts_ = std::vector<int32_t>();
sentence_ends_ = std::vector<int32_t>();
if (owner_->IsLineBreak())
return;
const std::u16string& text_content = GetOrComputeTextContentUTF16();
if (text_content.empty() ||
base::ContainsOnlyChars(text_content, base::kWhitespaceUTF16)) {
return;
}
// Unlike in ICU, a sentence boundary is not valid in Blink if it falls within
// some whitespace that is used to separate sentences. We therefore need to
// filter the boundaries returned by ICU and return a subset of them. For
// example we should exclude a sentence boundary that is between two space
// characters, "Hello. | there.".
// TODO(nektar): The above is not accomplished simply by using the
// `base::i18n::BreakIterator` class.
base::i18n::BreakIterator iter(text_content,
base::i18n::BreakIterator::BREAK_SENTENCE);
if (!iter.Init())
return;
while (iter.Advance()) {
sentence_starts_->push_back(base::checked_cast<int32_t>(iter.prev()));
sentence_ends_->push_back(base::checked_cast<int32_t>(iter.pos()));
}
}
void AXComputedNodeData::ComputeWordOffsetsIfNeeded() const {
if (word_starts_ || word_ends_) {
DCHECK_EQ(word_starts_->size(), word_ends_->size());
return; // Already cached.
}
word_starts_ = std::vector<int32_t>();
word_ends_ = std::vector<int32_t>();
const std::u16string& text_content = GetOrComputeTextContentUTF16();
if (text_content.empty())
return;
// Unlike in ICU, a word boundary is valid in Blink only if it is before, or
// immediately preceded by, an alphanumeric character, a series of punctuation
// marks, an underscore or a line break. We therefore need to filter the
// boundaries returned by ICU and return a subset of them. For example we
// should exclude a word boundary that is between two space characters, "Hello
// | there".
// TODO(nektar): Fix the fact that the `base::i18n::BreakIterator` class does
// not take into account underscores as word separators.
base::i18n::BreakIterator iter(text_content,
base::i18n::BreakIterator::BREAK_WORD);
if (!iter.Init())
return;
while (iter.Advance()) {
if (iter.IsWord()) {
word_starts_->push_back(base::checked_cast<int>(iter.prev()));
word_ends_->push_back(base::checked_cast<int>(iter.pos()));
}
}
}
std::string AXComputedNodeData::ComputeTextContentUTF8() const {
// If a text field has no descendants, then we compute its text content from
// its value or its placeholder. Otherwise we prefer to look at its descendant
// text nodes because Blink doesn't always add all trailing white space to the
// value attribute.
const bool is_atomic_text_field_without_descendants =
(owner_->data().IsTextField() && !owner_->GetUnignoredChildCount());
if (is_atomic_text_field_without_descendants) {
std::string value =
owner_->data().GetStringAttribute(ax::mojom::StringAttribute::kValue);
// If the value is empty, then there might be some placeholder text in the
// text field, or any other name that is derived from visible contents, even
// if the text field has no children, so we treat this as any other leaf
// node.
if (!value.empty())
return value;
}
// Ordinarily, atomic text fields are leaves, and for all leaves we directly
// retrieve their text content using the information provided by the tree
// source, such as Blink. However, for atomic text fields we need to exclude
// them from the set of leaf nodes when they expose any descendants. This is
// because we want to compute their text content from their descendant text
// nodes as we don't always trust the "value" attribute provided by Blink.
const bool is_atomic_text_field_with_descendants =
(owner_->data().IsTextField() && owner_->GetUnignoredChildCount());
if (owner_->IsLeaf() && !is_atomic_text_field_with_descendants) {
switch (owner_->data().GetNameFrom()) {
case ax::mojom::NameFrom::kNone:
// The accessible name is not displayed on screen, e.g. aria-label, or is
// not displayed directly inside the node, e.g. an associated label
// element.
case ax::mojom::NameFrom::kAttribute:
// The node's accessible name is explicitly empty.
case ax::mojom::NameFrom::kAttributeExplicitlyEmpty:
// The accessible name does not represent the entirety of the node's text
// content, e.g. a table's caption or a figure's figcaption.
case ax::mojom::NameFrom::kCaption:
case ax::mojom::NameFrom::kRelatedElement:
// The accessible name is not displayed directly inside the node but is
// visible via e.g. a tooltip.
case ax::mojom::NameFrom::kTitle:
case ax::mojom::NameFrom::kPopoverAttribute:
return std::string();
case ax::mojom::NameFrom::kContents:
// The placeholder text is initially displayed inside the text field and
// takes the place of its value.
case ax::mojom::NameFrom::kPlaceholder:
// The value attribute takes the place of the node's text content, e.g.
// the value of a submit button is displayed inside the button itself.
case ax::mojom::NameFrom::kValue:
return owner_->data().GetStringAttribute(
ax::mojom::StringAttribute::kName);
}
}
std::string text_content;
for (auto it = owner_->UnignoredChildrenCrossingTreeBoundaryBegin();
it != owner_->UnignoredChildrenCrossingTreeBoundaryEnd(); ++it) {
text_content += it->GetTextContentUTF8();
}
return text_content;
}
std::u16string AXComputedNodeData::ComputeTextContentUTF16() const {
return base::UTF8ToUTF16(ComputeTextContentUTF8());
}
} // namespace ui