2013-03-07 04:16:10 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 4; 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/. */
|
|
|
|
|
|
|
|
#include "mozilla/a11y/SelectionManager.h"
|
|
|
|
|
|
|
|
#include "DocAccessible-inl.h"
|
2014-05-01 08:45:40 -07:00
|
|
|
#include "HyperTextAccessible.h"
|
|
|
|
#include "HyperTextAccessible-inl.h"
|
2013-03-07 04:16:10 -08:00
|
|
|
#include "nsAccessibilityService.h"
|
|
|
|
#include "nsAccUtils.h"
|
|
|
|
#include "nsCoreUtils.h"
|
2013-10-26 07:58:53 -07:00
|
|
|
#include "nsEventShell.h"
|
2014-08-05 22:19:27 -07:00
|
|
|
#include "nsFrameSelection.h"
|
2013-03-07 04:16:10 -08:00
|
|
|
|
2013-09-10 15:18:59 -07:00
|
|
|
#include "nsIAccessibleTypes.h"
|
2013-03-07 04:16:10 -08:00
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsIPresShell.h"
|
2014-04-10 09:09:40 -07:00
|
|
|
#include "mozilla/dom/Selection.h"
|
2013-09-10 15:18:59 -07:00
|
|
|
#include "mozilla/dom/Element.h"
|
2013-03-07 04:16:10 -08:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::a11y;
|
2014-04-10 09:09:40 -07:00
|
|
|
using mozilla::dom::Selection;
|
2013-03-07 04:16:10 -08:00
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
struct mozilla::a11y::SelData final
|
2014-02-11 17:18:31 -08:00
|
|
|
{
|
|
|
|
SelData(Selection* aSel, int32_t aReason) :
|
|
|
|
mSel(aSel), mReason(aReason) {}
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<Selection> mSel;
|
2014-02-11 17:18:31 -08:00
|
|
|
int16_t mReason;
|
|
|
|
|
2014-04-01 17:21:24 -07:00
|
|
|
NS_INLINE_DECL_REFCOUNTING(SelData)
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Private destructor, to discourage deletion outside of Release():
|
|
|
|
~SelData() {}
|
2014-02-11 17:18:31 -08:00
|
|
|
};
|
|
|
|
|
2014-05-01 08:45:40 -07:00
|
|
|
SelectionManager::SelectionManager() :
|
|
|
|
mCaretOffset(-1), mAccWithCaret(nullptr)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-07 04:16:10 -08:00
|
|
|
void
|
|
|
|
SelectionManager::ClearControlSelectionListener()
|
|
|
|
{
|
2013-03-22 18:59:48 -07:00
|
|
|
if (!mCurrCtrlFrame)
|
2013-03-07 04:16:10 -08:00
|
|
|
return;
|
|
|
|
|
2013-03-22 18:59:48 -07:00
|
|
|
const nsFrameSelection* frameSel = mCurrCtrlFrame->GetConstFrameSelection();
|
2013-03-07 04:16:10 -08:00
|
|
|
NS_ASSERTION(frameSel, "No frame selection for the element!");
|
2013-03-22 18:59:48 -07:00
|
|
|
|
|
|
|
mCurrCtrlFrame = nullptr;
|
2013-03-07 04:16:10 -08:00
|
|
|
if (!frameSel)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Remove 'this' registered as selection listener for the normal selection.
|
|
|
|
Selection* normalSel =
|
|
|
|
frameSel->GetSelection(nsISelectionController::SELECTION_NORMAL);
|
|
|
|
normalSel->RemoveSelectionListener(this);
|
|
|
|
|
|
|
|
// Remove 'this' registered as selection listener for the spellcheck
|
|
|
|
// selection.
|
|
|
|
Selection* spellSel =
|
|
|
|
frameSel->GetSelection(nsISelectionController::SELECTION_SPELLCHECK);
|
|
|
|
spellSel->RemoveSelectionListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionManager::SetControlSelectionListener(dom::Element* aFocusedElm)
|
|
|
|
{
|
|
|
|
// When focus moves such that the caret is part of a new frame selection
|
|
|
|
// this removes the old selection listener and attaches a new one for
|
|
|
|
// the current focus.
|
|
|
|
ClearControlSelectionListener();
|
|
|
|
|
2013-03-22 18:59:48 -07:00
|
|
|
mCurrCtrlFrame = aFocusedElm->GetPrimaryFrame();
|
|
|
|
if (!mCurrCtrlFrame)
|
2013-03-07 04:16:10 -08:00
|
|
|
return;
|
|
|
|
|
2013-03-22 18:59:48 -07:00
|
|
|
const nsFrameSelection* frameSel = mCurrCtrlFrame->GetConstFrameSelection();
|
2013-03-07 04:16:10 -08:00
|
|
|
NS_ASSERTION(frameSel, "No frame selection for focused element!");
|
|
|
|
if (!frameSel)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Register 'this' as selection listener for the normal selection.
|
|
|
|
Selection* normalSel =
|
|
|
|
frameSel->GetSelection(nsISelectionController::SELECTION_NORMAL);
|
|
|
|
normalSel->AddSelectionListener(this);
|
|
|
|
|
|
|
|
// Register 'this' as selection listener for the spell check selection.
|
|
|
|
Selection* spellSel =
|
|
|
|
frameSel->GetSelection(nsISelectionController::SELECTION_SPELLCHECK);
|
|
|
|
spellSel->AddSelectionListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionManager::AddDocSelectionListener(nsIPresShell* aPresShell)
|
|
|
|
{
|
|
|
|
const nsFrameSelection* frameSel = aPresShell->ConstFrameSelection();
|
|
|
|
|
|
|
|
// Register 'this' as selection listener for the normal selection.
|
|
|
|
Selection* normalSel =
|
|
|
|
frameSel->GetSelection(nsISelectionController::SELECTION_NORMAL);
|
|
|
|
normalSel->AddSelectionListener(this);
|
|
|
|
|
|
|
|
// Register 'this' as selection listener for the spell check selection.
|
|
|
|
Selection* spellSel =
|
|
|
|
frameSel->GetSelection(nsISelectionController::SELECTION_SPELLCHECK);
|
|
|
|
spellSel->AddSelectionListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionManager::RemoveDocSelectionListener(nsIPresShell* aPresShell)
|
|
|
|
{
|
|
|
|
const nsFrameSelection* frameSel = aPresShell->ConstFrameSelection();
|
|
|
|
|
|
|
|
// Remove 'this' registered as selection listener for the normal selection.
|
|
|
|
Selection* normalSel =
|
|
|
|
frameSel->GetSelection(nsISelectionController::SELECTION_NORMAL);
|
|
|
|
normalSel->RemoveSelectionListener(this);
|
|
|
|
|
|
|
|
// Remove 'this' registered as selection listener for the spellcheck
|
|
|
|
// selection.
|
|
|
|
Selection* spellSel =
|
|
|
|
frameSel->GetSelection(nsISelectionController::SELECTION_SPELLCHECK);
|
|
|
|
spellSel->RemoveSelectionListener(this);
|
|
|
|
}
|
|
|
|
|
2013-10-26 07:58:53 -07:00
|
|
|
void
|
|
|
|
SelectionManager::ProcessTextSelChangeEvent(AccEvent* aEvent)
|
|
|
|
{
|
2014-02-11 17:18:31 -08:00
|
|
|
// Fire selection change event if it's not pure caret-move selection change,
|
|
|
|
// i.e. the accessible has or had not collapsed selection.
|
2013-10-26 07:58:53 -07:00
|
|
|
AccTextSelChangeEvent* event = downcast_accEvent(aEvent);
|
2014-02-11 17:18:31 -08:00
|
|
|
if (!event->IsCaretMoveOnly())
|
2013-10-26 07:58:53 -07:00
|
|
|
nsEventShell::FireEvent(aEvent);
|
|
|
|
|
|
|
|
// Fire caret move event if there's a caret in the selection.
|
|
|
|
nsINode* caretCntrNode =
|
2014-02-11 17:18:31 -08:00
|
|
|
nsCoreUtils::GetDOMNodeFromDOMPoint(event->mSel->GetFocusNode(),
|
|
|
|
event->mSel->FocusOffset());
|
2013-10-26 07:58:53 -07:00
|
|
|
if (!caretCntrNode)
|
|
|
|
return;
|
|
|
|
|
|
|
|
HyperTextAccessible* caretCntr = nsAccUtils::GetTextContainer(caretCntrNode);
|
|
|
|
NS_ASSERTION(caretCntr,
|
|
|
|
"No text container for focus while there's one for common ancestor?!");
|
|
|
|
if (!caretCntr)
|
|
|
|
return;
|
|
|
|
|
2014-05-01 08:45:40 -07:00
|
|
|
Selection* selection = caretCntr->DOMSelection();
|
2014-08-27 13:14:53 -07:00
|
|
|
|
|
|
|
// XXX Sometimes we can't get a selection for caretCntr, in that case assume
|
|
|
|
// event->mSel is correct.
|
|
|
|
if (!selection)
|
|
|
|
selection = event->mSel;
|
|
|
|
|
2014-05-01 08:45:40 -07:00
|
|
|
mCaretOffset = caretCntr->DOMPointToOffset(selection->GetFocusNode(),
|
|
|
|
selection->FocusOffset());
|
|
|
|
mAccWithCaret = caretCntr;
|
|
|
|
if (mCaretOffset != -1) {
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<AccCaretMoveEvent> caretMoveEvent =
|
2014-05-01 08:45:40 -07:00
|
|
|
new AccCaretMoveEvent(caretCntr, mCaretOffset, aEvent->FromUserInput());
|
2013-10-26 07:58:53 -07:00
|
|
|
nsEventShell::FireEvent(caretMoveEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-07 04:16:10 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
SelectionManager::NotifySelectionChanged(nsIDOMDocument* aDOMDocument,
|
|
|
|
nsISelection* aSelection,
|
|
|
|
int16_t aReason)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aDOMDocument);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> documentNode(do_QueryInterface(aDOMDocument));
|
|
|
|
DocAccessible* document = GetAccService()->GetDocAccessible(documentNode);
|
|
|
|
|
|
|
|
#ifdef A11Y_LOG
|
|
|
|
if (logging::IsEnabled(logging::eSelection))
|
2014-02-11 17:18:31 -08:00
|
|
|
logging::SelChange(aSelection, document, aReason);
|
2013-03-07 04:16:10 -08:00
|
|
|
#endif
|
|
|
|
|
2014-08-27 13:14:53 -07:00
|
|
|
if (document) {
|
2013-03-07 04:16:10 -08:00
|
|
|
// Selection manager has longer lifetime than any document accessible,
|
|
|
|
// so that we are guaranteed that the notification is processed before
|
|
|
|
// the selection manager is destroyed.
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<SelData> selData =
|
2014-02-11 17:18:31 -08:00
|
|
|
new SelData(static_cast<Selection*>(aSelection), aReason);
|
|
|
|
document->HandleNotification<SelectionManager, SelData>
|
|
|
|
(this, &SelectionManager::ProcessSelectionChanged, selData);
|
2013-03-07 04:16:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-02-11 17:18:31 -08:00
|
|
|
SelectionManager::ProcessSelectionChanged(SelData* aSelData)
|
2013-03-07 04:16:10 -08:00
|
|
|
{
|
2014-02-11 17:18:31 -08:00
|
|
|
Selection* selection = aSelData->mSel;
|
2013-10-29 20:19:04 -07:00
|
|
|
if (!selection->GetPresShell())
|
|
|
|
return;
|
|
|
|
|
2013-10-26 07:58:53 -07:00
|
|
|
const nsRange* range = selection->GetAnchorFocusRange();
|
|
|
|
nsINode* cntrNode = nullptr;
|
|
|
|
if (range)
|
|
|
|
cntrNode = range->GetCommonAncestor();
|
2014-02-11 17:18:31 -08:00
|
|
|
|
2013-10-26 07:58:53 -07:00
|
|
|
if (!cntrNode) {
|
|
|
|
cntrNode = selection->GetFrameSelection()->GetAncestorLimiter();
|
|
|
|
if (!cntrNode) {
|
|
|
|
cntrNode = selection->GetPresShell()->GetDocument();
|
2014-02-11 17:18:31 -08:00
|
|
|
NS_ASSERTION(aSelData->mSel->GetPresShell()->ConstFrameSelection() == selection->GetFrameSelection(),
|
2013-10-26 07:58:53 -07:00
|
|
|
"Wrong selection container was used!");
|
|
|
|
}
|
2013-03-07 04:16:10 -08:00
|
|
|
}
|
|
|
|
|
2013-10-26 07:58:53 -07:00
|
|
|
HyperTextAccessible* text = nsAccUtils::GetTextContainer(cntrNode);
|
|
|
|
if (!text) {
|
|
|
|
NS_NOTREACHED("We must reach document accessible implementing text interface!");
|
2013-03-07 04:16:10 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-26 07:58:53 -07:00
|
|
|
if (selection->GetType() == nsISelectionController::SELECTION_NORMAL) {
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<AccEvent> event =
|
2014-02-11 17:18:31 -08:00
|
|
|
new AccTextSelChangeEvent(text, selection, aSelData->mReason);
|
2013-10-26 07:58:53 -07:00
|
|
|
text->Document()->FireDelayedEvent(event);
|
2013-03-07 04:16:10 -08:00
|
|
|
|
2013-10-26 07:58:53 -07:00
|
|
|
} else if (selection->GetType() == nsISelectionController::SELECTION_SPELLCHECK) {
|
|
|
|
// XXX: fire an event for container accessible of the focus/anchor range
|
|
|
|
// of the spelcheck selection.
|
|
|
|
text->Document()->FireDelayedEvent(nsIAccessibleEvent::EVENT_TEXT_ATTRIBUTE_CHANGED,
|
|
|
|
text);
|
2013-03-07 04:16:10 -08:00
|
|
|
}
|
|
|
|
}
|