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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/accessibility/ax_node_data.h"
#include <set>
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
using base::DoubleToString;
using base::IntToString;
namespace ui {
namespace {
std::string IntVectorToString(const std::vector<int>& items) {
std::string str;
for (size_t i = 0; i < items.size(); ++i) {
if (i > 0)
str += ",";
str += IntToString(items[i]);
}
return str;
}
// Predicate that returns true if the first value of a pair is |first|.
template<typename FirstType, typename SecondType>
struct FirstIs {
FirstIs(FirstType first)
: first_(first) {}
bool operator()(std::pair<FirstType, SecondType> const& p) {
return p.first == first_;
}
FirstType first_;
};
// Helper function that finds a key in a vector of pairs by matching on the
// first value, and returns an iterator.
template<typename FirstType, typename SecondType>
typename std::vector<std::pair<FirstType, SecondType>>::const_iterator
FindInVectorOfPairs(
FirstType first,
const std::vector<std::pair<FirstType, SecondType>>& vector) {
return std::find_if(vector.begin(),
vector.end(),
FirstIs<FirstType, SecondType>(first));
}
} // namespace
AXNodeData::AXNodeData()
: id(-1),
role(AX_ROLE_UNKNOWN),
state(0xFFFFFFFF) {
}
AXNodeData::~AXNodeData() {
}
bool AXNodeData::HasBoolAttribute(AXBoolAttribute attribute) const {
auto iter = FindInVectorOfPairs(attribute, bool_attributes);
return iter != bool_attributes.end();
}
bool AXNodeData::GetBoolAttribute(AXBoolAttribute attribute) const {
bool result;
if (GetBoolAttribute(attribute, &result))
return result;
return false;
}
bool AXNodeData::GetBoolAttribute(
AXBoolAttribute attribute, bool* value) const {
auto iter = FindInVectorOfPairs(attribute, bool_attributes);
if (iter != bool_attributes.end()) {
*value = iter->second;
return true;
}
return false;
}
bool AXNodeData::HasFloatAttribute(AXFloatAttribute attribute) const {
auto iter = FindInVectorOfPairs(attribute, float_attributes);
return iter != float_attributes.end();
}
float AXNodeData::GetFloatAttribute(AXFloatAttribute attribute) const {
float result;
if (GetFloatAttribute(attribute, &result))
return result;
return 0.0;
}
bool AXNodeData::GetFloatAttribute(
AXFloatAttribute attribute, float* value) const {
auto iter = FindInVectorOfPairs(attribute, float_attributes);
if (iter != float_attributes.end()) {
*value = iter->second;
return true;
}
return false;
}
bool AXNodeData::HasIntAttribute(AXIntAttribute attribute) const {
auto iter = FindInVectorOfPairs(attribute, int_attributes);
return iter != int_attributes.end();
}
int AXNodeData::GetIntAttribute(AXIntAttribute attribute) const {
int result;
if (GetIntAttribute(attribute, &result))
return result;
return 0;
}
bool AXNodeData::GetIntAttribute(
AXIntAttribute attribute, int* value) const {
auto iter = FindInVectorOfPairs(attribute, int_attributes);
if (iter != int_attributes.end()) {
*value = iter->second;
return true;
}
return false;
}
bool AXNodeData::HasStringAttribute(AXStringAttribute attribute) const {
auto iter = FindInVectorOfPairs(attribute, string_attributes);
return iter != string_attributes.end();
}
const std::string& AXNodeData::GetStringAttribute(
AXStringAttribute attribute) const {
CR_DEFINE_STATIC_LOCAL(std::string, empty_string, ());
auto iter = FindInVectorOfPairs(attribute, string_attributes);
return iter != string_attributes.end() ? iter->second : empty_string;
}
bool AXNodeData::GetStringAttribute(
AXStringAttribute attribute, std::string* value) const {
auto iter = FindInVectorOfPairs(attribute, string_attributes);
if (iter != string_attributes.end()) {
*value = iter->second;
return true;
}
return false;
}
base::string16 AXNodeData::GetString16Attribute(
AXStringAttribute attribute) const {
std::string value_utf8;
if (!GetStringAttribute(attribute, &value_utf8))
return base::string16();
return base::UTF8ToUTF16(value_utf8);
}
bool AXNodeData::GetString16Attribute(
AXStringAttribute attribute,
base::string16* value) const {
std::string value_utf8;
if (!GetStringAttribute(attribute, &value_utf8))
return false;
*value = base::UTF8ToUTF16(value_utf8);
return true;
}
bool AXNodeData::HasIntListAttribute(AXIntListAttribute attribute) const {
auto iter = FindInVectorOfPairs(attribute, intlist_attributes);
return iter != intlist_attributes.end();
}
const std::vector<int32>& AXNodeData::GetIntListAttribute(
AXIntListAttribute attribute) const {
CR_DEFINE_STATIC_LOCAL(std::vector<int32>, empty_vector, ());
auto iter = FindInVectorOfPairs(attribute, intlist_attributes);
if (iter != intlist_attributes.end())
return iter->second;
return empty_vector;
}
bool AXNodeData::GetIntListAttribute(
AXIntListAttribute attribute, std::vector<int32>* value) const {
auto iter = FindInVectorOfPairs(attribute, intlist_attributes);
if (iter != intlist_attributes.end()) {
*value = iter->second;
return true;
}
return false;
}
bool AXNodeData::GetHtmlAttribute(
const char* html_attr, std::string* value) const {
for (size_t i = 0; i < html_attributes.size(); ++i) {
const std::string& attr = html_attributes[i].first;
if (base::LowerCaseEqualsASCII(attr, html_attr)) {
*value = html_attributes[i].second;
return true;
}
}
return false;
}
bool AXNodeData::GetHtmlAttribute(
const char* html_attr, base::string16* value) const {
std::string value_utf8;
if (!GetHtmlAttribute(html_attr, &value_utf8))
return false;
*value = base::UTF8ToUTF16(value_utf8);
return true;
}
void AXNodeData::AddStringAttribute(
AXStringAttribute attribute, const std::string& value) {
string_attributes.push_back(std::make_pair(attribute, value));
}
void AXNodeData::AddIntAttribute(
AXIntAttribute attribute, int value) {
int_attributes.push_back(std::make_pair(attribute, value));
}
void AXNodeData::AddFloatAttribute(
AXFloatAttribute attribute, float value) {
float_attributes.push_back(std::make_pair(attribute, value));
}
void AXNodeData::AddBoolAttribute(
AXBoolAttribute attribute, bool value) {
bool_attributes.push_back(std::make_pair(attribute, value));
}
void AXNodeData::AddIntListAttribute(
AXIntListAttribute attribute, const std::vector<int32>& value) {
intlist_attributes.push_back(std::make_pair(attribute, value));
}
void AXNodeData::SetName(std::string name) {
string_attributes.push_back(std::make_pair(AX_ATTR_NAME, name));
}
void AXNodeData::SetValue(std::string value) {
string_attributes.push_back(std::make_pair(AX_ATTR_VALUE, value));
}
std::string AXNodeData::ToString() const {
std::string result;
result += "id=" + IntToString(id);
result += " " + ui::ToString(role);
if (state & (1 << AX_STATE_BUSY))
result += " BUSY";
if (state & (1 << AX_STATE_CHECKED))
result += " CHECKED";
if (state & (1 << AX_STATE_COLLAPSED))
result += " COLLAPSED";
if (state & (1 << AX_STATE_EXPANDED))
result += " EXPANDED";
if (state & (1 << AX_STATE_FOCUSABLE))
result += " FOCUSABLE";
if (state & (1 << AX_STATE_FOCUSED))
result += " FOCUSED";
if (state & (1 << AX_STATE_HASPOPUP))
result += " HASPOPUP";
if (state & (1 << AX_STATE_HOVERED))
result += " HOVERED";
if (state & (1 << AX_STATE_INDETERMINATE))
result += " INDETERMINATE";
if (state & (1 << AX_STATE_INVISIBLE))
result += " INVISIBLE";
if (state & (1 << AX_STATE_LINKED))
result += " LINKED";
if (state & (1 << AX_STATE_MULTISELECTABLE))
result += " MULTISELECTABLE";
if (state & (1 << AX_STATE_OFFSCREEN))
result += " OFFSCREEN";
if (state & (1 << AX_STATE_PRESSED))
result += " PRESSED";
if (state & (1 << AX_STATE_PROTECTED))
result += " PROTECTED";
if (state & (1 << AX_STATE_READ_ONLY))
result += " READONLY";
if (state & (1 << AX_STATE_REQUIRED))
result += " REQUIRED";
if (state & (1 << AX_STATE_SELECTABLE))
result += " SELECTABLE";
if (state & (1 << AX_STATE_SELECTED))
result += " SELECTED";
if (state & (1 << AX_STATE_VERTICAL))
result += " VERTICAL";
if (state & (1 << AX_STATE_VISITED))
result += " VISITED";
result += " (" + IntToString(location.x()) + ", " +
IntToString(location.y()) + ")-(" +
IntToString(location.width()) + ", " +
IntToString(location.height()) + ")";
for (size_t i = 0; i < int_attributes.size(); ++i) {
std::string value = IntToString(int_attributes[i].second);
switch (int_attributes[i].first) {
case AX_ATTR_SCROLL_X:
result += " scroll_x=" + value;
break;
case AX_ATTR_SCROLL_X_MIN:
result += " scroll_x_min=" + value;
break;
case AX_ATTR_SCROLL_X_MAX:
result += " scroll_x_max=" + value;
break;
case AX_ATTR_SCROLL_Y:
result += " scroll_y=" + value;
break;
case AX_ATTR_SCROLL_Y_MIN:
result += " scroll_y_min=" + value;
break;
case AX_ATTR_SCROLL_Y_MAX:
result += " scroll_y_max=" + value;
break;
case AX_ATTR_HIERARCHICAL_LEVEL:
result += " level=" + value;
break;
case AX_ATTR_ANCHOR_OBJECT_ID:
result += " anchor_object_id=" + value;
break;
case AX_ATTR_ANCHOR_OFFSET:
result += " anchor_offset=" + value;
break;
case AX_ATTR_FOCUS_OBJECT_ID:
result += " focus_object_id=" + value;
break;
case AX_ATTR_FOCUS_OFFSET:
result += " focus_offset=" + value;
break;
case AX_ATTR_TEXT_SEL_START:
result += " sel_start=" + value;
break;
case AX_ATTR_TEXT_SEL_END:
result += " sel_end=" + value;
break;
case AX_ATTR_TABLE_ROW_COUNT:
result += " rows=" + value;
break;
case AX_ATTR_TABLE_COLUMN_COUNT:
result += " cols=" + value;
break;
case AX_ATTR_TABLE_CELL_COLUMN_INDEX:
result += " col=" + value;
break;
case AX_ATTR_TABLE_CELL_ROW_INDEX:
result += " row=" + value;
break;
case AX_ATTR_TABLE_CELL_COLUMN_SPAN:
result += " colspan=" + value;
break;
case AX_ATTR_TABLE_CELL_ROW_SPAN:
result += " rowspan=" + value;
break;
case AX_ATTR_TABLE_COLUMN_HEADER_ID:
result += " column_header_id=" + value;
break;
case AX_ATTR_TABLE_COLUMN_INDEX:
result += " column_index=" + value;
break;
case AX_ATTR_TABLE_HEADER_ID:
result += " header_id=" + value;
break;
case AX_ATTR_TABLE_ROW_HEADER_ID:
result += " row_header_id=" + value;
break;
case AX_ATTR_TABLE_ROW_INDEX:
result += " row_index=" + value;
break;
case AX_ATTR_SORT_DIRECTION:
switch (int_attributes[i].second) {
case AX_SORT_DIRECTION_UNSORTED:
result += " sort_direction=none";
break;
case AX_SORT_DIRECTION_ASCENDING:
result += " sort_direction=ascending";
break;
case AX_SORT_DIRECTION_DESCENDING:
result += " sort_direction=descending";
break;
case AX_SORT_DIRECTION_OTHER:
result += " sort_direction=other";
break;
}
break;
case AX_ATTR_TITLE_UI_ELEMENT:
result += " title_elem=" + value;
break;
case AX_ATTR_ACTIVEDESCENDANT_ID:
result += " activedescendant=" + value;
break;
case AX_ATTR_TREE_ID:
result += " tree_id=" + value;
break;
case AX_ATTR_CHILD_TREE_ID:
result += " child_tree_id=" + value;
break;
case AX_ATTR_PARENT_TREE_ID:
result += " parent_tree_id=" + value;
break;
case AX_ATTR_COLOR_VALUE:
result += base::StringPrintf(" color_value=&%X",
int_attributes[i].second);
break;
case AX_ATTR_BACKGROUND_COLOR:
result += base::StringPrintf(" background_color=&%X",
int_attributes[i].second);
break;
case AX_ATTR_COLOR:
result += base::StringPrintf(" color=&%X", int_attributes[i].second);
break;
case AX_ATTR_TEXT_DIRECTION:
switch (int_attributes[i].second) {
case AX_TEXT_DIRECTION_LTR:
result += " text_direction=ltr";
break;
case AX_TEXT_DIRECTION_RTL:
result += " text_direction=rtl";
break;
case AX_TEXT_DIRECTION_TTB:
result += " text_direction=ttb";
break;
case AX_TEXT_DIRECTION_BTT:
result += " text_direction=btt";
break;
}
case AX_ATTR_TEXT_STYLE: {
unsigned int text_style = int_attributes[i].second;
if (text_style == AX_TEXT_STYLE_NONE)
break;
std::string text_style_value(" text_style=");
if (text_style & AX_TEXT_STYLE_BOLD)
text_style_value += "bold,";
if (text_style & AX_TEXT_STYLE_ITALIC)
text_style_value += "italic,";
if (text_style & AX_TEXT_STYLE_UNDERLINE)
text_style_value += "underline,";
if (text_style & AX_TEXT_STYLE_LINE_THROUGH)
text_style_value += "line-through,";
result += text_style_value.substr(0, text_style_value.size() - 1);;
}
break;
case AX_ATTR_SET_SIZE:
result += " setsize=" + value;
break;
case AX_ATTR_POS_IN_SET:
result += " posinset=" + value;
break;
case AX_ATTR_INVALID_STATE:
switch (int_attributes[i].second) {
case AX_INVALID_STATE_FALSE:
result += " invalid_state=false";
break;
case AX_INVALID_STATE_TRUE:
result += " invalid_state=true";
break;
case AX_INVALID_STATE_SPELLING:
result += " invalid_state=spelling";
break;
case AX_INVALID_STATE_GRAMMAR:
result += " invalid_state=grammar";
break;
case AX_INVALID_STATE_OTHER:
result += " invalid_state=other";
break;
}
break;
case AX_INT_ATTRIBUTE_NONE:
break;
}
}
for (size_t i = 0; i < string_attributes.size(); ++i) {
std::string value = string_attributes[i].second;
switch (string_attributes[i].first) {
case AX_ATTR_DOC_URL:
result += " doc_url=" + value;
break;
case AX_ATTR_DOC_TITLE:
result += " doc_title=" + value;
break;
case AX_ATTR_DOC_MIMETYPE:
result += " doc_mimetype=" + value;
break;
case AX_ATTR_DOC_DOCTYPE:
result += " doc_doctype=" + value;
break;
case AX_ATTR_ACCESS_KEY:
result += " access_key=" + value;
break;
case AX_ATTR_ACTION:
result += " action=" + value;
break;
case AX_ATTR_AUTO_COMPLETE:
result += " autocomplete=" + value;
break;
case AX_ATTR_DESCRIPTION:
result += " description=" + value;
break;
case AX_ATTR_DISPLAY:
result += " display=" + value;
break;
case AX_ATTR_HELP:
result += " help=" + value;
break;
case AX_ATTR_HTML_TAG:
result += " html_tag=" + value;
break;
case AX_ATTR_ARIA_INVALID_VALUE:
result += " aria_invalid_value=" + value;
break;
case AX_ATTR_LIVE_RELEVANT:
result += " relevant=" + value;
break;
case AX_ATTR_LIVE_STATUS:
result += " live=" + value;
break;
case AX_ATTR_CONTAINER_LIVE_RELEVANT:
result += " container_relevant=" + value;
break;
case AX_ATTR_CONTAINER_LIVE_STATUS:
result += " container_live=" + value;
break;
case AX_ATTR_PLACEHOLDER:
result += "placeholder" + value;
break;
case AX_ATTR_ROLE:
result += " role=" + value;
break;
case AX_ATTR_SHORTCUT:
result += " shortcut=" + value;
break;
case AX_ATTR_URL:
result += " url=" + value;
break;
case AX_ATTR_NAME:
result += " name=" + value;
break;
case AX_ATTR_VALUE:
result += " value=" + value;
break;
case AX_STRING_ATTRIBUTE_NONE:
break;
}
}
for (size_t i = 0; i < float_attributes.size(); ++i) {
std::string value = DoubleToString(float_attributes[i].second);
switch (float_attributes[i].first) {
case AX_ATTR_DOC_LOADING_PROGRESS:
result += " doc_progress=" + value;
break;
case AX_ATTR_VALUE_FOR_RANGE:
result += " value_for_range=" + value;
break;
case AX_ATTR_MAX_VALUE_FOR_RANGE:
result += " max_value=" + value;
break;
case AX_ATTR_MIN_VALUE_FOR_RANGE:
result += " min_value=" + value;
break;
case AX_ATTR_FONT_SIZE:
result += " font_size=" + value;
break;
case AX_FLOAT_ATTRIBUTE_NONE:
break;
}
}
for (size_t i = 0; i < bool_attributes.size(); ++i) {
std::string value = bool_attributes[i].second ? "true" : "false";
switch (bool_attributes[i].first) {
case AX_ATTR_DOC_LOADED:
result += " doc_loaded=" + value;
break;
case AX_ATTR_BUTTON_MIXED:
result += " mixed=" + value;
break;
case AX_ATTR_LIVE_ATOMIC:
result += " atomic=" + value;
break;
case AX_ATTR_LIVE_BUSY:
result += " busy=" + value;
break;
case AX_ATTR_CONTAINER_LIVE_ATOMIC:
result += " container_atomic=" + value;
break;
case AX_ATTR_CONTAINER_LIVE_BUSY:
result += " container_busy=" + value;
break;
case AX_ATTR_ARIA_READONLY:
result += " aria_readonly=" + value;
break;
case AX_ATTR_CAN_SET_VALUE:
result += " can_set_value=" + value;
break;
case AX_ATTR_UPDATE_LOCATION_ONLY:
result += " update_location_only=" + value;
break;
case AX_ATTR_CANVAS_HAS_FALLBACK:
result += " has_fallback=" + value;
break;
case AX_BOOL_ATTRIBUTE_NONE:
break;
}
}
for (size_t i = 0; i < intlist_attributes.size(); ++i) {
const std::vector<int32>& values = intlist_attributes[i].second;
switch (intlist_attributes[i].first) {
case AX_ATTR_INDIRECT_CHILD_IDS:
result += " indirect_child_ids=" + IntVectorToString(values);
break;
case AX_ATTR_CONTROLS_IDS:
result += " controls_ids=" + IntVectorToString(values);
break;
case AX_ATTR_DESCRIBEDBY_IDS:
result += " describedby_ids=" + IntVectorToString(values);
break;
case AX_ATTR_FLOWTO_IDS:
result += " flowto_ids=" + IntVectorToString(values);
break;
case AX_ATTR_LABELLEDBY_IDS:
result += " labelledby_ids=" + IntVectorToString(values);
break;
case AX_ATTR_OWNS_IDS:
result += " owns_ids=" + IntVectorToString(values);
break;
case AX_ATTR_LINE_BREAKS:
result += " line_breaks=" + IntVectorToString(values);
break;
case AX_ATTR_CELL_IDS:
result += " cell_ids=" + IntVectorToString(values);
break;
case AX_ATTR_UNIQUE_CELL_IDS:
result += " unique_cell_ids=" + IntVectorToString(values);
break;
case AX_ATTR_CHARACTER_OFFSETS:
result += " character_offsets=" + IntVectorToString(values);
break;
case AX_ATTR_WORD_STARTS:
result += " word_starts=" + IntVectorToString(values);
break;
case AX_ATTR_WORD_ENDS:
result += " word_ends=" + IntVectorToString(values);
break;
case AX_INT_LIST_ATTRIBUTE_NONE:
break;
}
}
if (!child_ids.empty())
result += " child_ids=" + IntVectorToString(child_ids);
return result;
}
bool AXNodeData::IsRoot() const {
return (role == AX_ROLE_ROOT_WEB_AREA ||
role == AX_ROLE_DESKTOP);
}
void AXNodeData::SetRoot() {
role = AX_ROLE_ROOT_WEB_AREA;
}
} // namespace ui
|