| 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 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 const { | 333 const { |
| 331 gfx::Rect bounds = GetLocalBoundsForRange(start, len); | 334 gfx::Rect bounds = GetLocalBoundsForRange(start, len); |
| 332 | 335 |
| 333 // Adjust the bounds by the top left corner of the containing view's bounds | 336 // Adjust the bounds by the top left corner of the containing view's bounds |
| 334 // in screen coordinates. | 337 // in screen coordinates. |
| 335 bounds.Offset(manager_->GetViewBounds().OffsetFromOrigin()); | 338 bounds.Offset(manager_->GetViewBounds().OffsetFromOrigin()); |
| 336 | 339 |
| 337 return bounds; | 340 return bounds; |
| 338 } | 341 } |
| 339 | 342 |
| 343 int BrowserAccessibility::GetWordStartBoundary( |
| 344 int start, ui::TextBoundaryDirection direction) const { |
| 345 int word_start = 0; |
| 346 if (GetRole() != ui::AX_ROLE_STATIC_TEXT) { |
| 347 for (size_t i = 0; i < InternalChildCount() && start >= 0; ++i) { |
| 348 BrowserAccessibility* child = InternalGetChild(i); |
| 349 int child_len = child->GetStaticTextLenRecursive(); |
| 350 if (start >= child_len) { |
| 351 word_start += child_len; |
| 352 start -= child_len; |
| 353 continue; |
| 354 } |
| 355 |
| 356 word_start = child->GetWordStartBoundary(start, direction); |
| 357 } |
| 358 return word_start; |
| 359 } |
| 360 |
| 361 int child_start = 0; |
| 362 int child_end = 0; |
| 363 |
| 364 for (size_t i = 0; i < InternalChildCount(); ++i) { |
| 365 // The next child starts where the previous one ended. |
| 366 child_start = child_end; |
| 367 BrowserAccessibility* child = InternalGetChild(i); |
| 368 DCHECK_EQ(child->GetRole(), ui::AX_ROLE_INLINE_TEXT_BOX); |
| 369 std::string child_text; |
| 370 child->GetStringAttribute(ui::AX_ATTR_VALUE, &child_text); |
| 371 int child_len = static_cast<int>(child_text.size()); |
| 372 child_end += child_len; // End is one past the last character. |
| 373 |
| 374 if (start >= child_end) |
| 375 continue; |
| 376 |
| 377 const std::vector<int32>& word_starts = child->GetIntListAttribute( |
| 378 ui::AX_ATTR_WORD_STARTS); |
| 379 if (word_starts.empty()) |
| 380 continue; |
| 381 |
| 382 int local_start = start - child_start; |
| 383 std::vector<int32>::const_iterator iter = std::upper_bound( |
| 384 word_starts.cbegin(), word_starts.cend(), local_start); |
| 385 if (direction == ui::FORWARDS_DIRECTION) { |
| 386 if (iter != word_starts.cend()) { |
| 387 word_start = *iter; |
| 388 break; |
| 389 } else { |
| 390 // In case there are no more children or remaining children have no |
| 391 // text, e.g., an image. |
| 392 word_start = child_end; |
| 393 continue; |
| 394 } |
| 395 } else if (direction == ui::BACKWARDS_DIRECTION) { |
| 396 if (iter != word_starts.cbegin()) { |
| 397 --iter; |
| 398 word_start = *iter; |
| 399 } else { |
| 400 // Return the start of the last word in the previous child. |
| 401 for (int j = i - 1; j >= 0; --j) { |
| 402 BrowserAccessibility* child = InternalGetChild(j); |
| 403 const std::vector<int32>& word_starts = child->GetIntListAttribute( |
| 404 ui::AX_ATTR_WORD_STARTS); |
| 405 if (word_starts.empty()) |
| 406 continue; |
| 407 |
| 408 word_start = word_starts[word_starts.size() - 1]; |
| 409 } |
| 410 } |
| 411 break; |
| 412 } |
| 413 } |
| 414 |
| 415 return word_start; |
| 416 } |
| 417 |
| 340 BrowserAccessibility* BrowserAccessibility::BrowserAccessibilityForPoint( | 418 BrowserAccessibility* BrowserAccessibility::BrowserAccessibilityForPoint( |
| 341 const gfx::Point& point) { | 419 const gfx::Point& point) { |
| 342 // The best result found that's a child of this object. | 420 // The best result found that's a child of this object. |
| 343 BrowserAccessibility* child_result = NULL; | 421 BrowserAccessibility* child_result = NULL; |
| 344 // The best result that's an indirect descendant like grandchild, etc. | 422 // The best result that's an indirect descendant like grandchild, etc. |
| 345 BrowserAccessibility* descendant_result = NULL; | 423 BrowserAccessibility* descendant_result = NULL; |
| 346 | 424 |
| 347 // Walk the children recursively looking for the BrowserAccessibility that | 425 // Walk the children recursively looking for the BrowserAccessibility that |
| 348 // most tightly encloses the specified point. Walk backwards so that in | 426 // most tightly encloses the specified point. Walk backwards so that in |
| 349 // the absence of any other information, we assume the object that occurs | 427 // the absence of any other information, we assume the object that occurs |
| (...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 694 if (GetRole() == ui::AX_ROLE_STATIC_TEXT) | 772 if (GetRole() == ui::AX_ROLE_STATIC_TEXT) |
| 695 return static_cast<int>(GetStringAttribute(ui::AX_ATTR_VALUE).size()); | 773 return static_cast<int>(GetStringAttribute(ui::AX_ATTR_VALUE).size()); |
| 696 | 774 |
| 697 int len = 0; | 775 int len = 0; |
| 698 for (size_t i = 0; i < InternalChildCount(); ++i) | 776 for (size_t i = 0; i < InternalChildCount(); ++i) |
| 699 len += InternalGetChild(i)->GetStaticTextLenRecursive(); | 777 len += InternalGetChild(i)->GetStaticTextLenRecursive(); |
| 700 return len; | 778 return len; |
| 701 } | 779 } |
| 702 | 780 |
| 703 } // namespace content | 781 } // namespace content |
| OLD | NEW |