cc: Convert RS flattening unit test to LayerImpl
[chromium-blink-merge.git] / ui / accessibility / ax_node_data.cc
blob7dd0dd782a975214ea87c26780fbda554b97f2f6
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"
7 #include <set>
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;
17 namespace ui {
19 namespace {
21 std::string IntVectorToString(const std::vector<int>& items) {
22 std::string str;
23 for (size_t i = 0; i < items.size(); ++i) {
24 if (i > 0)
25 str += ",";
26 str += IntToString(items[i]);
28 return str;
31 // Predicate that returns true if the first value of a pair is |first|.
32 template<typename FirstType, typename SecondType>
33 struct FirstIs {
34 FirstIs(FirstType first)
35 : first_(first) {}
36 bool operator()(std::pair<FirstType, SecondType> const& p) {
37 return p.first == first_;
39 FirstType 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
46 FindInVectorOfPairs(
47 FirstType first,
48 const std::vector<std::pair<FirstType, SecondType>>& vector) {
49 return std::find_if(vector.begin(),
50 vector.end(),
51 FirstIs<FirstType, SecondType>(first));
54 } // namespace
56 AXNodeData::AXNodeData()
57 : id(-1),
58 role(AX_ROLE_UNKNOWN),
59 state(0xFFFFFFFF) {
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 {
71 bool result;
72 if (GetBoolAttribute(attribute, &result))
73 return result;
74 return false;
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;
82 return true;
85 return false;
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 {
94 float result;
95 if (GetFloatAttribute(attribute, &result))
96 return result;
97 return 0.0;
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;
105 return true;
108 return false;
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 {
117 int result;
118 if (GetIntAttribute(attribute, &result))
119 return result;
120 return 0;
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;
128 return true;
131 return false;
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;
151 return true;
154 return false;
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))
170 return false;
171 *value = base::UTF8ToUTF16(value_utf8);
172 return true;
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())
185 return iter->second;
186 return empty_vector;
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;
194 return true;
197 return false;
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;
206 return true;
210 return false;
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))
217 return false;
218 *value = base::UTF8ToUTF16(value_utf8);
219 return true;
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 {
256 std::string result;
258 result += "id=" + IntToString(id);
259 result += " " + ui::ToString(role);
261 if (state & (1 << AX_STATE_BUSY))
262 result += " 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))
282 result += " 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;
314 break;
315 case AX_ATTR_SCROLL_X_MIN:
316 result += " scroll_x_min=" + value;
317 break;
318 case AX_ATTR_SCROLL_X_MAX:
319 result += " scroll_x_max=" + value;
320 break;
321 case AX_ATTR_SCROLL_Y:
322 result += " scroll_y=" + value;
323 break;
324 case AX_ATTR_SCROLL_Y_MIN:
325 result += " scroll_y_min=" + value;
326 break;
327 case AX_ATTR_SCROLL_Y_MAX:
328 result += " scroll_y_max=" + value;
329 break;
330 case AX_ATTR_HIERARCHICAL_LEVEL:
331 result += " level=" + value;
332 break;
333 case AX_ATTR_ANCHOR_OBJECT_ID:
334 result += " anchor_object_id=" + value;
335 break;
336 case AX_ATTR_ANCHOR_OFFSET:
337 result += " anchor_offset=" + value;
338 break;
339 case AX_ATTR_FOCUS_OBJECT_ID:
340 result += " focus_object_id=" + value;
341 break;
342 case AX_ATTR_FOCUS_OFFSET:
343 result += " focus_offset=" + value;
344 break;
345 case AX_ATTR_TEXT_SEL_START:
346 result += " sel_start=" + value;
347 break;
348 case AX_ATTR_TEXT_SEL_END:
349 result += " sel_end=" + value;
350 break;
351 case AX_ATTR_TABLE_ROW_COUNT:
352 result += " rows=" + value;
353 break;
354 case AX_ATTR_TABLE_COLUMN_COUNT:
355 result += " cols=" + value;
356 break;
357 case AX_ATTR_TABLE_CELL_COLUMN_INDEX:
358 result += " col=" + value;
359 break;
360 case AX_ATTR_TABLE_CELL_ROW_INDEX:
361 result += " row=" + value;
362 break;
363 case AX_ATTR_TABLE_CELL_COLUMN_SPAN:
364 result += " colspan=" + value;
365 break;
366 case AX_ATTR_TABLE_CELL_ROW_SPAN:
367 result += " rowspan=" + value;
368 break;
369 case AX_ATTR_TABLE_COLUMN_HEADER_ID:
370 result += " column_header_id=" + value;
371 break;
372 case AX_ATTR_TABLE_COLUMN_INDEX:
373 result += " column_index=" + value;
374 break;
375 case AX_ATTR_TABLE_HEADER_ID:
376 result += " header_id=" + value;
377 break;
378 case AX_ATTR_TABLE_ROW_HEADER_ID:
379 result += " row_header_id=" + value;
380 break;
381 case AX_ATTR_TABLE_ROW_INDEX:
382 result += " row_index=" + value;
383 break;
384 case AX_ATTR_SORT_DIRECTION:
385 switch (int_attributes[i].second) {
386 case AX_SORT_DIRECTION_UNSORTED:
387 result += " sort_direction=none";
388 break;
389 case AX_SORT_DIRECTION_ASCENDING:
390 result += " sort_direction=ascending";
391 break;
392 case AX_SORT_DIRECTION_DESCENDING:
393 result += " sort_direction=descending";
394 break;
395 case AX_SORT_DIRECTION_OTHER:
396 result += " sort_direction=other";
397 break;
399 break;
400 case AX_ATTR_TITLE_UI_ELEMENT:
401 result += " title_elem=" + value;
402 break;
403 case AX_ATTR_ACTIVEDESCENDANT_ID:
404 result += " activedescendant=" + value;
405 break;
406 case AX_ATTR_TREE_ID:
407 result += " tree_id=" + value;
408 break;
409 case AX_ATTR_CHILD_TREE_ID:
410 result += " child_tree_id=" + value;
411 break;
412 case AX_ATTR_PARENT_TREE_ID:
413 result += " parent_tree_id=" + value;
414 break;
415 case AX_ATTR_COLOR_VALUE:
416 result += base::StringPrintf(" color_value=&%X",
417 int_attributes[i].second);
418 break;
419 case AX_ATTR_BACKGROUND_COLOR:
420 result += base::StringPrintf(" background_color=&%X",
421 int_attributes[i].second);
422 break;
423 case AX_ATTR_COLOR:
424 result += base::StringPrintf(" color=&%X", int_attributes[i].second);
425 break;
426 case AX_ATTR_TEXT_DIRECTION:
427 switch (int_attributes[i].second) {
428 case AX_TEXT_DIRECTION_LTR:
429 result += " text_direction=ltr";
430 break;
431 case AX_TEXT_DIRECTION_RTL:
432 result += " text_direction=rtl";
433 break;
434 case AX_TEXT_DIRECTION_TTB:
435 result += " text_direction=ttb";
436 break;
437 case AX_TEXT_DIRECTION_BTT:
438 result += " text_direction=btt";
439 break;
441 case AX_ATTR_TEXT_STYLE: {
442 unsigned int text_style = int_attributes[i].second;
443 if (text_style == AX_TEXT_STYLE_NONE)
444 break;
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);;
456 break;
457 case AX_ATTR_SET_SIZE:
458 result += " setsize=" + value;
459 break;
460 case AX_ATTR_POS_IN_SET:
461 result += " posinset=" + value;
462 break;
463 case AX_ATTR_INVALID_STATE:
464 switch (int_attributes[i].second) {
465 case AX_INVALID_STATE_FALSE:
466 result += " invalid_state=false";
467 break;
468 case AX_INVALID_STATE_TRUE:
469 result += " invalid_state=true";
470 break;
471 case AX_INVALID_STATE_SPELLING:
472 result += " invalid_state=spelling";
473 break;
474 case AX_INVALID_STATE_GRAMMAR:
475 result += " invalid_state=grammar";
476 break;
477 case AX_INVALID_STATE_OTHER:
478 result += " invalid_state=other";
479 break;
481 break;
482 case AX_INT_ATTRIBUTE_NONE:
483 break;
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;
492 break;
493 case AX_ATTR_DOC_TITLE:
494 result += " doc_title=" + value;
495 break;
496 case AX_ATTR_DOC_MIMETYPE:
497 result += " doc_mimetype=" + value;
498 break;
499 case AX_ATTR_DOC_DOCTYPE:
500 result += " doc_doctype=" + value;
501 break;
502 case AX_ATTR_ACCESS_KEY:
503 result += " access_key=" + value;
504 break;
505 case AX_ATTR_ACTION:
506 result += " action=" + value;
507 break;
508 case AX_ATTR_AUTO_COMPLETE:
509 result += " autocomplete=" + value;
510 break;
511 case AX_ATTR_DESCRIPTION:
512 result += " description=" + value;
513 break;
514 case AX_ATTR_DISPLAY:
515 result += " display=" + value;
516 break;
517 case AX_ATTR_HELP:
518 result += " help=" + value;
519 break;
520 case AX_ATTR_HTML_TAG:
521 result += " html_tag=" + value;
522 break;
523 case AX_ATTR_ARIA_INVALID_VALUE:
524 result += " aria_invalid_value=" + value;
525 break;
526 case AX_ATTR_LIVE_RELEVANT:
527 result += " relevant=" + value;
528 break;
529 case AX_ATTR_LIVE_STATUS:
530 result += " live=" + value;
531 break;
532 case AX_ATTR_CONTAINER_LIVE_RELEVANT:
533 result += " container_relevant=" + value;
534 break;
535 case AX_ATTR_CONTAINER_LIVE_STATUS:
536 result += " container_live=" + value;
537 break;
538 case AX_ATTR_PLACEHOLDER:
539 result += "placeholder" + value;
540 break;
541 case AX_ATTR_ROLE:
542 result += " role=" + value;
543 break;
544 case AX_ATTR_SHORTCUT:
545 result += " shortcut=" + value;
546 break;
547 case AX_ATTR_URL:
548 result += " url=" + value;
549 break;
550 case AX_ATTR_NAME:
551 result += " name=" + value;
552 break;
553 case AX_ATTR_VALUE:
554 result += " value=" + value;
555 break;
556 case AX_STRING_ATTRIBUTE_NONE:
557 break;
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;
566 break;
567 case AX_ATTR_VALUE_FOR_RANGE:
568 result += " value_for_range=" + value;
569 break;
570 case AX_ATTR_MAX_VALUE_FOR_RANGE:
571 result += " max_value=" + value;
572 break;
573 case AX_ATTR_MIN_VALUE_FOR_RANGE:
574 result += " min_value=" + value;
575 break;
576 case AX_ATTR_FONT_SIZE:
577 result += " font_size=" + value;
578 break;
579 case AX_FLOAT_ATTRIBUTE_NONE:
580 break;
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;
589 break;
590 case AX_ATTR_BUTTON_MIXED:
591 result += " mixed=" + value;
592 break;
593 case AX_ATTR_LIVE_ATOMIC:
594 result += " atomic=" + value;
595 break;
596 case AX_ATTR_LIVE_BUSY:
597 result += " busy=" + value;
598 break;
599 case AX_ATTR_CONTAINER_LIVE_ATOMIC:
600 result += " container_atomic=" + value;
601 break;
602 case AX_ATTR_CONTAINER_LIVE_BUSY:
603 result += " container_busy=" + value;
604 break;
605 case AX_ATTR_ARIA_READONLY:
606 result += " aria_readonly=" + value;
607 break;
608 case AX_ATTR_CAN_SET_VALUE:
609 result += " can_set_value=" + value;
610 break;
611 case AX_ATTR_UPDATE_LOCATION_ONLY:
612 result += " update_location_only=" + value;
613 break;
614 case AX_ATTR_CANVAS_HAS_FALLBACK:
615 result += " has_fallback=" + value;
616 break;
617 case AX_BOOL_ATTRIBUTE_NONE:
618 break;
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);
627 break;
628 case AX_ATTR_CONTROLS_IDS:
629 result += " controls_ids=" + IntVectorToString(values);
630 break;
631 case AX_ATTR_DESCRIBEDBY_IDS:
632 result += " describedby_ids=" + IntVectorToString(values);
633 break;
634 case AX_ATTR_FLOWTO_IDS:
635 result += " flowto_ids=" + IntVectorToString(values);
636 break;
637 case AX_ATTR_LABELLEDBY_IDS:
638 result += " labelledby_ids=" + IntVectorToString(values);
639 break;
640 case AX_ATTR_OWNS_IDS:
641 result += " owns_ids=" + IntVectorToString(values);
642 break;
643 case AX_ATTR_LINE_BREAKS:
644 result += " line_breaks=" + IntVectorToString(values);
645 break;
646 case AX_ATTR_CELL_IDS:
647 result += " cell_ids=" + IntVectorToString(values);
648 break;
649 case AX_ATTR_UNIQUE_CELL_IDS:
650 result += " unique_cell_ids=" + IntVectorToString(values);
651 break;
652 case AX_ATTR_CHARACTER_OFFSETS:
653 result += " character_offsets=" + IntVectorToString(values);
654 break;
655 case AX_ATTR_WORD_STARTS:
656 result += " word_starts=" + IntVectorToString(values);
657 break;
658 case AX_ATTR_WORD_ENDS:
659 result += " word_ends=" + IntVectorToString(values);
660 break;
661 case AX_INT_LIST_ATTRIBUTE_NONE:
662 break;
666 if (!child_ids.empty())
667 result += " child_ids=" + IntVectorToString(child_ids);
669 return result;
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;
681 } // namespace ui