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( | |
dmazzoni
2014/11/17 19:06:46
A few more comments in this function please?
| |
344 int start, ui::TextBoundaryDirection direction) const { | |
345 int prev_word_start = 0; | |
346 int word_start = 0; | |
347 DLOG(INFO) << "Original start = " << start; | |
348 if (GetRole() != ui::AX_ROLE_STATIC_TEXT) { | |
349 for (size_t i = 0; i < InternalChildCount(); ++i) { | |
350 BrowserAccessibility* child = InternalGetChild(i); | |
351 int child_len = child->GetStaticTextLenRecursive(); | |
352 DLOG(INFO) << "child_len = " << child_len; | |
353 int child_word_start = child->GetWordStartBoundary(start, direction); | |
354 word_start += child_word_start; | |
355 if (child_word_start != child_len) | |
356 break; | |
357 const std::vector<int32>& lines = child->GetIntListAttribute( | |
dmazzoni
2014/11/17 19:06:46
I think you can get rid of the line break search,
| |
358 ui::AX_ATTR_LINE_BREAKS); | |
359 for (size_t l = 0; l < lines.size(); ++l) { | |
360 DLOG(INFO) << "Line breaks " << l << " = " << lines[l]; | |
361 } | |
362 std::vector<int32>::const_iterator iter = std::find( | |
363 lines.cbegin(), lines.cend(), word_start); | |
364 if (iter != lines.cend()) { | |
365 child_len += 1; | |
366 word_start += 1; | |
367 DLOG(INFO) << "Found line break = " << word_start; | |
368 } | |
369 start -= child_len; | |
370 } | |
371 return word_start; | |
372 } | |
373 | |
374 int child_start = 0; | |
375 int child_end = 0; | |
376 for (size_t i = 0; i < InternalChildCount(); ++i) { | |
377 // The next child starts where the previous one ended. | |
378 child_start = child_end; | |
379 BrowserAccessibility* child = InternalGetChild(i); | |
380 DCHECK_EQ(child->GetRole(), ui::AX_ROLE_INLINE_TEXT_BOX); | |
381 std::string child_text; | |
382 child->GetStringAttribute(ui::AX_ATTR_VALUE, &child_text); | |
383 int child_len = static_cast<int>(child_text.size()); | |
384 child_end += child_len; // End is one past the last character. | |
385 DLOG(INFO) << "child_start = " << child_start << " child_end " << child_end; | |
386 | |
387 const std::vector<int32>& word_starts = child->GetIntListAttribute( | |
388 ui::AX_ATTR_WORD_STARTS); | |
389 if (word_starts.empty()) { | |
390 word_start = child_end; | |
391 continue; | |
392 } | |
393 DLOG(INFO) << "First word at = " << word_starts[0]; | |
394 DLOG(INFO) << "Text = " << child_text; | |
395 | |
396 int local_start = start - child_start; | |
dmazzoni
2014/11/17 19:06:46
nit: indentation
| |
397 std::vector<int32>::const_iterator iter = std::upper_bound( | |
398 word_starts.cbegin(), word_starts.cend(), local_start); | |
399 if (iter != word_starts.cend()) { | |
400 if (direction == ui::FORWARDS_DIRECTION) { | |
401 word_start = child_start + *iter; | |
402 DLOG(INFO) << "Forward found at word_start = " << word_start; | |
403 } else if (direction == ui::BACKWARDS_DIRECTION) { | |
404 if (iter == word_starts.cbegin()) { | |
405 // Return the position of the last word in the previous child. | |
406 word_start = prev_word_start; | |
407 DLOG(INFO) << "Backward prev at word_start = " << word_start; | |
408 } else { | |
409 word_start = child_start + *(iter - 1); | |
410 DLOG(INFO) << "Backward at word_start = " << word_start; | |
411 } | |
412 } | |
413 break; | |
414 } | |
415 prev_word_start = *(iter - 1); | |
416 word_start = child_end; | |
417 DLOG(INFO) << "Not found at word_start = " << word_start; | |
418 } | |
419 | |
420 DLOG(INFO) << "Final value word_start = " << word_start; | |
421 return word_start; | |
422 } | |
423 | |
340 BrowserAccessibility* BrowserAccessibility::BrowserAccessibilityForPoint( | 424 BrowserAccessibility* BrowserAccessibility::BrowserAccessibilityForPoint( |
341 const gfx::Point& point) { | 425 const gfx::Point& point) { |
342 // The best result found that's a child of this object. | 426 // The best result found that's a child of this object. |
343 BrowserAccessibility* child_result = NULL; | 427 BrowserAccessibility* child_result = NULL; |
344 // The best result that's an indirect descendant like grandchild, etc. | 428 // The best result that's an indirect descendant like grandchild, etc. |
345 BrowserAccessibility* descendant_result = NULL; | 429 BrowserAccessibility* descendant_result = NULL; |
346 | 430 |
347 // Walk the children recursively looking for the BrowserAccessibility that | 431 // Walk the children recursively looking for the BrowserAccessibility that |
348 // most tightly encloses the specified point. Walk backwards so that in | 432 // most tightly encloses the specified point. Walk backwards so that in |
349 // the absence of any other information, we assume the object that occurs | 433 // 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) | 778 if (GetRole() == ui::AX_ROLE_STATIC_TEXT) |
695 return static_cast<int>(GetStringAttribute(ui::AX_ATTR_VALUE).size()); | 779 return static_cast<int>(GetStringAttribute(ui::AX_ATTR_VALUE).size()); |
696 | 780 |
697 int len = 0; | 781 int len = 0; |
698 for (size_t i = 0; i < InternalChildCount(); ++i) | 782 for (size_t i = 0; i < InternalChildCount(); ++i) |
699 len += InternalGetChild(i)->GetStaticTextLenRecursive(); | 783 len += InternalGetChild(i)->GetStaticTextLenRecursive(); |
700 return len; | 784 return len; |
701 } | 785 } |
702 | 786 |
703 } // namespace content | 787 } // namespace content |
OLD | NEW |