1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_TextControlElement_h
8 #define mozilla_TextControlElement_h
10 #include "mozilla/dom/FromParser.h"
11 #include "mozilla/dom/NodeInfo.h"
12 #include "nsGenericHTMLElement.h"
15 class nsISelectionController
;
16 class nsFrameSelection
;
17 class nsTextControlFrame
;
22 class TextControlState
;
26 * This abstract class is used for the text control frame to get the editor and
27 * selection controller objects, and some helper properties.
29 class TextControlElement
: public nsGenericHTMLFormControlElementWithState
{
31 TextControlElement(already_AddRefed
<dom::NodeInfo
>&& aNodeInfo
,
32 dom::FromParser aFromParser
, FormControlType aType
)
33 : nsGenericHTMLFormControlElementWithState(std::move(aNodeInfo
),
34 aFromParser
, aType
) {};
36 NS_DECL_ISUPPORTS_INHERITED
37 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(
38 TextControlElement
, nsGenericHTMLFormControlElementWithState
)
41 * Return true always, i.e., even if this is an <input> but the type is not
42 * for a single line text control, this returns true. Use
43 * IsSingleLineTextControlOrTextArea() if you want to know whether this may
44 * work with a TextEditor.
46 bool IsTextControlElement() const final
{ return true; }
48 virtual bool IsSingleLineTextControlOrTextArea() const = 0;
50 NS_IMPL_FROMNODE_HELPER(TextControlElement
, IsTextControlElement())
53 * Tell the control that value has been deliberately changed (or not).
55 virtual void SetValueChanged(bool) = 0;
58 * Find out whether this is a single line text control. (text or password)
59 * @return whether this is a single line text control
61 virtual bool IsSingleLineTextControl() const = 0;
64 * Find out whether this control is a textarea.
65 * @return whether this is a textarea text control
67 virtual bool IsTextArea() const = 0;
70 * Find out whether this is a password control (input type=password)
71 * @return whether this is a password ontrol
73 virtual bool IsPasswordTextControl() const = 0;
76 * Get the cols attribute (if textarea) or a default
77 * @return the number of columns to use
79 virtual Maybe
<int32_t> GetCols() = 0;
80 int32_t GetColsOrDefault() { return GetCols().valueOr(DEFAULT_COLS
); }
83 * Get the column index to wrap at, or -1 if we shouldn't wrap
85 virtual int32_t GetWrapCols() = 0;
88 * Get the rows attribute (if textarea) or a default
89 * @return the number of rows to use
91 virtual int32_t GetRows() = 0;
94 * Get the default value of the text control
96 virtual void GetDefaultValueFromContent(nsAString
& aValue
,
97 bool aForDisplay
) = 0;
100 * Return true if the value of the control has been changed.
102 virtual bool ValueChanged() const = 0;
105 * Returns the used maxlength attribute value.
107 virtual int32_t UsedMaxLength() const = 0;
110 * Get the current value of the text editor.
112 * @param aValue the buffer to retrieve the value in
114 virtual void GetTextEditorValue(nsAString
& aValue
) const = 0;
117 * Get the editor object associated with the text editor.
118 * The return value is null if the control does not support an editor
119 * (for example, if it is a checkbox.)
120 * Note that GetTextEditor() creates editor if it hasn't been created yet.
121 * If you need editor only when the editor is there, you should use
122 * GetTextEditorWithoutCreation().
124 MOZ_CAN_RUN_SCRIPT
virtual TextEditor
* GetTextEditor() = 0;
125 virtual TextEditor
* GetTextEditorWithoutCreation() const = 0;
128 * Get the selection controller object associated with the text editor.
129 * The return value is null if the control does not support an editor
130 * (for example, if it is a checkbox.)
132 virtual nsISelectionController
* GetSelectionController() = 0;
134 virtual nsFrameSelection
* GetConstFrameSelection() = 0;
136 virtual TextControlState
* GetTextControlState() const = 0;
139 * Binds a frame to the text control. This is performed when a frame
140 * is created for the content node.
141 * Be aware, this must be called with script blocker.
143 virtual nsresult
BindToFrame(nsTextControlFrame
* aFrame
) = 0;
146 * Unbinds a frame from the text control. This is performed when a frame
147 * belonging to a content node is destroyed.
149 MOZ_CAN_RUN_SCRIPT
virtual void UnbindFromFrame(
150 nsTextControlFrame
* aFrame
) = 0;
153 * Creates an editor for the text control. This should happen when
154 * a frame has been created for the text control element, but the created
155 * editor may outlive the frame itself.
157 MOZ_CAN_RUN_SCRIPT
virtual nsresult
CreateEditor() = 0;
160 * Update preview value for the text control.
162 virtual void SetPreviewValue(const nsAString
& aValue
) = 0;
165 * Get the current preview value for text control.
167 virtual void GetPreviewValue(nsAString
& aValue
) = 0;
170 * Enable preview or autofilled state for the text control.
172 virtual void SetAutofillState(const nsAString
& aState
) = 0;
175 * Get the current preview or autofilled state for the text control.
177 virtual void GetAutofillState(nsAString
& aState
) = 0;
180 * Enable preview for text control.
182 virtual void EnablePreview() = 0;
185 * Find out whether this control enables preview for form autofoll.
187 virtual bool IsPreviewEnabled() = 0;
190 * Initialize the keyboard event listeners.
192 virtual void InitializeKeyboardEventListeners() = 0;
194 enum class ValueChangeKind
{
201 * Callback called whenever the value is changed.
203 * aKnownNewValue can be used to avoid value lookups if present (might be
204 * null, if the caller doesn't know the specific value that got set).
206 virtual void OnValueChanged(ValueChangeKind
, bool aNewValueEmpty
,
207 const nsAString
* aKnownNewValue
) = 0;
209 void OnValueChanged(ValueChangeKind aKind
, const nsAString
& aNewValue
) {
210 return OnValueChanged(aKind
, aNewValue
.IsEmpty(), &aNewValue
);
214 * Helpers for value manipulation from SetRangeText.
216 virtual void GetValueFromSetRangeText(nsAString
& aValue
) = 0;
217 MOZ_CAN_RUN_SCRIPT
virtual nsresult
SetValueFromSetRangeText(
218 const nsAString
& aValue
) = 0;
220 inline static constexpr int32_t DEFAULT_COLS
= 20;
221 inline static constexpr int32_t DEFAULT_ROWS
= 1;
222 inline static constexpr int32_t DEFAULT_ROWS_TEXTAREA
= 2;
223 inline static constexpr int32_t DEFAULT_UNDO_CAP
= 1000;
225 // wrap can be one of these three values.
227 eHTMLTextWrap_Off
= 1, // "off"
228 eHTMLTextWrap_Hard
= 2, // "hard"
229 eHTMLTextWrap_Soft
= 3 // the default
232 static bool GetWrapPropertyEnum(nsIContent
* aContent
,
233 nsHTMLTextWrap
& aWrapProp
);
236 * Does the editor have a selection cache?
238 * Note that this function has the side effect of making the editor for input
239 * elements be initialized eagerly.
241 virtual bool HasCachedSelection() = 0;
243 static already_AddRefed
<TextControlElement
>
244 GetTextControlElementFromEditingHost(nsIContent
* aHost
);
247 virtual ~TextControlElement() = default;
249 // The focusability state of this form control. eUnfocusable means that it
250 // shouldn't be focused at all, eInactiveWindow means it's in an inactive
251 // window, eActiveWindow means it's in an active window.
252 enum class FocusTristate
{ eUnfocusable
, eInactiveWindow
, eActiveWindow
};
254 // Get our focus state.
255 FocusTristate
FocusState();
258 } // namespace mozilla
260 #endif // mozilla_TextControlElement_h