nsTextEditorState.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef nsTextEditorState_h__
  6. #define nsTextEditorState_h__
  7. #include "nsString.h"
  8. #include "nsITextControlElement.h"
  9. #include "nsITextControlFrame.h"
  10. #include "nsCycleCollectionParticipant.h"
  11. #include "mozilla/dom/Element.h"
  12. #include "mozilla/Attributes.h"
  13. #include "mozilla/Maybe.h"
  14. #include "mozilla/WeakPtr.h"
  15. class nsTextInputListener;
  16. class nsTextControlFrame;
  17. class nsTextInputSelectionImpl;
  18. class nsAnonDivObserver;
  19. class nsISelectionController;
  20. class nsFrameSelection;
  21. class nsIEditor;
  22. class nsITextControlElement;
  23. class nsFrame;
  24. namespace mozilla {
  25. namespace dom {
  26. class HTMLInputElement;
  27. } // namespace dom
  28. } // namespace mozilla
  29. /**
  30. * nsTextEditorState is a class which is responsible for managing the state of
  31. * plaintext controls. This currently includes the following HTML elements:
  32. * <input type=text>
  33. * <input type=password>
  34. * <textarea>
  35. * and also XUL controls such as <textbox> which use one of these elements behind
  36. * the scenes.
  37. *
  38. * This class is held as a member of HTMLInputElement and nsHTMLTextAreaElement.
  39. * The public functions in this class include the public APIs which content/ uses.
  40. * Layout code uses the nsITextControlElement interface to invoke functions on this
  41. * class.
  42. *
  43. * The design motivation behind this class is maintaining all of the things which
  44. * collectively are considered the "state" of the text control in a single location.
  45. * This state includes several things:
  46. *
  47. * * The control's value. This value is stored in the mValue member, and is only
  48. * used when there is no frame for the control, or when the editor object has
  49. * not been initialized yet.
  50. *
  51. * * The control's associated frame. This value is stored in the mBoundFrame member.
  52. * A text control might never have an associated frame during its life cycle,
  53. * or might have several different ones, but at any given moment in time there is
  54. * a maximum of 1 bound frame to each text control.
  55. *
  56. * * The control's associated editor. This value is stored in the mEditor member.
  57. * An editor is initilized for the control only when necessary (that is, when either
  58. * the user is about to interact with the text control, or when some other code
  59. * needs to access the editor object. Without a frame bound to the control, an
  60. * editor is never initialzied. Once initialized, the editor might outlive the frame,
  61. * in which case the same editor will be used if a new frame gets bound to the
  62. * text control.
  63. *
  64. * * The anonymous content associated with the text control's frame, including the
  65. * value div (the DIV element responsible for holding the value of the text control)
  66. * and the placeholder div (the DIV element responsible for holding the placeholder
  67. * value of the text control.) These values are stored in the mRootNode and
  68. * mPlaceholderDiv members, respectively. They will be created when a
  69. * frame is bound to the text control. They will be destroyed when the frame is
  70. * unbound from the object. We could try and hold on to the anonymous content
  71. * between different frames, but unfortunately that is not currently possible
  72. * because they are not unbound from the document in time.
  73. *
  74. * * The frame selection controller. This value is stored in the mSelCon member.
  75. * The frame selection controller is responsible for maintaining the selection state
  76. * on a frame. It is created when a frame is bound to the text control element,
  77. * and will be destroy when the frame is being unbound from the text control element.
  78. * It is created alongside with the frame selection object which is stored in the
  79. * mFrameSel member.
  80. *
  81. * * The editor text listener. This value is stored in the mTextListener member.
  82. * Its job is to listen to selection and keyboard events, and act accordingly.
  83. * It is created when an a frame is first bound to the control, and will be destroyed
  84. * when the frame is unbound from the text control element.
  85. *
  86. * * The editor's cached value. This value is stored in the mCachedValue member.
  87. * It is used to improve the performance of append operations to the text
  88. * control. A mutation observer stored in the mMutationObserver has the job of
  89. * invalidating this cache when the anonymous contect containing the value is
  90. * changed.
  91. *
  92. * * The editor's cached selection properties. These vales are stored in the
  93. * mSelectionProperties member, and include the selection's start, end and
  94. * direction. They are only used when there is no frame available for the
  95. * text field.
  96. *
  97. *
  98. * As a general rule, nsTextEditorState objects own the value of the text control, and any
  99. * attempt to retrieve or set the value must be made through those objects. Internally,
  100. * the value can be represented in several different ways, based on the state the control is
  101. * in.
  102. *
  103. * * When the control is first initialized, its value is equal to the default value of
  104. * the DOM node. For <input> text controls, this default value is the value of the
  105. * value attribute. For <textarea> elements, this default value is the value of the
  106. * text node children of the element.
  107. *
  108. * * If the value has been changed through the DOM node (before the editor for the object
  109. * is initialized), the value is stored as a simple string inside the mValue member of
  110. * the nsTextEditorState object.
  111. *
  112. * * If an editor has been initialized for the control, the value is set and retrievd via
  113. * the nsIPlaintextEditor interface, and is internally managed by the editor as the
  114. * native anonymous content tree attached to the control's frame.
  115. *
  116. * * If the text editor state object is unbound from the control's frame, the value is
  117. * transferred to the mValue member variable, and will be managed there until a new
  118. * frame is bound to the text editor state object.
  119. */
  120. class RestoreSelectionState;
  121. class nsTextEditorState : public mozilla::SupportsWeakPtr<nsTextEditorState> {
  122. public:
  123. MOZ_DECLARE_WEAKREFERENCE_TYPENAME(nsTextEditorState)
  124. explicit nsTextEditorState(nsITextControlElement* aOwningElement);
  125. ~nsTextEditorState();
  126. void Traverse(nsCycleCollectionTraversalCallback& cb);
  127. void Unlink();
  128. nsIEditor* GetEditor();
  129. nsISelectionController* GetSelectionController() const;
  130. nsFrameSelection* GetConstFrameSelection();
  131. nsresult BindToFrame(nsTextControlFrame* aFrame);
  132. void UnbindFromFrame(nsTextControlFrame* aFrame);
  133. nsresult PrepareEditor(const nsAString *aValue = nullptr);
  134. void InitializeKeyboardEventListeners();
  135. enum SetValueFlags
  136. {
  137. // The call is for internal processing.
  138. eSetValue_Internal = 0,
  139. // The value is changed by a call of setUserInput() from chrome.
  140. eSetValue_BySetUserInput = 1 << 0,
  141. // The value is changed by changing value attribute of the element or
  142. // something like setRangeText().
  143. eSetValue_ByContent = 1 << 1,
  144. // Whether the value change should be notified to the frame/contet nor not.
  145. eSetValue_Notify = 1 << 2
  146. };
  147. MOZ_MUST_USE bool SetValue(const nsAString& aValue, uint32_t aFlags);
  148. void GetValue(nsAString& aValue, bool aIgnoreWrap) const;
  149. bool HasNonEmptyValue();
  150. // The following methods are for textarea element to use whether default
  151. // value or not.
  152. // XXX We might have to add assertion when it is into editable,
  153. // or reconsider fixing bug 597525 to remove these.
  154. void EmptyValue() { if (mValue) mValue->Truncate(); }
  155. bool IsEmpty() const { return mValue ? mValue->IsEmpty() : true; }
  156. nsresult CreatePlaceholderNode();
  157. mozilla::dom::Element* GetRootNode() {
  158. return mRootNode;
  159. }
  160. mozilla::dom::Element* GetPlaceholderNode() {
  161. return mPlaceholderDiv;
  162. }
  163. bool IsSingleLineTextControl() const {
  164. return mTextCtrlElement->IsSingleLineTextControl();
  165. }
  166. bool IsTextArea() const {
  167. return mTextCtrlElement->IsTextArea();
  168. }
  169. bool IsPlainTextControl() const {
  170. return mTextCtrlElement->IsPlainTextControl();
  171. }
  172. bool IsPasswordTextControl() const {
  173. return mTextCtrlElement->IsPasswordTextControl();
  174. }
  175. int32_t GetCols() {
  176. return mTextCtrlElement->GetCols();
  177. }
  178. int32_t GetWrapCols() {
  179. return mTextCtrlElement->GetWrapCols();
  180. }
  181. int32_t GetRows() {
  182. return mTextCtrlElement->GetRows();
  183. }
  184. // placeholder methods
  185. void UpdatePlaceholderVisibility(bool aNotify);
  186. bool GetPlaceholderVisibility() {
  187. return mPlaceholderVisibility;
  188. }
  189. void UpdatePlaceholderText(bool aNotify);
  190. /**
  191. * Get the maxlength attribute
  192. * @param aMaxLength the value of the max length attr
  193. * @returns false if attr not defined
  194. */
  195. int32_t GetMaxLength();
  196. void ClearValueCache() { mCachedValue.Truncate(); }
  197. void HideSelectionIfBlurred();
  198. struct SelectionProperties {
  199. public:
  200. SelectionProperties() : mStart(0), mEnd(0),
  201. mDirection(nsITextControlFrame::eForward) {}
  202. bool IsDefault() const
  203. {
  204. return mStart == 0 && mEnd == 0 &&
  205. mDirection == nsITextControlFrame::eForward;
  206. }
  207. int32_t GetStart() const
  208. {
  209. return mStart;
  210. }
  211. void SetStart(int32_t value)
  212. {
  213. mIsDirty = true;
  214. mStart = value;
  215. }
  216. int32_t GetEnd() const
  217. {
  218. return mEnd;
  219. }
  220. void SetEnd(int32_t value)
  221. {
  222. mIsDirty = true;
  223. mEnd = value;
  224. }
  225. nsITextControlFrame::SelectionDirection GetDirection() const
  226. {
  227. return mDirection;
  228. }
  229. void SetDirection(nsITextControlFrame::SelectionDirection value)
  230. {
  231. mIsDirty = true;
  232. mDirection = value;
  233. }
  234. // return true only if mStart, mEnd, or mDirection have been modified
  235. bool IsDirty() const
  236. {
  237. return mIsDirty;
  238. }
  239. private:
  240. int32_t mStart, mEnd;
  241. bool mIsDirty = false;
  242. nsITextControlFrame::SelectionDirection mDirection;
  243. };
  244. bool IsSelectionCached() const;
  245. SelectionProperties& GetSelectionProperties();
  246. void SetSelectionProperties(SelectionProperties& aProps);
  247. void WillInitEagerly() { mSelectionRestoreEagerInit = true; }
  248. bool HasNeverInitializedBefore() const { return !mEverInited; }
  249. void UpdateEditableState(bool aNotify) {
  250. if (mRootNode) {
  251. mRootNode->UpdateEditableState(aNotify);
  252. }
  253. }
  254. private:
  255. friend class RestoreSelectionState;
  256. // not copy constructible
  257. nsTextEditorState(const nsTextEditorState&);
  258. // not assignable
  259. void operator= (const nsTextEditorState&);
  260. nsresult CreateRootNode();
  261. void ValueWasChanged(bool aNotify);
  262. void DestroyEditor();
  263. void Clear();
  264. nsresult InitializeRootNode();
  265. void FinishedRestoringSelection();
  266. mozilla::dom::HTMLInputElement* GetParentNumberControl(nsFrame* aFrame) const;
  267. bool EditorHasComposition();
  268. class InitializationGuard {
  269. public:
  270. explicit InitializationGuard(nsTextEditorState& aState) :
  271. mState(aState),
  272. mGuardSet(false)
  273. {
  274. if (!mState.mInitializing) {
  275. mGuardSet = true;
  276. mState.mInitializing = true;
  277. }
  278. }
  279. ~InitializationGuard() {
  280. if (mGuardSet) {
  281. mState.mInitializing = false;
  282. }
  283. }
  284. bool IsInitializingRecursively() const {
  285. return !mGuardSet;
  286. }
  287. private:
  288. nsTextEditorState& mState;
  289. bool mGuardSet;
  290. };
  291. friend class InitializationGuard;
  292. friend class PrepareEditorEvent;
  293. // The text control element owns this object, and ensures that this object
  294. // has a smaller lifetime.
  295. nsITextControlElement* const MOZ_NON_OWNING_REF mTextCtrlElement;
  296. RefPtr<nsTextInputSelectionImpl> mSelCon;
  297. RefPtr<RestoreSelectionState> mRestoringSelection;
  298. nsCOMPtr<nsIEditor> mEditor;
  299. nsCOMPtr<mozilla::dom::Element> mRootNode;
  300. nsCOMPtr<mozilla::dom::Element> mPlaceholderDiv;
  301. nsTextControlFrame* mBoundFrame;
  302. RefPtr<nsTextInputListener> mTextListener;
  303. mozilla::Maybe<nsString> mValue;
  304. RefPtr<nsAnonDivObserver> mMutationObserver;
  305. mutable nsString mCachedValue; // Caches non-hard-wrapped value on a multiline control.
  306. // mValueBeingSet is available only while SetValue() is requesting to commit
  307. // composition. I.e., this is valid only while mIsCommittingComposition is
  308. // true. While active composition is being committed, GetValue() needs
  309. // the latest value which is set by SetValue(). So, this is cache for that.
  310. nsString mValueBeingSet;
  311. SelectionProperties mSelectionProperties;
  312. bool mEverInited; // Have we ever been initialized?
  313. bool mEditorInitialized;
  314. bool mInitializing; // Whether we're in the process of initialization
  315. bool mValueTransferInProgress; // Whether a value is being transferred to the frame
  316. bool mSelectionCached; // Whether mSelectionProperties is valid
  317. mutable bool mSelectionRestoreEagerInit; // Whether we're eager initing because of selection restore
  318. bool mPlaceholderVisibility;
  319. bool mIsCommittingComposition;
  320. };
  321. inline void
  322. ImplCycleCollectionUnlink(nsTextEditorState& aField)
  323. {
  324. aField.Unlink();
  325. }
  326. inline void
  327. ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
  328. nsTextEditorState& aField,
  329. const char* aName,
  330. uint32_t aFlags = 0)
  331. {
  332. aField.Traverse(aCallback);
  333. }
  334. #endif