| 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/i18n/break_iterator.h" |
| 7 #include "base/logging.h" | 8 #include "base/logging.h" |
| 8 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 9 | 10 |
| 10 namespace ui { | 11 namespace ui { |
| 11 | 12 |
| 12 // line_breaks is a Misnomer. Blink provides the start offsets of each line | 13 // line_breaks is a Misnomer. Blink provides the start offsets of each line |
| 13 // not the line breaks. | 14 // not the line breaks. |
| 14 // TODO(nektar): Rename line_breaks a11y attribute and variable references. | 15 // TODO(nektar): Rename line_breaks a11y attribute and variable references. |
| 15 size_t FindAccessibleTextBoundary(const base::string16& text, | 16 size_t FindAccessibleTextBoundary(const base::string16& text, |
| 16 const std::vector<int>& line_breaks, | 17 const std::vector<int>& line_breaks, |
| 17 TextBoundaryType boundary, | 18 TextBoundaryType boundary, |
| 18 size_t start_offset, | 19 size_t start_offset, |
| 19 TextBoundaryDirection direction) { | 20 TextBoundaryDirection direction) { |
| 20 size_t text_size = text.size(); | 21 size_t text_size = text.size(); |
| 21 DCHECK_LE(start_offset, text_size); | 22 DCHECK_LE(start_offset, text_size); |
| 22 | 23 |
| 23 if (boundary == CHAR_BOUNDARY) { | 24 if (boundary == CHAR_BOUNDARY) { |
| 24 if (direction == FORWARDS_DIRECTION && start_offset < text_size) | 25 if (direction == FORWARDS_DIRECTION && start_offset < text_size) |
| 25 return start_offset + 1; | 26 return start_offset + 1; |
| 26 else | 27 else |
| 27 return start_offset; | 28 return start_offset; |
| 28 } else if (boundary == LINE_BOUNDARY) { | 29 } |
| 30 |
| 31 base::i18n::BreakIterator word_iter(text, |
| 32 base::i18n::BreakIterator::BREAK_WORD); |
| 33 if (boundary == WORD_BOUNDARY) { |
| 34 if (!word_iter.Init()) |
| 35 return start_offset; |
| 36 } |
| 37 |
| 38 if (boundary == LINE_BOUNDARY) { |
| 29 if (direction == FORWARDS_DIRECTION) { | 39 if (direction == FORWARDS_DIRECTION) { |
| 30 for (size_t j = 0; j < line_breaks.size(); ++j) { | 40 for (size_t j = 0; j < line_breaks.size(); ++j) { |
| 31 size_t line_break = line_breaks[j] >= 0 ? line_breaks[j] : 0; | 41 size_t line_break = line_breaks[j] >= 0 ? line_breaks[j] : 0; |
| 32 if (line_break > start_offset) | 42 if (line_break > start_offset) |
| 33 return line_break; | 43 return line_break; |
| 34 } | 44 } |
| 35 return text_size; | 45 return text_size; |
| 36 } else { | 46 } else { |
| 37 for (size_t j = line_breaks.size(); j != 0; --j) { | 47 for (size_t j = line_breaks.size(); j != 0; --j) { |
| 38 size_t line_break = line_breaks[j - 1] >= 0 ? line_breaks[j - 1] : 0; | 48 size_t line_break = line_breaks[j - 1] >= 0 ? line_breaks[j - 1] : 0; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 55 return 0; | 65 return 0; |
| 56 pos = result - 1; | 66 pos = result - 1; |
| 57 } | 67 } |
| 58 | 68 |
| 59 switch (boundary) { | 69 switch (boundary) { |
| 60 case CHAR_BOUNDARY: | 70 case CHAR_BOUNDARY: |
| 61 case LINE_BOUNDARY: | 71 case LINE_BOUNDARY: |
| 62 NOTREACHED(); // These are handled above. | 72 NOTREACHED(); // These are handled above. |
| 63 break; | 73 break; |
| 64 case WORD_BOUNDARY: | 74 case WORD_BOUNDARY: |
| 65 if (base::IsUnicodeWhitespace(text[pos])) | 75 if (word_iter.IsStartOfWord(result)) { |
| 66 return result; | 76 // If we are searching forward and we are still at the start offset, |
| 77 // we need to find the next word. |
| 78 if (direction == BACKWARDS_DIRECTION || result != start_offset) |
| 79 return result; |
| 80 } |
| 67 break; | 81 break; |
| 68 case PARAGRAPH_BOUNDARY: | 82 case PARAGRAPH_BOUNDARY: |
| 69 if (text[pos] == '\n') | 83 if (text[pos] == '\n') |
| 70 return result; | 84 return result; |
| 71 break; | 85 break; |
| 72 case SENTENCE_BOUNDARY: | 86 case SENTENCE_BOUNDARY: |
| 73 if ((text[pos] == '.' || text[pos] == '!' || text[pos] == '?') && | 87 if ((text[pos] == '.' || text[pos] == '!' || text[pos] == '?') && |
| 74 (pos == text_size - 1 || | 88 (pos == text_size - 1 || |
| 75 base::IsUnicodeWhitespace(text[pos + 1]))) { | 89 base::IsUnicodeWhitespace(text[pos + 1]))) { |
| 76 return result; | 90 return result; |
| 77 } | 91 } |
| 78 break; | 92 break; |
| 79 case ALL_BOUNDARY: | 93 case ALL_BOUNDARY: |
| 80 default: | 94 default: |
| 81 break; | 95 break; |
| 82 } | 96 } |
| 83 | 97 |
| 84 if (direction == FORWARDS_DIRECTION) { | 98 if (direction == FORWARDS_DIRECTION) { |
| 85 result++; | 99 result++; |
| 86 } else { | 100 } else { |
| 87 result--; | 101 result--; |
| 88 } | 102 } |
| 89 } | 103 } |
| 90 } | 104 } |
| 91 | 105 |
| 92 } // Namespace ui | 106 } // namespace ui |
| OLD | NEW |