1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/accessible/src/generic/HyperTextAccessible-inl.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,165 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef mozilla_a11y_HyperTextAccessible_inl_h__ 1.10 +#define mozilla_a11y_HyperTextAccessible_inl_h__ 1.11 + 1.12 +#include "HyperTextAccessible.h" 1.13 + 1.14 +#include "nsAccUtils.h" 1.15 + 1.16 +#include "nsIClipboard.h" 1.17 +#include "nsIEditor.h" 1.18 +#include "nsIPersistentProperties2.h" 1.19 +#include "nsIPlaintextEditor.h" 1.20 + 1.21 +namespace mozilla { 1.22 +namespace a11y { 1.23 + 1.24 +inline bool 1.25 +HyperTextAccessible::IsValidOffset(int32_t aOffset) 1.26 +{ 1.27 + int32_t offset = ConvertMagicOffset(aOffset); 1.28 + return offset >= 0 && offset <= static_cast<int32_t>(CharacterCount()); 1.29 +} 1.30 + 1.31 +inline bool 1.32 +HyperTextAccessible::IsValidRange(int32_t aStartOffset, int32_t aEndOffset) 1.33 +{ 1.34 + int32_t startOffset = ConvertMagicOffset(aStartOffset); 1.35 + if (startOffset < 0) 1.36 + return false; 1.37 + 1.38 + int32_t endOffset = ConvertMagicOffset(aEndOffset); 1.39 + if (endOffset < 0 || startOffset > endOffset) 1.40 + return false; 1.41 + 1.42 + return endOffset <= static_cast<int32_t>(CharacterCount()); 1.43 +} 1.44 + 1.45 +inline bool 1.46 +HyperTextAccessible::AddToSelection(int32_t aStartOffset, int32_t aEndOffset) 1.47 +{ 1.48 + dom::Selection* domSel = DOMSelection(); 1.49 + return domSel && 1.50 + SetSelectionBoundsAt(domSel->GetRangeCount(), aStartOffset, aEndOffset); 1.51 +} 1.52 + 1.53 +inline void 1.54 +HyperTextAccessible::ReplaceText(const nsAString& aText) 1.55 +{ 1.56 + int32_t numChars = CharacterCount(); 1.57 + if (numChars != 0) 1.58 + DeleteText(0, numChars); 1.59 + 1.60 + InsertText(aText, 0); 1.61 +} 1.62 + 1.63 +inline void 1.64 +HyperTextAccessible::InsertText(const nsAString& aText, int32_t aPosition) 1.65 +{ 1.66 + nsCOMPtr<nsIEditor> editor = GetEditor(); 1.67 + nsCOMPtr<nsIPlaintextEditor> peditor(do_QueryInterface(editor)); 1.68 + if (peditor) { 1.69 + SetSelectionRange(aPosition, aPosition); 1.70 + peditor->InsertText(aText); 1.71 + } 1.72 +} 1.73 + 1.74 +inline void 1.75 +HyperTextAccessible::CopyText(int32_t aStartPos, int32_t aEndPos) 1.76 + { 1.77 + nsCOMPtr<nsIEditor> editor = GetEditor(); 1.78 + if (editor) { 1.79 + SetSelectionRange(aStartPos, aEndPos); 1.80 + editor->Copy(); 1.81 + } 1.82 + } 1.83 + 1.84 +inline void 1.85 +HyperTextAccessible::CutText(int32_t aStartPos, int32_t aEndPos) 1.86 + { 1.87 + nsCOMPtr<nsIEditor> editor = GetEditor(); 1.88 + if (editor) { 1.89 + SetSelectionRange(aStartPos, aEndPos); 1.90 + editor->Cut(); 1.91 + } 1.92 + } 1.93 + 1.94 +inline void 1.95 +HyperTextAccessible::DeleteText(int32_t aStartPos, int32_t aEndPos) 1.96 +{ 1.97 + nsCOMPtr<nsIEditor> editor = GetEditor(); 1.98 + if (editor) { 1.99 + SetSelectionRange(aStartPos, aEndPos); 1.100 + editor->DeleteSelection(nsIEditor::eNone, nsIEditor::eStrip); 1.101 + } 1.102 +} 1.103 + 1.104 +inline void 1.105 +HyperTextAccessible::PasteText(int32_t aPosition) 1.106 +{ 1.107 + nsCOMPtr<nsIEditor> editor = GetEditor(); 1.108 + if (editor) { 1.109 + SetSelectionRange(aPosition, aPosition); 1.110 + editor->Paste(nsIClipboard::kGlobalClipboard); 1.111 + } 1.112 +} 1.113 + 1.114 +inline int32_t 1.115 +HyperTextAccessible::ConvertMagicOffset(int32_t aOffset) 1.116 +{ 1.117 + if (aOffset == nsIAccessibleText::TEXT_OFFSET_END_OF_TEXT) 1.118 + return CharacterCount(); 1.119 + 1.120 + if (aOffset == nsIAccessibleText::TEXT_OFFSET_CARET) 1.121 + return CaretOffset(); 1.122 + 1.123 + return aOffset; 1.124 +} 1.125 + 1.126 +inline int32_t 1.127 +HyperTextAccessible::AdjustCaretOffset(int32_t aOffset) const 1.128 +{ 1.129 + // It is the same character offset when the caret is visually at the very 1.130 + // end of a line or the start of a new line (soft line break). Getting text 1.131 + // at the line should provide the line with the visual caret, otherwise 1.132 + // screen readers will announce the wrong line as the user presses up or 1.133 + // down arrow and land at the end of a line. 1.134 + if (aOffset > 0 && IsCaretAtEndOfLine()) 1.135 + return aOffset - 1; 1.136 + 1.137 + return aOffset; 1.138 +} 1.139 + 1.140 +inline bool 1.141 +HyperTextAccessible::IsCaretAtEndOfLine() const 1.142 +{ 1.143 + nsRefPtr<nsFrameSelection> frameSelection = FrameSelection(); 1.144 + return frameSelection && 1.145 + frameSelection->GetHint() == nsFrameSelection::HINTLEFT; 1.146 +} 1.147 + 1.148 +inline already_AddRefed<nsFrameSelection> 1.149 +HyperTextAccessible::FrameSelection() const 1.150 +{ 1.151 + nsIFrame* frame = GetFrame(); 1.152 + return frame ? frame->GetFrameSelection() : nullptr; 1.153 +} 1.154 + 1.155 +inline dom::Selection* 1.156 +HyperTextAccessible::DOMSelection() const 1.157 +{ 1.158 + nsRefPtr<nsFrameSelection> frameSelection = FrameSelection(); 1.159 + return frameSelection ? 1.160 + frameSelection->GetSelection(nsISelectionController::SELECTION_NORMAL) : 1.161 + nullptr; 1.162 +} 1.163 + 1.164 +} // namespace a11y 1.165 +} // namespace mozilla 1.166 + 1.167 +#endif 1.168 +