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_TEXT_SEL_START
:
334 result
+= " sel_start=" + value
;
336 case AX_ATTR_TEXT_SEL_END
:
337 result
+= " sel_end=" + value
;
339 case AX_ATTR_TABLE_ROW_COUNT
:
340 result
+= " rows=" + value
;
342 case AX_ATTR_TABLE_COLUMN_COUNT
:
343 result
+= " cols=" + value
;
345 case AX_ATTR_TABLE_CELL_COLUMN_INDEX
:
346 result
+= " col=" + value
;
348 case AX_ATTR_TABLE_CELL_ROW_INDEX
:
349 result
+= " row=" + value
;
351 case AX_ATTR_TABLE_CELL_COLUMN_SPAN
:
352 result
+= " colspan=" + value
;
354 case AX_ATTR_TABLE_CELL_ROW_SPAN
:
355 result
+= " rowspan=" + value
;
357 case AX_ATTR_TABLE_COLUMN_HEADER_ID
:
358 result
+= " column_header_id=" + value
;
360 case AX_ATTR_TABLE_COLUMN_INDEX
:
361 result
+= " column_index=" + value
;
363 case AX_ATTR_TABLE_HEADER_ID
:
364 result
+= " header_id=" + value
;
366 case AX_ATTR_TABLE_ROW_HEADER_ID
:
367 result
+= " row_header_id=" + value
;
369 case AX_ATTR_TABLE_ROW_INDEX
:
370 result
+= " row_index=" + value
;
372 case AX_ATTR_SORT_DIRECTION
:
373 switch (int_attributes
[i
].second
) {
374 case AX_SORT_DIRECTION_UNSORTED
:
375 result
+= " sort_direction=none";
377 case AX_SORT_DIRECTION_ASCENDING
:
378 result
+= " sort_direction=ascending";
380 case AX_SORT_DIRECTION_DESCENDING
:
381 result
+= " sort_direction=descending";
383 case AX_SORT_DIRECTION_OTHER
:
384 result
+= " sort_direction=other";
388 case AX_ATTR_TITLE_UI_ELEMENT
:
389 result
+= " title_elem=" + value
;
391 case AX_ATTR_ACTIVEDESCENDANT_ID
:
392 result
+= " activedescendant=" + value
;
394 case AX_ATTR_TREE_ID
:
395 result
+= " tree_id=" + value
;
397 case AX_ATTR_CHILD_TREE_ID
:
398 result
+= " child_tree_id=" + value
;
400 case AX_ATTR_PARENT_TREE_ID
:
401 result
+= " parent_tree_id=" + value
;
403 case AX_ATTR_COLOR_VALUE
:
404 result
+= base::StringPrintf(" color_value=&%X",
405 int_attributes
[i
].second
);
407 case AX_ATTR_BACKGROUND_COLOR
:
408 result
+= base::StringPrintf(" background_color=&%X",
409 int_attributes
[i
].second
);
412 result
+= base::StringPrintf(" color=&%X", int_attributes
[i
].second
);
414 case AX_ATTR_TEXT_DIRECTION
:
415 switch (int_attributes
[i
].second
) {
416 case AX_TEXT_DIRECTION_LTR
:
417 result
+= " text_direction=ltr";
419 case AX_TEXT_DIRECTION_RTL
:
420 result
+= " text_direction=rtl";
422 case AX_TEXT_DIRECTION_TTB
:
423 result
+= " text_direction=ttb";
425 case AX_TEXT_DIRECTION_BTT
:
426 result
+= " text_direction=btt";
429 case AX_ATTR_TEXT_STYLE
: {
430 unsigned int text_style
= int_attributes
[i
].second
;
431 if (text_style
== AX_TEXT_STYLE_NONE
)
433 std::string
text_style_value(" text_style=");
434 if (text_style
& AX_TEXT_STYLE_BOLD
)
435 text_style_value
+= "bold,";
436 if (text_style
& AX_TEXT_STYLE_ITALIC
)
437 text_style_value
+= "italic,";
438 if (text_style
& AX_TEXT_STYLE_UNDERLINE
)
439 text_style_value
+= "underline,";
440 if (text_style
& AX_TEXT_STYLE_LINE_THROUGH
)
441 text_style_value
+= "line-through,";
442 result
+= text_style_value
.substr(0, text_style_value
.size() - 1);;
445 case AX_ATTR_SET_SIZE
:
446 result
+= " setsize=" + value
;
448 case AX_ATTR_POS_IN_SET
:
449 result
+= " posinset=" + value
;
451 case AX_ATTR_INVALID_STATE
:
452 switch (int_attributes
[i
].second
) {
453 case AX_INVALID_STATE_FALSE
:
454 result
+= " invalid_state=false";
456 case AX_INVALID_STATE_TRUE
:
457 result
+= " invalid_state=true";
459 case AX_INVALID_STATE_SPELLING
:
460 result
+= " invalid_state=spelling";
462 case AX_INVALID_STATE_GRAMMAR
:
463 result
+= " invalid_state=grammar";
465 case AX_INVALID_STATE_OTHER
:
466 result
+= " invalid_state=other";
470 case AX_INT_ATTRIBUTE_NONE
:
475 for (size_t i
= 0; i
< string_attributes
.size(); ++i
) {
476 std::string value
= string_attributes
[i
].second
;
477 switch (string_attributes
[i
].first
) {
478 case AX_ATTR_DOC_URL
:
479 result
+= " doc_url=" + value
;
481 case AX_ATTR_DOC_TITLE
:
482 result
+= " doc_title=" + value
;
484 case AX_ATTR_DOC_MIMETYPE
:
485 result
+= " doc_mimetype=" + value
;
487 case AX_ATTR_DOC_DOCTYPE
:
488 result
+= " doc_doctype=" + value
;
490 case AX_ATTR_ACCESS_KEY
:
491 result
+= " access_key=" + value
;
494 result
+= " action=" + value
;
496 case AX_ATTR_AUTO_COMPLETE
:
497 result
+= " autocomplete=" + value
;
499 case AX_ATTR_DESCRIPTION
:
500 result
+= " description=" + value
;
502 case AX_ATTR_DISPLAY
:
503 result
+= " display=" + value
;
506 result
+= " help=" + value
;
508 case AX_ATTR_HTML_TAG
:
509 result
+= " html_tag=" + value
;
511 case AX_ATTR_ARIA_INVALID_VALUE
:
512 result
+= " aria_invalid_value=" + value
;
514 case AX_ATTR_LIVE_RELEVANT
:
515 result
+= " relevant=" + value
;
517 case AX_ATTR_LIVE_STATUS
:
518 result
+= " live=" + value
;
520 case AX_ATTR_CONTAINER_LIVE_RELEVANT
:
521 result
+= " container_relevant=" + value
;
523 case AX_ATTR_CONTAINER_LIVE_STATUS
:
524 result
+= " container_live=" + value
;
526 case AX_ATTR_PLACEHOLDER
:
527 result
+= "placeholder" + value
;
530 result
+= " role=" + value
;
532 case AX_ATTR_SHORTCUT
:
533 result
+= " shortcut=" + value
;
536 result
+= " url=" + value
;
539 result
+= " name=" + value
;
542 result
+= " value=" + value
;
544 case AX_STRING_ATTRIBUTE_NONE
:
549 for (size_t i
= 0; i
< float_attributes
.size(); ++i
) {
550 std::string value
= DoubleToString(float_attributes
[i
].second
);
551 switch (float_attributes
[i
].first
) {
552 case AX_ATTR_DOC_LOADING_PROGRESS
:
553 result
+= " doc_progress=" + value
;
555 case AX_ATTR_VALUE_FOR_RANGE
:
556 result
+= " value_for_range=" + value
;
558 case AX_ATTR_MAX_VALUE_FOR_RANGE
:
559 result
+= " max_value=" + value
;
561 case AX_ATTR_MIN_VALUE_FOR_RANGE
:
562 result
+= " min_value=" + value
;
564 case AX_ATTR_FONT_SIZE
:
565 result
+= " font_size=" + value
;
567 case AX_FLOAT_ATTRIBUTE_NONE
:
572 for (size_t i
= 0; i
< bool_attributes
.size(); ++i
) {
573 std::string value
= bool_attributes
[i
].second
? "true" : "false";
574 switch (bool_attributes
[i
].first
) {
575 case AX_ATTR_DOC_LOADED
:
576 result
+= " doc_loaded=" + value
;
578 case AX_ATTR_BUTTON_MIXED
:
579 result
+= " mixed=" + value
;
581 case AX_ATTR_LIVE_ATOMIC
:
582 result
+= " atomic=" + value
;
584 case AX_ATTR_LIVE_BUSY
:
585 result
+= " busy=" + value
;
587 case AX_ATTR_CONTAINER_LIVE_ATOMIC
:
588 result
+= " container_atomic=" + value
;
590 case AX_ATTR_CONTAINER_LIVE_BUSY
:
591 result
+= " container_busy=" + value
;
593 case AX_ATTR_ARIA_READONLY
:
594 result
+= " aria_readonly=" + value
;
596 case AX_ATTR_CAN_SET_VALUE
:
597 result
+= " can_set_value=" + value
;
599 case AX_ATTR_UPDATE_LOCATION_ONLY
:
600 result
+= " update_location_only=" + value
;
602 case AX_ATTR_CANVAS_HAS_FALLBACK
:
603 result
+= " has_fallback=" + value
;
605 case AX_BOOL_ATTRIBUTE_NONE
:
610 for (size_t i
= 0; i
< intlist_attributes
.size(); ++i
) {
611 const std::vector
<int32
>& values
= intlist_attributes
[i
].second
;
612 switch (intlist_attributes
[i
].first
) {
613 case AX_ATTR_INDIRECT_CHILD_IDS
:
614 result
+= " indirect_child_ids=" + IntVectorToString(values
);
616 case AX_ATTR_CONTROLS_IDS
:
617 result
+= " controls_ids=" + IntVectorToString(values
);
619 case AX_ATTR_DESCRIBEDBY_IDS
:
620 result
+= " describedby_ids=" + IntVectorToString(values
);
622 case AX_ATTR_FLOWTO_IDS
:
623 result
+= " flowto_ids=" + IntVectorToString(values
);
625 case AX_ATTR_LABELLEDBY_IDS
:
626 result
+= " labelledby_ids=" + IntVectorToString(values
);
628 case AX_ATTR_OWNS_IDS
:
629 result
+= " owns_ids=" + IntVectorToString(values
);
631 case AX_ATTR_LINE_BREAKS
:
632 result
+= " line_breaks=" + IntVectorToString(values
);
634 case AX_ATTR_CELL_IDS
:
635 result
+= " cell_ids=" + IntVectorToString(values
);
637 case AX_ATTR_UNIQUE_CELL_IDS
:
638 result
+= " unique_cell_ids=" + IntVectorToString(values
);
640 case AX_ATTR_CHARACTER_OFFSETS
:
641 result
+= " character_offsets=" + IntVectorToString(values
);
643 case AX_ATTR_WORD_STARTS
:
644 result
+= " word_starts=" + IntVectorToString(values
);
646 case AX_ATTR_WORD_ENDS
:
647 result
+= " word_ends=" + IntVectorToString(values
);
649 case AX_INT_LIST_ATTRIBUTE_NONE
:
654 if (!child_ids
.empty())
655 result
+= " child_ids=" + IntVectorToString(child_ids
);
660 bool AXNodeData::IsRoot() const {
661 return (role
== AX_ROLE_ROOT_WEB_AREA
||
662 role
== AX_ROLE_DESKTOP
);
665 void AXNodeData::SetRoot() {
666 role
= AX_ROLE_ROOT_WEB_AREA
;