Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / html / TextControlElement.h
blobabca4719533045bf0c6a2e3cdc5235301b9b7ed4
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"
14 class nsIContent;
15 class nsISelectionController;
16 class nsFrameSelection;
17 class nsTextControlFrame;
19 namespace mozilla {
21 class ErrorResult;
22 class TextControlState;
23 class TextEditor;
25 /**
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 {
30 public:
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)
40 /**
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())
52 /**
53 * Tell the control that value has been deliberately changed (or not).
55 virtual void SetValueChanged(bool) = 0;
57 /**
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;
63 /**
64 * Find out whether this control is a textarea.
65 * @return whether this is a textarea text control
67 virtual bool IsTextArea() const = 0;
69 /**
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;
75 /**
76 * Get the cols attribute (if textarea) or a default
77 * @return the number of columns to use
79 virtual int32_t GetCols() = 0;
81 /**
82 * Get the column index to wrap at, or -1 if we shouldn't wrap
84 virtual int32_t GetWrapCols() = 0;
86 /**
87 * Get the rows attribute (if textarea) or a default
88 * @return the number of rows to use
90 virtual int32_t GetRows() = 0;
92 /**
93 * Get the default value of the text control
95 virtual void GetDefaultValueFromContent(nsAString& aValue,
96 bool aForDisplay) = 0;
98 /**
99 * Return true if the value of the control has been changed.
101 virtual bool ValueChanged() const = 0;
104 * Returns the used maxlength attribute value.
106 virtual int32_t UsedMaxLength() const = 0;
109 * Get the current value of the text editor.
111 * @param aValue the buffer to retrieve the value in
113 virtual void GetTextEditorValue(nsAString& aValue) const = 0;
116 * Get the editor object associated with the text editor.
117 * The return value is null if the control does not support an editor
118 * (for example, if it is a checkbox.)
119 * Note that GetTextEditor() creates editor if it hasn't been created yet.
120 * If you need editor only when the editor is there, you should use
121 * GetTextEditorWithoutCreation().
123 MOZ_CAN_RUN_SCRIPT virtual TextEditor* GetTextEditor() = 0;
124 virtual TextEditor* GetTextEditorWithoutCreation() const = 0;
127 * Get the selection controller object associated with the text editor.
128 * The return value is null if the control does not support an editor
129 * (for example, if it is a checkbox.)
131 virtual nsISelectionController* GetSelectionController() = 0;
133 virtual nsFrameSelection* GetConstFrameSelection() = 0;
135 virtual TextControlState* GetTextControlState() const = 0;
138 * Binds a frame to the text control. This is performed when a frame
139 * is created for the content node.
140 * Be aware, this must be called with script blocker.
142 virtual nsresult BindToFrame(nsTextControlFrame* aFrame) = 0;
145 * Unbinds a frame from the text control. This is performed when a frame
146 * belonging to a content node is destroyed.
148 MOZ_CAN_RUN_SCRIPT virtual void UnbindFromFrame(
149 nsTextControlFrame* aFrame) = 0;
152 * Creates an editor for the text control. This should happen when
153 * a frame has been created for the text control element, but the created
154 * editor may outlive the frame itself.
156 MOZ_CAN_RUN_SCRIPT virtual nsresult CreateEditor() = 0;
159 * Update preview value for the text control.
161 virtual void SetPreviewValue(const nsAString& aValue) = 0;
164 * Get the current preview value for text control.
166 virtual void GetPreviewValue(nsAString& aValue) = 0;
169 * Enable preview for text control.
171 virtual void EnablePreview() = 0;
174 * Find out whether this control enables preview for form autofoll.
176 virtual bool IsPreviewEnabled() = 0;
179 * Initialize the keyboard event listeners.
181 virtual void InitializeKeyboardEventListeners() = 0;
183 enum class ValueChangeKind {
184 Internal,
185 Script,
186 UserInteraction,
190 * Callback called whenever the value is changed.
192 * aKnownNewValue can be used to avoid value lookups if present (might be
193 * null, if the caller doesn't know the specific value that got set).
195 virtual void OnValueChanged(ValueChangeKind, bool aNewValueEmpty,
196 const nsAString* aKnownNewValue) = 0;
198 void OnValueChanged(ValueChangeKind aKind, const nsAString& aNewValue) {
199 return OnValueChanged(aKind, aNewValue.IsEmpty(), &aNewValue);
203 * Helpers for value manipulation from SetRangeText.
205 virtual void GetValueFromSetRangeText(nsAString& aValue) = 0;
206 MOZ_CAN_RUN_SCRIPT virtual nsresult SetValueFromSetRangeText(
207 const nsAString& aValue) = 0;
209 static const int32_t DEFAULT_COLS = 20;
210 static const int32_t DEFAULT_ROWS = 1;
211 static const int32_t DEFAULT_ROWS_TEXTAREA = 2;
212 static const int32_t DEFAULT_UNDO_CAP = 1000;
214 // wrap can be one of these three values.
215 typedef enum {
216 eHTMLTextWrap_Off = 1, // "off"
217 eHTMLTextWrap_Hard = 2, // "hard"
218 eHTMLTextWrap_Soft = 3 // the default
219 } nsHTMLTextWrap;
221 static bool GetWrapPropertyEnum(nsIContent* aContent,
222 nsHTMLTextWrap& aWrapProp);
225 * Does the editor have a selection cache?
227 * Note that this function has the side effect of making the editor for input
228 * elements be initialized eagerly.
230 virtual bool HasCachedSelection() = 0;
232 static already_AddRefed<TextControlElement>
233 GetTextControlElementFromEditingHost(nsIContent* aHost);
235 protected:
236 virtual ~TextControlElement() = default;
238 // The focusability state of this form control. eUnfocusable means that it
239 // shouldn't be focused at all, eInactiveWindow means it's in an inactive
240 // window, eActiveWindow means it's in an active window.
241 enum class FocusTristate { eUnfocusable, eInactiveWindow, eActiveWindow };
243 // Get our focus state.
244 FocusTristate FocusState();
247 } // namespace mozilla
249 #endif // mozilla_TextControlElement_h