Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/accessible/generic/HyperTextAccessible-inl.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#ifndef mozilla_a11y_HyperTextAccessible_inl_h__
7
#define mozilla_a11y_HyperTextAccessible_inl_h__
8
9
#include "HyperTextAccessible.h"
10
11
#include "nsAccUtils.h"
12
13
#include "nsIClipboard.h"
14
#include "nsIEditor.h"
15
#include "nsIPersistentProperties2.h"
16
#include "nsFrameSelection.h"
17
18
#include "mozilla/TextEditor.h"
19
20
namespace mozilla {
21
namespace a11y {
22
23
inline bool
24
HyperTextAccessible::IsValidOffset(int32_t aOffset)
25
0
{
26
0
  index_t offset = ConvertMagicOffset(aOffset);
27
0
  return offset.IsValid() && offset <= CharacterCount();
28
0
}
29
30
inline bool
31
HyperTextAccessible::IsValidRange(int32_t aStartOffset, int32_t aEndOffset)
32
0
{
33
0
  index_t startOffset = ConvertMagicOffset(aStartOffset);
34
0
  index_t endOffset = ConvertMagicOffset(aEndOffset);
35
0
  return startOffset.IsValid() && endOffset.IsValid() &&
36
0
    startOffset <= endOffset && endOffset <= CharacterCount();
37
0
}
38
39
inline void
40
HyperTextAccessible::SetCaretOffset(int32_t aOffset)
41
0
{
42
0
  SetSelectionRange(aOffset, aOffset);
43
0
  // XXX: Force cache refresh until a good solution for AT emulation of user
44
0
  // input is implemented (AccessFu caret movement).
45
0
  SelectionMgr()->UpdateCaretOffset(this, aOffset);
46
0
}
47
48
inline bool
49
HyperTextAccessible::AddToSelection(int32_t aStartOffset, int32_t aEndOffset)
50
0
{
51
0
  dom::Selection* domSel = DOMSelection();
52
0
  return domSel &&
53
0
    SetSelectionBoundsAt(domSel->RangeCount(), aStartOffset, aEndOffset);
54
0
}
55
56
inline void
57
HyperTextAccessible::ReplaceText(const nsAString& aText)
58
0
{
59
0
  if (aText.Length() == 0) {
60
0
    DeleteText(0, CharacterCount());
61
0
    return;
62
0
  }
63
0
64
0
  SetSelectionRange(0, CharacterCount());
65
0
66
0
  RefPtr<TextEditor> textEditor = GetEditor();
67
0
  if (!textEditor) {
68
0
    return;
69
0
  }
70
0
71
0
  DebugOnly<nsresult> rv = textEditor->InsertTextAsAction(aText);
72
0
  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to insert the new text");
73
0
}
74
75
inline void
76
HyperTextAccessible::InsertText(const nsAString& aText, int32_t aPosition)
77
0
{
78
0
  RefPtr<TextEditor> textEditor = GetEditor();
79
0
  if (textEditor) {
80
0
    SetSelectionRange(aPosition, aPosition);
81
0
    DebugOnly<nsresult> rv = textEditor->InsertTextAsAction(aText);
82
0
    NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to insert the text");
83
0
  }
84
0
}
85
86
inline void
87
HyperTextAccessible::CopyText(int32_t aStartPos, int32_t aEndPos)
88
0
  {
89
0
    RefPtr<TextEditor> textEditor = GetEditor();
90
0
    if (textEditor) {
91
0
      SetSelectionRange(aStartPos, aEndPos);
92
0
      textEditor->Copy();
93
0
    }
94
0
  }
95
96
inline void
97
HyperTextAccessible::CutText(int32_t aStartPos, int32_t aEndPos)
98
0
  {
99
0
    RefPtr<TextEditor> textEditor = GetEditor();
100
0
    if (textEditor) {
101
0
      SetSelectionRange(aStartPos, aEndPos);
102
0
      textEditor->Cut();
103
0
    }
104
0
  }
105
106
inline void
107
HyperTextAccessible::DeleteText(int32_t aStartPos, int32_t aEndPos)
108
0
{
109
0
  RefPtr<TextEditor> textEditor = GetEditor();
110
0
  if (!textEditor) {
111
0
    return;
112
0
  }
113
0
  SetSelectionRange(aStartPos, aEndPos);
114
0
  DebugOnly<nsresult> rv =
115
0
    textEditor->DeleteSelectionAsAction(nsIEditor::eNone, nsIEditor::eStrip);
116
0
  NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to delete text");
117
0
}
118
119
inline void
120
HyperTextAccessible::PasteText(int32_t aPosition)
121
0
{
122
0
  RefPtr<TextEditor> textEditor = GetEditor();
123
0
  if (textEditor) {
124
0
    SetSelectionRange(aPosition, aPosition);
125
0
    textEditor->PasteAsAction(nsIClipboard::kGlobalClipboard);
126
0
  }
127
0
}
128
129
inline index_t
130
HyperTextAccessible::ConvertMagicOffset(int32_t aOffset) const
131
0
{
132
0
  if (aOffset == nsIAccessibleText::TEXT_OFFSET_END_OF_TEXT)
133
0
    return CharacterCount();
134
0
135
0
  if (aOffset == nsIAccessibleText::TEXT_OFFSET_CARET)
136
0
    return CaretOffset();
137
0
138
0
  return aOffset;
139
0
}
140
141
inline uint32_t
142
HyperTextAccessible::AdjustCaretOffset(uint32_t aOffset) const
143
0
{
144
0
  // It is the same character offset when the caret is visually at the very
145
0
  // end of a line or the start of a new line (soft line break). Getting text
146
0
  // at the line should provide the line with the visual caret, otherwise
147
0
  // screen readers will announce the wrong line as the user presses up or
148
0
  // down arrow and land at the end of a line.
149
0
  if (aOffset > 0 && IsCaretAtEndOfLine())
150
0
    return aOffset - 1;
151
0
152
0
  return aOffset;
153
0
}
154
155
inline bool
156
HyperTextAccessible::IsCaretAtEndOfLine() const
157
0
{
158
0
  RefPtr<nsFrameSelection> frameSelection = FrameSelection();
159
0
  return frameSelection &&
160
0
    frameSelection->GetHint() == CARET_ASSOCIATE_BEFORE;
161
0
}
162
163
inline already_AddRefed<nsFrameSelection>
164
HyperTextAccessible::FrameSelection() const
165
0
{
166
0
  nsIFrame* frame = GetFrame();
167
0
  return frame ? frame->GetFrameSelection() : nullptr;
168
0
}
169
170
inline dom::Selection*
171
HyperTextAccessible::DOMSelection() const
172
0
{
173
0
  RefPtr<nsFrameSelection> frameSelection = FrameSelection();
174
0
  return frameSelection ? frameSelection->GetSelection(SelectionType::eNormal) :
175
0
                          nullptr;
176
0
}
177
178
} // namespace a11y
179
} // namespace mozilla
180
181
#endif
182