OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ui/accessibility/ax_text_utils.h" | 5 #include "ui/accessibility/ax_text_utils.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 | 9 |
10 namespace ui { | 10 namespace ui { |
11 | 11 |
| 12 // line_breaks is a Misnomer. Blink provides the start offsets of each line |
| 13 // not the line breaks. |
| 14 // TODO(nektar): Rename line_breaks a11y attribute and variable references. |
12 size_t FindAccessibleTextBoundary(const base::string16& text, | 15 size_t FindAccessibleTextBoundary(const base::string16& text, |
13 const std::vector<int>& line_breaks, | 16 const std::vector<int>& line_breaks, |
14 TextBoundaryType boundary, | 17 TextBoundaryType boundary, |
15 size_t start_offset, | 18 size_t start_offset, |
16 TextBoundaryDirection direction) { | 19 TextBoundaryDirection direction) { |
17 size_t text_size = text.size(); | 20 size_t text_size = text.size(); |
18 DCHECK(start_offset <= text_size); | 21 DCHECK_LE(start_offset, text_size); |
19 | 22 |
20 if (boundary == CHAR_BOUNDARY) { | 23 if (boundary == CHAR_BOUNDARY) { |
21 if (direction == FORWARDS_DIRECTION && start_offset < text_size) | 24 if (direction == FORWARDS_DIRECTION && start_offset < text_size) |
22 return start_offset + 1; | 25 return start_offset + 1; |
23 else | 26 else |
24 return start_offset; | 27 return start_offset; |
25 } else if (boundary == LINE_BOUNDARY) { | 28 } else if (boundary == LINE_BOUNDARY) { |
26 if (direction == FORWARDS_DIRECTION) { | 29 if (direction == FORWARDS_DIRECTION) { |
27 for (size_t j = 0; j < line_breaks.size(); ++j) { | 30 for (size_t j = 0; j < line_breaks.size(); ++j) { |
28 size_t line_break = line_breaks[j] >= 0 ? line_breaks[j] : 0; | 31 size_t line_break = line_breaks[j] >= 0 ? line_breaks[j] : 0; |
29 if (line_break > start_offset) | 32 if (line_break > start_offset) |
30 return line_break; | 33 return line_break; |
31 } | 34 } |
32 return text_size; | 35 return text_size; |
33 } else { | 36 } else { |
34 for (size_t j = line_breaks.size(); j != 0; --j) { | 37 for (size_t j = line_breaks.size(); j != 0; --j) { |
35 size_t line_break = line_breaks[j - 1] >= 0 ? line_breaks[j - 1] : 0; | 38 size_t line_break = line_breaks[j - 1] >= 0 ? line_breaks[j - 1] : 0; |
36 if (line_break < start_offset) | 39 if (line_break <= start_offset) |
37 return line_break; | 40 return line_break; |
38 } | 41 } |
39 return 0; | 42 return 0; |
40 } | 43 } |
41 } | 44 } |
42 | 45 |
43 size_t result = start_offset; | 46 size_t result = start_offset; |
44 for (;;) { | 47 for (;;) { |
45 size_t pos; | 48 size_t pos; |
46 if (direction == FORWARDS_DIRECTION) { | 49 if (direction == FORWARDS_DIRECTION) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 | 82 |
80 if (direction == FORWARDS_DIRECTION) { | 83 if (direction == FORWARDS_DIRECTION) { |
81 result++; | 84 result++; |
82 } else { | 85 } else { |
83 result--; | 86 result--; |
84 } | 87 } |
85 } | 88 } |
86 } | 89 } |
87 | 90 |
88 } // Namespace ui | 91 } // Namespace ui |
OLD | NEW |