2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-04-22 00:17:41 +04:00
|
|
|
|
|
|
|
#ifndef nsTextEditorState_h__
|
|
|
|
#define nsTextEditorState_h__
|
|
|
|
|
|
|
|
#include "nsAutoPtr.h"
|
2011-08-09 01:28:04 +04:00
|
|
|
#include "nsString.h"
|
2010-04-22 00:17:41 +04:00
|
|
|
#include "nsITextControlElement.h"
|
2011-07-30 01:31:57 +04:00
|
|
|
#include "nsITextControlFrame.h"
|
2010-04-22 00:17:41 +04:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2013-12-03 18:40:10 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2015-04-17 04:13:24 +03:00
|
|
|
#include "mozilla/Attributes.h"
|
2016-03-04 17:56:13 +03:00
|
|
|
#include "mozilla/Maybe.h"
|
2012-10-10 23:22:27 +04:00
|
|
|
#include "mozilla/WeakPtr.h"
|
2010-04-22 00:17:41 +04:00
|
|
|
|
|
|
|
class nsTextInputListener;
|
|
|
|
class nsTextControlFrame;
|
|
|
|
class nsTextInputSelectionImpl;
|
|
|
|
class nsAnonDivObserver;
|
|
|
|
class nsISelectionController;
|
|
|
|
class nsFrameSelection;
|
|
|
|
class nsIEditor;
|
|
|
|
class nsITextControlElement;
|
2014-08-01 04:25:31 +04:00
|
|
|
class nsFrame;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
class HTMLInputElement;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2010-04-22 00:17:41 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* nsTextEditorState is a class which is responsible for managing the state of
|
|
|
|
* plaintext controls. This currently includes the following HTML elements:
|
|
|
|
* <input type=text>
|
|
|
|
* <input type=password>
|
|
|
|
* <textarea>
|
|
|
|
* and also XUL controls such as <textbox> which use one of these elements behind
|
|
|
|
* the scenes.
|
|
|
|
*
|
2013-03-28 23:41:32 +04:00
|
|
|
* This class is held as a member of HTMLInputElement and nsHTMLTextAreaElement.
|
2010-04-22 00:17:41 +04:00
|
|
|
* The public functions in this class include the public APIs which content/ uses.
|
|
|
|
* Layout code uses the nsITextControlElement interface to invoke functions on this
|
|
|
|
* class.
|
|
|
|
*
|
|
|
|
* The design motivation behind this class is maintaining all of the things which
|
|
|
|
* collectively are considered the "state" of the text control in a single location.
|
|
|
|
* This state includes several things:
|
|
|
|
*
|
|
|
|
* * The control's value. This value is stored in the mValue member, and is only
|
|
|
|
* used when there is no frame for the control, or when the editor object has
|
|
|
|
* not been initialized yet.
|
|
|
|
*
|
|
|
|
* * The control's associated frame. This value is stored in the mBoundFrame member.
|
|
|
|
* A text control might never have an associated frame during its life cycle,
|
|
|
|
* or might have several different ones, but at any given moment in time there is
|
|
|
|
* a maximum of 1 bound frame to each text control.
|
|
|
|
*
|
|
|
|
* * The control's associated editor. This value is stored in the mEditor member.
|
|
|
|
* An editor is initilized for the control only when necessary (that is, when either
|
|
|
|
* the user is about to interact with the text control, or when some other code
|
|
|
|
* needs to access the editor object. Without a frame bound to the control, an
|
|
|
|
* editor is never initialzied. Once initialized, the editor might outlive the frame,
|
|
|
|
* in which case the same editor will be used if a new frame gets bound to the
|
|
|
|
* text control.
|
|
|
|
*
|
|
|
|
* * The anonymous content associated with the text control's frame, including the
|
|
|
|
* value div (the DIV element responsible for holding the value of the text control)
|
|
|
|
* and the placeholder div (the DIV element responsible for holding the placeholder
|
|
|
|
* value of the text control.) These values are stored in the mRootNode and
|
|
|
|
* mPlaceholderDiv members, respectively. They will be created when a
|
|
|
|
* frame is bound to the text control. They will be destroyed when the frame is
|
|
|
|
* unbound from the object. We could try and hold on to the anonymous content
|
|
|
|
* between different frames, but unfortunately that is not currently possible
|
|
|
|
* because they are not unbound from the document in time.
|
|
|
|
*
|
|
|
|
* * The frame selection controller. This value is stored in the mSelCon member.
|
|
|
|
* The frame selection controller is responsible for maintaining the selection state
|
|
|
|
* on a frame. It is created when a frame is bound to the text control element,
|
|
|
|
* and will be destroy when the frame is being unbound from the text control element.
|
|
|
|
* It is created alongside with the frame selection object which is stored in the
|
|
|
|
* mFrameSel member.
|
|
|
|
*
|
|
|
|
* * The editor text listener. This value is stored in the mTextListener member.
|
|
|
|
* Its job is to listen to selection and keyboard events, and act accordingly.
|
|
|
|
* It is created when an a frame is first bound to the control, and will be destroyed
|
|
|
|
* when the frame is unbound from the text control element.
|
|
|
|
*
|
|
|
|
* * The editor's cached value. This value is stored in the mCachedValue member.
|
|
|
|
* It is used to improve the performance of append operations to the text
|
2011-03-03 05:36:36 +03:00
|
|
|
* control. A mutation observer stored in the mMutationObserver has the job of
|
2010-04-22 00:17:41 +04:00
|
|
|
* invalidating this cache when the anonymous contect containing the value is
|
|
|
|
* changed.
|
|
|
|
*
|
2011-07-30 01:31:57 +04:00
|
|
|
* * The editor's cached selection properties. These vales are stored in the
|
|
|
|
* mSelectionProperties member, and include the selection's start, end and
|
|
|
|
* direction. They are only used when there is no frame available for the
|
|
|
|
* text field.
|
|
|
|
*
|
2010-04-22 00:17:41 +04:00
|
|
|
*
|
|
|
|
* As a general rule, nsTextEditorState objects own the value of the text control, and any
|
|
|
|
* attempt to retrieve or set the value must be made through those objects. Internally,
|
|
|
|
* the value can be represented in several different ways, based on the state the control is
|
|
|
|
* in.
|
|
|
|
*
|
|
|
|
* * When the control is first initialized, its value is equal to the default value of
|
|
|
|
* the DOM node. For <input> text controls, this default value is the value of the
|
|
|
|
* value attribute. For <textarea> elements, this default value is the value of the
|
|
|
|
* text node children of the element.
|
|
|
|
*
|
|
|
|
* * If the value has been changed through the DOM node (before the editor for the object
|
|
|
|
* is initialized), the value is stored as a simple string inside the mValue member of
|
|
|
|
* the nsTextEditorState object.
|
|
|
|
*
|
|
|
|
* * If an editor has been initialized for the control, the value is set and retrievd via
|
|
|
|
* the nsIPlaintextEditor interface, and is internally managed by the editor as the
|
|
|
|
* native anonymous content tree attached to the control's frame.
|
|
|
|
*
|
|
|
|
* * If the text editor state object is unbound from the control's frame, the value is
|
|
|
|
* transferred to the mValue member variable, and will be managed there until a new
|
|
|
|
* frame is bound to the text editor state object.
|
|
|
|
*/
|
|
|
|
|
2011-05-21 12:25:27 +04:00
|
|
|
class RestoreSelectionState;
|
|
|
|
|
2012-10-10 23:22:27 +04:00
|
|
|
class nsTextEditorState : public mozilla::SupportsWeakPtr<nsTextEditorState> {
|
2010-04-22 00:17:41 +04:00
|
|
|
public:
|
2015-04-24 19:43:01 +03:00
|
|
|
MOZ_DECLARE_WEAKREFERENCE_TYPENAME(nsTextEditorState)
|
2010-04-22 00:17:41 +04:00
|
|
|
explicit nsTextEditorState(nsITextControlElement* aOwningElement);
|
|
|
|
~nsTextEditorState();
|
|
|
|
|
2012-04-24 08:19:26 +04:00
|
|
|
void Traverse(nsCycleCollectionTraversalCallback& cb);
|
|
|
|
void Unlink();
|
2010-04-22 00:17:41 +04:00
|
|
|
|
|
|
|
nsIEditor* GetEditor();
|
|
|
|
nsISelectionController* GetSelectionController() const;
|
|
|
|
nsFrameSelection* GetConstFrameSelection();
|
|
|
|
nsresult BindToFrame(nsTextControlFrame* aFrame);
|
|
|
|
void UnbindFromFrame(nsTextControlFrame* aFrame);
|
2012-07-30 18:20:58 +04:00
|
|
|
nsresult PrepareEditor(const nsAString *aValue = nullptr);
|
2010-04-22 00:17:41 +04:00
|
|
|
void InitializeKeyboardEventListeners();
|
|
|
|
|
2015-06-18 17:56:20 +03:00
|
|
|
enum SetValueFlags
|
|
|
|
{
|
|
|
|
// The call is for internal processing.
|
|
|
|
eSetValue_Internal = 0,
|
|
|
|
// The value is changed by a call of setUserInput() from chrome.
|
|
|
|
eSetValue_BySetUserInput = 1 << 0,
|
|
|
|
// The value is changed by changing value attribute of the element or
|
|
|
|
// something like setRangeText().
|
|
|
|
eSetValue_ByContent = 1 << 1,
|
|
|
|
// Whether the value change should be notified to the frame/contet nor not.
|
|
|
|
eSetValue_Notify = 1 << 2
|
|
|
|
};
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE bool SetValue(const nsAString& aValue, uint32_t aFlags);
|
2011-09-29 10:19:26 +04:00
|
|
|
void GetValue(nsAString& aValue, bool aIgnoreWrap) const;
|
2013-03-25 23:35:55 +04:00
|
|
|
void EmptyValue() { if (mValue) mValue->Truncate(); }
|
|
|
|
bool IsEmpty() const { return mValue ? mValue->IsEmpty() : true; }
|
2010-04-22 00:17:41 +04:00
|
|
|
|
2010-10-19 17:11:07 +04:00
|
|
|
nsresult CreatePlaceholderNode();
|
|
|
|
|
2013-12-03 18:40:10 +04:00
|
|
|
mozilla::dom::Element* GetRootNode() {
|
2010-04-22 00:17:41 +04:00
|
|
|
if (!mRootNode)
|
|
|
|
CreateRootNode();
|
|
|
|
return mRootNode;
|
|
|
|
}
|
2013-12-03 18:40:10 +04:00
|
|
|
mozilla::dom::Element* GetPlaceholderNode() {
|
2010-04-22 00:17:41 +04:00
|
|
|
return mPlaceholderDiv;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool IsSingleLineTextControl() const {
|
2010-04-22 00:17:41 +04:00
|
|
|
return mTextCtrlElement->IsSingleLineTextControl();
|
|
|
|
}
|
2011-09-29 10:19:26 +04:00
|
|
|
bool IsTextArea() const {
|
2010-04-22 00:17:41 +04:00
|
|
|
return mTextCtrlElement->IsTextArea();
|
|
|
|
}
|
2011-09-29 10:19:26 +04:00
|
|
|
bool IsPlainTextControl() const {
|
2010-04-22 00:17:41 +04:00
|
|
|
return mTextCtrlElement->IsPlainTextControl();
|
|
|
|
}
|
2011-09-29 10:19:26 +04:00
|
|
|
bool IsPasswordTextControl() const {
|
2010-04-22 00:17:41 +04:00
|
|
|
return mTextCtrlElement->IsPasswordTextControl();
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t GetCols() {
|
2010-04-22 00:17:41 +04:00
|
|
|
return mTextCtrlElement->GetCols();
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t GetWrapCols() {
|
2010-04-22 00:17:41 +04:00
|
|
|
return mTextCtrlElement->GetWrapCols();
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t GetRows() {
|
2010-04-22 00:17:41 +04:00
|
|
|
return mTextCtrlElement->GetRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
// placeholder methods
|
2012-11-09 14:31:34 +04:00
|
|
|
void UpdatePlaceholderVisibility(bool aNotify);
|
2012-11-09 14:22:29 +04:00
|
|
|
bool GetPlaceholderVisibility() {
|
|
|
|
return mPlaceholderVisibility;
|
|
|
|
}
|
2011-09-29 10:19:26 +04:00
|
|
|
void UpdatePlaceholderText(bool aNotify);
|
2010-04-22 00:17:41 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the maxlength attribute
|
|
|
|
* @param aMaxLength the value of the max length attr
|
2011-10-17 18:59:28 +04:00
|
|
|
* @returns false if attr not defined
|
2010-04-22 00:17:41 +04:00
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
bool GetMaxLength(int32_t* aMaxLength);
|
2010-04-22 00:17:41 +04:00
|
|
|
|
|
|
|
void ClearValueCache() { mCachedValue.Truncate(); }
|
|
|
|
|
2011-04-12 06:03:50 +04:00
|
|
|
void HideSelectionIfBlurred();
|
|
|
|
|
2011-07-30 01:31:57 +04:00
|
|
|
struct SelectionProperties {
|
|
|
|
SelectionProperties() : mStart(0), mEnd(0),
|
|
|
|
mDirection(nsITextControlFrame::eForward) {}
|
|
|
|
bool IsDefault() const {
|
|
|
|
return mStart == 0 && mEnd == 0 &&
|
|
|
|
mDirection == nsITextControlFrame::eForward;
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mStart, mEnd;
|
2011-07-30 01:31:57 +04:00
|
|
|
nsITextControlFrame::SelectionDirection mDirection;
|
|
|
|
};
|
|
|
|
|
2014-08-01 04:25:31 +04:00
|
|
|
bool IsSelectionCached() const;
|
|
|
|
SelectionProperties& GetSelectionProperties();
|
2011-10-17 18:59:28 +04:00
|
|
|
void WillInitEagerly() { mSelectionRestoreEagerInit = true; }
|
2011-09-29 10:19:26 +04:00
|
|
|
bool HasNeverInitializedBefore() const { return !mEverInited; }
|
2011-07-30 01:31:57 +04:00
|
|
|
|
2012-03-23 19:35:40 +04:00
|
|
|
void UpdateEditableState(bool aNotify) {
|
|
|
|
if (mRootNode) {
|
|
|
|
mRootNode->UpdateEditableState(aNotify);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-22 00:17:41 +04:00
|
|
|
private:
|
2011-05-21 12:25:27 +04:00
|
|
|
friend class RestoreSelectionState;
|
|
|
|
|
2010-04-22 00:17:41 +04:00
|
|
|
// not copy constructible
|
|
|
|
nsTextEditorState(const nsTextEditorState&);
|
|
|
|
// not assignable
|
|
|
|
void operator= (const nsTextEditorState&);
|
|
|
|
|
|
|
|
nsresult CreateRootNode();
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
void ValueWasChanged(bool aNotify);
|
2010-04-22 00:17:41 +04:00
|
|
|
|
|
|
|
void DestroyEditor();
|
|
|
|
void Clear();
|
|
|
|
|
2011-09-13 23:09:51 +04:00
|
|
|
nsresult InitializeRootNode();
|
|
|
|
|
2015-04-14 19:26:56 +03:00
|
|
|
void FinishedRestoringSelection();
|
2011-05-21 12:25:27 +04:00
|
|
|
|
2014-08-01 04:25:31 +04:00
|
|
|
mozilla::dom::HTMLInputElement* GetParentNumberControl(nsFrame* aFrame) const;
|
|
|
|
|
2015-06-18 17:56:20 +03:00
|
|
|
bool EditorHasComposition();
|
|
|
|
|
2010-08-04 22:59:35 +04:00
|
|
|
class InitializationGuard {
|
|
|
|
public:
|
|
|
|
explicit InitializationGuard(nsTextEditorState& aState) :
|
|
|
|
mState(aState),
|
2011-10-17 18:59:28 +04:00
|
|
|
mGuardSet(false)
|
2010-08-04 22:59:35 +04:00
|
|
|
{
|
|
|
|
if (!mState.mInitializing) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mGuardSet = true;
|
|
|
|
mState.mInitializing = true;
|
2010-08-04 22:59:35 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
~InitializationGuard() {
|
|
|
|
if (mGuardSet) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mState.mInitializing = false;
|
2010-08-04 22:59:35 +04:00
|
|
|
}
|
|
|
|
}
|
2011-09-29 10:19:26 +04:00
|
|
|
bool IsInitializingRecursively() const {
|
2010-08-04 22:59:35 +04:00
|
|
|
return !mGuardSet;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsTextEditorState& mState;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mGuardSet;
|
2010-08-04 22:59:35 +04:00
|
|
|
};
|
|
|
|
friend class InitializationGuard;
|
2011-07-19 22:28:50 +04:00
|
|
|
friend class PrepareEditorEvent;
|
2010-08-04 22:59:35 +04:00
|
|
|
|
2015-04-17 04:13:24 +03:00
|
|
|
// The text control element owns this object, and ensures that this object
|
|
|
|
// has a smaller lifetime.
|
|
|
|
nsITextControlElement* const MOZ_NON_OWNING_REF mTextCtrlElement;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsTextInputSelectionImpl> mSelCon;
|
|
|
|
RefPtr<RestoreSelectionState> mRestoringSelection;
|
2010-04-22 00:17:41 +04:00
|
|
|
nsCOMPtr<nsIEditor> mEditor;
|
2013-12-03 18:40:10 +04:00
|
|
|
nsCOMPtr<mozilla::dom::Element> mRootNode;
|
|
|
|
nsCOMPtr<mozilla::dom::Element> mPlaceholderDiv;
|
2010-04-22 00:17:41 +04:00
|
|
|
nsTextControlFrame* mBoundFrame;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsTextInputListener> mTextListener;
|
2016-03-04 18:29:01 +03:00
|
|
|
mozilla::Maybe<nsString> mValue;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsAnonDivObserver> mMutationObserver;
|
2010-04-22 00:17:41 +04:00
|
|
|
mutable nsString mCachedValue; // Caches non-hard-wrapped value on a multiline control.
|
2015-06-18 17:56:20 +03:00
|
|
|
// mValueBeingSet is available only while SetValue() is requesting to commit
|
|
|
|
// composition. I.e., this is valid only while mIsCommittingComposition is
|
|
|
|
// true. While active composition is being committed, GetValue() needs
|
|
|
|
// the latest value which is set by SetValue(). So, this is cache for that.
|
|
|
|
nsString mValueBeingSet;
|
|
|
|
SelectionProperties mSelectionProperties;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mEverInited; // Have we ever been initialized?
|
|
|
|
bool mEditorInitialized;
|
|
|
|
bool mInitializing; // Whether we're in the process of initialization
|
|
|
|
bool mValueTransferInProgress; // Whether a value is being transferred to the frame
|
|
|
|
bool mSelectionCached; // Whether mSelectionProperties is valid
|
|
|
|
mutable bool mSelectionRestoreEagerInit; // Whether we're eager initing because of selection restore
|
2012-11-09 14:22:29 +04:00
|
|
|
bool mPlaceholderVisibility;
|
2015-06-18 17:56:20 +03:00
|
|
|
bool mIsCommittingComposition;
|
2010-04-22 00:17:41 +04:00
|
|
|
};
|
|
|
|
|
2013-05-22 03:23:53 +04:00
|
|
|
inline void
|
|
|
|
ImplCycleCollectionUnlink(nsTextEditorState& aField)
|
|
|
|
{
|
|
|
|
aField.Unlink();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
|
|
|
|
nsTextEditorState& aField,
|
|
|
|
const char* aName,
|
|
|
|
uint32_t aFlags = 0)
|
|
|
|
{
|
|
|
|
aField.Traverse(aCallback);
|
|
|
|
}
|
|
|
|
|
2010-04-22 00:17:41 +04:00
|
|
|
#endif
|