Source code

Revision control

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 "TextControlState.h"
#include "mozilla/TextInputListener.h"
#include "nsCOMPtr.h"
#include "nsView.h"
#include "nsCaret.h"
#include "nsLayoutCID.h"
#include "nsITextControlFrame.h"
#include "nsContentCreatorFunctions.h"
#include "nsTextControlFrame.h"
#include "nsIControllers.h"
#include "nsIControllerContext.h"
#include "nsAttrValue.h"
#include "nsAttrValueInlines.h"
#include "nsGenericHTMLElement.h"
#include "nsIDOMEventListener.h"
#include "nsIWidget.h"
#include "nsIDocumentEncoder.h"
#include "nsPIDOMWindow.h"
#include "nsServiceManagerUtils.h"
#include "mozilla/dom/Selection.h"
#include "mozilla/EventListenerManager.h"
#include "nsContentUtils.h"
#include "mozilla/Preferences.h"
#include "nsTextNode.h"
#include "nsIController.h"
#include "mozilla/AutoRestore.h"
#include "mozilla/InputEventOptions.h"
#include "mozilla/PresShell.h"
#include "mozilla/TextEvents.h"
#include "mozilla/dom/Event.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/dom/HTMLInputElement.h"
#include "mozilla/dom/HTMLTextAreaElement.h"
#include "mozilla/dom/Text.h"
#include "mozilla/StaticPrefs_dom.h"
#include "nsFrameSelection.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/Telemetry.h"
#include "mozilla/ShortcutKeys.h"
#include "mozilla/KeyEventHandler.h"
#include "mozilla/dom/KeyboardEvent.h"
#include "mozilla/ScrollTypes.h"
namespace mozilla {
using namespace dom;
/*****************************************************************************
* TextControlElement
*****************************************************************************/
NS_IMPL_CYCLE_COLLECTION_CLASS(TextControlElement)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(
TextControlElement, nsGenericHTMLFormElementWithState)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(
TextControlElement, nsGenericHTMLFormElementWithState)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(
TextControlElement, nsGenericHTMLFormElementWithState)
/*static*/
bool TextControlElement::GetWrapPropertyEnum(
nsIContent* aContent, TextControlElement::nsHTMLTextWrap& aWrapProp) {
// soft is the default; "physical" defaults to soft as well because all other
// browsers treat it that way and there is no real reason to maintain physical
// and virtual as separate entities if no one else does. Only hard and off
// do anything different.
aWrapProp = eHTMLTextWrap_Soft; // the default
if (!aContent->IsHTMLElement()) {
return false;
}
static mozilla::dom::Element::AttrValuesArray strings[] = {
nsGkAtoms::HARD, nsGkAtoms::OFF, nullptr};
switch (aContent->AsElement()->FindAttrValueIn(
kNameSpaceID_None, nsGkAtoms::wrap, strings, eIgnoreCase)) {
case 0:
aWrapProp = eHTMLTextWrap_Hard;
break;
case 1:
aWrapProp = eHTMLTextWrap_Off;
break;
}
return true;
}
/*static*/
already_AddRefed<TextControlElement>
TextControlElement::GetTextControlElementFromEditingHost(nsIContent* aHost) {
if (!aHost) {
return nullptr;
}
RefPtr<TextControlElement> parent =
TextControlElement::FromNodeOrNull(aHost->GetParent());
return parent.forget();
}
using ValueChangeKind = TextControlElement::ValueChangeKind;
inline nsresult SetEditorFlagsIfNecessary(EditorBase& aEditorBase,
uint32_t aFlags) {
if (aEditorBase.Flags() == aFlags) {
return NS_OK;
}
return aEditorBase.SetFlags(aFlags);
}
/*****************************************************************************
* mozilla::AutoInputEventSuppresser
*****************************************************************************/
class MOZ_STACK_CLASS AutoInputEventSuppresser final {
public:
explicit AutoInputEventSuppresser(TextEditor* aTextEditor)
: mTextEditor(aTextEditor),
// To protect against a reentrant call to SetValue, we check whether
// another SetValue is already happening for this editor. If it is,
// we must wait until we unwind to re-enable oninput events.
mOuterTransaction(aTextEditor->IsSuppressingDispatchingInputEvent()) {
MOZ_ASSERT(mTextEditor);
mTextEditor->SuppressDispatchingInputEvent(true);
}
~AutoInputEventSuppresser() {
mTextEditor->SuppressDispatchingInputEvent(mOuterTransaction);
}
private:
RefPtr<TextEditor> mTextEditor;
bool mOuterTransaction;
};
/*****************************************************************************
* mozilla::RestoreSelectionState
*****************************************************************************/
class RestoreSelectionState : public Runnable {
public:
RestoreSelectionState(TextControlState* aState, nsTextControlFrame* aFrame)
: Runnable("RestoreSelectionState"),
mFrame(aFrame),
mTextControlState(aState) {}
MOZ_CAN_RUN_SCRIPT_BOUNDARY NS_IMETHOD Run() override {
if (!mTextControlState) {
return NS_OK;
}
AutoHideSelectionChanges hideSelectionChanges(
mFrame->GetConstFrameSelection());
if (mFrame) {
// SetSelectionRange leads to
// Selection::AddRangeAndSelectFramesAndNotifyListeners which flushes
// Layout - need to block script to avoid nested PrepareEditor calls (bug
// 642800).
nsAutoScriptBlocker scriptBlocker;
TextControlState::SelectionProperties& properties =
mTextControlState->GetSelectionProperties();
if (properties.IsDirty()) {
mFrame->SetSelectionRange(properties.GetStart(), properties.GetEnd(),
properties.GetDirection());
}
}
if (mTextControlState) {
mTextControlState->FinishedRestoringSelection();
}
return NS_OK;
}
// Let the text editor tell us we're no longer relevant - avoids use of
// AutoWeakFrame
void Revoke() {
mFrame = nullptr;
mTextControlState = nullptr;
}
private:
nsTextControlFrame* mFrame;
TextControlState* mTextControlState;
};
/*****************************************************************************
* mozilla::AutoRestoreEditorState
*****************************************************************************/
class MOZ_RAII AutoRestoreEditorState final {
public:
explicit AutoRestoreEditorState(TextEditor* aTextEditor)
: mTextEditor(aTextEditor),
mSavedFlags(mTextEditor->Flags()),
mSavedMaxLength(mTextEditor->MaxTextLength()) {
MOZ_ASSERT(mTextEditor);
// EditorBase::SetFlags() is a virtual method. Even though it does nothing
// if new flags and current flags are same, the calling cost causes
// appearing the method in profile. So, this class should check if it's
// necessary to call.
uint32_t flags = mSavedFlags;
flags &= ~(nsIEditor::eEditorReadonlyMask);
flags |= nsIEditor::eEditorDontEchoPassword;
if (mSavedFlags != flags) {
mTextEditor->SetFlags(flags);
}
mTextEditor->SetMaxTextLength(-1);
}
~AutoRestoreEditorState() {
mTextEditor->SetMaxTextLength(mSavedMaxLength);
SetEditorFlagsIfNecessary(*mTextEditor, mSavedFlags);
}
private:
TextEditor* mTextEditor;
uint32_t mSavedFlags;
int32_t mSavedMaxLength;
};
/*****************************************************************************
* mozilla::AutoDisableUndo
*****************************************************************************/
class MOZ_RAII AutoDisableUndo final {
public:
explicit AutoDisableUndo(TextEditor* aTextEditor)
: mTextEditor(aTextEditor), mNumberOfMaximumTransactions(0) {
MOZ_ASSERT(mTextEditor);
mNumberOfMaximumTransactions =
mTextEditor ? mTextEditor->NumberOfMaximumTransactions() : 0;
DebugOnly<bool> disabledUndoRedo = mTextEditor->DisableUndoRedo();
NS_WARNING_ASSERTION(disabledUndoRedo,
"Failed to disable undo/redo transactions");
}
~AutoDisableUndo() {
// Don't change enable/disable of undo/redo if it's enabled after
// it's disabled by the constructor because we shouldn't change
// the maximum undo/redo count to the old value.
if (mTextEditor->IsUndoRedoEnabled()) {
return;
}
// If undo/redo was enabled, mNumberOfMaximumTransactions is -1 or lager
// than 0. Only when it's 0, it was disabled.
if (mNumberOfMaximumTransactions) {
DebugOnly<bool> enabledUndoRedo =
mTextEditor->EnableUndoRedo(mNumberOfMaximumTransactions);
NS_WARNING_ASSERTION(enabledUndoRedo,
"Failed to enable undo/redo transactions");
} else {
DebugOnly<bool> disabledUndoRedo = mTextEditor->DisableUndoRedo();
NS_WARNING_ASSERTION(disabledUndoRedo,
"Failed to disable undo/redo transactions");
}
}
private:
TextEditor* mTextEditor;
int32_t mNumberOfMaximumTransactions;
};
static bool SuppressEventHandlers(nsPresContext* aPresContext) {
bool suppressHandlers = false;
if (aPresContext) {
// Right now we only suppress event handlers and controller manipulation
// when in a print preview or print context!
// In the current implementation, we only paginate when
// printing or in print preview.
suppressHandlers = aPresContext->IsPaginated();
}
return suppressHandlers;
}
/*****************************************************************************
* mozilla::TextInputSelectionController
*****************************************************************************/
class TextInputSelectionController final : public nsSupportsWeakReference,
public nsISelectionController {
~TextInputSelectionController() = default;
public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(TextInputSelectionController,
nsISelectionController)
TextInputSelectionController(PresShell* aPresShell, nsIContent* aLimiter);
void SetScrollableFrame(nsIScrollableFrame* aScrollableFrame);
nsFrameSelection* GetConstFrameSelection() { return mFrameSelection; }
// Will return null if !mFrameSelection.
Selection* GetSelection(SelectionType aSelectionType);
// NSISELECTIONCONTROLLER INTERFACES
NS_IMETHOD SetDisplaySelection(int16_t toggle) override;
NS_IMETHOD GetDisplaySelection(int16_t* _retval) override;
NS_IMETHOD SetSelectionFlags(int16_t aInEnable) override;
NS_IMETHOD GetSelectionFlags(int16_t* aOutEnable) override;
NS_IMETHOD GetSelectionFromScript(RawSelectionType aRawSelectionType,
Selection** aSelection) override;
Selection* GetSelection(RawSelectionType aRawSelectionType) override;
NS_IMETHOD ScrollSelectionIntoView(RawSelectionType aRawSelectionType,
int16_t aRegion, int16_t aFlags) override;
NS_IMETHOD RepaintSelection(RawSelectionType aRawSelectionType) override;
nsresult RepaintSelection(nsPresContext* aPresContext,
SelectionType aSelectionType);
NS_IMETHOD SetCaretEnabled(bool enabled) override;
NS_IMETHOD SetCaretReadOnly(bool aReadOnly) override;
NS_IMETHOD GetCaretEnabled(bool* _retval) override;
NS_IMETHOD GetCaretVisible(bool* _retval) override;
NS_IMETHOD SetCaretVisibilityDuringSelection(bool aVisibility) override;
NS_IMETHOD PhysicalMove(int16_t aDirection, int16_t aAmount,
bool aExtend) override;
NS_IMETHOD CharacterMove(bool aForward, bool aExtend) override;
NS_IMETHOD WordMove(bool aForward, bool aExtend) override;
NS_IMETHOD LineMove(bool aForward, bool aExtend) override;
NS_IMETHOD IntraLineMove(bool aForward, bool aExtend) override;
MOZ_CAN_RUN_SCRIPT
NS_IMETHOD PageMove(bool aForward, bool aExtend) override;
NS_IMETHOD CompleteScroll(bool aForward) override;
NS_IMETHOD CompleteMove(bool aForward, bool aExtend) override;
NS_IMETHOD ScrollPage(bool aForward) override;
NS_IMETHOD ScrollLine(bool aForward) override;
NS_IMETHOD ScrollCharacter(bool aRight) override;
NS_IMETHOD SelectAll(void) override;
NS_IMETHOD CheckVisibility(nsINode* node, int16_t startOffset,
int16_t EndOffset, bool* _retval) override;
virtual nsresult CheckVisibilityContent(nsIContent* aNode,
int16_t aStartOffset,
int16_t aEndOffset,
bool* aRetval) override;
void SelectionWillTakeFocus() override;
void SelectionWillLoseFocus() override;
private:
RefPtr<nsFrameSelection> mFrameSelection;
nsIScrollableFrame* mScrollFrame;
nsWeakPtr mPresShellWeak;
};
NS_IMPL_CYCLE_COLLECTING_ADDREF(TextInputSelectionController)
NS_IMPL_CYCLE_COLLECTING_RELEASE(TextInputSelectionController)
NS_INTERFACE_TABLE_HEAD(TextInputSelectionController)
NS_INTERFACE_TABLE(TextInputSelectionController, nsISelectionController,
nsISelectionDisplay, nsISupportsWeakReference)
NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(TextInputSelectionController)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTION_WEAK(TextInputSelectionController, mFrameSelection)
TextInputSelectionController::TextInputSelectionController(
PresShell* aPresShell, nsIContent* aLimiter)
: mScrollFrame(nullptr) {
if (aPresShell) {
bool accessibleCaretEnabled =
PresShell::AccessibleCaretEnabled(aLimiter->OwnerDoc()->GetDocShell());
mFrameSelection =
new nsFrameSelection(aPresShell, aLimiter, accessibleCaretEnabled);
mPresShellWeak = do_GetWeakReference(aPresShell);
}
}
void TextInputSelectionController::SetScrollableFrame(
nsIScrollableFrame* aScrollableFrame) {
mScrollFrame = aScrollableFrame;
if (!mScrollFrame && mFrameSelection) {
mFrameSelection->DisconnectFromPresShell();
mFrameSelection = nullptr;
}
}
Selection* TextInputSelectionController::GetSelection(
SelectionType aSelectionType) {
if (!mFrameSelection) {
return nullptr;
}
return mFrameSelection->GetSelection(aSelectionType);
}
NS_IMETHODIMP
TextInputSelectionController::SetDisplaySelection(int16_t aToggle) {
if (!mFrameSelection) {
return NS_ERROR_NULL_POINTER;
}
mFrameSelection->SetDisplaySelection(aToggle);
return NS_OK;
}
NS_IMETHODIMP
TextInputSelectionController::GetDisplaySelection(int16_t* aToggle) {
if (!mFrameSelection) {
return NS_ERROR_NULL_POINTER;
}
*aToggle = mFrameSelection->GetDisplaySelection();
return NS_OK;
}
NS_IMETHODIMP
TextInputSelectionController::SetSelectionFlags(int16_t aToggle) {
return NS_OK; // stub this out. not used in input
}
NS_IMETHODIMP
TextInputSelectionController::GetSelectionFlags(int16_t* aOutEnable) {
*aOutEnable = nsISelectionDisplay::DISPLAY_TEXT;
return NS_OK;
}
NS_IMETHODIMP
TextInputSelectionController::GetSelectionFromScript(
RawSelectionType aRawSelectionType, Selection** aSelection) {
if (!mFrameSelection) {
return NS_ERROR_NULL_POINTER;
}
*aSelection =
mFrameSelection->GetSelection(ToSelectionType(aRawSelectionType));
// GetSelection() fails only when aRawSelectionType is invalid value.
if (!(*aSelection)) {
return NS_ERROR_INVALID_ARG;
}
NS_ADDREF(*aSelection);
return NS_OK;
}
Selection* TextInputSelectionController::GetSelection(
RawSelectionType aRawSelectionType) {
return GetSelection(ToSelectionType(aRawSelectionType));
}
NS_IMETHODIMP
TextInputSelectionController::ScrollSelectionIntoView(
RawSelectionType aRawSelectionType, int16_t aRegion, int16_t aFlags) {
if (!mFrameSelection) {
return NS_ERROR_NULL_POINTER;
}
RefPtr<nsFrameSelection> frameSelection = mFrameSelection;
return frameSelection->ScrollSelectionIntoView(
ToSelectionType(aRawSelectionType), aRegion, aFlags);
}
NS_IMETHODIMP
TextInputSelectionController::RepaintSelection(
RawSelectionType aRawSelectionType) {
if (!mFrameSelection) {
return NS_ERROR_NULL_POINTER;
}
RefPtr<nsFrameSelection> frameSelection = mFrameSelection;
return frameSelection->RepaintSelection(ToSelectionType(aRawSelectionType));
}
nsresult TextInputSelectionController::RepaintSelection(
nsPresContext* aPresContext, SelectionType aSelectionType) {
if (!mFrameSelection) {
return NS_ERROR_NULL_POINTER;
}
RefPtr<nsFrameSelection> frameSelection = mFrameSelection;
return frameSelection->RepaintSelection(aSelectionType);
}
NS_IMETHODIMP
TextInputSelectionController::SetCaretEnabled(bool enabled) {
if (!mPresShellWeak) {
return NS_ERROR_NOT_INITIALIZED;
}
RefPtr<PresShell> presShell = do_QueryReferent(mPresShellWeak);
if (!presShell) {
return NS_ERROR_FAILURE;
}
// tell the pres shell to enable the caret, rather than settings its
// visibility directly. this way the presShell's idea of caret visibility is
// maintained.
presShell->SetCaretEnabled(enabled);
return NS_OK;
}
NS_IMETHODIMP
TextInputSelectionController::SetCaretReadOnly(bool aReadOnly) {
if (!mPresShellWeak) {
return NS_ERROR_NOT_INITIALIZED;
}
nsresult rv;
RefPtr<PresShell> presShell = do_QueryReferent(mPresShellWeak, &rv);
if (!presShell) {
return NS_ERROR_FAILURE;
}
RefPtr<nsCaret> caret = presShell->GetCaret();
if (!caret) {
return NS_ERROR_FAILURE;
}
if (!mFrameSelection) {
return NS_ERROR_FAILURE;
}
Selection* selection = mFrameSelection->GetSelection(SelectionType::eNormal);
if (selection) {
caret->SetCaretReadOnly(aReadOnly);
}
return NS_OK;
}
NS_IMETHODIMP
TextInputSelectionController::GetCaretEnabled(bool* _retval) {
return GetCaretVisible(_retval);
}
NS_IMETHODIMP
TextInputSelectionController::GetCaretVisible(bool* _retval) {
if (!mPresShellWeak) {
return NS_ERROR_NOT_INITIALIZED;
}
nsresult rv;
RefPtr<PresShell> presShell = do_QueryReferent(mPresShellWeak, &rv);
if (!presShell) {
return NS_ERROR_FAILURE;
}
RefPtr<nsCaret> caret = presShell->GetCaret();
if (!caret) {
return NS_ERROR_FAILURE;
}
*_retval = caret->IsVisible();
return NS_OK;
}
NS_IMETHODIMP
TextInputSelectionController::SetCaretVisibilityDuringSelection(
bool aVisibility) {
if (!mPresShellWeak) {
return NS_ERROR_NOT_INITIALIZED;
}
nsresult rv;
RefPtr<PresShell> presShell = do_QueryReferent(mPresShellWeak, &rv);
if (!presShell) {
return NS_ERROR_FAILURE;
}
RefPtr<nsCaret> caret = presShell->GetCaret();
if (!caret) {
return NS_ERROR_FAILURE;
}
Selection* selection = mFrameSelection->GetSelection(SelectionType::eNormal);
if (selection) {
caret->SetVisibilityDuringSelection(aVisibility);
}
return NS_OK;
}
NS_IMETHODIMP
TextInputSelectionController::PhysicalMove(int16_t aDirection, int16_t aAmount,
bool aExtend) {
if (!mFrameSelection) {
return NS_ERROR_NULL_POINTER;
}
RefPtr<nsFrameSelection> frameSelection = mFrameSelection;
return frameSelection->PhysicalMove(aDirection, aAmount, aExtend);
}
NS_IMETHODIMP
TextInputSelectionController::CharacterMove(bool aForward, bool aExtend) {
if (!mFrameSelection) {
return NS_ERROR_NULL_POINTER;
}
RefPtr<nsFrameSelection> frameSelection = mFrameSelection;
return frameSelection->CharacterMove(aForward, aExtend);
}
NS_IMETHODIMP
TextInputSelectionController::WordMove(bool aForward, bool aExtend) {
if (!mFrameSelection) {
return NS_ERROR_NULL_POINTER;
}
RefPtr<nsFrameSelection> frameSelection = mFrameSelection;
return frameSelection->WordMove(aForward, aExtend);
}
NS_IMETHODIMP
TextInputSelectionController::LineMove(bool aForward, bool aExtend) {
if (!mFrameSelection) {
return NS_ERROR_NULL_POINTER;
}
RefPtr<nsFrameSelection> frameSelection = mFrameSelection;
nsresult result = frameSelection->LineMove(aForward, aExtend);
if (NS_FAILED(result)) {
result = CompleteMove(aForward, aExtend);
}
return result;
}
NS_IMETHODIMP
TextInputSelectionController::IntraLineMove(bool aForward, bool aExtend) {
if (!mFrameSelection) {
return NS_ERROR_NULL_POINTER;
}
RefPtr<nsFrameSelection> frameSelection = mFrameSelection;
return frameSelection->IntraLineMove(aForward, aExtend);
}
NS_IMETHODIMP
TextInputSelectionController::PageMove(bool aForward, bool aExtend) {
// expected behavior for PageMove is to scroll AND move the caret
// and to remain relative position of the caret in view. see Bug 4302.
if (mScrollFrame) {
RefPtr<nsFrameSelection> frameSelection = mFrameSelection;
nsIFrame* scrollFrame = do_QueryFrame(mScrollFrame);
// We won't scroll parent scrollable element of mScrollFrame. Therefore,
// this may be handled when mScrollFrame is completely outside of the view.
// In such case, user may be confused since they might have wanted to
// scroll a parent scrollable element. For making clearer which element
// handles PageDown/PageUp, we should move selection into view even if
// selection is not changed.
return frameSelection->PageMove(aForward, aExtend, scrollFrame,
nsFrameSelection::SelectionIntoView::Yes);
}
// Similarly, if there is no scrollable frame, we should move the editor
// frame into the view for making it clearer which element handles
// PageDown/PageUp.
return ScrollSelectionIntoView(
nsISelectionController::SELECTION_NORMAL,
nsISelectionController::SELECTION_FOCUS_REGION,
nsISelectionController::SCROLL_SYNCHRONOUS |
nsISelectionController::SCROLL_FOR_CARET_MOVE);
}
NS_IMETHODIMP
TextInputSelectionController::CompleteScroll(bool aForward) {
if (!mScrollFrame) {
return NS_ERROR_NOT_INITIALIZED;
}
mScrollFrame->ScrollBy(nsIntPoint(0, aForward ? 1 : -1), ScrollUnit::WHOLE,
ScrollMode::Instant);
return NS_OK;
}
NS_IMETHODIMP
TextInputSelectionController::CompleteMove(bool aForward, bool aExtend) {
if (NS_WARN_IF(!mFrameSelection)) {
return NS_ERROR_NULL_POINTER;
}
RefPtr<nsFrameSelection> frameSelection = mFrameSelection;
// grab the parent / root DIV for this text widget
nsIContent* parentDIV = frameSelection->GetLimiter();
if (!parentDIV) {
return NS_ERROR_UNEXPECTED;
}
// make the caret be either at the very beginning (0) or the very end
int32_t offset = 0;
CaretAssociationHint hint = CARET_ASSOCIATE_BEFORE;
if (aForward) {
offset = parentDIV->GetChildCount();
// Prevent the caret from being placed after the last
// BR node in the content tree!
if (offset > 0) {
nsIContent* child = parentDIV->GetLastChild();
if (child->IsHTMLElement(nsGkAtoms::br)) {
--offset;
hint = CARET_ASSOCIATE_AFTER; // for Bug 106855
}
}
}
const nsFrameSelection::FocusMode focusMode =
aExtend ? nsFrameSelection::FocusMode::kExtendSelection
: nsFrameSelection::FocusMode::kCollapseToNewPoint;
frameSelection->HandleClick(parentDIV, offset, offset, focusMode, hint);
// if we got this far, attempt to scroll no matter what the above result is
return CompleteScroll(aForward);
}
NS_IMETHODIMP
TextInputSelectionController::ScrollPage(bool aForward) {
if (!mScrollFrame) {
return NS_ERROR_NOT_INITIALIZED;
}
mScrollFrame->ScrollBy(nsIntPoint(0, aForward ? 1 : -1), ScrollUnit::PAGES,
ScrollMode::Smooth);
return NS_OK;
}
NS_IMETHODIMP
TextInputSelectionController::ScrollLine(bool aForward) {
if (!mScrollFrame) {
return NS_ERROR_NOT_INITIALIZED;
}
mScrollFrame->ScrollBy(nsIntPoint(0, aForward ? 1 : -1), ScrollUnit::LINES,
ScrollMode::Smooth);
return NS_OK;
}
NS_IMETHODIMP
TextInputSelectionController::ScrollCharacter(bool aRight) {
if (!mScrollFrame) {
return NS_ERROR_NOT_INITIALIZED;
}
mScrollFrame->ScrollBy(nsIntPoint(aRight ? 1 : -1, 0), ScrollUnit::LINES,
ScrollMode::Smooth);
return NS_OK;
}
NS_IMETHODIMP
TextInputSelectionController::SelectAll() {
if (!mFrameSelection) {
return NS_ERROR_NULL_POINTER;
}
RefPtr<nsFrameSelection> frameSelection = mFrameSelection;
return frameSelection->SelectAll();
}
void TextInputSelectionController::SelectionWillTakeFocus() {
if (mFrameSelection) {
if (PresShell* shell = mFrameSelection->GetPresShell()) {
shell->FrameSelectionWillTakeFocus(*mFrameSelection);
}
}
}
void TextInputSelectionController::SelectionWillLoseFocus() {
if (mFrameSelection) {
if (PresShell* shell = mFrameSelection->GetPresShell()) {
shell->FrameSelectionWillLoseFocus(*mFrameSelection);
}
}
}
NS_IMETHODIMP
TextInputSelectionController::CheckVisibility(nsINode* node,
int16_t startOffset,
int16_t EndOffset,
bool* _retval) {
if (!mPresShellWeak) {
return NS_ERROR_NOT_INITIALIZED;
}
nsresult rv;
nsCOMPtr<nsISelectionController> presShell =
do_QueryReferent(mPresShellWeak, &rv);
if (!presShell) {
return NS_ERROR_FAILURE;
}
return presShell->CheckVisibility(node, startOffset, EndOffset, _retval);
}
nsresult TextInputSelectionController::CheckVisibilityContent(
nsIContent* aNode, int16_t aStartOffset, int16_t aEndOffset,
bool* aRetval) {
if (!mPresShellWeak) {
return NS_ERROR_NOT_INITIALIZED;
}
nsCOMPtr<nsISelectionController> presShell = do_QueryReferent(mPresShellWeak);
if (NS_WARN_IF(!presShell)) {
return NS_ERROR_FAILURE;
}
return presShell->CheckVisibilityContent(aNode, aStartOffset, aEndOffset,
aRetval);
}
/*****************************************************************************
* mozilla::TextInputListener
*****************************************************************************/
TextInputListener::TextInputListener(TextControlElement* aTxtCtrlElement)
: mFrame(nullptr),
mTxtCtrlElement(aTxtCtrlElement),
mTextControlState(aTxtCtrlElement ? aTxtCtrlElement->GetTextControlState()
: nullptr),
mSelectionWasCollapsed(true),
mHadUndoItems(false),
mHadRedoItems(false),
mSettingValue(false),
mSetValueChanged(true),
mListeningToSelectionChange(false) {}
NS_IMPL_CYCLE_COLLECTING_ADDREF(TextInputListener)
NS_IMPL_CYCLE_COLLECTING_RELEASE(TextInputListener)
NS_INTERFACE_MAP_BEGIN(TextInputListener)
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_INTERFACE_MAP_ENTRY(nsIDOMEventListener)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMEventListener)
NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(TextInputListener)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTION_CLASS(TextInputListener)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(TextInputListener)
NS_IMPL_CYCLE_COLLECTION_UNLINK_WEAK_REFERENCE
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(TextInputListener)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
void TextInputListener::OnSelectionChange(Selection& aSelection,
int16_t aReason) {
if (!mListeningToSelectionChange) {
return;
}
AutoWeakFrame weakFrame = mFrame;
// Fire the select event
// The specs don't exactly say when we should fire the select event.
// IE: Whenever you add/remove a character to/from the selection. Also
// each time for select all. Also if you get to the end of the text
// field you will get new event for each keypress or a continuous
// stream of events if you use the mouse. IE will fire select event
// when the selection collapses to nothing if you are holding down
// the shift or mouse button.
// Mozilla: If we have non-empty selection we will fire a new event for each
// keypress (or mouseup) if the selection changed. Mozilla will also
// create the event each time select all is called, even if
// everything was previously selected, becase technically select all
// will first collapse and then extend. Mozilla will never create an
// event if the selection collapses to nothing.
bool collapsed = aSelection.IsCollapsed();
if (!collapsed && (aReason & (nsISelectionListener::MOUSEUP_REASON |
nsISelectionListener::KEYPRESS_REASON |
nsISelectionListener::SELECTALL_REASON))) {
if (nsCOMPtr<nsIContent> content = mFrame->GetContent()) {
if (nsCOMPtr<Document> doc = content->GetComposedDoc()) {
if (RefPtr<PresShell> presShell = doc->GetPresShell()) {
nsEventStatus status = nsEventStatus_eIgnore;
WidgetEvent event(true, eFormSelect);
presShell->HandleEventWithTarget(&event, mFrame, content, &status);
}
}
}
}
// if the collapsed state did not change, don't fire notifications
if (collapsed == mSelectionWasCollapsed) {
return;
}
mSelectionWasCollapsed = collapsed;
if (!weakFrame.IsAlive() || !mFrame ||
!nsContentUtils::IsFocusedContent(mFrame->GetContent())) {
return;
}
UpdateTextInputCommands(u"select"_ns, &aSelection, aReason);
}
MOZ_CAN_RUN_SCRIPT
static void DoCommandCallback(Command aCommand, void* aData) {
nsTextControlFrame* frame = static_cast<nsTextControlFrame*>(aData);
nsIContent* content = frame->GetContent();
nsCOMPtr<nsIControllers> controllers;
HTMLInputElement* input = HTMLInputElement::FromNode(content);
if (input) {
input->GetControllers(getter_AddRefs(controllers));
} else {
HTMLTextAreaElement* textArea = HTMLTextAreaElement::FromNode(content);
if (textArea) {
textArea->GetControllers(getter_AddRefs(controllers));
}
}
if (!controllers) {
NS_WARNING("Could not get controllers");
return;
}
const char* commandStr = WidgetKeyboardEvent::GetCommandStr(aCommand);
nsCOMPtr<nsIController> controller;
controllers->GetControllerForCommand(commandStr, getter_AddRefs(controller));
if (!controller) {
return;
}
bool commandEnabled;
if (NS_WARN_IF(NS_FAILED(
controller->IsCommandEnabled(commandStr, &commandEnabled)))) {
return;
}
if (commandEnabled) {
controller->DoCommand(commandStr);
}
}
NS_IMETHODIMP
TextInputListener::HandleEvent(Event* aEvent) {
if (aEvent->DefaultPrevented()) {
return NS_OK;
}
if (!aEvent->IsTrusted()) {
return NS_OK;
}
RefPtr<KeyboardEvent> keyEvent = aEvent->AsKeyboardEvent();
if (!keyEvent) {
return NS_ERROR_UNEXPECTED;
}
WidgetKeyboardEvent* widgetKeyEvent =
aEvent->WidgetEventPtr()->AsKeyboardEvent();
if (!widgetKeyEvent) {
return NS_ERROR_UNEXPECTED;
}
{
auto* input = HTMLInputElement::FromNode(mTxtCtrlElement);
if (input && input->StepsInputValue(*widgetKeyEvent)) {
// As an special case, don't handle key events that would step the value
// of our <input type=number>.
return NS_OK;
}
}
KeyEventHandler* keyHandlers = ShortcutKeys::GetHandlers(
mTxtCtrlElement->IsTextArea() ? HandlerType::eTextArea
: HandlerType::eInput);
RefPtr<nsAtom> eventTypeAtom =
ShortcutKeys::ConvertEventToDOMEventType(widgetKeyEvent);
for (KeyEventHandler* handler = keyHandlers; handler;
handler = handler->GetNextHandler()) {
if (!handler->EventTypeEquals(eventTypeAtom)) {
continue;
}
if (!handler->KeyEventMatched(keyEvent, 0, IgnoreModifierState())) {
continue;
}
// XXX Do we execute only one handler even if the handler neither stops
// propagation nor prevents default of the event?
RefPtr<TextControlElement> textControlElement(mTxtCtrlElement);
nsresult rv = handler->ExecuteHandler(textControlElement, aEvent);
if (NS_SUCCEEDED(rv)) {
return rv;
}
}
if (widgetKeyEvent->mMessage != eKeyPress) {
return NS_OK;
}
nsIWidget::NativeKeyBindingsType nativeKeyBindingsType =
mTxtCtrlElement->IsTextArea()
? nsIWidget::NativeKeyBindingsForMultiLineEditor
: nsIWidget::NativeKeyBindingsForSingleLineEditor;
nsIWidget* widget = widgetKeyEvent->mWidget;
// If the event is created by chrome script, the widget is nullptr.
if (!widget) {
widget = mFrame->GetNearestWidget();
if (NS_WARN_IF(!widget)) {
return NS_OK;
}
}
// WidgetKeyboardEvent::ExecuteEditCommands() requires non-nullptr mWidget.
// If the event is created by chrome script, it is nullptr but we need to
// execute native key bindings. Therefore, we need to set widget to
// WidgetEvent::mWidget temporarily.
AutoRestore<nsCOMPtr<nsIWidget>> saveWidget(widgetKeyEvent->mWidget);
widgetKeyEvent->mWidget = widget;
if (widgetKeyEvent->ExecuteEditCommands(nativeKeyBindingsType,
DoCommandCallback, mFrame)) {
aEvent->PreventDefault();
}
return NS_OK;
}
nsresult TextInputListener::OnEditActionHandled(TextEditor& aTextEditor) {
if (mFrame) {
AutoWeakFrame weakFrame = mFrame;
nsITextControlFrame* frameBase = do_QueryFrame(mFrame);
nsTextControlFrame* frame = static_cast<nsTextControlFrame*>(frameBase);
NS_ASSERTION(frame, "Where is our frame?");
//
// Update the undo / redo menus
//
size_t numUndoItems = aTextEditor.NumberOfUndoItems();
size_t numRedoItems = aTextEditor.NumberOfRedoItems();
if ((numUndoItems && !mHadUndoItems) || (!numUndoItems && mHadUndoItems) ||
(numRedoItems && !mHadRedoItems) || (!numRedoItems && mHadRedoItems)) {
// Modify the menu if undo or redo items are different
UpdateTextInputCommands(u"undo"_ns);
mHadUndoItems = numUndoItems != 0;
mHadRedoItems = numRedoItems != 0;
}
if (weakFrame.IsAlive()) {
HandleValueChanged(frame);
}
}
return mTextControlState ? mTextControlState->OnEditActionHandled() : NS_OK;
}
void TextInputListener::HandleValueChanged(nsTextControlFrame* aFrame) {
// Make sure we know we were changed (do NOT set this to false if there are
// no undo items; JS could change the value and we'd still need to save it)
if (mSetValueChanged) {
if (!aFrame) {
nsITextControlFrame* frameBase = do_QueryFrame(mFrame);
aFrame = static_cast<nsTextControlFrame*>(frameBase);
NS_ASSERTION(aFrame, "Where is our frame?");
}
aFrame->SetValueChanged(true);
}
if (!mSettingValue) {
mTxtCtrlElement->OnValueChanged(ValueChangeKind::UserInteraction);
}
}
nsresult TextInputListener::UpdateTextInputCommands(
const nsAString& aCommandsToUpdate, Selection* aSelection,
int16_t aReason) {
nsIContent* content = mFrame->GetContent();
if (NS_WARN_IF(!content)) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<Document> doc = content->GetComposedDoc();
if (NS_WARN_IF(!doc)) {
return NS_ERROR_FAILURE;
}
nsPIDOMWindowOuter* domWindow = doc->GetWindow();
if (NS_WARN_IF(!domWindow)) {
return NS_ERROR_FAILURE;
}
domWindow->UpdateCommands(aCommandsToUpdate, aSelection, aReason);
return NS_OK;
}
/*****************************************************************************
* mozilla::AutoTextControlHandlingState
*
* This class is temporarily created in the stack and can manage nested
* handling state of TextControlState. While this instance exists, lifetime of
* TextControlState which created the instance is guaranteed. In other words,
* you can use this class as "kungFuDeathGrip" for TextControlState.
*****************************************************************************/
enum class TextControlAction {
CacheForReuse,
CommitComposition,
Destructor,
PrepareEditor,
SetRangeText,
SetSelectionRange,
SetValue,
UnbindFromFrame,
Unlink,
};
class MOZ_STACK_CLASS AutoTextControlHandlingState {
public:
AutoTextControlHandlingState() = delete;
explicit AutoTextControlHandlingState(const AutoTextControlHandlingState&) =
delete;
AutoTextControlHandlingState(AutoTextControlHandlingState&&) = delete;
void operator=(AutoTextControlHandlingState&) = delete;
void operator=(const AutoTextControlHandlingState&) = delete;
/**
* Generic constructor. If TextControlAction does not require additional
* data, must use this constructor.
*/
MOZ_CAN_RUN_SCRIPT AutoTextControlHandlingState(
TextControlState& aTextControlState, TextControlAction aTextControlAction)
: mParent(aTextControlState.mHandlingState),
mTextControlState(aTextControlState),
mTextCtrlElement(aTextControlState.mTextCtrlElement),
mTextInputListener(aTextControlState.mTextListener),
mTextControlAction(aTextControlAction) {
MOZ_ASSERT(aTextControlAction != TextControlAction::SetValue,
"Use specific constructor");
mTextControlState.mHandlingState = this;
if (Is(TextControlAction::CommitComposition)) {
MOZ_ASSERT(mParent);
MOZ_ASSERT(mParent->Is(TextControlAction::SetValue));
// If we're trying to commit composition before handling SetValue,
// the parent old values will be outdated so that we need to clear
// them.
mParent->InvalidateOldValue();
}
}
/**
* TextControlAction::SetValue specific constructor. Current setting value
* must be specified and the creator should check whether we succeeded to
* allocate memory for line breaker conversion.
*/
MOZ_CAN_RUN_SCRIPT AutoTextControlHandlingState(
TextControlState& aTextControlState, TextControlAction aTextControlAction,
const nsAString& aSettingValue, const nsAString* aOldValue,
uint32_t aFlags, ErrorResult& aRv)
: mParent(aTextControlState.mHandlingState),
mTextControlState(aTextControlState),
mTextCtrlElement(aTextControlState.mTextCtrlElement),
mTextInputListener(aTextControlState.mTextListener),
mSettingValue(aSettingValue),
mOldValue(aOldValue),
mSetValueFlags(aFlags),
mTextControlAction(aTextControlAction) {
MOZ_ASSERT(aTextControlAction == TextControlAction::SetValue,
"Use generic constructor");
mTextControlState.mHandlingState = this;
if (!nsContentUtils::PlatformToDOMLineBreaks(mSettingValue, fallible)) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return;
}
// Update all setting value's new value because older value shouldn't
// overwrite newer value.
if (mParent) {
// If SetValue is nested, parents cannot trust their old value anymore.
// So, we need to clear them.
mParent->UpdateSettingValueAndInvalidateOldValue(mSettingValue);
}
}
MOZ_CAN_RUN_SCRIPT ~AutoTextControlHandlingState() {
mTextControlState.mHandlingState = mParent;
if (!mParent && mTextControlStateDestroyed) {
mTextControlState.DeleteOrCacheForReuse();
}
if (!mTextControlStateDestroyed && mPreareEditorLater) {
MOZ_ASSERT(nsContentUtils::IsSafeToRunScript());
mTextControlState.PrepareEditor();
}
}
void OnDestroyTextControlState() {
if (IsHandling(TextControlAction::Destructor) ||
IsHandling(TextControlAction::CacheForReuse)) {
// Do nothing since mTextContrlState.DeleteOrCacheForReuse() has
// already been called.
return;
}
mTextControlStateDestroyed = true;
if (mParent) {
mParent->OnDestroyTextControlState();
}
}
void PrepareEditorLater() {
MOZ_ASSERT(IsHandling(TextControlAction::SetValue));
MOZ_ASSERT(!IsHandling(TextControlAction::PrepareEditor));
// Look for the top most SetValue.
AutoTextControlHandlingState* settingValue = nullptr;
for (AutoTextControlHandlingState* handlingSomething = this;
handlingSomething; handlingSomething = handlingSomething->mParent) {
if (handlingSomething->Is(TextControlAction::SetValue)) {
settingValue = handlingSomething;
}
}
settingValue->mPreareEditorLater = true;
}
/**
* WillSetValueWithTextEditor() is called when TextControlState sets
* value with its mTextEditor.
*/
void WillSetValueWithTextEditor() {
MOZ_ASSERT(Is(TextControlAction::SetValue));
MOZ_ASSERT(mTextControlState.mBoundFrame);
mTextControlFrame = mTextControlState.mBoundFrame;
// If we'reemulating user input, we don't need to manage mTextInputListener
// by ourselves since everything should be handled by TextEditor as normal
// user input.
if (mSetValueFlags & TextControlState::eSetValue_BySetUserInput) {
return;
}
// Otherwise, if we're setting the value programatically, we need to manage
// mTextInputListener by ourselves since TextEditor users special path
// for the performance.
mTextInputListener->SettingValue(true);
mTextInputListener->SetValueChanged(mSetValueFlags &
TextControlState::eSetValue_Notify);
mEditActionHandled = false;
// Even if falling back to `TextControlState::SetValueWithoutTextEditor()`
// due to editor destruction, it shouldn't dispatch "beforeinput" event
// anymore. Therefore, we should mark that we've already dispatched
// "beforeinput" event.
WillDispatchBeforeInputEvent();
}
/**
* WillDispatchBeforeInputEvent() is called immediately before dispatching
* "beforeinput" event in `TextControlState`.
*/
void WillDispatchBeforeInputEvent() {
mBeforeInputEventHasBeenDispatched = true;
}
/**
* OnEditActionHandled() is called when the TextEditor handles something
* and immediately before dispatching "input" event.
*/
MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE nsresult OnEditActionHandled() {
MOZ_ASSERT(!mEditActionHandled);
mEditActionHandled = true;
if (!Is(TextControlAction::SetValue)) {
return NS_OK;
}
if (!(mSetValueFlags & TextControlState::eSetValue_BySetUserInput)) {
mTextInputListener->SetValueChanged(true);
mTextInputListener->SettingValue(
mParent && mParent->IsHandling(TextControlAction::SetValue));
if (!(mSetValueFlags & TextControlState::eSetValue_Notify)) {
// Listener doesn't update frame, but it is required for
// placeholder
mTextControlState.ValueWasChanged();
}
}
if (!IsOriginalTextControlFrameAlive()) {
return SetValueWithoutTextEditorAgain() ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}
// The new value never includes line breaks caused by hard-wrap.
// So, mCachedValue can always cache the new value.
nsITextControlFrame* textControlFrame =
do_QueryFrame(mTextControlFrame.GetFrame());
return static_cast<nsTextControlFrame*>(textControlFrame)
->CacheValue(mSettingValue, fallible)
? NS_OK
: NS_ERROR_OUT_OF_MEMORY;
}
/**
* SetValueWithoutTextEditorAgain() should be called if the frame for
* mTextControlState was destroyed during setting value.
*/
MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE bool SetValueWithoutTextEditorAgain() {
MOZ_ASSERT(!IsOriginalTextControlFrameAlive());
// If the frame was destroyed because of a flush somewhere inside
// TextEditor, mBoundFrame here will be nullptr. But it's also
// possible for the frame to go away because of another reason (such
// as deleting the existing selection -- see bug 574558), in which
// case we don't need to reset the value here.
if (mTextControlState.mBoundFrame) {
return true;
}
// XXX It's odd to drop flags except eSetValue_Notify. Probably, this
// intended to drop eSetValue_BySetUserInput and eSetValue_ByContent,
// but other flags are added later.
ErrorResult error;
AutoTextControlHandlingState handlingSetValueWithoutEditor(
mTextControlState, TextControlAction::SetValue, mSettingValue,
mOldValue, mSetValueFlags & TextControlState::eSetValue_Notify, error);
if (error.Failed()) {
MOZ_ASSERT(error.ErrorCodeIs(NS_ERROR_OUT_OF_MEMORY));
error.SuppressException();
return false;
}
return mTextControlState.SetValueWithoutTextEditor(
handlingSetValueWithoutEditor);
}
bool IsTextControlStateDestroyed() const {
return mTextControlStateDestroyed;
}
bool IsOriginalTextControlFrameAlive() const {
return const_cast<AutoTextControlHandlingState*>(this)
->mTextControlFrame.IsAlive();
}
bool HasEditActionHandled() const { return mEditActionHandled; }
bool HasBeforeInputEventDispatched() const {
return mBeforeInputEventHasBeenDispatched;
}
bool Is(TextControlAction aTextControlAction) const {
return mTextControlAction == aTextControlAction;
}
bool IsHandling(TextControlAction aTextControlAction) const {
if (mTextControlAction == aTextControlAction) {
return true;
}
return mParent ? mParent->IsHandling(aTextControlAction) : false;
}
TextControlElement* GetTextControlElement() const { return mTextCtrlElement; }
TextInputListener* GetTextInputListener() const { return mTextInputListener; }
uint32_t GetSetValueFlags() const {
MOZ_ASSERT(Is(TextControlAction::SetValue));
return mSetValueFlags;
}
const nsAString* GetOldValue() const {
MOZ_ASSERT(Is(TextControlAction::SetValue));
return mOldValue;
}
const nsString& GetSettingValue() const {
MOZ_ASSERT(IsHandling(TextControlAction::SetValue));
if (mTextControlAction == TextControlAction::SetValue) {
return mSettingValue;
}
return mParent->GetSettingValue();
}
private:
void UpdateSettingValueAndInvalidateOldValue(const nsString& aSettingValue) {
if (mTextControlAction == TextControlAction::SetValue) {
mSettingValue = aSettingValue;
}
mOldValue = nullptr;
if (mParent) {
mParent->UpdateSettingValueAndInvalidateOldValue(aSettingValue);
}
}
void InvalidateOldValue() {
mOldValue = nullptr;
if (mParent) {
mParent->InvalidateOldValue();
}
}
AutoTextControlHandlingState* const mParent;
TextControlState& mTextControlState;
// mTextControlFrame should be set immediately before calling methods
// which may destroy the frame. Then, you can check whether the frame
// was destroyed/replaced.
AutoWeakFrame mTextControlFrame;
// mTextCtrlElement grabs TextControlState::mTextCtrlElement since
// if the text control element releases mTextControlState, only this
// can guarantee the instance of the text control element.
RefPtr<TextControlElement> const mTextCtrlElement;
// mTextInputListener grabs TextControlState::mTextListener because if
// TextControlState is unbind from the frame, it's released.
RefPtr<TextInputListener> const mTextInputListener;
nsString mSettingValue;
const nsAString* mOldValue = nullptr;
uint32_t mSetValueFlags = 0;
TextControlAction const mTextControlAction;
bool mTextControlStateDestroyed = false;
bool mEditActionHandled = false;
bool mPreareEditorLater = false;
bool mBeforeInputEventHasBeenDispatched = false;
};
/*****************************************************************************
* mozilla::TextControlState
*****************************************************************************/
AutoTArray<TextControlState*, TextControlState::kMaxCountOfCacheToReuse>*
TextControlState::sReleasedInstances = nullptr;
bool TextControlState::sHasShutDown = false;
TextControlState::TextControlState(TextControlElement* aOwningElement)
: mTextCtrlElement(aOwningElement),
mBoundFrame(nullptr),
mEverInited(false),
mEditorInitialized(false),
mValueTransferInProgress(false),
mSelectionCached(true),
mPlaceholderVisibility(false),
mPreviewVisibility(false)
// When adding more member variable initializations here, add the same
// also to ::Construct.
{
MOZ_COUNT_CTOR(TextControlState);
static_assert(sizeof(*this) <= 128,
"Please keep small TextControlState as far as possible");
}
TextControlState* TextControlState::Construct(
TextControlElement* aOwningElement) {
if (sReleasedInstances && !sReleasedInstances->IsEmpty()) {
TextControlState* state = sReleasedInstances->PopLastElement();
state->mTextCtrlElement = aOwningElement;
state->mBoundFrame = nullptr;
state->mSelectionProperties = SelectionProperties();
state->mEverInited = false;
state->mEditorInitialized = false;
state->mValueTransferInProgress = false;
state->mSelectionCached = true;
state->mPlaceholderVisibility = false;
state->mPreviewVisibility = false;
// When adding more member variable initializations here, add the same
// also to the constructor.
return state;
}
return new TextControlState(aOwningElement);
}
TextControlState::~TextControlState() {
MOZ_ASSERT(!mHandlingState);
MOZ_COUNT_DTOR(TextControlState);
AutoTextControlHandlingState handlingDesctructor(
*this, TextControlAction::Destructor);
Clear();
}
void TextControlState::Shutdown() {
sHasShutDown = true;
if (sReleasedInstances) {
for (TextControlState* textControlState : *sReleasedInstances) {
textControlState->DeleteOrCacheForReuse();
}
delete sReleasedInstances;
}
}
void TextControlState::Destroy() {
// If we're handling something, we should be deleted later.
if (mHandlingState) {
mHandlingState->OnDestroyTextControlState();
return;
}
DeleteOrCacheForReuse();
// Note that this instance may have already been deleted here. Don't touch
// any members.
}
void TextControlState::DeleteOrCacheForReuse() {
MOZ_ASSERT(!IsBusy());
// If we can cache this instance, we should do it instead of deleting it.
if (!sHasShutDown && (!sReleasedInstances || sReleasedInstances->Length() <
kMaxCountOfCacheToReuse)) {
AutoTextControlHandlingState handlingCacheForReuse(
*this, TextControlAction::CacheForReuse);
// Prepare for reuse, unlink and release any refcountable objects.
UnlinkInternal();
mValue.reset();
mTextCtrlElement = nullptr;
// Put this instance to the cache. Note that now, the array may be full,
// but it's not problem to cache more instances than kMaxCountOfCacheToReuse
// because it just requires reallocation cost of the array buffer.
if (!sReleasedInstances) {
sReleasedInstances =
new AutoTArray<TextControlState*, kMaxCountOfCacheToReuse>;
}
sReleasedInstances->AppendElement(this);
return;
}
delete this;
}
nsresult TextControlState::OnEditActionHandled() {
return mHandlingState ? mHandlingState->OnEditActionHandled() : NS_OK;
}
Element* TextControlState::GetRootNode() {
return mBoundFrame ? mBoundFrame->GetRootNode() : nullptr;
}
Element* TextControlState::GetPreviewNode() {
return mBoundFrame ? mBoundFrame->GetPreviewNode() : nullptr;
}
void TextControlState::Clear() {
MOZ_ASSERT(mHandlingState);
MOZ_ASSERT(mHandlingState->Is(TextControlAction::Destructor) ||
mHandlingState->Is(TextControlAction::CacheForReuse) ||
mHandlingState->Is(TextControlAction::Unlink));
if (mTextEditor) {
mTextEditor->SetTextInputListener(nullptr);
}
if (mBoundFrame) {
// Oops, we still have a frame!
// This should happen when the type of a text input control is being changed
// to something which is not a text control. In this case, we should
// pretend that a frame is being destroyed, and clean up after ourselves
// properly.
UnbindFromFrame(mBoundFrame);
mTextEditor = nullptr;
} else {
// If we have a bound frame around, UnbindFromFrame will call DestroyEditor
// for us.
DestroyEditor();
}
mTextListener = nullptr;
}
void TextControlState::Unlink() {
AutoTextControlHandlingState handlingUnlink(*this, TextControlAction::Unlink);
UnlinkInternal();
}
void TextControlState::UnlinkInternal() {
MOZ_ASSERT(mHandlingState);
MOZ_ASSERT(mHandlingState->Is(TextControlAction::Unlink) ||
mHandlingState->Is(TextControlAction::CacheForReuse));
TextControlState* tmp = this;
tmp->Clear();
NS_IMPL_CYCLE_COLLECTION_UNLINK(mSelCon)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mTextEditor)
}
void TextControlState::Traverse(nsCycleCollectionTraversalCallback& cb) {
TextControlState* tmp = this;
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSelCon)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTextEditor)
}
nsFrameSelection* TextControlState::GetConstFrameSelection() {
return mSelCon ? mSelCon->GetConstFrameSelection() : nullptr;
}
TextEditor* TextControlState::GetTextEditor() {
// Note that if the instance is destroyed in PrepareEditor(), it returns
// NS_ERROR_NOT_INITIALIZED so that we don't need to create kungFuDeathGrip
// in this hot path.
if (!mTextEditor && NS_WARN_IF(NS_FAILED(PrepareEditor()))) {
return nullptr;
}
return mTextEditor;
}
TextEditor* TextControlState::GetTextEditorWithoutCreation() {
return mTextEditor;
}
nsISelectionController* TextControlState::GetSelectionController() const {
return mSelCon;
}
// Helper class, used below in BindToFrame().
class PrepareEditorEvent : public Runnable {
public:
PrepareEditorEvent(TextControlState& aState, nsIContent* aOwnerContent,
const nsAString& aCurrentValue)
: Runnable("PrepareEditorEvent"),
mState(&aState),
mOwnerContent(aOwnerContent),
mCurrentValue(aCurrentValue) {
aState.mValueTransferInProgress = true;
}
MOZ_CAN_RUN_SCRIPT_BOUNDARY NS_IMETHOD Run() override {
if (NS_WARN_IF(!mState)) {
return NS_ERROR_NULL_POINTER;
}
// Transfer the saved value to the editor if we have one
const nsAString* value = nullptr;
if (!mCurrentValue.IsEmpty()) {
value = &mCurrentValue;
}
nsAutoScriptBlocker scriptBlocker;
mState->PrepareEditor(value);
mState->mValueTransferInProgress = false;
return NS_OK;
}
private:
WeakPtr<TextControlState> mState;
nsCOMPtr<nsIContent> mOwnerContent; // strong reference
nsAutoString mCurrentValue;
};
nsresult TextControlState::BindToFrame(nsTextControlFrame* aFrame) {
MOZ_ASSERT(
!nsContentUtils::IsSafeToRunScript(),
"TextControlState::BindToFrame() has to be called with script blocker");
NS_ASSERTION(aFrame, "The frame to bind to should be valid");
if (!aFrame) {
return NS_ERROR_INVALID_ARG;
}
NS_ASSERTION(!mBoundFrame, "Cannot bind twice, need to unbind first");
if (mBoundFrame) {
return NS_ERROR_FAILURE;
}
// If we'll need to transfer our current value to the editor, save it before
// binding to the frame.
nsAutoString currentValue;
if (mTextEditor) {
GetValue(currentValue, true);
}
mBoundFrame = aFrame;
Element* rootNode = aFrame->GetRootNode();
MOZ_ASSERT(rootNode);
PresShell* presShell = aFrame->PresContext()->GetPresShell();
MOZ_ASSERT(presShell);
// Create a SelectionController
mSelCon = new TextInputSelectionController(presShell, rootNode);
MOZ_ASSERT(!mTextListener, "Should not overwrite the object");
mTextListener = new TextInputListener(mTextCtrlElement);
mTextListener->SetFrame(mBoundFrame);
// Editor will override this as needed from InitializeSelection.
mSelCon->SetDisplaySelection(nsISelectionController::SELECTION_HIDDEN);
// Get the caret and make it a selection listener.
// FYI: It's safe to use raw pointer for calling
// Selection::AddSelectionListner() because it only appends the listener
// to its internal array.
Selection* selection = mSelCon->GetSelection(SelectionType::eNormal);
if (selection) {
RefPtr<nsCaret> caret = presShell->GetCaret();
if (caret) {
selection->AddSelectionListener(caret);
}
mTextListener->StartToListenToSelectionChange();
}
// If an editor exists from before, prepare it for usage
if (mTextEditor) {
if (NS_WARN_IF(!mTextCtrlElement)) {
return NS_ERROR_FAILURE;
}
// Set the correct direction on the newly created root node
if (mTextEditor->IsRightToLeft()) {
rootNode->SetAttr(kNameSpaceID_None, nsGkAtoms::dir, u"rtl"_ns, false);
} else if (mTextEditor->IsLeftToRight()) {
rootNode->SetAttr(kNameSpaceID_None, nsGkAtoms::dir, u"ltr"_ns, false);
} else {
// otherwise, inherit the content node's direction
}
nsContentUtils::AddScriptRunner(
new PrepareEditorEvent(*this, mTextCtrlElement, currentValue));
}
return NS_OK;
}
struct MOZ_STACK_CLASS PreDestroyer {
void Init(TextEditor* aTextEditor) { mTextEditor = aTextEditor; }
MOZ_CAN_RUN_SCRIPT ~PreDestroyer() {
if (mTextEditor) {
MOZ_KnownLive(mTextEditor)->PreDestroy(true);
}
}
void Swap(RefPtr<TextEditor>& aTextEditor) {
return mTextEditor.swap(aTextEditor);
}
private:
RefPtr<TextEditor> mTextEditor;
};
nsresult TextControlState::PrepareEditor(const nsAString* aValue) {
if (!mBoundFrame) {
// Cannot create an editor without a bound frame.
// Don't return a failure code, because js callers can't handle that.
return NS_OK;
}
if (mEditorInitialized) {
// Do not initialize the editor multiple times.
return NS_OK;
}
AutoHideSelectionChanges hideSelectionChanges(GetConstFrameSelection());
if (mHandlingState) {
// Don't attempt to initialize recursively!
if (mHandlingState->IsHandling(TextControlAction::PrepareEditor)) {
return NS_ERROR_NOT_INITIALIZED;
}
// Reschedule creating editor later if we're setting value.
if (mHandlingState->IsHandling(TextControlAction::SetValue)) {
mHandlingState->PrepareEditorLater();
return NS_ERROR_NOT_INITIALIZED;
}
}
MOZ_ASSERT(mTextCtrlElement);
AutoTextControlHandlingState preparingEditor(
*this, TextControlAction::PrepareEditor);
// Note that we don't check mTextEditor here, because we might already have
// one around, in which case we don't create a new one, and we'll just tie
// the required machinery to it.
nsPresContext* presContext = mBoundFrame->PresContext();
PresShell* presShell = presContext->GetPresShell();
// Setup the editor flags
uint32_t editorFlags = nsIEditor::eEditorPlaintextMask;
if (IsSingleLineTextControl()) {
editorFlags |= nsIEditor::eEditorSingleLineMask;
}
if (IsPasswordTextControl()) {
editorFlags |= nsIEditor::eEditorPasswordMask;
}
// All nsTextControlFrames are widgets
editorFlags |= nsIEditor::eEditorWidgetMask;
// Spell check is diabled at creation time. It is enabled once
// the editor comes into focus.
editorFlags |= nsIEditor::eEditorSkipSpellCheck;
bool shouldInitializeEditor = false;
RefPtr<TextEditor> newTextEditor; // the editor that we might create
nsresult rv = NS_OK;
PreDestroyer preDestroyer;
if (!mTextEditor) {
shouldInitializeEditor = true;
// Create an editor
newTextEditor = new TextEditor();
preDestroyer.Init(newTextEditor);
// Make sure we clear out the non-breaking space before we initialize the
// editor
rv = mBoundFrame->UpdateValueDisplay(true, true);
if (NS_FAILED(rv)) {
NS_WARNING("nsTextControlFrame::UpdateValueDisplay() failed");
return rv;
}
} else {
if (aValue || !mEditorInitialized) {
// Set the correct value in the root node
rv = mBoundFrame->UpdateValueDisplay(true, !mEditorInitialized, aValue);
if (NS_FAILED(rv)) {
NS_WARNING("nsTextControlFrame::UpdateValueDisplay() failed");
return rv;
}
}
newTextEditor = mTextEditor; // just pretend that we have a new editor!
// Don't lose application flags in the process.
if (newTextEditor->IsMailEditor()) {
editorFlags |= nsIEditor::eEditorMailMask;
}
}
// Get the current value of the textfield from the content.
// Note that if we've created a new editor, mTextEditor is null at this stage,
// so we will get the real value from the content.
nsAutoString defaultValue;
if (aValue) {
defaultValue = *aValue;
} else {
GetValue(defaultValue, true);
}
if (!mEditorInitialized) {
// Now initialize the editor.
//
// NOTE: Conversion of '\n' to <BR> happens inside the
// editor's Init() call.
// Get the DOM document
nsCOMPtr<Document> doc = presShell->GetDocument();
if (NS_WARN_IF(!doc)) {
return NS_ERROR_FAILURE;
}
// What follows is a bit of a hack. The editor uses the public DOM APIs
// for its content manipulations, and it causes it to fail some security
// checks deep inside when initializing. So we explictly make it clear that
// we're native code.
// Note that any script that's directly trying to access our value
// has to be going through some scriptable object to do that and that
// already does the relevant security checks.
AutoNoJSAPI nojsapi;
RefPtr<Element> rootElement = GetRootNode();
RefPtr<TextInputSelectionController> selectionController = mSelCon;
rv = newTextEditor->Init(*doc, rootElement, selectionController,
editorFlags, defaultValue);
if (NS_FAILED(rv)) {
NS_WARNING("TextEditor::Init() failed");
return rv;
}
}
// Initialize the controller for the editor
if (!SuppressEventHandlers(presContext)) {
nsCOMPtr<nsIControllers> controllers;
if (HTMLInputElement* inputElement =
HTMLInputElement::FromNodeOrNull(mTextCtrlElement)) {
rv = inputElement->GetControllers(getter_AddRefs(controllers));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
} else {
HTMLTextAreaElement* textAreaElement =
HTMLTextAreaElement::FromNodeOrNull(mTextCtrlElement);
if (!textAreaElement) {
return NS_ERROR_FAILURE;
}
rv = textAreaElement->GetControllers(getter_AddRefs(controllers));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
}
if (controllers) {
uint32_t numControllers;
bool found = false;
rv = controllers->GetControllerCount(&numControllers);
for (uint32_t i = 0; i < numControllers; i++) {
nsCOMPtr<nsIController> controller;
rv = controllers->GetControllerAt(i, getter_AddRefs(controller));
if (NS_SUCCEEDED(rv) && controller) {
nsCOMPtr<nsIControllerContext> editController =
do_QueryInterface(controller);
if (editController) {
editController->SetCommandContext(
static_cast<nsIEditor*>(newTextEditor));
found = true;
}
}
}
if (!found) {
rv = NS_ERROR_FAILURE;
}
}
}
// Initialize the plaintext editor
if (shouldInitializeEditor) {
const int32_t wrapCols = GetWrapCols();
MOZ_ASSERT(wrapCols >= 0);
newTextEditor->SetWrapColumn(wrapCols);
}
// Set max text field length
newTextEditor->SetMaxTextLength(mTextCtrlElement->UsedMaxLength());
editorFlags = newTextEditor->Flags();
// Check if the readonly attribute is set.
//
// TODO: Should probably call IsDisabled(), as it is cheaper.
if (mTextCtrlElement->HasAttr(kNameSpaceID_None, nsGkAtoms::readonly) ||
mTextCtrlElement->HasAttr(kNameSpaceID_None, nsGkAtoms::disabled)) {
editorFlags |= nsIEditor::eEditorReadonlyMask;
}
SetEditorFlagsIfNecessary(*newTextEditor, editorFlags);