|
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 "nsIPlaintextEditor.h" |
|
17 |
|
18 namespace mozilla { |
|
19 namespace a11y { |
|
20 |
|
21 inline bool |
|
22 HyperTextAccessible::IsValidOffset(int32_t aOffset) |
|
23 { |
|
24 int32_t offset = ConvertMagicOffset(aOffset); |
|
25 return offset >= 0 && offset <= static_cast<int32_t>(CharacterCount()); |
|
26 } |
|
27 |
|
28 inline bool |
|
29 HyperTextAccessible::IsValidRange(int32_t aStartOffset, int32_t aEndOffset) |
|
30 { |
|
31 int32_t startOffset = ConvertMagicOffset(aStartOffset); |
|
32 if (startOffset < 0) |
|
33 return false; |
|
34 |
|
35 int32_t endOffset = ConvertMagicOffset(aEndOffset); |
|
36 if (endOffset < 0 || startOffset > endOffset) |
|
37 return false; |
|
38 |
|
39 return endOffset <= static_cast<int32_t>(CharacterCount()); |
|
40 } |
|
41 |
|
42 inline bool |
|
43 HyperTextAccessible::AddToSelection(int32_t aStartOffset, int32_t aEndOffset) |
|
44 { |
|
45 dom::Selection* domSel = DOMSelection(); |
|
46 return domSel && |
|
47 SetSelectionBoundsAt(domSel->GetRangeCount(), aStartOffset, aEndOffset); |
|
48 } |
|
49 |
|
50 inline void |
|
51 HyperTextAccessible::ReplaceText(const nsAString& aText) |
|
52 { |
|
53 int32_t numChars = CharacterCount(); |
|
54 if (numChars != 0) |
|
55 DeleteText(0, numChars); |
|
56 |
|
57 InsertText(aText, 0); |
|
58 } |
|
59 |
|
60 inline void |
|
61 HyperTextAccessible::InsertText(const nsAString& aText, int32_t aPosition) |
|
62 { |
|
63 nsCOMPtr<nsIEditor> editor = GetEditor(); |
|
64 nsCOMPtr<nsIPlaintextEditor> peditor(do_QueryInterface(editor)); |
|
65 if (peditor) { |
|
66 SetSelectionRange(aPosition, aPosition); |
|
67 peditor->InsertText(aText); |
|
68 } |
|
69 } |
|
70 |
|
71 inline void |
|
72 HyperTextAccessible::CopyText(int32_t aStartPos, int32_t aEndPos) |
|
73 { |
|
74 nsCOMPtr<nsIEditor> editor = GetEditor(); |
|
75 if (editor) { |
|
76 SetSelectionRange(aStartPos, aEndPos); |
|
77 editor->Copy(); |
|
78 } |
|
79 } |
|
80 |
|
81 inline void |
|
82 HyperTextAccessible::CutText(int32_t aStartPos, int32_t aEndPos) |
|
83 { |
|
84 nsCOMPtr<nsIEditor> editor = GetEditor(); |
|
85 if (editor) { |
|
86 SetSelectionRange(aStartPos, aEndPos); |
|
87 editor->Cut(); |
|
88 } |
|
89 } |
|
90 |
|
91 inline void |
|
92 HyperTextAccessible::DeleteText(int32_t aStartPos, int32_t aEndPos) |
|
93 { |
|
94 nsCOMPtr<nsIEditor> editor = GetEditor(); |
|
95 if (editor) { |
|
96 SetSelectionRange(aStartPos, aEndPos); |
|
97 editor->DeleteSelection(nsIEditor::eNone, nsIEditor::eStrip); |
|
98 } |
|
99 } |
|
100 |
|
101 inline void |
|
102 HyperTextAccessible::PasteText(int32_t aPosition) |
|
103 { |
|
104 nsCOMPtr<nsIEditor> editor = GetEditor(); |
|
105 if (editor) { |
|
106 SetSelectionRange(aPosition, aPosition); |
|
107 editor->Paste(nsIClipboard::kGlobalClipboard); |
|
108 } |
|
109 } |
|
110 |
|
111 inline int32_t |
|
112 HyperTextAccessible::ConvertMagicOffset(int32_t aOffset) |
|
113 { |
|
114 if (aOffset == nsIAccessibleText::TEXT_OFFSET_END_OF_TEXT) |
|
115 return CharacterCount(); |
|
116 |
|
117 if (aOffset == nsIAccessibleText::TEXT_OFFSET_CARET) |
|
118 return CaretOffset(); |
|
119 |
|
120 return aOffset; |
|
121 } |
|
122 |
|
123 inline int32_t |
|
124 HyperTextAccessible::AdjustCaretOffset(int32_t aOffset) const |
|
125 { |
|
126 // It is the same character offset when the caret is visually at the very |
|
127 // end of a line or the start of a new line (soft line break). Getting text |
|
128 // at the line should provide the line with the visual caret, otherwise |
|
129 // screen readers will announce the wrong line as the user presses up or |
|
130 // down arrow and land at the end of a line. |
|
131 if (aOffset > 0 && IsCaretAtEndOfLine()) |
|
132 return aOffset - 1; |
|
133 |
|
134 return aOffset; |
|
135 } |
|
136 |
|
137 inline bool |
|
138 HyperTextAccessible::IsCaretAtEndOfLine() const |
|
139 { |
|
140 nsRefPtr<nsFrameSelection> frameSelection = FrameSelection(); |
|
141 return frameSelection && |
|
142 frameSelection->GetHint() == nsFrameSelection::HINTLEFT; |
|
143 } |
|
144 |
|
145 inline already_AddRefed<nsFrameSelection> |
|
146 HyperTextAccessible::FrameSelection() const |
|
147 { |
|
148 nsIFrame* frame = GetFrame(); |
|
149 return frame ? frame->GetFrameSelection() : nullptr; |
|
150 } |
|
151 |
|
152 inline dom::Selection* |
|
153 HyperTextAccessible::DOMSelection() const |
|
154 { |
|
155 nsRefPtr<nsFrameSelection> frameSelection = FrameSelection(); |
|
156 return frameSelection ? |
|
157 frameSelection->GetSelection(nsISelectionController::SELECTION_NORMAL) : |
|
158 nullptr; |
|
159 } |
|
160 |
|
161 } // namespace a11y |
|
162 } // namespace mozilla |
|
163 |
|
164 #endif |
|
165 |