Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / ui / accessibility / ax_text_utils.cc
blob1e68559027ec8faf455b80440fda9e60b46b0bd5
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
3 // found in the LICENSE file.
5 #include "ui/accessibility/ax_text_utils.h"
7 #include "base/logging.h"
8 #include "base/strings/string_util.h"
10 namespace ui {
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.
15 size_t FindAccessibleTextBoundary(const base::string16& text,
16 const std::vector<int>& line_breaks,
17 TextBoundaryType boundary,
18 size_t start_offset,
19 TextBoundaryDirection direction) {
20 size_t text_size = text.size();
21 DCHECK_LE(start_offset, text_size);
23 if (boundary == CHAR_BOUNDARY) {
24 if (direction == FORWARDS_DIRECTION && start_offset < text_size)
25 return start_offset + 1;
26 else
27 return start_offset;
28 } else if (boundary == LINE_BOUNDARY) {
29 if (direction == FORWARDS_DIRECTION) {
30 for (size_t j = 0; j < line_breaks.size(); ++j) {
31 size_t line_break = line_breaks[j] >= 0 ? line_breaks[j] : 0;
32 if (line_break > start_offset)
33 return line_break;
35 return text_size;
36 } else {
37 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;
39 if (line_break <= start_offset)
40 return line_break;
42 return 0;
46 size_t result = start_offset;
47 for (;;) {
48 size_t pos;
49 if (direction == FORWARDS_DIRECTION) {
50 if (result >= text_size)
51 return text_size;
52 pos = result;
53 } else {
54 if (result == 0)
55 return 0;
56 pos = result - 1;
59 switch (boundary) {
60 case CHAR_BOUNDARY:
61 case LINE_BOUNDARY:
62 NOTREACHED(); // These are handled above.
63 break;
64 case WORD_BOUNDARY:
65 if (base::IsUnicodeWhitespace(text[pos]))
66 return result;
67 break;
68 case PARAGRAPH_BOUNDARY:
69 if (text[pos] == '\n')
70 return result;
71 break;
72 case SENTENCE_BOUNDARY:
73 if ((text[pos] == '.' || text[pos] == '!' || text[pos] == '?') &&
74 (pos == text_size - 1 ||
75 base::IsUnicodeWhitespace(text[pos + 1]))) {
76 return result;
78 break;
79 case ALL_BOUNDARY:
80 default:
81 break;
84 if (direction == FORWARDS_DIRECTION) {
85 result++;
86 } else {
87 result--;
92 } // Namespace ui