summaryrefslogtreecommitdiff
path: root/chromium/ui/accessibility/ax_text_utils.cc
blob: fd0457763f455fac21a9260da8ba33d5a071b0e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/accessibility/ax_text_utils.h"

#include "base/i18n/break_iterator.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/strings/grit/ui_strings.h"

namespace ui {

// line_breaks is a Misnomer. Blink provides the start offsets of each line
// not the line breaks.
// TODO(nektar): Rename line_breaks a11y attribute and variable references.
size_t FindAccessibleTextBoundary(const base::string16& text,
                                  const std::vector<int>& line_breaks,
                                  TextBoundaryType boundary,
                                  size_t start_offset,
                                  TextBoundaryDirection direction,
                                  AXTextAffinity affinity) {
  size_t text_size = text.size();
  DCHECK_LE(start_offset, text_size);

  if (boundary == CHAR_BOUNDARY) {
    if (direction == FORWARDS_DIRECTION && start_offset < text_size)
      return start_offset + 1;
    else
      return start_offset;
  }

  base::i18n::BreakIterator word_iter(text,
                                      base::i18n::BreakIterator::BREAK_WORD);
  if (boundary == WORD_BOUNDARY) {
    if (!word_iter.Init())
      return start_offset;
  }

  if (boundary == LINE_BOUNDARY) {
    if (direction == FORWARDS_DIRECTION) {
      for (size_t j = 0; j < line_breaks.size(); ++j) {
          size_t line_break = line_breaks[j] >= 0 ? line_breaks[j] : 0;
        if ((affinity == AX_TEXT_AFFINITY_DOWNSTREAM &&
             line_break > start_offset) ||
            (affinity == AX_TEXT_AFFINITY_UPSTREAM &&
             line_break >= start_offset)) {
          return line_break;
        }
      }
      return text_size;
    } else {
      for (size_t j = line_breaks.size(); j != 0; --j) {
        size_t line_break = line_breaks[j - 1] >= 0 ? line_breaks[j - 1] : 0;
        if ((affinity == AX_TEXT_AFFINITY_DOWNSTREAM &&
             line_break <= start_offset) ||
            (affinity == AX_TEXT_AFFINITY_UPSTREAM &&
             line_break < start_offset)) {
          return line_break;
        }
      }
      return 0;
    }
  }

  size_t result = start_offset;
  for (;;) {
    size_t pos;
    if (direction == FORWARDS_DIRECTION) {
      if (result >= text_size)
        return text_size;
      pos = result;
    } else {
      if (result == 0)
        return 0;
      pos = result - 1;
    }

    switch (boundary) {
      case CHAR_BOUNDARY:
      case LINE_BOUNDARY:
        NOTREACHED();  // These are handled above.
        break;
      case WORD_BOUNDARY:
        if (word_iter.IsStartOfWord(result)) {
          // If we are searching forward and we are still at the start offset,
          // we need to find the next word.
          if (direction == BACKWARDS_DIRECTION || result != start_offset)
            return result;
        }
        break;
      case PARAGRAPH_BOUNDARY:
        if (text[pos] == '\n')
          return result;
        break;
      case SENTENCE_BOUNDARY:
        if ((text[pos] == '.' || text[pos] == '!' || text[pos] == '?') &&
            (pos == text_size - 1 ||
             base::IsUnicodeWhitespace(text[pos + 1]))) {
          return result;
        }
        break;
      case ALL_BOUNDARY:
      default:
        break;
    }

    if (direction == FORWARDS_DIRECTION) {
      result++;
    } else {
      result--;
    }
  }
}

base::string16 ActionVerbToLocalizedString(
    const AXDefaultActionVerb action_verb) {
  switch (action_verb) {
    case AX_DEFAULT_ACTION_VERB_NONE:
      return base::string16();
    case AX_DEFAULT_ACTION_VERB_ACTIVATE:
      return l10n_util::GetStringUTF16(IDS_AX_ACTIVATE_ACTION_VERB);
    case AX_DEFAULT_ACTION_VERB_CHECK:
      return l10n_util::GetStringUTF16(IDS_AX_CHECK_ACTION_VERB);
    case AX_DEFAULT_ACTION_VERB_CLICK:
      return l10n_util::GetStringUTF16(IDS_AX_CLICK_ACTION_VERB);
    case AX_DEFAULT_ACTION_VERB_CLICK_ANCESTOR:
      return l10n_util::GetStringUTF16(IDS_AX_CLICK_ANCESTOR_ACTION_VERB);
    case AX_DEFAULT_ACTION_VERB_JUMP:
      return l10n_util::GetStringUTF16(IDS_AX_JUMP_ACTION_VERB);
    case AX_DEFAULT_ACTION_VERB_OPEN:
      return l10n_util::GetStringUTF16(IDS_AX_OPEN_ACTION_VERB);
    case AX_DEFAULT_ACTION_VERB_PRESS:
      return l10n_util::GetStringUTF16(IDS_AX_PRESS_ACTION_VERB);
    case AX_DEFAULT_ACTION_VERB_SELECT:
      return l10n_util::GetStringUTF16(IDS_AX_SELECT_ACTION_VERB);
    case AX_DEFAULT_ACTION_VERB_UNCHECK:
      return l10n_util::GetStringUTF16(IDS_AX_UNCHECK_ACTION_VERB);
  }
  NOTREACHED();
  return base::string16();
}

// Some APIs on Linux and Windows need to return non-localized action names.
base::string16 ActionVerbToUnlocalizedString(
    const AXDefaultActionVerb action_verb) {
  switch (action_verb) {
    case ui::AX_DEFAULT_ACTION_VERB_NONE:
      return base::UTF8ToUTF16("none");
    case ui::AX_DEFAULT_ACTION_VERB_ACTIVATE:
      return base::UTF8ToUTF16("activate");
    case ui::AX_DEFAULT_ACTION_VERB_CHECK:
      return base::UTF8ToUTF16("check");
    case ui::AX_DEFAULT_ACTION_VERB_CLICK:
      return base::UTF8ToUTF16("click");
    case ui::AX_DEFAULT_ACTION_VERB_CLICK_ANCESTOR:
      return base::UTF8ToUTF16("click-ancestor");
    case ui::AX_DEFAULT_ACTION_VERB_JUMP:
      return base::UTF8ToUTF16("jump");
    case ui::AX_DEFAULT_ACTION_VERB_OPEN:
      return base::UTF8ToUTF16("open");
    case ui::AX_DEFAULT_ACTION_VERB_PRESS:
      return base::UTF8ToUTF16("press");
    case ui::AX_DEFAULT_ACTION_VERB_SELECT:
      return base::UTF8ToUTF16("select");
    case ui::AX_DEFAULT_ACTION_VERB_UNCHECK:
      return base::UTF8ToUTF16("uncheck");
  }
  NOTREACHED();
  return base::string16();
}

}  // namespace ui