2013-03-07 16:16:10 +04: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 19:45:40 +04:00
|
|
|
#include "HyperTextAccessible.h"
|
|
|
|
#include "HyperTextAccessible-inl.h"
|
2013-03-07 16:16:10 +04:00
|
|
|
#include "nsAccessibilityService.h"
|
|
|
|
#include "nsAccUtils.h"
|
|
|
|
#include "nsCoreUtils.h"
|
2013-10-26 18:58:53 +04:00
|
|
|
#include "nsEventShell.h"
|
2014-08-06 09:19:27 +04:00
|
|
|
#include "nsFrameSelection.h"
|
2013-03-07 16:16:10 +04:00
|
|
|
|
2013-09-11 02:18:59 +04:00
|
|
|
#include "nsIAccessibleTypes.h"
|
2013-03-07 16:16:10 +04:00
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsIPresShell.h"
|
2014-04-10 20:09:40 +04:00
|
|
|
#include "mozilla/dom/Selection.h"
|
2013-09-11 02:18:59 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2013-03-07 16:16:10 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::a11y;
|
2014-04-10 20:09:40 +04:00
|
|
|
using mozilla::dom::Selection;
|
2013-03-07 16:16:10 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
struct mozilla::a11y::SelData final
|
2014-02-12 05:18:31 +04:00
|
|
|
{
|
|
|
|
SelData(Selection* aSel, int32_t aReason) :
|
|
|
|
mSel(aSel), mReason(aReason) {}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Selection> mSel;
|
2014-02-12 05:18:31 +04:00
|
|
|
int16_t mReason;
|
|
|
|
|
2014-04-02 04:21:24 +04:00
|
|
|
NS_INLINE_DECL_REFCOUNTING(SelData)
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Private destructor, to discourage deletion outside of Release():
|
|
|
|
~SelData() {}
|
2014-02-12 05:18:31 +04:00
|
|
|
};
|
|
|
|
|
2014-05-01 19:45:40 +04:00
|
|
|
SelectionManager::SelectionManager() :
|
|
|
|
mCaretOffset(-1), mAccWithCaret(nullptr)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-07 16:16:10 +04:00
|
|
|
void
|
|
|
|
SelectionManager::ClearControlSelectionListener()
|
|
|
|
{
|
|
|
|
|
|
|
|
// Remove 'this' registered as selection listener for the normal selection.
|
2017-07-27 02:02:37 +03:00
|
|
|
nsCOMPtr<nsISelection> normalSel = do_QueryReferent(mCurrCtrlNormalSel);
|
|
|
|
if (normalSel) {
|
|
|
|
normalSel->AsSelection()->RemoveSelectionListener(this);
|
|
|
|
mCurrCtrlNormalSel = nullptr;
|
|
|
|
}
|
2013-03-07 16:16:10 +04:00
|
|
|
|
|
|
|
// Remove 'this' registered as selection listener for the spellcheck
|
|
|
|
// selection.
|
2017-07-27 02:02:37 +03:00
|
|
|
nsCOMPtr<nsISelection> spellSel = do_QueryReferent(mCurrCtrlSpellSel);
|
|
|
|
if (spellSel) {
|
|
|
|
spellSel->AsSelection()->RemoveSelectionListener(this);
|
|
|
|
mCurrCtrlSpellSel = nullptr;
|
|
|
|
}
|
2013-03-07 16:16:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2017-07-27 02:02:37 +03:00
|
|
|
nsIFrame* controlFrame = aFocusedElm->GetPrimaryFrame();
|
|
|
|
if (!controlFrame)
|
2013-03-07 16:16:10 +04:00
|
|
|
return;
|
|
|
|
|
2017-07-27 02:02:37 +03:00
|
|
|
const nsFrameSelection* frameSel = controlFrame->GetConstFrameSelection();
|
2013-03-07 16:16:10 +04:00
|
|
|
NS_ASSERTION(frameSel, "No frame selection for focused element!");
|
|
|
|
if (!frameSel)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Register 'this' as selection listener for the normal selection.
|
2017-07-27 02:02:37 +03:00
|
|
|
nsCOMPtr<nsISelection> normalSel = frameSel->GetSelection(SelectionType::eNormal);
|
|
|
|
normalSel->AsSelection()->AddSelectionListener(this);
|
|
|
|
mCurrCtrlNormalSel = do_GetWeakReference(normalSel);
|
2013-03-07 16:16:10 +04:00
|
|
|
|
|
|
|
// Register 'this' as selection listener for the spell check selection.
|
2017-07-27 02:02:37 +03:00
|
|
|
nsCOMPtr<nsISelection> spellSel = frameSel->GetSelection(SelectionType::eSpellCheck);
|
|
|
|
spellSel->AsSelection()->AddSelectionListener(this);
|
|
|
|
mCurrCtrlSpellSel = do_GetWeakReference(spellSel);
|
2013-03-07 16:16:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionManager::AddDocSelectionListener(nsIPresShell* aPresShell)
|
|
|
|
{
|
|
|
|
const nsFrameSelection* frameSel = aPresShell->ConstFrameSelection();
|
|
|
|
|
|
|
|
// Register 'this' as selection listener for the normal selection.
|
2016-06-09 12:35:22 +03:00
|
|
|
Selection* normalSel = frameSel->GetSelection(SelectionType::eNormal);
|
2013-03-07 16:16:10 +04:00
|
|
|
normalSel->AddSelectionListener(this);
|
|
|
|
|
|
|
|
// Register 'this' as selection listener for the spell check selection.
|
2016-06-09 12:37:36 +03:00
|
|
|
Selection* spellSel = frameSel->GetSelection(SelectionType::eSpellCheck);
|
2013-03-07 16:16:10 +04:00
|
|
|
spellSel->AddSelectionListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SelectionManager::RemoveDocSelectionListener(nsIPresShell* aPresShell)
|
|
|
|
{
|
|
|
|
const nsFrameSelection* frameSel = aPresShell->ConstFrameSelection();
|
|
|
|
|
|
|
|
// Remove 'this' registered as selection listener for the normal selection.
|
2016-06-09 12:35:22 +03:00
|
|
|
Selection* normalSel = frameSel->GetSelection(SelectionType::eNormal);
|
2013-03-07 16:16:10 +04:00
|
|
|
normalSel->RemoveSelectionListener(this);
|
|
|
|
|
|
|
|
// Remove 'this' registered as selection listener for the spellcheck
|
|
|
|
// selection.
|
2016-06-09 12:37:36 +03:00
|
|
|
Selection* spellSel = frameSel->GetSelection(SelectionType::eSpellCheck);
|
2013-03-07 16:16:10 +04:00
|
|
|
spellSel->RemoveSelectionListener(this);
|
|
|
|
}
|
|
|
|
|
2013-10-26 18:58:53 +04:00
|
|
|
void
|
|
|
|
SelectionManager::ProcessTextSelChangeEvent(AccEvent* aEvent)
|
|
|
|
{
|
2014-02-12 05:18:31 +04: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 18:58:53 +04:00
|
|
|
AccTextSelChangeEvent* event = downcast_accEvent(aEvent);
|
2014-02-12 05:18:31 +04:00
|
|
|
if (!event->IsCaretMoveOnly())
|
2013-10-26 18:58:53 +04:00
|
|
|
nsEventShell::FireEvent(aEvent);
|
|
|
|
|
|
|
|
// Fire caret move event if there's a caret in the selection.
|
|
|
|
nsINode* caretCntrNode =
|
2014-02-12 05:18:31 +04:00
|
|
|
nsCoreUtils::GetDOMNodeFromDOMPoint(event->mSel->GetFocusNode(),
|
|
|
|
event->mSel->FocusOffset());
|
2013-10-26 18:58:53 +04: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 19:45:40 +04:00
|
|
|
Selection* selection = caretCntr->DOMSelection();
|
2014-08-28 00:14:53 +04: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 19:45:40 +04:00
|
|
|
mCaretOffset = caretCntr->DOMPointToOffset(selection->GetFocusNode(),
|
|
|
|
selection->FocusOffset());
|
|
|
|
mAccWithCaret = caretCntr;
|
|
|
|
if (mCaretOffset != -1) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AccCaretMoveEvent> caretMoveEvent =
|
2014-05-01 19:45:40 +04:00
|
|
|
new AccCaretMoveEvent(caretCntr, mCaretOffset, aEvent->FromUserInput());
|
2013-10-26 18:58:53 +04:00
|
|
|
nsEventShell::FireEvent(caretMoveEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-07 16:16:10 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
SelectionManager::NotifySelectionChanged(nsIDOMDocument* aDOMDocument,
|
|
|
|
nsISelection* aSelection,
|
|
|
|
int16_t aReason)
|
|
|
|
{
|
2016-06-17 16:32:49 +03:00
|
|
|
if (NS_WARN_IF(!aDOMDocument) || NS_WARN_IF(!aSelection)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2013-03-07 16:16:10 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> documentNode(do_QueryInterface(aDOMDocument));
|
|
|
|
DocAccessible* document = GetAccService()->GetDocAccessible(documentNode);
|
|
|
|
|
|
|
|
#ifdef A11Y_LOG
|
|
|
|
if (logging::IsEnabled(logging::eSelection))
|
2014-02-12 05:18:31 +04:00
|
|
|
logging::SelChange(aSelection, document, aReason);
|
2013-03-07 16:16:10 +04:00
|
|
|
#endif
|
|
|
|
|
2014-08-28 00:14:53 +04:00
|
|
|
if (document) {
|
2013-03-07 16:16:10 +04: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-18 08:24:48 +03:00
|
|
|
RefPtr<SelData> selData =
|
2016-06-17 16:32:49 +03:00
|
|
|
new SelData(aSelection->AsSelection(), aReason);
|
2014-02-12 05:18:31 +04:00
|
|
|
document->HandleNotification<SelectionManager, SelData>
|
|
|
|
(this, &SelectionManager::ProcessSelectionChanged, selData);
|
2013-03-07 16:16:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-02-12 05:18:31 +04:00
|
|
|
SelectionManager::ProcessSelectionChanged(SelData* aSelData)
|
2013-03-07 16:16:10 +04:00
|
|
|
{
|
2014-02-12 05:18:31 +04:00
|
|
|
Selection* selection = aSelData->mSel;
|
2013-10-30 07:19:04 +04:00
|
|
|
if (!selection->GetPresShell())
|
|
|
|
return;
|
|
|
|
|
2013-10-26 18:58:53 +04:00
|
|
|
const nsRange* range = selection->GetAnchorFocusRange();
|
|
|
|
nsINode* cntrNode = nullptr;
|
|
|
|
if (range)
|
|
|
|
cntrNode = range->GetCommonAncestor();
|
2014-02-12 05:18:31 +04:00
|
|
|
|
2013-10-26 18:58:53 +04:00
|
|
|
if (!cntrNode) {
|
|
|
|
cntrNode = selection->GetFrameSelection()->GetAncestorLimiter();
|
|
|
|
if (!cntrNode) {
|
|
|
|
cntrNode = selection->GetPresShell()->GetDocument();
|
2014-02-12 05:18:31 +04:00
|
|
|
NS_ASSERTION(aSelData->mSel->GetPresShell()->ConstFrameSelection() == selection->GetFrameSelection(),
|
2013-10-26 18:58:53 +04:00
|
|
|
"Wrong selection container was used!");
|
|
|
|
}
|
2013-03-07 16:16:10 +04:00
|
|
|
}
|
|
|
|
|
2013-10-26 18:58:53 +04:00
|
|
|
HyperTextAccessible* text = nsAccUtils::GetTextContainer(cntrNode);
|
|
|
|
if (!text) {
|
|
|
|
NS_NOTREACHED("We must reach document accessible implementing text interface!");
|
2013-03-07 16:16:10 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-09 12:35:22 +03:00
|
|
|
if (selection->GetType() == SelectionType::eNormal) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AccEvent> event =
|
2014-02-12 05:18:31 +04:00
|
|
|
new AccTextSelChangeEvent(text, selection, aSelData->mReason);
|
2013-10-26 18:58:53 +04:00
|
|
|
text->Document()->FireDelayedEvent(event);
|
2013-03-07 16:16:10 +04:00
|
|
|
|
2016-06-09 12:37:36 +03:00
|
|
|
} else if (selection->GetType() == SelectionType::eSpellCheck) {
|
2013-10-26 18:58:53 +04:00
|
|
|
// 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 16:16:10 +04:00
|
|
|
}
|
|
|
|
}
|