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"
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
,
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;
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
)
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
)
46 size_t result
= start_offset
;
49 if (direction
== FORWARDS_DIRECTION
) {
50 if (result
>= text_size
)
62 NOTREACHED(); // These are handled above.
65 if (base::IsUnicodeWhitespace(text
[pos
]))
68 case PARAGRAPH_BOUNDARY
:
69 if (text
[pos
] == '\n')
72 case SENTENCE_BOUNDARY
:
73 if ((text
[pos
] == '.' || text
[pos
] == '!' || text
[pos
] == '?') &&
74 (pos
== text_size
- 1 ||
75 base::IsUnicodeWhitespace(text
[pos
+ 1]))) {
84 if (direction
== FORWARDS_DIRECTION
) {