TextControlElement.h (7827B)
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/. */ 6 7 #ifndef mozilla_TextControlElement_h 8 #define mozilla_TextControlElement_h 9 10 #include "mozilla/dom/FromParser.h" 11 #include "mozilla/dom/NodeInfo.h" 12 #include "nsGenericHTMLElement.h" 13 14 class nsIContent; 15 class nsISelectionController; 16 class nsFrameSelection; 17 class nsTextControlFrame; 18 19 namespace mozilla { 20 21 class ErrorResult; 22 class TextControlState; 23 class TextEditor; 24 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. 28 */ 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) {}; 35 36 NS_DECL_ISUPPORTS_INHERITED 37 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED( 38 TextControlElement, nsGenericHTMLFormControlElementWithState) 39 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. 45 */ 46 bool IsTextControlElement() const final { return true; } 47 48 virtual bool IsSingleLineTextControlOrTextArea() const = 0; 49 50 NS_IMPL_FROMNODE_HELPER(TextControlElement, IsTextControlElement()) 51 52 /** 53 * Tell the control that value has been deliberately changed (or not). 54 */ 55 virtual void SetValueChanged(bool) = 0; 56 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 60 */ 61 virtual bool IsSingleLineTextControl() const = 0; 62 63 /** 64 * Find out whether this control is a textarea. 65 * @return whether this is a textarea text control 66 */ 67 virtual bool IsTextArea() const = 0; 68 69 /** 70 * Find out whether this is a password control (input type=password) 71 * @return whether this is a password ontrol 72 */ 73 virtual bool IsPasswordTextControl() const = 0; 74 75 /** 76 * Get the cols attribute (if textarea) or a default 77 * @return the number of columns to use 78 */ 79 virtual Maybe<int32_t> GetCols() = 0; 80 int32_t GetColsOrDefault() { return GetCols().valueOr(DEFAULT_COLS); } 81 82 /** 83 * Get the column index to wrap at, or -1 if we shouldn't wrap 84 */ 85 virtual int32_t GetWrapCols() = 0; 86 87 /** 88 * Get the rows attribute (if textarea) or a default 89 * @return the number of rows to use 90 */ 91 virtual int32_t GetRows() = 0; 92 93 /** 94 * Get the default value of the text control 95 */ 96 virtual void GetDefaultValueFromContent(nsAString& aValue, 97 bool aForDisplay) = 0; 98 99 /** 100 * Return true if the value of the control has been changed. 101 */ 102 virtual bool ValueChanged() const = 0; 103 104 /** 105 * Returns the used maxlength attribute value. 106 */ 107 virtual int32_t UsedMaxLength() const = 0; 108 109 /** 110 * Get the current value of the text editor. 111 * 112 * @param aValue the buffer to retrieve the value in 113 */ 114 virtual void GetTextEditorValue(nsAString& aValue) const = 0; 115 116 /** 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 * GetExtantTextEditor(). 123 */ 124 MOZ_CAN_RUN_SCRIPT virtual TextEditor* GetTextEditor() = 0; 125 virtual TextEditor* GetExtantTextEditor() const = 0; 126 127 /** 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.) 131 */ 132 virtual nsISelectionController* GetSelectionController() = 0; 133 134 virtual nsFrameSelection* GetIndependentFrameSelection() const = 0; 135 136 virtual TextControlState* GetTextControlState() const = 0; 137 138 /** 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. 142 */ 143 virtual nsresult BindToFrame(nsTextControlFrame* aFrame) = 0; 144 145 /** 146 * Unbinds a frame from the text control. This is performed when a frame 147 * belonging to a content node is destroyed. 148 */ 149 MOZ_CAN_RUN_SCRIPT virtual void UnbindFromFrame( 150 nsTextControlFrame* aFrame) = 0; 151 152 /** 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. 156 */ 157 MOZ_CAN_RUN_SCRIPT virtual nsresult CreateEditor() = 0; 158 159 /** 160 * Update preview value for the text control. 161 */ 162 virtual void SetPreviewValue(const nsAString& aValue) = 0; 163 164 /** 165 * Get the current preview value for text control. 166 */ 167 virtual void GetPreviewValue(nsAString& aValue) = 0; 168 169 /** 170 * Enable preview or autofilled state for the text control. 171 */ 172 virtual void SetAutofillState(const nsAString& aState) = 0; 173 174 /** 175 * Get the current preview or autofilled state for the text control. 176 */ 177 virtual void GetAutofillState(nsAString& aState) = 0; 178 179 /** 180 * Enable preview for text control. 181 */ 182 virtual void EnablePreview() = 0; 183 184 /** 185 * Find out whether this control enables preview for form autofoll. 186 */ 187 virtual bool IsPreviewEnabled() = 0; 188 189 /** 190 * Initialize the keyboard event listeners. 191 */ 192 virtual void InitializeKeyboardEventListeners() = 0; 193 194 enum class ValueChangeKind { 195 Internal, 196 Script, 197 UserInteraction, 198 }; 199 200 /** 201 * Callback called whenever the value is changed. 202 * 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). 205 */ 206 virtual void OnValueChanged(ValueChangeKind, bool aNewValueEmpty, 207 const nsAString* aKnownNewValue) = 0; 208 209 void OnValueChanged(ValueChangeKind aKind, const nsAString& aNewValue) { 210 return OnValueChanged(aKind, aNewValue.IsEmpty(), &aNewValue); 211 } 212 213 /** 214 * Helpers for value manipulation from SetRangeText. 215 */ 216 virtual void GetValueFromSetRangeText(nsAString& aValue) = 0; 217 MOZ_CAN_RUN_SCRIPT virtual nsresult SetValueFromSetRangeText( 218 const nsAString& aValue) = 0; 219 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; 224 225 /** 226 * Does the editor have a selection cache? 227 * 228 * Note that this function has the side effect of making the editor for input 229 * elements be initialized eagerly. 230 */ 231 virtual bool HasCachedSelection() = 0; 232 233 static already_AddRefed<TextControlElement> 234 GetTextControlElementFromEditingHost(nsIContent* aHost); 235 236 protected: 237 virtual ~TextControlElement() = default; 238 239 // The focusability state of this form control. eUnfocusable means that it 240 // shouldn't be focused at all, eInactiveWindow means it's in an inactive 241 // window, eActiveWindow means it's in an active window. 242 enum class FocusTristate { eUnfocusable, eInactiveWindow, eActiveWindow }; 243 244 // Get our focus state. 245 FocusTristate FocusState(); 246 }; 247 248 } // namespace mozilla 249 250 #endif // mozilla_TextControlElement_h