Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ui / accessibility / ax_node_data.cc
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.
4
5 #include "ui/accessibility/ax_node_data.h"
6
7 #include <set>
8
9 #include "base/containers/hash_tables.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13
14 using base::DoubleToString;
15 using base::IntToString;
16
17 namespace ui {
18
19 namespace {
20
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]);
27   }
28   return str;
29 }
30
31 }  // Anonymous namespace
32
33 AXNodeData::AXNodeData()
34     : id(-1),
35       role(AX_ROLE_UNKNOWN),
36       state(-1) {
37 }
38
39 AXNodeData::~AXNodeData() {
40 }
41
42 void AXNodeData::AddStringAttribute(
43     AXStringAttribute attribute, const std::string& value) {
44   string_attributes.push_back(std::make_pair(attribute, value));
45 }
46
47 void AXNodeData::AddIntAttribute(
48     AXIntAttribute attribute, int value) {
49   int_attributes.push_back(std::make_pair(attribute, value));
50 }
51
52 void AXNodeData::AddFloatAttribute(
53     AXFloatAttribute attribute, float value) {
54   float_attributes.push_back(std::make_pair(attribute, value));
55 }
56
57 void AXNodeData::AddBoolAttribute(
58     AXBoolAttribute attribute, bool value) {
59   bool_attributes.push_back(std::make_pair(attribute, value));
60 }
61
62 void AXNodeData::AddIntListAttribute(
63     AXIntListAttribute attribute, const std::vector<int32>& value) {
64   intlist_attributes.push_back(std::make_pair(attribute, value));
65 }
66
67 void AXNodeData::SetName(std::string name) {
68   string_attributes.push_back(std::make_pair(AX_ATTR_NAME, name));
69 }
70
71 void AXNodeData::SetValue(std::string value) {
72   string_attributes.push_back(std::make_pair(AX_ATTR_VALUE, value));
73 }
74
75 std::string AXNodeData::ToString() const {
76   std::string result;
77
78   result += "id=" + IntToString(id);
79   result += " " + ui::ToString(role);
80
81   if (state & (1 << ui::AX_STATE_BUSY))
82     result += " BUSY";
83   if (state & (1 << ui::AX_STATE_CHECKED))
84     result += " CHECKED";
85   if (state & (1 << ui::AX_STATE_COLLAPSED))
86     result += " COLLAPSED";
87   if (state & (1 << ui::AX_STATE_EXPANDED))
88     result += " EXPANDED";
89   if (state & (1 << ui::AX_STATE_FOCUSABLE))
90     result += " FOCUSABLE";
91   if (state & (1 << ui::AX_STATE_FOCUSED))
92     result += " FOCUSED";
93   if (state & (1 << ui::AX_STATE_HASPOPUP))
94     result += " HASPOPUP";
95   if (state & (1 << ui::AX_STATE_HOVERED))
96     result += " HOVERED";
97   if (state & (1 << ui::AX_STATE_INDETERMINATE))
98     result += " INDETERMINATE";
99   if (state & (1 << ui::AX_STATE_INVISIBLE))
100     result += " INVISIBLE";
101   if (state & (1 << ui::AX_STATE_LINKED))
102     result += " LINKED";
103   if (state & (1 << ui::AX_STATE_MULTISELECTABLE))
104     result += " MULTISELECTABLE";
105   if (state & (1 << ui::AX_STATE_OFFSCREEN))
106     result += " OFFSCREEN";
107   if (state & (1 << ui::AX_STATE_PRESSED))
108     result += " PRESSED";
109   if (state & (1 << ui::AX_STATE_PROTECTED))
110     result += " PROTECTED";
111   if (state & (1 << ui::AX_STATE_READ_ONLY))
112     result += " READONLY";
113   if (state & (1 << ui::AX_STATE_REQUIRED))
114     result += " REQUIRED";
115   if (state & (1 << ui::AX_STATE_SELECTABLE))
116     result += " SELECTABLE";
117   if (state & (1 << ui::AX_STATE_SELECTED))
118     result += " SELECTED";
119   if (state & (1 << ui::AX_STATE_VERTICAL))
120     result += " VERTICAL";
121   if (state & (1 << ui::AX_STATE_VISITED))
122     result += " VISITED";
123
124   result += " (" + IntToString(location.x()) + ", " +
125                    IntToString(location.y()) + ")-(" +
126                    IntToString(location.width()) + ", " +
127                    IntToString(location.height()) + ")";
128
129   for (size_t i = 0; i < int_attributes.size(); ++i) {
130     std::string value = IntToString(int_attributes[i].second);
131     switch (int_attributes[i].first) {
132       case AX_ATTR_SCROLL_X:
133         result += " scroll_x=" + value;
134         break;
135       case AX_ATTR_SCROLL_X_MIN:
136         result += " scroll_x_min=" + value;
137         break;
138       case AX_ATTR_SCROLL_X_MAX:
139         result += " scroll_x_max=" + value;
140         break;
141       case AX_ATTR_SCROLL_Y:
142         result += " scroll_y=" + value;
143         break;
144       case AX_ATTR_SCROLL_Y_MIN:
145         result += " scroll_y_min=" + value;
146         break;
147       case AX_ATTR_SCROLL_Y_MAX:
148         result += " scroll_y_max=" + value;
149         break;
150       case AX_ATTR_HIERARCHICAL_LEVEL:
151         result += " level=" + value;
152         break;
153       case AX_ATTR_TEXT_SEL_START:
154         result += " sel_start=" + value;
155         break;
156       case AX_ATTR_TEXT_SEL_END:
157         result += " sel_end=" + value;
158         break;
159       case AX_ATTR_TABLE_ROW_COUNT:
160         result += " rows=" + value;
161         break;
162       case AX_ATTR_TABLE_COLUMN_COUNT:
163         result += " cols=" + value;
164         break;
165       case AX_ATTR_TABLE_CELL_COLUMN_INDEX:
166         result += " col=" + value;
167         break;
168       case AX_ATTR_TABLE_CELL_ROW_INDEX:
169         result += " row=" + value;
170         break;
171       case AX_ATTR_TABLE_CELL_COLUMN_SPAN:
172         result += " colspan=" + value;
173         break;
174       case AX_ATTR_TABLE_CELL_ROW_SPAN:
175         result += " rowspan=" + value;
176         break;
177       case AX_ATTR_TABLE_COLUMN_HEADER_ID:
178         result += " column_header_id=" + value;
179         break;
180       case AX_ATTR_TABLE_COLUMN_INDEX:
181         result += " column_index=" + value;
182         break;
183       case AX_ATTR_TABLE_HEADER_ID:
184         result += " header_id=" + value;
185         break;
186       case AX_ATTR_TABLE_ROW_HEADER_ID:
187         result += " row_header_id=" + value;
188         break;
189       case AX_ATTR_TABLE_ROW_INDEX:
190         result += " row_index=" + value;
191         break;
192       case AX_ATTR_TITLE_UI_ELEMENT:
193         result += " title_elem=" + value;
194         break;
195       case AX_ATTR_COLOR_VALUE_RED:
196         result += " color_value_red=" + value;
197         break;
198       case AX_ATTR_COLOR_VALUE_GREEN:
199         result += " color_value_green=" + value;
200         break;
201       case AX_ATTR_COLOR_VALUE_BLUE:
202         result += " color_value_blue=" + value;
203         break;
204       case AX_ATTR_TEXT_DIRECTION:
205         switch (int_attributes[i].second) {
206           case AX_TEXT_DIRECTION_LR:
207           default:
208             result += " text_direction=lr";
209             break;
210           case AX_TEXT_DIRECTION_RL:
211             result += " text_direction=rl";
212             break;
213           case AX_TEXT_DIRECTION_TB:
214             result += " text_direction=tb";
215             break;
216           case AX_TEXT_DIRECTION_BT:
217             result += " text_direction=bt";
218             break;
219         }
220         break;
221       case AX_INT_ATTRIBUTE_NONE:
222         break;
223     }
224   }
225
226   for (size_t i = 0; i < string_attributes.size(); ++i) {
227     std::string value = string_attributes[i].second;
228     switch (string_attributes[i].first) {
229       case AX_ATTR_DOC_URL:
230         result += " doc_url=" + value;
231         break;
232       case AX_ATTR_DOC_TITLE:
233         result += " doc_title=" + value;
234         break;
235       case AX_ATTR_DOC_MIMETYPE:
236         result += " doc_mimetype=" + value;
237         break;
238       case AX_ATTR_DOC_DOCTYPE:
239         result += " doc_doctype=" + value;
240         break;
241       case AX_ATTR_ACCESS_KEY:
242         result += " access_key=" + value;
243         break;
244       case AX_ATTR_ACTION:
245         result += " action=" + value;
246         break;
247       case AX_ATTR_DESCRIPTION:
248         result += " description=" + value;
249         break;
250       case AX_ATTR_DISPLAY:
251         result += " display=" + value;
252         break;
253       case AX_ATTR_HELP:
254         result += " help=" + value;
255         break;
256       case AX_ATTR_HTML_TAG:
257         result += " html_tag=" + value;
258         break;
259       case AX_ATTR_LIVE_RELEVANT:
260         result += " relevant=" + value;
261         break;
262       case AX_ATTR_LIVE_STATUS:
263         result += " live=" + value;
264         break;
265       case AX_ATTR_CONTAINER_LIVE_RELEVANT:
266         result += " container_relevant=" + value;
267         break;
268       case AX_ATTR_CONTAINER_LIVE_STATUS:
269         result += " container_live=" + value;
270         break;
271       case AX_ATTR_ROLE:
272         result += " role=" + value;
273         break;
274       case AX_ATTR_SHORTCUT:
275         result += " shortcut=" + value;
276         break;
277       case AX_ATTR_URL:
278         result += " url=" + value;
279         break;
280       case AX_ATTR_NAME:
281         result += " name=" + value;
282         break;
283       case AX_ATTR_VALUE:
284         result += " value=" + value;
285         break;
286       case AX_STRING_ATTRIBUTE_NONE:
287         break;
288     }
289   }
290
291   for (size_t i = 0; i < float_attributes.size(); ++i) {
292     std::string value = DoubleToString(float_attributes[i].second);
293     switch (float_attributes[i].first) {
294       case AX_ATTR_DOC_LOADING_PROGRESS:
295         result += " doc_progress=" + value;
296         break;
297       case AX_ATTR_VALUE_FOR_RANGE:
298         result += " value_for_range=" + value;
299         break;
300       case AX_ATTR_MAX_VALUE_FOR_RANGE:
301         result += " max_value=" + value;
302         break;
303       case AX_ATTR_MIN_VALUE_FOR_RANGE:
304         result += " min_value=" + value;
305         break;
306       case AX_FLOAT_ATTRIBUTE_NONE:
307         break;
308     }
309   }
310
311   for (size_t i = 0; i < bool_attributes.size(); ++i) {
312     std::string value = bool_attributes[i].second ? "true" : "false";
313     switch (bool_attributes[i].first) {
314       case AX_ATTR_DOC_LOADED:
315         result += " doc_loaded=" + value;
316         break;
317       case AX_ATTR_BUTTON_MIXED:
318         result += " mixed=" + value;
319         break;
320       case AX_ATTR_LIVE_ATOMIC:
321         result += " atomic=" + value;
322         break;
323       case AX_ATTR_LIVE_BUSY:
324         result += " busy=" + value;
325         break;
326       case AX_ATTR_CONTAINER_LIVE_ATOMIC:
327         result += " container_atomic=" + value;
328         break;
329       case AX_ATTR_CONTAINER_LIVE_BUSY:
330         result += " container_busy=" + value;
331         break;
332       case AX_ATTR_ARIA_READONLY:
333         result += " aria_readonly=" + value;
334         break;
335       case AX_ATTR_CAN_SET_VALUE:
336         result += " can_set_value=" + value;
337         break;
338       case AX_ATTR_UPDATE_LOCATION_ONLY:
339         result += " update_location_only=" + value;
340         break;
341       case AX_ATTR_CANVAS_HAS_FALLBACK:
342         result += " has_fallback=" + value;
343         break;
344       case AX_BOOL_ATTRIBUTE_NONE:
345         break;
346     }
347   }
348
349   for (size_t i = 0; i < intlist_attributes.size(); ++i) {
350     const std::vector<int32>& values = intlist_attributes[i].second;
351     switch (intlist_attributes[i].first) {
352       case AX_ATTR_INDIRECT_CHILD_IDS:
353         result += " indirect_child_ids=" + IntVectorToString(values);
354         break;
355       case AX_ATTR_LINE_BREAKS:
356         result += " line_breaks=" + IntVectorToString(values);
357         break;
358       case AX_ATTR_CELL_IDS:
359         result += " cell_ids=" + IntVectorToString(values);
360         break;
361       case AX_ATTR_UNIQUE_CELL_IDS:
362         result += " unique_cell_ids=" + IntVectorToString(values);
363         break;
364       case AX_ATTR_CHARACTER_OFFSETS:
365         result += " character_offsets=" + IntVectorToString(values);
366         break;
367       case AX_ATTR_WORD_STARTS:
368         result += " word_starts=" + IntVectorToString(values);
369         break;
370       case AX_ATTR_WORD_ENDS:
371         result += " word_ends=" + IntVectorToString(values);
372         break;
373       case AX_INT_LIST_ATTRIBUTE_NONE:
374         break;
375     }
376   }
377
378   if (!child_ids.empty())
379     result += " child_ids=" + IntVectorToString(child_ids);
380
381   return result;
382 }
383
384 }  // namespace ui