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/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"
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 } // Anonymous namespace
33 AXNodeData::AXNodeData()
35 role(AX_ROLE_UNKNOWN),
39 AXNodeData::~AXNodeData() {
42 void AXNodeData::AddStringAttribute(
43 AXStringAttribute attribute, const std::string& value) {
44 string_attributes.push_back(std::make_pair(attribute, value));
47 void AXNodeData::AddIntAttribute(
48 AXIntAttribute attribute, int value) {
49 int_attributes.push_back(std::make_pair(attribute, value));
52 void AXNodeData::AddFloatAttribute(
53 AXFloatAttribute attribute, float value) {
54 float_attributes.push_back(std::make_pair(attribute, value));
57 void AXNodeData::AddBoolAttribute(
58 AXBoolAttribute attribute, bool value) {
59 bool_attributes.push_back(std::make_pair(attribute, value));
62 void AXNodeData::AddIntListAttribute(
63 AXIntListAttribute attribute, const std::vector<int32>& value) {
64 intlist_attributes.push_back(std::make_pair(attribute, value));
67 void AXNodeData::SetName(std::string name) {
68 string_attributes.push_back(std::make_pair(AX_ATTR_NAME, name));
71 void AXNodeData::SetValue(std::string value) {
72 string_attributes.push_back(std::make_pair(AX_ATTR_VALUE, value));
75 std::string AXNodeData::ToString() const {
78 result += "id=" + IntToString(id);
79 result += " " + ui::ToString(role);
81 if (state & (1 << ui::AX_STATE_BUSY))
83 if (state & (1 << ui::AX_STATE_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))
93 if (state & (1 << ui::AX_STATE_HASPOPUP))
94 result += " HASPOPUP";
95 if (state & (1 << ui::AX_STATE_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))
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";
124 result += " (" + IntToString(location.x()) + ", " +
125 IntToString(location.y()) + ")-(" +
126 IntToString(location.width()) + ", " +
127 IntToString(location.height()) + ")";
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;
135 case AX_ATTR_SCROLL_X_MIN:
136 result += " scroll_x_min=" + value;
138 case AX_ATTR_SCROLL_X_MAX:
139 result += " scroll_x_max=" + value;
141 case AX_ATTR_SCROLL_Y:
142 result += " scroll_y=" + value;
144 case AX_ATTR_SCROLL_Y_MIN:
145 result += " scroll_y_min=" + value;
147 case AX_ATTR_SCROLL_Y_MAX:
148 result += " scroll_y_max=" + value;
150 case AX_ATTR_HIERARCHICAL_LEVEL:
151 result += " level=" + value;
153 case AX_ATTR_TEXT_SEL_START:
154 result += " sel_start=" + value;
156 case AX_ATTR_TEXT_SEL_END:
157 result += " sel_end=" + value;
159 case AX_ATTR_TABLE_ROW_COUNT:
160 result += " rows=" + value;
162 case AX_ATTR_TABLE_COLUMN_COUNT:
163 result += " cols=" + value;
165 case AX_ATTR_TABLE_CELL_COLUMN_INDEX:
166 result += " col=" + value;
168 case AX_ATTR_TABLE_CELL_ROW_INDEX:
169 result += " row=" + value;
171 case AX_ATTR_TABLE_CELL_COLUMN_SPAN:
172 result += " colspan=" + value;
174 case AX_ATTR_TABLE_CELL_ROW_SPAN:
175 result += " rowspan=" + value;
177 case AX_ATTR_TABLE_COLUMN_HEADER_ID:
178 result += " column_header_id=" + value;
180 case AX_ATTR_TABLE_COLUMN_INDEX:
181 result += " column_index=" + value;
183 case AX_ATTR_TABLE_HEADER_ID:
184 result += " header_id=" + value;
186 case AX_ATTR_TABLE_ROW_HEADER_ID:
187 result += " row_header_id=" + value;
189 case AX_ATTR_TABLE_ROW_INDEX:
190 result += " row_index=" + value;
192 case AX_ATTR_TITLE_UI_ELEMENT:
193 result += " title_elem=" + value;
195 case AX_ATTR_COLOR_VALUE_RED:
196 result += " color_value_red=" + value;
198 case AX_ATTR_COLOR_VALUE_GREEN:
199 result += " color_value_green=" + value;
201 case AX_ATTR_COLOR_VALUE_BLUE:
202 result += " color_value_blue=" + value;
204 case AX_ATTR_TEXT_DIRECTION:
205 switch (int_attributes[i].second) {
206 case AX_TEXT_DIRECTION_LR:
208 result += " text_direction=lr";
210 case AX_TEXT_DIRECTION_RL:
211 result += " text_direction=rl";
213 case AX_TEXT_DIRECTION_TB:
214 result += " text_direction=tb";
216 case AX_TEXT_DIRECTION_BT:
217 result += " text_direction=bt";
221 case AX_INT_ATTRIBUTE_NONE:
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;
232 case AX_ATTR_DOC_TITLE:
233 result += " doc_title=" + value;
235 case AX_ATTR_DOC_MIMETYPE:
236 result += " doc_mimetype=" + value;
238 case AX_ATTR_DOC_DOCTYPE:
239 result += " doc_doctype=" + value;
241 case AX_ATTR_ACCESS_KEY:
242 result += " access_key=" + value;
245 result += " action=" + value;
247 case AX_ATTR_DESCRIPTION:
248 result += " description=" + value;
250 case AX_ATTR_DISPLAY:
251 result += " display=" + value;
254 result += " help=" + value;
256 case AX_ATTR_HTML_TAG:
257 result += " html_tag=" + value;
259 case AX_ATTR_LIVE_RELEVANT:
260 result += " relevant=" + value;
262 case AX_ATTR_LIVE_STATUS:
263 result += " live=" + value;
265 case AX_ATTR_CONTAINER_LIVE_RELEVANT:
266 result += " container_relevant=" + value;
268 case AX_ATTR_CONTAINER_LIVE_STATUS:
269 result += " container_live=" + value;
272 result += " role=" + value;
274 case AX_ATTR_SHORTCUT:
275 result += " shortcut=" + value;
278 result += " url=" + value;
281 result += " name=" + value;
284 result += " value=" + value;
286 case AX_STRING_ATTRIBUTE_NONE:
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;
297 case AX_ATTR_VALUE_FOR_RANGE:
298 result += " value_for_range=" + value;
300 case AX_ATTR_MAX_VALUE_FOR_RANGE:
301 result += " max_value=" + value;
303 case AX_ATTR_MIN_VALUE_FOR_RANGE:
304 result += " min_value=" + value;
306 case AX_FLOAT_ATTRIBUTE_NONE:
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;
317 case AX_ATTR_BUTTON_MIXED:
318 result += " mixed=" + value;
320 case AX_ATTR_LIVE_ATOMIC:
321 result += " atomic=" + value;
323 case AX_ATTR_LIVE_BUSY:
324 result += " busy=" + value;
326 case AX_ATTR_CONTAINER_LIVE_ATOMIC:
327 result += " container_atomic=" + value;
329 case AX_ATTR_CONTAINER_LIVE_BUSY:
330 result += " container_busy=" + value;
332 case AX_ATTR_ARIA_READONLY:
333 result += " aria_readonly=" + value;
335 case AX_ATTR_CAN_SET_VALUE:
336 result += " can_set_value=" + value;
338 case AX_ATTR_UPDATE_LOCATION_ONLY:
339 result += " update_location_only=" + value;
341 case AX_ATTR_CANVAS_HAS_FALLBACK:
342 result += " has_fallback=" + value;
344 case AX_BOOL_ATTRIBUTE_NONE:
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);
355 case AX_ATTR_LINE_BREAKS:
356 result += " line_breaks=" + IntVectorToString(values);
358 case AX_ATTR_CELL_IDS:
359 result += " cell_ids=" + IntVectorToString(values);
361 case AX_ATTR_UNIQUE_CELL_IDS:
362 result += " unique_cell_ids=" + IntVectorToString(values);
364 case AX_ATTR_CHARACTER_OFFSETS:
365 result += " character_offsets=" + IntVectorToString(values);
367 case AX_ATTR_WORD_STARTS:
368 result += " word_starts=" + IntVectorToString(values);
370 case AX_ATTR_WORD_ENDS:
371 result += " word_ends=" + IntVectorToString(values);
373 case AX_INT_LIST_ATTRIBUTE_NONE:
378 if (!child_ids.empty())
379 result += " child_ids=" + IntVectorToString(child_ids);