1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/accessibility/ax_node_data.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h"
14 using base::DoubleToString
;
15 using base::IntToString
;
21 std::string
IntVectorToString(const std::vector
<int>& items
) {
23 for (size_t i
= 0; i
< items
.size(); ++i
) {
26 str
+= IntToString(items
[i
]);
31 // Predicate that returns true if the first value of a pair is |first|.
32 template<typename FirstType
, typename SecondType
>
34 FirstIs(FirstType first
)
36 bool operator()(std::pair
<FirstType
, SecondType
> const& p
) {
37 return p
.first
== first_
;
42 // Helper function that finds a key in a vector of pairs by matching on the
43 // first value, and returns an iterator.
44 template<typename FirstType
, typename SecondType
>
45 typename
std::vector
<std::pair
<FirstType
, SecondType
>>::const_iterator
48 const std::vector
<std::pair
<FirstType
, SecondType
>>& vector
) {
49 return std::find_if(vector
.begin(),
51 FirstIs
<FirstType
, SecondType
>(first
));
56 AXNodeData::AXNodeData()
58 role(AX_ROLE_UNKNOWN
),
62 AXNodeData::~AXNodeData() {
65 bool AXNodeData::HasBoolAttribute(AXBoolAttribute attribute
) const {
66 auto iter
= FindInVectorOfPairs(attribute
, bool_attributes
);
67 return iter
!= bool_attributes
.end();
70 bool AXNodeData::GetBoolAttribute(AXBoolAttribute attribute
) const {
72 if (GetBoolAttribute(attribute
, &result
))
77 bool AXNodeData::GetBoolAttribute(
78 AXBoolAttribute attribute
, bool* value
) const {
79 auto iter
= FindInVectorOfPairs(attribute
, bool_attributes
);
80 if (iter
!= bool_attributes
.end()) {
81 *value
= iter
->second
;
88 bool AXNodeData::HasFloatAttribute(AXFloatAttribute attribute
) const {
89 auto iter
= FindInVectorOfPairs(attribute
, float_attributes
);
90 return iter
!= float_attributes
.end();
93 float AXNodeData::GetFloatAttribute(AXFloatAttribute attribute
) const {
95 if (GetFloatAttribute(attribute
, &result
))
100 bool AXNodeData::GetFloatAttribute(
101 AXFloatAttribute attribute
, float* value
) const {
102 auto iter
= FindInVectorOfPairs(attribute
, float_attributes
);
103 if (iter
!= float_attributes
.end()) {
104 *value
= iter
->second
;
111 bool AXNodeData::HasIntAttribute(AXIntAttribute attribute
) const {
112 auto iter
= FindInVectorOfPairs(attribute
, int_attributes
);
113 return iter
!= int_attributes
.end();
116 int AXNodeData::GetIntAttribute(AXIntAttribute attribute
) const {
118 if (GetIntAttribute(attribute
, &result
))
123 bool AXNodeData::GetIntAttribute(
124 AXIntAttribute attribute
, int* value
) const {
125 auto iter
= FindInVectorOfPairs(attribute
, int_attributes
);
126 if (iter
!= int_attributes
.end()) {
127 *value
= iter
->second
;
134 bool AXNodeData::HasStringAttribute(AXStringAttribute attribute
) const {
135 auto iter
= FindInVectorOfPairs(attribute
, string_attributes
);
136 return iter
!= string_attributes
.end();
139 const std::string
& AXNodeData::GetStringAttribute(
140 AXStringAttribute attribute
) const {
141 CR_DEFINE_STATIC_LOCAL(std::string
, empty_string
, ());
142 auto iter
= FindInVectorOfPairs(attribute
, string_attributes
);
143 return iter
!= string_attributes
.end() ? iter
->second
: empty_string
;
146 bool AXNodeData::GetStringAttribute(
147 AXStringAttribute attribute
, std::string
* value
) const {
148 auto iter
= FindInVectorOfPairs(attribute
, string_attributes
);
149 if (iter
!= string_attributes
.end()) {
150 *value
= iter
->second
;
157 base::string16
AXNodeData::GetString16Attribute(
158 AXStringAttribute attribute
) const {
159 std::string value_utf8
;
160 if (!GetStringAttribute(attribute
, &value_utf8
))
161 return base::string16();
162 return base::UTF8ToUTF16(value_utf8
);
165 bool AXNodeData::GetString16Attribute(
166 AXStringAttribute attribute
,
167 base::string16
* value
) const {
168 std::string value_utf8
;
169 if (!GetStringAttribute(attribute
, &value_utf8
))
171 *value
= base::UTF8ToUTF16(value_utf8
);
175 bool AXNodeData::HasIntListAttribute(AXIntListAttribute attribute
) const {
176 auto iter
= FindInVectorOfPairs(attribute
, intlist_attributes
);
177 return iter
!= intlist_attributes
.end();
180 const std::vector
<int32
>& AXNodeData::GetIntListAttribute(
181 AXIntListAttribute attribute
) const {
182 CR_DEFINE_STATIC_LOCAL(std::vector
<int32
>, empty_vector
, ());
183 auto iter
= FindInVectorOfPairs(attribute
, intlist_attributes
);
184 if (iter
!= intlist_attributes
.end())
189 bool AXNodeData::GetIntListAttribute(
190 AXIntListAttribute attribute
, std::vector
<int32
>* value
) const {
191 auto iter
= FindInVectorOfPairs(attribute
, intlist_attributes
);
192 if (iter
!= intlist_attributes
.end()) {
193 *value
= iter
->second
;
200 bool AXNodeData::GetHtmlAttribute(
201 const char* html_attr
, std::string
* value
) const {
202 for (size_t i
= 0; i
< html_attributes
.size(); ++i
) {
203 const std::string
& attr
= html_attributes
[i
].first
;
204 if (base::LowerCaseEqualsASCII(attr
, html_attr
)) {
205 *value
= html_attributes
[i
].second
;
213 bool AXNodeData::GetHtmlAttribute(
214 const char* html_attr
, base::string16
* value
) const {
215 std::string value_utf8
;
216 if (!GetHtmlAttribute(html_attr
, &value_utf8
))
218 *value
= base::UTF8ToUTF16(value_utf8
);
222 void AXNodeData::AddStringAttribute(
223 AXStringAttribute attribute
, const std::string
& value
) {
224 string_attributes
.push_back(std::make_pair(attribute
, value
));
227 void AXNodeData::AddIntAttribute(
228 AXIntAttribute attribute
, int value
) {
229 int_attributes
.push_back(std::make_pair(attribute
, value
));
232 void AXNodeData::AddFloatAttribute(
233 AXFloatAttribute attribute
, float value
) {
234 float_attributes
.push_back(std::make_pair(attribute
, value
));
237 void AXNodeData::AddBoolAttribute(
238 AXBoolAttribute attribute
, bool value
) {
239 bool_attributes
.push_back(std::make_pair(attribute
, value
));
242 void AXNodeData::AddIntListAttribute(
243 AXIntListAttribute attribute
, const std::vector
<int32
>& value
) {
244 intlist_attributes
.push_back(std::make_pair(attribute
, value
));
247 void AXNodeData::SetName(std::string name
) {
248 string_attributes
.push_back(std::make_pair(AX_ATTR_NAME
, name
));
251 void AXNodeData::SetValue(std::string value
) {
252 string_attributes
.push_back(std::make_pair(AX_ATTR_VALUE
, value
));
255 std::string
AXNodeData::ToString() const {
258 result
+= "id=" + IntToString(id
);
259 result
+= " " + ui::ToString(role
);
261 if (state
& (1 << AX_STATE_BUSY
))
263 if (state
& (1 << AX_STATE_CHECKED
))
264 result
+= " CHECKED";
265 if (state
& (1 << AX_STATE_COLLAPSED
))
266 result
+= " COLLAPSED";
267 if (state
& (1 << AX_STATE_EXPANDED
))
268 result
+= " EXPANDED";
269 if (state
& (1 << AX_STATE_FOCUSABLE
))
270 result
+= " FOCUSABLE";
271 if (state
& (1 << AX_STATE_FOCUSED
))
272 result
+= " FOCUSED";
273 if (state
& (1 << AX_STATE_HASPOPUP
))
274 result
+= " HASPOPUP";
275 if (state
& (1 << AX_STATE_HOVERED
))
276 result
+= " HOVERED";
277 if (state
& (1 << AX_STATE_INDETERMINATE
))
278 result
+= " INDETERMINATE";
279 if (state
& (1 << AX_STATE_INVISIBLE
))
280 result
+= " INVISIBLE";
281 if (state
& (1 << AX_STATE_LINKED
))
283 if (state
& (1 << AX_STATE_MULTISELECTABLE
))
284 result
+= " MULTISELECTABLE";
285 if (state
& (1 << AX_STATE_OFFSCREEN
))
286 result
+= " OFFSCREEN";
287 if (state
& (1 << AX_STATE_PRESSED
))
288 result
+= " PRESSED";
289 if (state
& (1 << AX_STATE_PROTECTED
))
290 result
+= " PROTECTED";
291 if (state
& (1 << AX_STATE_READ_ONLY
))
292 result
+= " READONLY";
293 if (state
& (1 << AX_STATE_REQUIRED
))
294 result
+= " REQUIRED";
295 if (state
& (1 << AX_STATE_SELECTABLE
))
296 result
+= " SELECTABLE";
297 if (state
& (1 << AX_STATE_SELECTED
))
298 result
+= " SELECTED";
299 if (state
& (1 << AX_STATE_VERTICAL
))
300 result
+= " VERTICAL";
301 if (state
& (1 << AX_STATE_VISITED
))
302 result
+= " VISITED";
304 result
+= " (" + IntToString(location
.x()) + ", " +
305 IntToString(location
.y()) + ")-(" +
306 IntToString(location
.width()) + ", " +
307 IntToString(location
.height()) + ")";
309 for (size_t i
= 0; i
< int_attributes
.size(); ++i
) {
310 std::string value
= IntToString(int_attributes
[i
].second
);
311 switch (int_attributes
[i
].first
) {
312 case AX_ATTR_SCROLL_X
:
313 result
+= " scroll_x=" + value
;
315 case AX_ATTR_SCROLL_X_MIN
:
316 result
+= " scroll_x_min=" + value
;
318 case AX_ATTR_SCROLL_X_MAX
:
319 result
+= " scroll_x_max=" + value
;
321 case AX_ATTR_SCROLL_Y
:
322 result
+= " scroll_y=" + value
;
324 case AX_ATTR_SCROLL_Y_MIN
:
325 result
+= " scroll_y_min=" + value
;
327 case AX_ATTR_SCROLL_Y_MAX
:
328 result
+= " scroll_y_max=" + value
;
330 case AX_ATTR_HIERARCHICAL_LEVEL
:
331 result
+= " level=" + value
;
333 case AX_ATTR_ANCHOR_OBJECT_ID
:
334 result
+= " anchor_object_id=" + value
;
336 case AX_ATTR_ANCHOR_OFFSET
:
337 result
+= " anchor_offset=" + value
;
339 case AX_ATTR_FOCUS_OBJECT_ID
:
340 result
+= " focus_object_id=" + value
;
342 case AX_ATTR_FOCUS_OFFSET
:
343 result
+= " focus_offset=" + value
;
345 case AX_ATTR_TEXT_SEL_START
:
346 result
+= " sel_start=" + value
;
348 case AX_ATTR_TEXT_SEL_END
:
349 result
+= " sel_end=" + value
;
351 case AX_ATTR_TABLE_ROW_COUNT
:
352 result
+= " rows=" + value
;
354 case AX_ATTR_TABLE_COLUMN_COUNT
:
355 result
+= " cols=" + value
;
357 case AX_ATTR_TABLE_CELL_COLUMN_INDEX
:
358 result
+= " col=" + value
;
360 case AX_ATTR_TABLE_CELL_ROW_INDEX
:
361 result
+= " row=" + value
;
363 case AX_ATTR_TABLE_CELL_COLUMN_SPAN
:
364 result
+= " colspan=" + value
;
366 case AX_ATTR_TABLE_CELL_ROW_SPAN
:
367 result
+= " rowspan=" + value
;
369 case AX_ATTR_TABLE_COLUMN_HEADER_ID
:
370 result
+= " column_header_id=" + value
;
372 case AX_ATTR_TABLE_COLUMN_INDEX
:
373 result
+= " column_index=" + value
;
375 case AX_ATTR_TABLE_HEADER_ID
:
376 result
+= " header_id=" + value
;
378 case AX_ATTR_TABLE_ROW_HEADER_ID
:
379 result
+= " row_header_id=" + value
;
381 case AX_ATTR_TABLE_ROW_INDEX
:
382 result
+= " row_index=" + value
;
384 case AX_ATTR_SORT_DIRECTION
:
385 switch (int_attributes
[i
].second
) {
386 case AX_SORT_DIRECTION_UNSORTED
:
387 result
+= " sort_direction=none";
389 case AX_SORT_DIRECTION_ASCENDING
:
390 result
+= " sort_direction=ascending";
392 case AX_SORT_DIRECTION_DESCENDING
:
393 result
+= " sort_direction=descending";
395 case AX_SORT_DIRECTION_OTHER
:
396 result
+= " sort_direction=other";
400 case AX_ATTR_TITLE_UI_ELEMENT
:
401 result
+= " title_elem=" + value
;
403 case AX_ATTR_ACTIVEDESCENDANT_ID
:
404 result
+= " activedescendant=" + value
;
406 case AX_ATTR_TREE_ID
:
407 result
+= " tree_id=" + value
;
409 case AX_ATTR_CHILD_TREE_ID
:
410 result
+= " child_tree_id=" + value
;
412 case AX_ATTR_PARENT_TREE_ID
:
413 result
+= " parent_tree_id=" + value
;
415 case AX_ATTR_COLOR_VALUE
:
416 result
+= base::StringPrintf(" color_value=&%X",
417 int_attributes
[i
].second
);
419 case AX_ATTR_BACKGROUND_COLOR
:
420 result
+= base::StringPrintf(" background_color=&%X",
421 int_attributes
[i
].second
);
424 result
+= base::StringPrintf(" color=&%X", int_attributes
[i
].second
);
426 case AX_ATTR_TEXT_DIRECTION
:
427 switch (int_attributes
[i
].second
) {
428 case AX_TEXT_DIRECTION_LTR
:
429 result
+= " text_direction=ltr";
431 case AX_TEXT_DIRECTION_RTL
:
432 result
+= " text_direction=rtl";
434 case AX_TEXT_DIRECTION_TTB
:
435 result
+= " text_direction=ttb";
437 case AX_TEXT_DIRECTION_BTT
:
438 result
+= " text_direction=btt";
441 case AX_ATTR_TEXT_STYLE
: {
442 unsigned int text_style
= int_attributes
[i
].second
;
443 if (text_style
== AX_TEXT_STYLE_NONE
)
445 std::string
text_style_value(" text_style=");
446 if (text_style
& AX_TEXT_STYLE_BOLD
)
447 text_style_value
+= "bold,";
448 if (text_style
& AX_TEXT_STYLE_ITALIC
)
449 text_style_value
+= "italic,";
450 if (text_style
& AX_TEXT_STYLE_UNDERLINE
)
451 text_style_value
+= "underline,";
452 if (text_style
& AX_TEXT_STYLE_LINE_THROUGH
)
453 text_style_value
+= "line-through,";
454 result
+= text_style_value
.substr(0, text_style_value
.size() - 1);;
457 case AX_ATTR_SET_SIZE
:
458 result
+= " setsize=" + value
;
460 case AX_ATTR_POS_IN_SET
:
461 result
+= " posinset=" + value
;
463 case AX_ATTR_INVALID_STATE
:
464 switch (int_attributes
[i
].second
) {
465 case AX_INVALID_STATE_FALSE
:
466 result
+= " invalid_state=false";
468 case AX_INVALID_STATE_TRUE
:
469 result
+= " invalid_state=true";
471 case AX_INVALID_STATE_SPELLING
:
472 result
+= " invalid_state=spelling";
474 case AX_INVALID_STATE_GRAMMAR
:
475 result
+= " invalid_state=grammar";
477 case AX_INVALID_STATE_OTHER
:
478 result
+= " invalid_state=other";
482 case AX_INT_ATTRIBUTE_NONE
:
487 for (size_t i
= 0; i
< string_attributes
.size(); ++i
) {
488 std::string value
= string_attributes
[i
].second
;
489 switch (string_attributes
[i
].first
) {
490 case AX_ATTR_DOC_URL
:
491 result
+= " doc_url=" + value
;
493 case AX_ATTR_DOC_TITLE
:
494 result
+= " doc_title=" + value
;
496 case AX_ATTR_DOC_MIMETYPE
:
497 result
+= " doc_mimetype=" + value
;
499 case AX_ATTR_DOC_DOCTYPE
:
500 result
+= " doc_doctype=" + value
;
502 case AX_ATTR_ACCESS_KEY
:
503 result
+= " access_key=" + value
;
506 result
+= " action=" + value
;
508 case AX_ATTR_AUTO_COMPLETE
:
509 result
+= " autocomplete=" + value
;
511 case AX_ATTR_DESCRIPTION
:
512 result
+= " description=" + value
;
514 case AX_ATTR_DISPLAY
:
515 result
+= " display=" + value
;
518 result
+= " help=" + value
;
520 case AX_ATTR_HTML_TAG
:
521 result
+= " html_tag=" + value
;
523 case AX_ATTR_ARIA_INVALID_VALUE
:
524 result
+= " aria_invalid_value=" + value
;
526 case AX_ATTR_LIVE_RELEVANT
:
527 result
+= " relevant=" + value
;
529 case AX_ATTR_LIVE_STATUS
:
530 result
+= " live=" + value
;
532 case AX_ATTR_CONTAINER_LIVE_RELEVANT
:
533 result
+= " container_relevant=" + value
;
535 case AX_ATTR_CONTAINER_LIVE_STATUS
:
536 result
+= " container_live=" + value
;
538 case AX_ATTR_PLACEHOLDER
:
539 result
+= "placeholder" + value
;
542 result
+= " role=" + value
;
544 case AX_ATTR_SHORTCUT
:
545 result
+= " shortcut=" + value
;
548 result
+= " url=" + value
;
551 result
+= " name=" + value
;
554 result
+= " value=" + value
;
556 case AX_STRING_ATTRIBUTE_NONE
:
561 for (size_t i
= 0; i
< float_attributes
.size(); ++i
) {
562 std::string value
= DoubleToString(float_attributes
[i
].second
);
563 switch (float_attributes
[i
].first
) {
564 case AX_ATTR_DOC_LOADING_PROGRESS
:
565 result
+= " doc_progress=" + value
;
567 case AX_ATTR_VALUE_FOR_RANGE
:
568 result
+= " value_for_range=" + value
;
570 case AX_ATTR_MAX_VALUE_FOR_RANGE
:
571 result
+= " max_value=" + value
;
573 case AX_ATTR_MIN_VALUE_FOR_RANGE
:
574 result
+= " min_value=" + value
;
576 case AX_ATTR_FONT_SIZE
:
577 result
+= " font_size=" + value
;
579 case AX_FLOAT_ATTRIBUTE_NONE
:
584 for (size_t i
= 0; i
< bool_attributes
.size(); ++i
) {
585 std::string value
= bool_attributes
[i
].second
? "true" : "false";
586 switch (bool_attributes
[i
].first
) {
587 case AX_ATTR_DOC_LOADED
:
588 result
+= " doc_loaded=" + value
;
590 case AX_ATTR_BUTTON_MIXED
:
591 result
+= " mixed=" + value
;
593 case AX_ATTR_LIVE_ATOMIC
:
594 result
+= " atomic=" + value
;
596 case AX_ATTR_LIVE_BUSY
:
597 result
+= " busy=" + value
;
599 case AX_ATTR_CONTAINER_LIVE_ATOMIC
:
600 result
+= " container_atomic=" + value
;
602 case AX_ATTR_CONTAINER_LIVE_BUSY
:
603 result
+= " container_busy=" + value
;
605 case AX_ATTR_ARIA_READONLY
:
606 result
+= " aria_readonly=" + value
;
608 case AX_ATTR_CAN_SET_VALUE
:
609 result
+= " can_set_value=" + value
;
611 case AX_ATTR_UPDATE_LOCATION_ONLY
:
612 result
+= " update_location_only=" + value
;
614 case AX_ATTR_CANVAS_HAS_FALLBACK
:
615 result
+= " has_fallback=" + value
;
617 case AX_BOOL_ATTRIBUTE_NONE
:
622 for (size_t i
= 0; i
< intlist_attributes
.size(); ++i
) {
623 const std::vector
<int32
>& values
= intlist_attributes
[i
].second
;
624 switch (intlist_attributes
[i
].first
) {
625 case AX_ATTR_INDIRECT_CHILD_IDS
:
626 result
+= " indirect_child_ids=" + IntVectorToString(values
);
628 case AX_ATTR_CONTROLS_IDS
:
629 result
+= " controls_ids=" + IntVectorToString(values
);
631 case AX_ATTR_DESCRIBEDBY_IDS
:
632 result
+= " describedby_ids=" + IntVectorToString(values
);
634 case AX_ATTR_FLOWTO_IDS
:
635 result
+= " flowto_ids=" + IntVectorToString(values
);
637 case AX_ATTR_LABELLEDBY_IDS
:
638 result
+= " labelledby_ids=" + IntVectorToString(values
);
640 case AX_ATTR_OWNS_IDS
:
641 result
+= " owns_ids=" + IntVectorToString(values
);
643 case AX_ATTR_LINE_BREAKS
:
644 result
+= " line_breaks=" + IntVectorToString(values
);
646 case AX_ATTR_CELL_IDS
:
647 result
+= " cell_ids=" + IntVectorToString(values
);
649 case AX_ATTR_UNIQUE_CELL_IDS
:
650 result
+= " unique_cell_ids=" + IntVectorToString(values
);
652 case AX_ATTR_CHARACTER_OFFSETS
:
653 result
+= " character_offsets=" + IntVectorToString(values
);
655 case AX_ATTR_WORD_STARTS
:
656 result
+= " word_starts=" + IntVectorToString(values
);
658 case AX_ATTR_WORD_ENDS
:
659 result
+= " word_ends=" + IntVectorToString(values
);
661 case AX_INT_LIST_ATTRIBUTE_NONE
:
666 if (!child_ids
.empty())
667 result
+= " child_ids=" + IntVectorToString(child_ids
);
672 bool AXNodeData::IsRoot() const {
673 return (role
== AX_ROLE_ROOT_WEB_AREA
||
674 role
== AX_ROLE_DESKTOP
);
677 void AXNodeData::SetRoot() {
678 role
= AX_ROLE_ROOT_WEB_AREA
;