accessible/src/generic/HyperTextAccessible-inl.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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/. */
     6 #ifndef mozilla_a11y_HyperTextAccessible_inl_h__
     7 #define mozilla_a11y_HyperTextAccessible_inl_h__
     9 #include "HyperTextAccessible.h"
    11 #include "nsAccUtils.h"
    13 #include "nsIClipboard.h"
    14 #include "nsIEditor.h"
    15 #include "nsIPersistentProperties2.h"
    16 #include "nsIPlaintextEditor.h"
    18 namespace mozilla {
    19 namespace a11y {
    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 }
    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;
    35   int32_t endOffset = ConvertMagicOffset(aEndOffset);
    36   if (endOffset < 0 || startOffset > endOffset)
    37     return false;
    39   return endOffset <= static_cast<int32_t>(CharacterCount());
    40 }
    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 }
    50 inline void
    51 HyperTextAccessible::ReplaceText(const nsAString& aText)
    52 {
    53   int32_t numChars = CharacterCount();
    54   if (numChars != 0)
    55     DeleteText(0, numChars);
    57   InsertText(aText, 0);
    58 }
    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 }
    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   }
    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   }
    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 }
   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 }
   111 inline int32_t
   112 HyperTextAccessible::ConvertMagicOffset(int32_t aOffset)
   113 {
   114   if (aOffset == nsIAccessibleText::TEXT_OFFSET_END_OF_TEXT)
   115     return CharacterCount();
   117   if (aOffset == nsIAccessibleText::TEXT_OFFSET_CARET)
   118     return CaretOffset();
   120   return aOffset;
   121 }
   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;
   134   return aOffset;
   135 }
   137 inline bool
   138 HyperTextAccessible::IsCaretAtEndOfLine() const
   139 {
   140   nsRefPtr<nsFrameSelection> frameSelection = FrameSelection();
   141   return frameSelection &&
   142     frameSelection->GetHint() == nsFrameSelection::HINTLEFT;
   143 }
   145 inline already_AddRefed<nsFrameSelection>
   146 HyperTextAccessible::FrameSelection() const
   147 {
   148   nsIFrame* frame = GetFrame();
   149   return frame ? frame->GetFrameSelection() : nullptr;
   150 }
   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 }
   161 } // namespace a11y
   162 } // namespace mozilla
   164 #endif

mercurial