Source code

Revision control

Copy as Markdown

Other Tools

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "ContentEventHandler.h"
#include "IMEContentObserver.h"
#include "mozilla/Assertions.h"
#include "mozilla/AsyncEventDispatcher.h"
#include "mozilla/AutoRestore.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/EventStateManager.h"
#include "mozilla/IMEStateManager.h"
#include "mozilla/Logging.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/PresShell.h"
#include "mozilla/StaticPrefs_test.h"
#include "mozilla/TextComposition.h"
#include "mozilla/TextControlElement.h"
#include "mozilla/TextEvents.h"
#include "mozilla/dom/AncestorIterator.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/Selection.h"
#include "nsContentUtils.h"
#include "nsAtom.h"
#include "nsDocShell.h"
#include "nsGkAtoms.h"
#include "nsIContent.h"
#include "nsIFrame.h"
#include "nsINode.h"
#include "nsISelectionController.h"
#include "nsISupports.h"
#include "nsIWeakReferenceUtils.h"
#include "nsIWidget.h"
#include "nsPresContext.h"
#include "nsRange.h"
#include "nsRefreshDriver.h"
#include "WritingModes.h"
#include "nsString.h"
namespace mozilla {
using RawNodePosition = ContentEventHandler::RawNodePosition;
using namespace dom;
using namespace widget;
LazyLogModule sIMECOLog("IMEContentObserver");
LazyLogModule sCacheLog("IMEContentObserverCache");
static const char* ToChar(bool aBool) { return aBool ? "true" : "false"; }
/******************************************************************************
* mozilla::IMEContentObserver
******************************************************************************/
NS_IMPL_CYCLE_COLLECTION_CLASS(IMEContentObserver)
// Note that we don't need to add mFirstAddedContainer nor
// mLastAddedContainer to cycle collection because they are non-null only
// during short time and shouldn't be touched while they are non-null.
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(IMEContentObserver)
nsAutoScriptBlocker scriptBlocker;
tmp->NotifyIMEOfBlur();
tmp->UnregisterObservers();
NS_IMPL_CYCLE_COLLECTION_UNLINK(mSelection)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mRootElement)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mEditableNode)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDocShell)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mEditorBase)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDocumentObserver)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mEndOfAddedTextCache.mContainerNode)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mEndOfAddedTextCache.mContent)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mStartOfRemovingTextRangeCache.mContainerNode)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mStartOfRemovingTextRangeCache.mContent)
NS_IMPL_CYCLE_COLLECTION_UNLINK_WEAK_REFERENCE
tmp->mIMENotificationRequests = nullptr;
tmp->mESM = nullptr;
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(IMEContentObserver)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWidget)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFocusedWidget)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSelection)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRootElement)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEditableNode)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDocShell)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEditorBase)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDocumentObserver)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEndOfAddedTextCache.mContainerNode)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEndOfAddedTextCache.mContent)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(
mStartOfRemovingTextRangeCache.mContainerNode)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mStartOfRemovingTextRangeCache.mContent)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IMEContentObserver)
NS_INTERFACE_MAP_ENTRY(nsIMutationObserver)
NS_INTERFACE_MAP_ENTRY(nsIReflowObserver)
NS_INTERFACE_MAP_ENTRY(nsIScrollObserver)
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIReflowObserver)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(IMEContentObserver)
NS_IMPL_CYCLE_COLLECTING_RELEASE(IMEContentObserver)
IMEContentObserver::IMEContentObserver() {
#ifdef DEBUG
// TODO: Make this test as GTest.
mTextChangeData.Test();
#endif
}
void IMEContentObserver::Init(nsIWidget& aWidget, nsPresContext& aPresContext,
Element* aElement, EditorBase& aEditorBase) {
State state = GetState();
if (NS_WARN_IF(state == eState_Observing)) {
return; // Nothing to do.
}
bool firstInitialization = state != eState_StoppedObserving;
if (!firstInitialization) {
// If this is now trying to initialize with new contents, all observers
// should be registered again for simpler implementation.
UnregisterObservers();
Clear();
}
mESM = aPresContext.EventStateManager();
mESM->OnStartToObserveContent(this);
mWidget = &aWidget;
mIMENotificationRequests = &mWidget->IMENotificationRequestsRef();
if (!InitWithEditor(aPresContext, aElement, aEditorBase)) {
MOZ_LOG(sIMECOLog, LogLevel::Error,
("0x%p Init() FAILED, due to InitWithEditor() "
"failure",
this));
Clear();
return;
}
if (firstInitialization) {
// Now, try to send NOTIFY_IME_OF_FOCUS to IME via the widget.
MaybeNotifyIMEOfFocusSet();
// When this is called first time, IME has not received NOTIFY_IME_OF_FOCUS
// yet since NOTIFY_IME_OF_FOCUS will be sent to widget asynchronously.
// So, we need to do nothing here. After NOTIFY_IME_OF_FOCUS has been
// sent, OnIMEReceivedFocus() will be called and content, selection and/or
// position changes will be observed
return;
}
// When this is called after editor reframing (i.e., the root editable node
// is also recreated), IME has usually received NOTIFY_IME_OF_FOCUS. In this
// case, we need to restart to observe content, selection and/or position
// changes in new root editable node.
ObserveEditableNode();
if (!NeedsToNotifyIMEOfSomething()) {
return;
}
// Some change events may wait to notify IME because this was being
// initialized. It is the time to flush them.
FlushMergeableNotifications();
}
void IMEContentObserver::OnIMEReceivedFocus() {
// While Init() notifies IME of focus, pending layout may be flushed
// because the notification may cause querying content. Then, recursive
// call of Init() with the latest content may occur. In such case, we
// shouldn't keep first initialization which notified IME of focus.
if (GetState() != eState_Initializing) {
MOZ_LOG(sIMECOLog, LogLevel::Warning,
("0x%p OnIMEReceivedFocus(), "
"but the state is not \"initializing\", so does nothing",
this));
return;
}
// NOTIFY_IME_OF_FOCUS might cause recreating IMEContentObserver
// instance via IMEStateManager::UpdateIMEState(). So, this
// instance might already have been destroyed, check it.
if (!mRootElement) {
MOZ_LOG(sIMECOLog, LogLevel::Warning,
("0x%p OnIMEReceivedFocus(), "
"but mRootElement has already been cleared, so does nothing",
this));
return;
}
// Start to observe which is needed by IME when IME actually has focus.
ObserveEditableNode();
if (!NeedsToNotifyIMEOfSomething()) {
return;
}
// Some change events may wait to notify IME because this was being
// initialized. It is the time to flush them.
FlushMergeableNotifications();
}
bool IMEContentObserver::InitWithEditor(nsPresContext& aPresContext,
Element* aElement,
EditorBase& aEditorBase) {
// mEditableNode is one of
// - Anonymous <div> in <input> or <textarea>
// - Editing host if it's not in the design mode
// - Document if it's in the design mode
mEditableNode = IMEStateManager::GetRootEditableNode(aPresContext, aElement);
if (NS_WARN_IF(!mEditableNode)) {
return false;
}
mEditorBase = &aEditorBase;
RefPtr<PresShell> presShell = aPresContext.GetPresShell();
// get selection and root content
nsCOMPtr<nsISelectionController> selCon;
if (mEditableNode->IsContent()) {
nsIFrame* frame = mEditableNode->AsContent()->GetPrimaryFrame();
if (NS_WARN_IF(!frame)) {
return false;
}
frame->GetSelectionController(&aPresContext, getter_AddRefs(selCon));
} else {
// mEditableNode is a document
selCon = presShell;
}
if (NS_WARN_IF(!selCon)) {
return false;
}
mSelection = selCon->GetSelection(nsISelectionController::SELECTION_NORMAL);
if (NS_WARN_IF(!mSelection)) {
return false;
}
if (mEditorBase->IsTextEditor()) {
mRootElement = mEditorBase->GetRoot(); // The anonymous <div>
MOZ_ASSERT(mRootElement);
MOZ_ASSERT(mRootElement->GetFirstChild());
if (auto* text = Text::FromNodeOrNull(
mRootElement ? mRootElement->GetFirstChild() : nullptr)) {
mTextControlValueLength = ContentEventHandler::GetNativeTextLength(*text);
}
mIsTextControl = true;
} else if (const nsRange* selRange = mSelection->GetRangeAt(0)) {
MOZ_ASSERT(!mIsTextControl);
if (NS_WARN_IF(!selRange->GetStartContainer())) {
return false;
}
// If an editing host has focus, mRootElement is it.
// Otherwise, if we're in the design mode, mRootElement is the <body> if
// there is and startContainer is not outside of the <body>. Otherwise, the
// document element is used instead.
nsCOMPtr<nsINode> startContainer = selRange->GetStartContainer();
mRootElement = Element::FromNodeOrNull(
startContainer->GetSelectionRootContent(presShell));
} else {
MOZ_ASSERT(!mIsTextControl);
// If an editing host has focus, mRootElement is it.
// Otherwise, if we're in the design mode, mRootElement is the <body> if
// there is. Otherwise, the document element is used instead.
nsCOMPtr<nsINode> editableNode = mEditableNode;
mRootElement = Element::FromNodeOrNull(
editableNode->GetSelectionRootContent(presShell));
}
if (!mRootElement && mEditableNode->IsDocument()) {
// The document node is editable, but there are no contents, this document
// is not editable.
return false;
}
if (NS_WARN_IF(!mRootElement)) {
return false;
}
mDocShell = aPresContext.GetDocShell();
if (NS_WARN_IF(!mDocShell)) {
return false;
}
mDocumentObserver = new DocumentObserver(*this);
return true;
}
void IMEContentObserver::Clear() {
mEditorBase = nullptr;
mSelection = nullptr;
mEditableNode = nullptr;
mRootElement = nullptr;
mDocShell = nullptr;
// Should be safe to clear mDocumentObserver here even though it grabs
// this instance in most cases because this is called by Init() or Destroy().
// The callers of Init() grab this instance with local RefPtr.
// The caller of Destroy() also grabs this instance with local RefPtr.
// So, this won't cause refcount of this instance become 0.
mDocumentObserver = nullptr;
}
void IMEContentObserver::ObserveEditableNode() {
MOZ_RELEASE_ASSERT(mSelection);
MOZ_RELEASE_ASSERT(mRootElement);
MOZ_RELEASE_ASSERT(GetState() != eState_Observing);
// If this is called before sending NOTIFY_IME_OF_FOCUS (it's possible when
// the editor is reframed before sending NOTIFY_IME_OF_FOCUS asynchronously),
// the notification requests of mWidget may be different from after the widget
// receives NOTIFY_IME_OF_FOCUS. So, this should be called again by
// OnIMEReceivedFocus() which is called after sending NOTIFY_IME_OF_FOCUS.
if (!mIMEHasFocus) {
MOZ_ASSERT(!mWidget || mNeedsToNotifyIMEOfFocusSet ||
mSendingNotification == NOTIFY_IME_OF_FOCUS,
"Wow, OnIMEReceivedFocus() won't be called?");
return;
}
mIsObserving = true;
if (mEditorBase) {
mEditorBase->SetIMEContentObserver(this);
}
MOZ_LOG(sIMECOLog, LogLevel::Info,
("0x%p ObserveEditableNode(), starting to observe 0x%p (%s)", this,
mRootElement.get(), ToString(*mRootElement).c_str()));
mRootElement->AddMutationObserver(this);
// If it's in a document (should be so), we can use document observer to
// reduce redundant computation of text change offsets.
Document* doc = mRootElement->GetComposedDoc();
if (doc) {
RefPtr<DocumentObserver> documentObserver = mDocumentObserver;
documentObserver->Observe(doc);
}
if (mDocShell) {
// Add scroll position listener and reflow observer to detect position
// and size changes
mDocShell->AddWeakScrollObserver(this);
mDocShell->AddWeakReflowObserver(this);
}
}
void IMEContentObserver::NotifyIMEOfBlur() {
// Prevent any notifications to be sent IME.
nsCOMPtr<nsIWidget> widget;
mWidget.swap(widget);
mIMENotificationRequests = nullptr;
// If we hasn't been set focus, we shouldn't send blur notification to IME.
if (!mIMEHasFocus) {
return;
}
// mWidget must have been non-nullptr if IME has focus.
MOZ_RELEASE_ASSERT(widget);
RefPtr<IMEContentObserver> kungFuDeathGrip(this);
MOZ_LOG(sIMECOLog, LogLevel::Info,
("0x%p NotifyIMEOfBlur(), sending NOTIFY_IME_OF_BLUR", this));
// For now, we need to send blur notification in any condition because
// we don't have any simple ways to send blur notification asynchronously.
// After this call, Destroy() or Unlink() will stop observing the content
// and forget everything. Therefore, if it's not safe to send notification
// when script blocker is unlocked, we cannot send blur notification after
// that and before next focus notification.
// Anyway, as far as we know, IME doesn't try to query content when it loses
// focus. So, this may not cause any problem.
mIMEHasFocus = false;
IMEStateManager::NotifyIME(IMENotification(NOTIFY_IME_OF_BLUR), widget);
MOZ_LOG(sIMECOLog, LogLevel::Debug,
("0x%p NotifyIMEOfBlur(), sent NOTIFY_IME_OF_BLUR", this));
}
void IMEContentObserver::UnregisterObservers() {
if (!mIsObserving) {
return;
}
MOZ_LOG(sIMECOLog, LogLevel::Info,
("0x%p UnregisterObservers(), stop observing 0x%p (%s)", this,
mRootElement.get(),
mRootElement ? ToString(*mRootElement).c_str() : "nullptr"));
mIsObserving = false;
if (mEditorBase) {
mEditorBase->SetIMEContentObserver(nullptr);
}
if (mSelection) {
mSelectionData.Clear();
mFocusedWidget = nullptr;
}
if (mRootElement) {
mRootElement->RemoveMutationObserver(this);
}
if (mDocumentObserver) {
RefPtr<DocumentObserver> documentObserver = mDocumentObserver;
documentObserver->StopObserving();
}
if (mDocShell) {
mDocShell->RemoveWeakScrollObserver(this);
mDocShell->RemoveWeakReflowObserver(this);
}
}
nsPresContext* IMEContentObserver::GetPresContext() const {
return mESM ? mESM->GetPresContext() : nullptr;
}
void IMEContentObserver::Destroy() {
// WARNING: When you change this method, you have to check Unlink() too.
// Note that don't send any notifications later from here. I.e., notify
// IMEStateManager of the blur synchronously because IMEStateManager needs to
// stop notifying the main process if this is requested by the main process.
NotifyIMEOfBlur();
UnregisterObservers();
Clear();
mWidget = nullptr;
mIMENotificationRequests = nullptr;
if (mESM) {
mESM->OnStopObservingContent(this);
mESM = nullptr;
}
}
bool IMEContentObserver::Destroyed() const { return !mWidget; }
void IMEContentObserver::DisconnectFromEventStateManager() { mESM = nullptr; }
bool IMEContentObserver::MaybeReinitialize(nsIWidget& aWidget,
nsPresContext& aPresContext,
Element* aElement,
EditorBase& aEditorBase) {
if (!IsObservingContent(aPresContext, aElement)) {
return false;
}
if (GetState() == eState_StoppedObserving) {
Init(aWidget, aPresContext, aElement, aEditorBase);
}
return IsObserving(aPresContext, aElement);
}
bool IMEContentObserver::IsObserving(const nsPresContext& aPresContext,
const Element* aElement) const {
if (GetState() != eState_Observing) {
return false;
}
// If aElement is not a text control, aElement is an editing host or entire
// the document is editable in the design mode. Therefore, return false if
// we're observing an anonymous subtree of a text control.
if (!aElement || !aElement->IsTextControlElement() ||
!static_cast<const TextControlElement*>(aElement)
->IsSingleLineTextControlOrTextArea()) {
if (mIsTextControl) {
return false;
}
}
// If aElement is a text control, return true if we're observing the anonymous
// subtree of aElement. Therefore, return false if we're observing with
// HTMLEditor.
else if (!mIsTextControl) {
return false;
}
return IsObservingContent(aPresContext, aElement);
}
bool IMEContentObserver::IsBeingInitializedFor(
const nsPresContext& aPresContext, const Element* aElement,
const EditorBase& aEditorBase) const {
return GetState() == eState_Initializing && mEditorBase == &aEditorBase &&
IsObservingContent(aPresContext, aElement);
}
bool IMEContentObserver::IsObserving(
const TextComposition& aTextComposition) const {
if (GetState() != eState_Observing) {
return false;
}
nsPresContext* const presContext = aTextComposition.GetPresContext();
if (NS_WARN_IF(!presContext)) {
return false;
}
if (presContext != GetPresContext()) {
return false; // observing different document
}
auto* const elementHavingComposition =
Element::FromNodeOrNull(aTextComposition.GetEventTargetNode());
bool isObserving = IsObservingContent(*presContext, elementHavingComposition);
#ifdef DEBUG
if (isObserving) {
if (mIsTextControl) {
MOZ_ASSERT(elementHavingComposition);
MOZ_ASSERT(elementHavingComposition->IsTextControlElement(),
"Should've never started to observe non-text-control element");
// XXX Our fake focus move has not been implemented properly. So, the
// following assertions may fail, but I don't like to make the failures
// cause crash even in debug builds because it may block developers to
// debug web-compat issues. On the other hand, it'd be nice if we can
// detect the bug with automated tests. Therefore, the following
// assertions are NS_ASSERTION.
NS_ASSERTION(static_cast<TextControlElement*>(elementHavingComposition)
->IsSingleLineTextControlOrTextArea(),
"Should've stopped observing when the type is changed");
NS_ASSERTION(!elementHavingComposition->IsInDesignMode(),
"Should've stopped observing when the design mode started");
} else if (elementHavingComposition) {
NS_ASSERTION(
!elementHavingComposition->IsTextControlElement() ||
!static_cast<TextControlElement*>(elementHavingComposition)
->IsSingleLineTextControlOrTextArea(),
"Should've never started to observe text-control element or "
"stopped observing it when the type is changed");
} else {
MOZ_ASSERT(presContext->GetPresShell());
MOZ_ASSERT(presContext->GetPresShell()->GetDocument());
NS_ASSERTION(
presContext->GetPresShell()->GetDocument()->IsInDesignMode(),
"Should be observing entire the document only in the design mode");
}
}
#endif // #ifdef DEBUG
return isObserving;
}
IMEContentObserver::State IMEContentObserver::GetState() const {
if (!mSelection || !mRootElement || !mEditableNode) {
return eState_NotObserving; // failed to initialize or finalized.
}
if (!mRootElement->IsInComposedDoc()) {
// the focused editor has already been reframed.
return eState_StoppedObserving;
}
return mIsObserving ? eState_Observing : eState_Initializing;
}
bool IMEContentObserver::IsObservingContent(const nsPresContext& aPresContext,
const Element* aElement) const {
return mEditableNode ==
IMEStateManager::GetRootEditableNode(aPresContext, aElement);
}
bool IMEContentObserver::IsEditorHandlingEventForComposition() const {
if (!mWidget) {
return false;
}
RefPtr<TextComposition> composition =
IMEStateManager::GetTextCompositionFor(mWidget);
if (!composition) {
return false;
}
return composition->EditorIsHandlingLatestChange();
}
bool IMEContentObserver::IsEditorComposing() const {
// Note that don't use TextComposition here. The important thing is,
// whether the editor already started to handle composition because
// web contents can change selection, text content and/or something from
// compositionstart event listener which is run before EditorBase handles it.
if (NS_WARN_IF(!mEditorBase)) {
return false;
}
return mEditorBase->IsIMEComposing();
}
nsresult IMEContentObserver::GetSelectionAndRoot(Selection** aSelection,
Element** aRootElement) const {
if (!mEditableNode || !mSelection) {
return NS_ERROR_NOT_AVAILABLE;
}
NS_ASSERTION(mSelection && mRootElement, "uninitialized content observer");
NS_ADDREF(*aSelection = mSelection);
NS_ADDREF(*aRootElement = mRootElement);
return NS_OK;
}
void IMEContentObserver::OnSelectionChange(Selection& aSelection) {
if (!mIsObserving) {
return;
}
if (mWidget) {
bool causedByComposition = IsEditorHandlingEventForComposition();
bool causedBySelectionEvent = TextComposition::IsHandlingSelectionEvent();
bool duringComposition = IsEditorComposing();
MaybeNotifyIMEOfSelectionChange(causedByComposition, causedBySelectionEvent,
duringComposition);
}
}
void IMEContentObserver::ScrollPositionChanged() {
if (!NeedsPositionChangeNotification()) {
return;
}
MaybeNotifyIMEOfPositionChange();
}
NS_IMETHODIMP
IMEContentObserver::Reflow(DOMHighResTimeStamp aStart,
DOMHighResTimeStamp aEnd) {
if (!NeedsPositionChangeNotification()) {
return NS_OK;
}
MaybeNotifyIMEOfPositionChange();
return NS_OK;
}
NS_IMETHODIMP
IMEContentObserver::ReflowInterruptible(DOMHighResTimeStamp aStart,
DOMHighResTimeStamp aEnd) {
if (!NeedsPositionChangeNotification()) {
return NS_OK;
}
MaybeNotifyIMEOfPositionChange();
return NS_OK;
}
nsresult IMEContentObserver::HandleQueryContentEvent(
WidgetQueryContentEvent* aEvent) {
// If the instance has normal selection cache and the query event queries
// normal selection's range, it should use the cached selection which was
// sent to the widget. However, if this instance has already received new
// selection change notification but hasn't updated the cache yet (i.e.,
// not sending selection change notification to IME, don't use the cached
// value. Note that don't update selection cache here since if you update
// selection cache here, IMENotificationSender won't notify IME of selection
// change because it looks like that the selection isn't actually changed.
const bool isSelectionCacheAvailable = aEvent->mUseNativeLineBreak &&
mSelectionData.IsInitialized() &&
!mNeedsToNotifyIMEOfSelectionChange;
if (isSelectionCacheAvailable && aEvent->mMessage == eQuerySelectedText &&
aEvent->mInput.mSelectionType == SelectionType::eNormal) {
aEvent->EmplaceReply();
if (mSelectionData.HasRange()) {
aEvent->mReply->mOffsetAndData.emplace(mSelectionData.mOffset,
mSelectionData.String(),
OffsetAndDataFor::SelectedString);
aEvent->mReply->mReversed = mSelectionData.mReversed;
}
aEvent->mReply->mContentsRoot = mRootElement;
aEvent->mReply->mWritingMode = mSelectionData.GetWritingMode();
// The selection cache in IMEContentObserver must always have been in
// an editing host (or an editable anonymous <div> element). Therefore,
// we set mIsEditableContent to true here even though it's already been
// blurred or changed its editable state but the selection cache has not
// been invalidated yet.
aEvent->mReply->mIsEditableContent = true;
MOZ_LOG(sIMECOLog, LogLevel::Debug,
("0x%p HandleQueryContentEvent(aEvent={ "
"mMessage=%s, mReply=%s })",
this, ToChar(aEvent->mMessage), ToString(aEvent->mReply).c_str()));
return NS_OK;
}
MOZ_LOG(sIMECOLog, LogLevel::Info,
("0x%p HandleQueryContentEvent(aEvent={ mMessage=%s })", this,
ToChar(aEvent->mMessage)));
// If we can make the event's input offset absolute with TextComposition or
// mSelection, we should set it here for reducing the cost of computing
// selection start offset. If ContentEventHandler receives a
// WidgetQueryContentEvent whose input offset is relative to insertion point,
// it computes current selection start offset (this may be expensive) and
// make the offset absolute value itself.
// Note that calling MakeOffsetAbsolute() makes the event a query event with
// absolute offset. So, ContentEventHandler doesn't pay any additional cost
// after calling MakeOffsetAbsolute() here.
if (aEvent->mInput.mRelativeToInsertionPoint &&
aEvent->mInput.IsValidEventMessage(aEvent->mMessage)) {
RefPtr<TextComposition> composition =
IMEStateManager::GetTextCompositionFor(aEvent->mWidget);
if (composition) {
uint32_t compositionStart = composition->NativeOffsetOfStartComposition();
if (NS_WARN_IF(!aEvent->mInput.MakeOffsetAbsolute(compositionStart))) {
return NS_ERROR_FAILURE;
}
} else if (isSelectionCacheAvailable && mSelectionData.HasRange()) {
const uint32_t selectionStart = mSelectionData.mOffset;
if (NS_WARN_IF(!aEvent->mInput.MakeOffsetAbsolute(selectionStart))) {
return NS_ERROR_FAILURE;
}
}
}
AutoRestore<bool> handling(mIsHandlingQueryContentEvent);
mIsHandlingQueryContentEvent = true;
ContentEventHandler handler(GetPresContext());
nsresult rv = handler.HandleQueryContentEvent(aEvent);
if (NS_WARN_IF(Destroyed())) {
// If this has already destroyed during querying the content, the query
// is outdated even if it's succeeded. So, make the query fail.
aEvent->mReply.reset();
MOZ_LOG(sIMECOLog, LogLevel::Warning,
("0x%p HandleQueryContentEvent(), WARNING, "
"IMEContentObserver has been destroyed during the query, "
"making the query fail",
this));
return rv;
}
if (aEvent->Succeeded() &&
NS_WARN_IF(aEvent->mReply->mContentsRoot != mRootElement)) {
// Focus has changed unexpectedly, so make the query fail.
aEvent->mReply.reset();
}
return rv;
}
nsresult IMEContentObserver::MaybeHandleSelectionEvent(
nsPresContext* aPresContext, WidgetSelectionEvent* aEvent) {
MOZ_ASSERT(aEvent);
MOZ_ASSERT(aEvent->mMessage == eSetSelection);
NS_ASSERTION(!mNeedsToNotifyIMEOfSelectionChange,
"Selection cache has not been updated yet");
MOZ_LOG(
sIMECOLog, LogLevel::Debug,
("0x%p MaybeHandleSelectionEvent(aEvent={ "
"mMessage=%s, mOffset=%u, mLength=%u, mReversed=%s, "
"mExpandToClusterBoundary=%s, mUseNativeLineBreak=%s }), "
"mSelectionData=%s",
this, ToChar(aEvent->mMessage), aEvent->mOffset, aEvent->mLength,
ToChar(aEvent->mReversed), ToChar(aEvent->mExpandToClusterBoundary),
ToChar(aEvent->mUseNativeLineBreak), ToString(mSelectionData).c_str()));
// When we have Selection cache, and the caller wants to set same selection
// range, we shouldn't try to compute same range because it may be impossible
// if the range boundary is around element boundaries which won't be
// serialized with line breaks like close tags of inline elements. In that
// case, inserting new text at different point may be different from intention
// of users or web apps which set current selection.
// FIXME: We cache only selection data computed with native line breaker
// lengths. Perhaps, we should improve the struct to have both data of
// offset and length. E.g., adding line break counts for both offset and
// length.
if (!mNeedsToNotifyIMEOfSelectionChange && aEvent->mUseNativeLineBreak &&
mSelectionData.IsInitialized() && mSelectionData.HasRange() &&
mSelectionData.StartOffset() == aEvent->mOffset &&
mSelectionData.Length() == aEvent->mLength) {
if (RefPtr<Selection> selection = mSelection) {
selection->ScrollIntoView(nsISelectionController::SELECTION_FOCUS_REGION);
}
aEvent->mSucceeded = true;
return NS_OK;
}
ContentEventHandler handler(aPresContext);
return handler.OnSelectionEvent(aEvent);
}
bool IMEContentObserver::OnMouseButtonEvent(nsPresContext& aPresContext,
WidgetMouseEvent& aMouseEvent) {
if (!mIMENotificationRequests ||
!mIMENotificationRequests->WantMouseButtonEventOnChar()) {
return false;
}
if (!aMouseEvent.IsTrusted() || aMouseEvent.DefaultPrevented() ||
!aMouseEvent.mWidget) {
return false;
}
// Now, we need to notify only mouse down and mouse up event.
switch (aMouseEvent.mMessage) {
case eMouseUp:
case eMouseDown:
break;
default:
return false;
}
if (NS_WARN_IF(!mWidget) || NS_WARN_IF(mWidget->Destroyed())) {
return false;
}
WidgetQueryContentEvent queryCharAtPointEvent(true, eQueryCharacterAtPoint,
aMouseEvent.mWidget);
queryCharAtPointEvent.mRefPoint = aMouseEvent.mRefPoint;
ContentEventHandler handler(&aPresContext);
handler.OnQueryCharacterAtPoint(&queryCharAtPointEvent);
if (NS_WARN_IF(queryCharAtPointEvent.Failed()) ||
queryCharAtPointEvent.DidNotFindChar()) {
return false;
}
// The widget might be destroyed during querying the content since it
// causes flushing layout.
if (!mWidget || NS_WARN_IF(mWidget->Destroyed())) {
return false;
}
// The result character rect is relative to the top level widget.
// We should notify it with offset in the widget.
nsIWidget* topLevelWidget = mWidget->GetTopLevelWidget();
if (topLevelWidget && topLevelWidget != mWidget) {
queryCharAtPointEvent.mReply->mRect.MoveBy(
topLevelWidget->WidgetToScreenOffset() -
mWidget->WidgetToScreenOffset());
}
// The refPt is relative to its widget.
// We should notify it with offset in the widget.
if (aMouseEvent.mWidget != mWidget) {
queryCharAtPointEvent.mRefPoint +=
aMouseEvent.mWidget->WidgetToScreenOffset() -
mWidget->WidgetToScreenOffset();
}
IMENotification notification(NOTIFY_IME_OF_MOUSE_BUTTON_EVENT);
notification.mMouseButtonEventData.mEventMessage = aMouseEvent.mMessage;
notification.mMouseButtonEventData.mOffset =
queryCharAtPointEvent.mReply->StartOffset();
notification.mMouseButtonEventData.mCursorPos =
queryCharAtPointEvent.mRefPoint;
notification.mMouseButtonEventData.mCharRect =
queryCharAtPointEvent.mReply->mRect;
notification.mMouseButtonEventData.mButton = aMouseEvent.mButton;
notification.mMouseButtonEventData.mButtons = aMouseEvent.mButtons;
notification.mMouseButtonEventData.mModifiers = aMouseEvent.mModifiers;
nsresult rv = IMEStateManager::NotifyIME(notification, mWidget);
if (NS_WARN_IF(NS_FAILED(rv))) {
return false;
}
bool consumed = (rv == NS_SUCCESS_EVENT_CONSUMED);
if (consumed) {
aMouseEvent.PreventDefault();
}
return consumed;
}
void IMEContentObserver::CharacterDataWillChange(
nsIContent* aContent, const CharacterDataChangeInfo& aInfo) {
if (!aContent->IsText()) {
return; // Ignore if it's a comment node or something other invisible data
// node.
}
MOZ_ASSERT(mPreCharacterDataChangeLength < 0,
"CharacterDataChanged() should've reset "
"mPreCharacterDataChangeLength");
if (!NeedsTextChangeNotification() ||
!nsContentUtils::IsInSameAnonymousTree(mRootElement, aContent)) {
return;
}
mEndOfAddedTextCache.Clear(__FUNCTION__);
mStartOfRemovingTextRangeCache.Clear(__FUNCTION__);
// Although we don't assume this change occurs while this is storing
// the range of added consecutive nodes, if it actually happens, we need to
// flush them since this change may occur before or in the range. So, it's
// safe to flush pending computation of mTextChangeData before handling this.
if (mAddedContentCache.HasCache()) {
NotifyIMEOfCachedConsecutiveNewNodes(__FUNCTION__);
}
mPreCharacterDataChangeLength = ContentEventHandler::GetNativeTextLength(
*aContent->AsText(), aInfo.mChangeStart, aInfo.mChangeEnd);
MOZ_ASSERT(
mPreCharacterDataChangeLength >= aInfo.mChangeEnd - aInfo.mChangeStart,
"The computed length must be same as or larger than XP length");
}
void IMEContentObserver::CharacterDataChanged(
nsIContent* aContent, const CharacterDataChangeInfo& aInfo) {
if (!aContent->IsText()) {
return; // Ignore if it's a comment node or something other invisible data
// node.
}
// Let TextComposition have a change to update composition string range in
// the text node if the change is caused by the web apps.
if (mWidget && !IsEditorHandlingEventForComposition()) {
if (RefPtr<TextComposition> composition =
IMEStateManager::GetTextCompositionFor(mWidget)) {
composition->OnCharacterDataChanged(*aContent->AsText(), aInfo);
}
}
if (!NeedsTextChangeNotification() ||
!nsContentUtils::IsInSameAnonymousTree(mRootElement, aContent)) {
return;
}
if (mAddedContentCache.HasCache()) {
NotifyIMEOfCachedConsecutiveNewNodes(__FUNCTION__);
}
mEndOfAddedTextCache.Clear(__FUNCTION__);
mStartOfRemovingTextRangeCache.Clear(__FUNCTION__);
MOZ_ASSERT(
!mAddedContentCache.HasCache(),
"The stored range should be flushed before actually the data is changed");
int64_t removedLength = mPreCharacterDataChangeLength;
mPreCharacterDataChangeLength = -1;
MOZ_ASSERT(removedLength >= 0,
"mPreCharacterDataChangeLength should've been set by "
"CharacterDataWillChange()");
uint32_t offset = 0;
if (mIsTextControl) {
// If we're observing a text control, mRootElement is the anonymous <div>
// element which has only one text node and/or invisible <br> element.
// TextEditor assumes this structure when it handles editing commands.
// Therefore, it's safe to assume same things here.
MOZ_ASSERT(mRootElement->GetFirstChild() == aContent);
if (aInfo.mChangeStart) {
offset = ContentEventHandler::GetNativeTextLength(*aContent->AsText(), 0,
aInfo.mChangeStart);
}
} else {
nsresult rv = ContentEventHandler::GetFlatTextLengthInRange(
RawNodePosition::BeforeFirstContentOf(*mRootElement),
RawNodePosition(aContent, aInfo.mChangeStart), mRootElement, &offset,
LINE_BREAK_TYPE_NATIVE);
if (NS_WARN_IF(NS_FAILED(rv))) {
return;
}
}
uint32_t newLength = ContentEventHandler::GetNativeTextLength(
*aContent->AsText(), aInfo.mChangeStart,
aInfo.mChangeStart + aInfo.mReplaceLength);
uint32_t oldEnd = offset + static_cast<uint32_t>(removedLength);
uint32_t newEnd = offset + newLength;
TextChangeData data(offset, oldEnd, newEnd,
IsEditorHandlingEventForComposition(),
IsEditorComposing());
MaybeNotifyIMEOfTextChange(data);
}
void IMEContentObserver::ContentAdded(nsINode* aContainer,
nsIContent* aFirstContent,
nsIContent* aLastContent) {
if (!NeedsTextChangeNotification() ||
!nsContentUtils::IsInSameAnonymousTree(mRootElement, aFirstContent)) {
return;
}
// We can skip everything when a padding <br> element is added since its text
// length is 0.
if (aFirstContent == aLastContent) {
if (const auto* brElement = HTMLBRElement::FromNode(aFirstContent)) {
if (MOZ_LIKELY(!brElement->HasChildNodes()) &&
(brElement->IsPaddingForEmptyEditor() ||
brElement->IsPaddingForEmptyLastLine())) {
return;
}
}
}
MOZ_ASSERT(IsInDocumentChange());
MOZ_ASSERT_IF(aFirstContent, aFirstContent->GetParentNode() == aContainer);
MOZ_ASSERT_IF(aLastContent, aLastContent->GetParentNode() == aContainer);
// While a document change, new nodes should be added consecutively in a
// container node. Therefore, we can cache the first added node and the last
// added node until ending the document change at least. Then, we can avoid
// to compute first added node offset in the flattened text repeatedly.
bool needToCache = true;
if (mAddedContentCache.HasCache()) {
MOZ_DIAGNOSTIC_ASSERT(aFirstContent->GetParentNode() ==
aLastContent->GetParentNode());
if (mAddedContentCache.IsInRange(*aFirstContent, mRootElement)) {
// The new content nodes are in the range, we can include their text
// length when we flush the cached range later. Therefore, we need to
// do nothing in this case.
needToCache = false;
MOZ_LOG(sCacheLog, LogLevel::Info,
("ContentAdded: mAddedContentCache already caches the give "
"content nodes"));
MOZ_ASSERT(mAddedContentCache.IsInRange(*aLastContent, mRootElement));
}
// When new nodes are inserted in a different container, let's flush the
// preceding content first. Then, we should restart to cache the new
// inserted nodes.
else if (!mAddedContentCache.CanMergeWith(*aFirstContent, *aLastContent,
mRootElement)) {
MOZ_LOG(sCacheLog, LogLevel::Info,
("ContentAdded: mAddedContentCache was cached not in current "
"document change and new content nodes cannot be merged"));
mEndOfAddedTextCache.Clear(__FUNCTION__);
mStartOfRemovingTextRangeCache.Clear(__FUNCTION__);
OffsetAndLengthAdjustments differences;
Result<std::pair<uint32_t, uint32_t>, nsresult> offsetAndLength =
mAddedContentCache.ComputeFlatTextRangeBeforeInsertingNewContent(
*aFirstContent, *aLastContent, mRootElement, differences);
if (NS_WARN_IF(offsetAndLength.isErr())) {
MOZ_LOG(sCacheLog, LogLevel::Error,
("ContentAdded: "
"AddedContentCache::"
"ComputeFlatTextRangeExcludingInsertingNewContent() failed"));
mAddedContentCache.Clear(__FUNCTION__);
return;
}
NotifyIMEOfCachedConsecutiveNewNodes(
__FUNCTION__, Some(offsetAndLength.inspect().first),
Some(offsetAndLength.inspect().second), differences);
mAddedContentCache.Clear(__FUNCTION__);
}
}
mEndOfAddedTextCache.ContentAdded(__FUNCTION__, *aFirstContent, *aLastContent,
Nothing(), mRootElement);
mStartOfRemovingTextRangeCache.ContentAdded(
__FUNCTION__, *aFirstContent, *aLastContent, Nothing(), mRootElement);
if (!needToCache) {
return;
}
// Okay, now, we can start to cache new nodes or merge the range of new
// nodes with the cached range.
if (!mAddedContentCache.TryToCache(*aFirstContent, *aLastContent,
mRootElement)) {
// Flush the old range first.
MOZ_LOG(sCacheLog, LogLevel::Info,
("ContentAdded: called during a document change flushed "
"previous added nodes (aFirstContent=%s, aLastContent=%s)",
ToString(RefPtr<nsINode>(aFirstContent)).c_str(),
ToString(RefPtr<nsINode>(aLastContent)).c_str()));
NotifyIMEOfCachedConsecutiveNewNodes(__FUNCTION__);
MOZ_ASSERT(!mAddedContentCache.HasCache());
MOZ_ALWAYS_TRUE(mAddedContentCache.TryToCache(*aFirstContent, *aLastContent,
mRootElement));
}
}
void IMEContentObserver::NotifyIMEOfCachedConsecutiveNewNodes(
const char* aCallerName,
const Maybe<uint32_t>& aOffsetOfFirstContent /* = Nothing() */,
const Maybe<uint32_t>& aLengthOfContentNNodes /* = Nothing() */,
const OffsetAndLengthAdjustments& aAdjustments /* = Nothing() */) {
MOZ_ASSERT(mAddedContentCache.HasCache());
MOZ_LOG(sIMECOLog, LogLevel::Debug,
("0x%p "
"IMEContentObserver::NotifyIMEOfCachedConsecutiveNewNodes(), "
"flushing stored consecutive nodes",
this));
MOZ_LOG(sCacheLog, LogLevel::Info,
("NotifyIMEOfCachedConsecutiveNewNodes: called by %s "
"(mAddedContentCache=%s)",
aCallerName, ToString(mAddedContentCache).c_str()));
// If 2 <div> elements are inserted into the DOM, we wan't the text length
// from start of the first <div> (including line break caused by its open
// tag) to end of the second <div>. I.e., we want to compute:
// ...{<div>.....</div><div>......</div>}...
// ^ ^ ^ ^
// | mFirst | |
// | mLast |
// offset (offset + length)
Maybe<uint32_t> offset =
aOffsetOfFirstContent.isSome()
? aOffsetOfFirstContent
: mEndOfAddedTextCache.GetFlatTextLengthBeforeContent(
*mAddedContentCache.mFirst,
mAddedContentCache.mFirst->GetPreviousSibling(), mRootElement);
if (offset.isNothing()) {
Result<uint32_t, nsresult> textLengthBeforeFirstContentOrError =
FlatTextCache::ComputeTextLengthBeforeContent(
*mAddedContentCache.mFirst, mRootElement);
if (NS_WARN_IF(textLengthBeforeFirstContentOrError.isErr())) {
mEndOfAddedTextCache.Clear(__FUNCTION__);
mStartOfRemovingTextRangeCache.Clear(__FUNCTION__);
MOZ_LOG(
sCacheLog, LogLevel::Error,
("NotifyContentAdded: failed to compute text length before mFirst"));
mAddedContentCache.Clear(__FUNCTION__);
return;
}
offset = Some(textLengthBeforeFirstContentOrError.unwrap());
}
Maybe<uint32_t> length = aLengthOfContentNNodes;
if (aLengthOfContentNNodes.isNothing()) {
Result<uint32_t, nsresult> addingLengthOrError =
FlatTextCache::ComputeTextLengthStartOfContentToEndOfContent(
*mAddedContentCache.mFirst, *mAddedContentCache.mLast,
mRootElement);
if (NS_WARN_IF(addingLengthOrError.isErr())) {
mEndOfAddedTextCache.Clear(__FUNCTION__);
mStartOfRemovingTextRangeCache.Clear(__FUNCTION__);
MOZ_LOG(sCacheLog, LogLevel::Error,
("NotifyContentAdded: failed to compute text length of added"));
mAddedContentCache.Clear(__FUNCTION__);
return;
}
length = Some(addingLengthOrError.inspect());
}
// If multiple lines are being inserted in an HTML editor, next call of
// NotifyContentAdded() is for adding next node. Therefore, caching the text
// length can skip to compute the text length before the adding node and
// before of it.
mEndOfAddedTextCache.CacheFlatTextLengthBeforeEndOfContent(
__FUNCTION__, *mAddedContentCache.mLast,
aAdjustments.AdjustedEndOffset(*offset + *length), mRootElement);
mStartOfRemovingTextRangeCache.ContentAdded(
__FUNCTION__, *mAddedContentCache.mFirst, *mAddedContentCache.mLast,
Some(aAdjustments.AdjustedEndOffset(*offset + *length)), mRootElement);
mAddedContentCache.Clear(__FUNCTION__);
if (*length == 0u) {
return;
}