Chromium Code Reviews| Index: content/browser/accessibility/browser_accessibility.cc |
| diff --git a/content/browser/accessibility/browser_accessibility.cc b/content/browser/accessibility/browser_accessibility.cc |
| index b41a21b5bbe123d50b0d06df4ff574abedadbe43..a144869ca44d1ac85eb6555214596cd7be575966 100644 |
| --- a/content/browser/accessibility/browser_accessibility.cc |
| +++ b/content/browser/accessibility/browser_accessibility.cc |
| @@ -4,12 +4,15 @@ |
| #include "content/browser/accessibility/browser_accessibility.h" |
| +#include <algorithm> |
| + |
| #include "base/logging.h" |
| #include "base/strings/string_number_conversions.h" |
| #include "base/strings/string_util.h" |
| #include "base/strings/utf_string_conversions.h" |
| #include "content/browser/accessibility/browser_accessibility_manager.h" |
| #include "content/common/accessibility_messages.h" |
| +#include "ui/accessibility/ax_text_utils.h" |
| namespace content { |
| @@ -307,6 +310,75 @@ gfx::Rect BrowserAccessibility::GetGlobalBoundsForRange(int start, int len) |
| return bounds; |
| } |
| +int BrowserAccessibility::GetWordStartBoundary( |
| + int start, ui::TextBoundaryDirection direction) const { |
| + int prev_word_start = 0; |
| + int word_start = 0; |
| + if (GetRole() != ui::AX_ROLE_STATIC_TEXT) { |
| + int prev_child_y = -1; |
| + for (size_t i = 0; i < InternalChildCount(); ++i) { |
| + BrowserAccessibility* child = InternalGetChild(i); |
| + int child_len = child->GetStaticTextLenRecursive(); |
| + int child_word_start = child->GetWordStartBoundary(start, direction); |
| + word_start += child_word_start; |
| + if (child_word_start != child_len) |
| + break; |
| + gfx::Rect child_rect = child->GetLocation(); |
| + int child_y = child_rect.y(); |
| + if (prev_child_y == -1) |
| + prev_child_y = child_y; |
| + if (prev_child_y != child_y) { |
| + child_len += 1; |
| + word_start += 1; |
| + prev_child_y = child_y; |
| + } |
| + start -= child_len; |
| + } |
| + return word_start; |
| + } |
| + |
| + int child_start = 0; |
| + int child_end = 0; |
| + for (size_t i = 0; i < InternalChildCount(); ++i) { |
| + // The next child starts where the previous one ended. |
| + child_start = child_end; |
| + BrowserAccessibility* child = InternalGetChild(i); |
| + DCHECK_EQ(child->GetRole(), ui::AX_ROLE_INLINE_TEXT_BOX); |
| + std::string child_text; |
| + child->GetStringAttribute(ui::AX_ATTR_VALUE, &child_text); |
|
dmazzoni
2015/02/20 18:16:21
Minor nit: it's slightly better to write:
const s
|
| + int child_len = static_cast<int>(child_text.size()); |
| + child_end += child_len; // End is one past the last character. |
| + |
| + const std::vector<int32>& word_starts = child->GetIntListAttribute( |
| + ui::AX_ATTR_WORD_STARTS); |
| + if (word_starts.empty()) { |
| + word_start = child_end; |
| + continue; |
| + } |
| + |
| + int local_start = start - child_start; |
| + std::vector<int32>::const_iterator iter = std::upper_bound( |
| + word_starts.cbegin(), word_starts.cend(), local_start); |
| + if (iter != word_starts.cend()) { |
| + if (direction == ui::FORWARDS_DIRECTION) { |
| + word_start = child_start + *iter; |
| + } else if (direction == ui::BACKWARDS_DIRECTION) { |
|
dmazzoni
2015/02/20 18:16:21
nit: handle the case where direction is not FORWAR
|
| + if (iter == word_starts.cbegin()) { |
| + // Return the position of the last word in the previous child. |
| + word_start = prev_word_start; |
| + } else { |
| + word_start = child_start + *(iter - 1); |
| + } |
| + } |
| + break; |
| + } |
| + prev_word_start = *(iter - 1); |
| + word_start = child_end; |
| + } |
| + |
| + return word_start; |
| +} |
| + |
| BrowserAccessibility* BrowserAccessibility::BrowserAccessibilityForPoint( |
| const gfx::Point& point) { |
| // The best result found that's a child of this object. |