OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/accessibility/browser_accessibility.h" | 5 #include "content/browser/accessibility/browser_accessibility.h" |
6 | 6 |
| 7 #include <algorithm> |
| 8 |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
8 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
9 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
11 #include "content/browser/accessibility/browser_accessibility_manager.h" | 13 #include "content/browser/accessibility/browser_accessibility_manager.h" |
12 #include "content/common/accessibility_messages.h" | 14 #include "content/common/accessibility_messages.h" |
| 15 #include "ui/accessibility/ax_text_utils.h" |
13 | 16 |
14 namespace content { | 17 namespace content { |
15 | 18 |
16 #if !defined(OS_MACOSX) && \ | 19 #if !defined(OS_MACOSX) && \ |
17 !defined(OS_WIN) && \ | 20 !defined(OS_WIN) && \ |
18 !defined(OS_ANDROID) | 21 !defined(OS_ANDROID) |
19 // We have subclassess of BrowserAccessibility on Mac and Win. For any other | 22 // We have subclassess of BrowserAccessibility on Mac and Win. For any other |
20 // platform, instantiate the base class. | 23 // platform, instantiate the base class. |
21 // static | 24 // static |
22 BrowserAccessibility* BrowserAccessibility::Create() { | 25 BrowserAccessibility* BrowserAccessibility::Create() { |
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 const { | 303 const { |
301 gfx::Rect bounds = GetLocalBoundsForRange(start, len); | 304 gfx::Rect bounds = GetLocalBoundsForRange(start, len); |
302 | 305 |
303 // Adjust the bounds by the top left corner of the containing view's bounds | 306 // Adjust the bounds by the top left corner of the containing view's bounds |
304 // in screen coordinates. | 307 // in screen coordinates. |
305 bounds.Offset(manager_->GetViewBounds().OffsetFromOrigin()); | 308 bounds.Offset(manager_->GetViewBounds().OffsetFromOrigin()); |
306 | 309 |
307 return bounds; | 310 return bounds; |
308 } | 311 } |
309 | 312 |
| 313 int BrowserAccessibility::GetWordStartBoundary( |
| 314 int start, ui::TextBoundaryDirection direction) const { |
| 315 int word_start = 0; |
| 316 int prev_word_start = 0; |
| 317 if (GetRole() != ui::AX_ROLE_STATIC_TEXT) { |
| 318 for (size_t i = 0; i < InternalChildCount(); ++i) { |
| 319 BrowserAccessibility* child = InternalGetChild(i); |
| 320 int child_len = child->GetStaticTextLenRecursive(); |
| 321 int child_word_start = child->GetWordStartBoundary(start, direction); |
| 322 word_start += child_word_start; |
| 323 if (child_word_start != child_len) |
| 324 break; |
| 325 start -= child_len; |
| 326 } |
| 327 return word_start; |
| 328 } |
| 329 |
| 330 int child_start = 0; |
| 331 int child_end = 0; |
| 332 for (size_t i = 0; i < InternalChildCount(); ++i) { |
| 333 // The next child starts where the previous one ended. |
| 334 child_start = child_end; |
| 335 BrowserAccessibility* child = InternalGetChild(i); |
| 336 DCHECK_EQ(child->GetRole(), ui::AX_ROLE_INLINE_TEXT_BOX); |
| 337 const std::string& child_text = child->GetStringAttribute( |
| 338 ui::AX_ATTR_VALUE); |
| 339 int child_len = static_cast<int>(child_text.size()); |
| 340 child_end += child_len; // End is one past the last character. |
| 341 |
| 342 const std::vector<int32>& word_starts = child->GetIntListAttribute( |
| 343 ui::AX_ATTR_WORD_STARTS); |
| 344 if (word_starts.empty()) { |
| 345 word_start = child_end; |
| 346 continue; |
| 347 } |
| 348 |
| 349 int local_start = start - child_start; |
| 350 std::vector<int32>::const_iterator iter = std::upper_bound( |
| 351 word_starts.begin(), word_starts.end(), local_start); |
| 352 if (iter != word_starts.end()) { |
| 353 if (direction == ui::FORWARDS_DIRECTION) { |
| 354 word_start = child_start + *iter; |
| 355 } else if (direction == ui::BACKWARDS_DIRECTION) { |
| 356 if (iter == word_starts.begin()) { |
| 357 // Return the position of the last word in the previous child. |
| 358 word_start = prev_word_start; |
| 359 } else { |
| 360 word_start = child_start + *(iter - 1); |
| 361 } |
| 362 } else { |
| 363 NOTREACHED(); |
| 364 } |
| 365 break; |
| 366 } |
| 367 |
| 368 // No word start that is >= to the requested offset has been found. |
| 369 prev_word_start = child_start + *(iter - 1); |
| 370 if (direction == ui::FORWARDS_DIRECTION) { |
| 371 word_start = child_end; |
| 372 } else if (direction == ui::BACKWARDS_DIRECTION) { |
| 373 word_start = prev_word_start; |
| 374 } else { |
| 375 NOTREACHED(); |
| 376 } |
| 377 } |
| 378 |
| 379 return word_start; |
| 380 } |
| 381 |
310 BrowserAccessibility* BrowserAccessibility::BrowserAccessibilityForPoint( | 382 BrowserAccessibility* BrowserAccessibility::BrowserAccessibilityForPoint( |
311 const gfx::Point& point) { | 383 const gfx::Point& point) { |
312 // The best result found that's a child of this object. | 384 // The best result found that's a child of this object. |
313 BrowserAccessibility* child_result = NULL; | 385 BrowserAccessibility* child_result = NULL; |
314 // The best result that's an indirect descendant like grandchild, etc. | 386 // The best result that's an indirect descendant like grandchild, etc. |
315 BrowserAccessibility* descendant_result = NULL; | 387 BrowserAccessibility* descendant_result = NULL; |
316 | 388 |
317 // Walk the children recursively looking for the BrowserAccessibility that | 389 // Walk the children recursively looking for the BrowserAccessibility that |
318 // most tightly encloses the specified point. Walk backwards so that in | 390 // most tightly encloses the specified point. Walk backwards so that in |
319 // the absence of any other information, we assume the object that occurs | 391 // the absence of any other information, we assume the object that occurs |
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
709 } | 781 } |
710 need_to_offset_web_area = true; | 782 need_to_offset_web_area = true; |
711 } | 783 } |
712 parent = parent->GetParentForBoundsCalculation(); | 784 parent = parent->GetParentForBoundsCalculation(); |
713 } | 785 } |
714 | 786 |
715 return bounds; | 787 return bounds; |
716 } | 788 } |
717 | 789 |
718 } // namespace content | 790 } // namespace content |
OLD | NEW |