2017-10-27 01:08:41 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2017-06-19 01:07:36 +03:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implementation of mozilla::dom::SelectionChangeListener
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SelectionChangeListener.h"
|
|
|
|
|
2017-10-26 12:40:12 +03:00
|
|
|
#include "mozilla/AsyncEventDispatcher.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2017-06-19 01:07:36 +03:00
|
|
|
#include "nsContentUtils.h"
|
2017-10-26 12:40:12 +03:00
|
|
|
#include "nsIDocument.h"
|
2017-06-19 01:07:36 +03:00
|
|
|
#include "nsFrameSelection.h"
|
|
|
|
#include "nsRange.h"
|
2018-05-08 20:52:35 +03:00
|
|
|
#include "mozilla/dom/Selection.h"
|
2017-06-19 01:07:36 +03:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
|
|
|
SelectionChangeListener::RawRangeData::RawRangeData(const nsRange* aRange)
|
|
|
|
{
|
|
|
|
mozilla::ErrorResult rv;
|
2017-07-11 16:46:11 +03:00
|
|
|
mStartContainer = aRange->GetStartContainer(rv);
|
2017-06-19 01:07:36 +03:00
|
|
|
rv.SuppressException();
|
2017-07-11 16:57:55 +03:00
|
|
|
mEndContainer = aRange->GetEndContainer(rv);
|
2017-06-19 01:07:36 +03:00
|
|
|
rv.SuppressException();
|
|
|
|
mStartOffset = aRange->GetStartOffset(rv);
|
|
|
|
rv.SuppressException();
|
|
|
|
mEndOffset = aRange->GetEndOffset(rv);
|
|
|
|
rv.SuppressException();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SelectionChangeListener::RawRangeData::Equals(const nsRange* aRange)
|
|
|
|
{
|
|
|
|
mozilla::ErrorResult rv;
|
2017-07-11 16:46:11 +03:00
|
|
|
bool eq = mStartContainer == aRange->GetStartContainer(rv);
|
2017-06-19 01:07:36 +03:00
|
|
|
rv.SuppressException();
|
2017-07-11 16:57:55 +03:00
|
|
|
eq = eq && mEndContainer == aRange->GetEndContainer(rv);
|
2017-06-19 01:07:36 +03:00
|
|
|
rv.SuppressException();
|
|
|
|
eq = eq && mStartOffset == aRange->GetStartOffset(rv);
|
|
|
|
rv.SuppressException();
|
|
|
|
eq = eq && mEndOffset == aRange->GetEndOffset(rv);
|
|
|
|
rv.SuppressException();
|
|
|
|
return eq;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
|
|
|
|
SelectionChangeListener::RawRangeData& aField,
|
|
|
|
const char* aName,
|
|
|
|
uint32_t aFlags = 0)
|
|
|
|
{
|
2017-07-11 16:46:11 +03:00
|
|
|
ImplCycleCollectionTraverse(aCallback, aField.mStartContainer,
|
|
|
|
"mStartContainer", aFlags);
|
2017-07-11 16:57:55 +03:00
|
|
|
ImplCycleCollectionTraverse(aCallback, aField.mEndContainer,
|
|
|
|
"mEndContainer", aFlags);
|
2017-06-19 01:07:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(SelectionChangeListener)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(SelectionChangeListener)
|
|
|
|
tmp->mOldRanges.Clear();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(SelectionChangeListener)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOldRanges);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SelectionChangeListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISelectionListener)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(SelectionChangeListener)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(SelectionChangeListener)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2018-05-08 20:52:35 +03:00
|
|
|
SelectionChangeListener::NotifySelectionChanged(nsIDocument* aDoc,
|
|
|
|
Selection* aSel, int16_t aReason)
|
2017-06-19 01:07:36 +03:00
|
|
|
{
|
2018-05-08 20:52:35 +03:00
|
|
|
nsIDocument* doc = aSel->GetParentObject();
|
2017-06-19 01:07:36 +03:00
|
|
|
if (!(doc && nsContentUtils::IsSystemPrincipal(doc->NodePrincipal())) &&
|
|
|
|
!nsFrameSelection::sSelectionEventsEnabled) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the ranges have actually changed
|
|
|
|
// Don't bother checking this if we are hiding changes.
|
2018-05-08 20:52:35 +03:00
|
|
|
if (mOldRanges.Length() == aSel->RangeCount() &&
|
|
|
|
!aSel->IsBlockingSelectionChangeEvents()) {
|
2017-06-19 01:07:36 +03:00
|
|
|
bool changed = false;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < mOldRanges.Length(); i++) {
|
2018-05-08 20:52:35 +03:00
|
|
|
if (!mOldRanges[i].Equals(aSel->GetRangeAt(i))) {
|
2017-06-19 01:07:36 +03:00
|
|
|
changed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!changed) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The ranges have actually changed, update the mOldRanges array
|
|
|
|
mOldRanges.ClearAndRetainStorage();
|
2018-05-08 20:52:35 +03:00
|
|
|
for (size_t i = 0; i < aSel->RangeCount(); i++) {
|
|
|
|
mOldRanges.AppendElement(RawRangeData(aSel->GetRangeAt(i)));
|
2017-06-19 01:07:36 +03:00
|
|
|
}
|
|
|
|
|
2017-06-22 21:01:27 +03:00
|
|
|
if (doc) {
|
|
|
|
nsPIDOMWindowInner* inner = doc->GetInnerWindow();
|
|
|
|
if (inner && !inner->HasSelectionChangeEventListeners()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-19 01:07:36 +03:00
|
|
|
// If we are hiding changes, then don't do anything else. We do this after we
|
|
|
|
// update mOldRanges so that changes after the changes stop being hidden don't
|
|
|
|
// incorrectly trigger a change, even though they didn't change anything
|
2018-05-08 20:52:35 +03:00
|
|
|
if (aSel->IsBlockingSelectionChangeEvents()) {
|
2017-06-19 01:07:36 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The spec currently doesn't say that we should dispatch this event on text
|
|
|
|
// controls, so for now we only support doing that under a pref, disabled by
|
|
|
|
// default.
|
|
|
|
// See https://github.com/w3c/selection-api/issues/53.
|
|
|
|
if (nsFrameSelection::sSelectionEventsOnTextControlsEnabled) {
|
|
|
|
nsCOMPtr<nsINode> target;
|
|
|
|
|
|
|
|
// Check if we should be firing this event to a different node than the
|
|
|
|
// document. The limiter of the nsFrameSelection will be within the native
|
|
|
|
// anonymous subtree of the node we want to fire the event on. We need to
|
|
|
|
// climb up the parent chain to escape the native anonymous subtree, and then
|
|
|
|
// fire the event.
|
2018-05-08 20:52:35 +03:00
|
|
|
if (const nsFrameSelection* fs = aSel->GetFrameSelection()) {
|
2017-06-19 01:07:36 +03:00
|
|
|
if (nsCOMPtr<nsIContent> root = fs->GetLimiter()) {
|
|
|
|
while (root && root->IsInNativeAnonymousSubtree()) {
|
|
|
|
root = root->GetParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
target = root.forget();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we didn't get a target before, we can instead fire the event at the document.
|
|
|
|
if (!target) {
|
2018-05-08 20:52:35 +03:00
|
|
|
target = aDoc;
|
2017-06-19 01:07:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (target) {
|
|
|
|
RefPtr<AsyncEventDispatcher> asyncDispatcher =
|
2017-06-17 06:43:14 +03:00
|
|
|
new AsyncEventDispatcher(target, eSelectionChange, false);
|
2017-06-19 01:07:36 +03:00
|
|
|
asyncDispatcher->PostDOMEvent();
|
|
|
|
}
|
|
|
|
} else {
|
2018-05-08 20:52:35 +03:00
|
|
|
if (const nsFrameSelection* fs = aSel->GetFrameSelection()) {
|
2017-06-19 01:07:36 +03:00
|
|
|
if (nsCOMPtr<nsIContent> root = fs->GetLimiter()) {
|
|
|
|
if (root->IsInNativeAnonymousSubtree()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-08 20:52:35 +03:00
|
|
|
if (aDoc) {
|
2017-06-19 01:07:36 +03:00
|
|
|
RefPtr<AsyncEventDispatcher> asyncDispatcher =
|
2018-05-08 20:52:35 +03:00
|
|
|
new AsyncEventDispatcher(aDoc, eSelectionChange, false);
|
2017-06-19 01:07:36 +03:00
|
|
|
asyncDispatcher->PostDOMEvent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|