2013-11-18 17:19:33 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_a11y_HyperTextAccessible_inl_h__
|
|
|
|
#define mozilla_a11y_HyperTextAccessible_inl_h__
|
|
|
|
|
|
|
|
#include "HyperTextAccessible.h"
|
|
|
|
|
|
|
|
#include "nsAccUtils.h"
|
|
|
|
|
|
|
|
#include "nsIClipboard.h"
|
|
|
|
#include "nsIEditor.h"
|
|
|
|
#include "nsIPersistentProperties2.h"
|
|
|
|
#include "nsIPlaintextEditor.h"
|
2014-08-06 09:19:27 +04:00
|
|
|
#include "nsFrameSelection.h"
|
2013-11-18 17:19:33 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace a11y {
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
HyperTextAccessible::IsValidOffset(int32_t aOffset)
|
|
|
|
{
|
2014-05-17 03:16:34 +04:00
|
|
|
index_t offset = ConvertMagicOffset(aOffset);
|
|
|
|
return offset.IsValid() && offset <= CharacterCount();
|
2013-11-18 17:19:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
HyperTextAccessible::IsValidRange(int32_t aStartOffset, int32_t aEndOffset)
|
|
|
|
{
|
2014-05-17 03:16:34 +04:00
|
|
|
index_t startOffset = ConvertMagicOffset(aStartOffset);
|
|
|
|
index_t endOffset = ConvertMagicOffset(aEndOffset);
|
|
|
|
return startOffset.IsValid() && endOffset.IsValid() &&
|
|
|
|
startOffset <= endOffset && endOffset <= CharacterCount();
|
2013-11-18 17:19:33 +04:00
|
|
|
}
|
|
|
|
|
2014-05-01 19:45:40 +04:00
|
|
|
inline void
|
|
|
|
HyperTextAccessible::SetCaretOffset(int32_t aOffset)
|
|
|
|
{
|
|
|
|
SetSelectionRange(aOffset, aOffset);
|
|
|
|
// XXX: Force cache refresh until a good solution for AT emulation of user
|
|
|
|
// input is implemented (AccessFu caret movement).
|
|
|
|
SelectionMgr()->UpdateCaretOffset(this, aOffset);
|
|
|
|
}
|
|
|
|
|
2013-11-18 17:19:33 +04:00
|
|
|
inline bool
|
|
|
|
HyperTextAccessible::AddToSelection(int32_t aStartOffset, int32_t aEndOffset)
|
|
|
|
{
|
2014-04-10 20:09:40 +04:00
|
|
|
dom::Selection* domSel = DOMSelection();
|
2013-11-18 17:19:33 +04:00
|
|
|
return domSel &&
|
2015-02-25 19:08:00 +03:00
|
|
|
SetSelectionBoundsAt(domSel->RangeCount(), aStartOffset, aEndOffset);
|
2013-11-18 17:19:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
HyperTextAccessible::ReplaceText(const nsAString& aText)
|
|
|
|
{
|
|
|
|
int32_t numChars = CharacterCount();
|
|
|
|
if (numChars != 0)
|
|
|
|
DeleteText(0, numChars);
|
|
|
|
|
|
|
|
InsertText(aText, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
HyperTextAccessible::InsertText(const nsAString& aText, int32_t aPosition)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIEditor> editor = GetEditor();
|
|
|
|
nsCOMPtr<nsIPlaintextEditor> peditor(do_QueryInterface(editor));
|
|
|
|
if (peditor) {
|
|
|
|
SetSelectionRange(aPosition, aPosition);
|
|
|
|
peditor->InsertText(aText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
HyperTextAccessible::CopyText(int32_t aStartPos, int32_t aEndPos)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIEditor> editor = GetEditor();
|
|
|
|
if (editor) {
|
|
|
|
SetSelectionRange(aStartPos, aEndPos);
|
|
|
|
editor->Copy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
HyperTextAccessible::CutText(int32_t aStartPos, int32_t aEndPos)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIEditor> editor = GetEditor();
|
|
|
|
if (editor) {
|
|
|
|
SetSelectionRange(aStartPos, aEndPos);
|
|
|
|
editor->Cut();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
HyperTextAccessible::DeleteText(int32_t aStartPos, int32_t aEndPos)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIEditor> editor = GetEditor();
|
|
|
|
if (editor) {
|
|
|
|
SetSelectionRange(aStartPos, aEndPos);
|
|
|
|
editor->DeleteSelection(nsIEditor::eNone, nsIEditor::eStrip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
HyperTextAccessible::PasteText(int32_t aPosition)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIEditor> editor = GetEditor();
|
|
|
|
if (editor) {
|
|
|
|
SetSelectionRange(aPosition, aPosition);
|
|
|
|
editor->Paste(nsIClipboard::kGlobalClipboard);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-17 03:16:34 +04:00
|
|
|
inline index_t
|
2014-04-26 06:42:19 +04:00
|
|
|
HyperTextAccessible::ConvertMagicOffset(int32_t aOffset) const
|
2013-11-18 17:19:33 +04:00
|
|
|
{
|
|
|
|
if (aOffset == nsIAccessibleText::TEXT_OFFSET_END_OF_TEXT)
|
|
|
|
return CharacterCount();
|
|
|
|
|
|
|
|
if (aOffset == nsIAccessibleText::TEXT_OFFSET_CARET)
|
|
|
|
return CaretOffset();
|
|
|
|
|
2014-05-17 03:16:34 +04:00
|
|
|
return aOffset;
|
2013-11-18 17:19:33 +04:00
|
|
|
}
|
|
|
|
|
2014-04-26 06:42:19 +04:00
|
|
|
inline uint32_t
|
|
|
|
HyperTextAccessible::AdjustCaretOffset(uint32_t aOffset) const
|
2013-11-18 17:19:33 +04:00
|
|
|
{
|
|
|
|
// It is the same character offset when the caret is visually at the very
|
|
|
|
// end of a line or the start of a new line (soft line break). Getting text
|
|
|
|
// at the line should provide the line with the visual caret, otherwise
|
|
|
|
// screen readers will announce the wrong line as the user presses up or
|
|
|
|
// down arrow and land at the end of a line.
|
2013-12-28 23:14:08 +04:00
|
|
|
if (aOffset > 0 && IsCaretAtEndOfLine())
|
|
|
|
return aOffset - 1;
|
|
|
|
|
2013-11-18 17:19:33 +04:00
|
|
|
return aOffset;
|
|
|
|
}
|
|
|
|
|
2013-12-28 23:14:08 +04:00
|
|
|
inline bool
|
|
|
|
HyperTextAccessible::IsCaretAtEndOfLine() const
|
|
|
|
{
|
|
|
|
nsRefPtr<nsFrameSelection> frameSelection = FrameSelection();
|
|
|
|
return frameSelection &&
|
2014-08-06 09:19:27 +04:00
|
|
|
frameSelection->GetHint() == CARET_ASSOCIATE_BEFORE;
|
2013-12-28 23:14:08 +04:00
|
|
|
}
|
|
|
|
|
2014-02-06 02:46:05 +04:00
|
|
|
inline already_AddRefed<nsFrameSelection>
|
|
|
|
HyperTextAccessible::FrameSelection() const
|
|
|
|
{
|
|
|
|
nsIFrame* frame = GetFrame();
|
|
|
|
return frame ? frame->GetFrameSelection() : nullptr;
|
|
|
|
}
|
|
|
|
|
2014-04-10 20:09:40 +04:00
|
|
|
inline dom::Selection*
|
2013-11-18 17:19:33 +04:00
|
|
|
HyperTextAccessible::DOMSelection() const
|
|
|
|
{
|
|
|
|
nsRefPtr<nsFrameSelection> frameSelection = FrameSelection();
|
|
|
|
return frameSelection ?
|
|
|
|
frameSelection->GetSelection(nsISelectionController::SELECTION_NORMAL) :
|
|
|
|
nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace a11y
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|