Bug 1719855 - Take over preventDefaulted infomation for long-tap events to the origin...
[gecko.git] / dom / html / HTMLTextAreaElement.h
blobf0cb2cf9b1f1569eec455669eef5062d68a763a1
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_dom_HTMLTextAreaElement_h
8 #define mozilla_dom_HTMLTextAreaElement_h
10 #include "mozilla/Attributes.h"
11 #include "mozilla/TextControlElement.h"
12 #include "mozilla/TextControlState.h"
13 #include "mozilla/TextEditor.h"
14 #include "mozilla/dom/ConstraintValidation.h"
15 #include "mozilla/dom/HTMLFormElement.h"
16 #include "mozilla/dom/HTMLInputElementBinding.h"
17 #include "nsIControllers.h"
18 #include "nsCOMPtr.h"
19 #include "nsGenericHTMLElement.h"
20 #include "nsStubMutationObserver.h"
21 #include "nsGkAtoms.h"
23 class nsIControllers;
24 class nsPresContext;
26 namespace mozilla {
28 class EventChainPostVisitor;
29 class EventChainPreVisitor;
30 class PresState;
32 namespace dom {
34 class FormData;
36 class HTMLTextAreaElement final : public TextControlElement,
37 public nsStubMutationObserver,
38 public ConstraintValidation {
39 public:
40 using ConstraintValidation::GetValidationMessage;
41 using ValueSetterOption = TextControlState::ValueSetterOption;
42 using ValueSetterOptions = TextControlState::ValueSetterOptions;
44 explicit HTMLTextAreaElement(
45 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
46 FromParser aFromParser = NOT_FROM_PARSER);
48 // nsISupports
49 NS_DECL_ISUPPORTS_INHERITED
51 NS_IMPL_FROMNODE_HTML_WITH_TAG(HTMLTextAreaElement, textarea)
53 int32_t TabIndexDefault() override;
55 // Element
56 bool IsInteractiveHTMLContent() const override { return true; }
58 // nsGenericHTMLElement
59 bool IsDisabledForEvents(WidgetEvent* aEvent) override;
61 // nsGenericHTMLFormElement
62 void SaveState() override;
64 // FIXME: Shouldn't be a CAN_RUN_SCRIPT_BOUNDARY probably?
65 MOZ_CAN_RUN_SCRIPT_BOUNDARY bool RestoreState(PresState*) override;
67 // nsIFormControl
68 MOZ_CAN_RUN_SCRIPT_BOUNDARY
69 NS_IMETHOD Reset() override;
70 NS_IMETHOD SubmitNamesValues(FormData* aFormData) override;
72 void FieldSetDisabledChanged(bool aNotify) override;
74 ElementState IntrinsicState() const override;
76 void SetLastValueChangeWasInteractive(bool);
78 // TextControlElement
79 void SetValueChanged(bool aValueChanged) override;
80 bool IsSingleLineTextControl() const override;
81 bool IsTextArea() const override;
82 bool IsPasswordTextControl() const override;
83 int32_t GetCols() override;
84 int32_t GetWrapCols() override;
85 int32_t GetRows() override;
86 void GetDefaultValueFromContent(nsAString& aValue) override;
87 bool ValueChanged() const override;
88 void GetTextEditorValue(nsAString& aValue, bool aIgnoreWrap) const override;
89 MOZ_CAN_RUN_SCRIPT TextEditor* GetTextEditor() override;
90 TextEditor* GetTextEditorWithoutCreation() override;
91 nsISelectionController* GetSelectionController() override;
92 nsFrameSelection* GetConstFrameSelection() override;
93 TextControlState* GetTextControlState() const override { return mState; }
94 nsresult BindToFrame(nsTextControlFrame* aFrame) override;
95 MOZ_CAN_RUN_SCRIPT void UnbindFromFrame(nsTextControlFrame* aFrame) override;
96 MOZ_CAN_RUN_SCRIPT nsresult CreateEditor() override;
97 void SetPreviewValue(const nsAString& aValue) override;
98 void GetPreviewValue(nsAString& aValue) override;
99 void EnablePreview() override;
100 bool IsPreviewEnabled() override;
101 void InitializeKeyboardEventListeners() override;
102 void OnValueChanged(ValueChangeKind, bool aNewValueEmpty,
103 const nsAString* aKnownNewValue) override;
104 void GetValueFromSetRangeText(nsAString& aValue) override;
105 MOZ_CAN_RUN_SCRIPT nsresult
106 SetValueFromSetRangeText(const nsAString& aValue) override;
107 bool HasCachedSelection() override;
109 // nsIContent
110 nsresult BindToTree(BindContext&, nsINode& aParent) override;
111 void UnbindFromTree(bool aNullParent = true) override;
112 bool ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
113 const nsAString& aValue,
114 nsIPrincipal* aMaybeScriptedPrincipal,
115 nsAttrValue& aResult) override;
116 nsMapRuleToAttributesFunc GetAttributeMappingFunction() const override;
117 nsChangeHint GetAttributeChangeHint(const nsAtom* aAttribute,
118 int32_t aModType) const override;
119 NS_IMETHOD_(bool) IsAttributeMapped(const nsAtom* aAttribute) const override;
121 void GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
122 nsresult PreHandleEvent(EventChainVisitor& aVisitor) override;
123 nsresult PostHandleEvent(EventChainPostVisitor& aVisitor) override;
125 bool IsHTMLFocusable(bool aWithMouse, bool* aIsFocusable,
126 int32_t* aTabIndex) override;
128 void DoneAddingChildren(bool aHaveNotified) override;
130 nsresult Clone(dom::NodeInfo*, nsINode** aResult) const override;
132 MOZ_CAN_RUN_SCRIPT_BOUNDARY
133 nsresult CopyInnerTo(Element* aDest);
136 * Called when an attribute is about to be changed
138 void BeforeSetAttr(int32_t aNameSpaceID, nsAtom* aName,
139 const nsAttrValue* aValue, bool aNotify) override;
141 // nsIMutationObserver
142 NS_DECL_NSIMUTATIONOBSERVER_CHARACTERDATACHANGED
143 NS_DECL_NSIMUTATIONOBSERVER_CONTENTAPPENDED
144 NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED
145 NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED
147 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLTextAreaElement,
148 TextControlElement)
150 // nsIConstraintValidation
151 bool IsTooLong();
152 bool IsTooShort();
153 bool IsValueMissing() const;
154 void UpdateTooLongValidityState();
155 void UpdateTooShortValidityState();
156 void UpdateValueMissingValidityState();
157 void UpdateBarredFromConstraintValidation();
159 // ConstraintValidation
160 nsresult GetValidationMessage(nsAString& aValidationMessage,
161 ValidityStateType aType) override;
163 // Web IDL binding methods
164 void GetAutocomplete(DOMString& aValue);
165 void SetAutocomplete(const nsAString& aValue, ErrorResult& aRv) {
166 SetHTMLAttr(nsGkAtoms::autocomplete, aValue, aRv);
168 uint32_t Cols() { return GetUnsignedIntAttr(nsGkAtoms::cols, DEFAULT_COLS); }
169 void SetCols(uint32_t aCols, ErrorResult& aError) {
170 uint32_t cols = aCols ? aCols : DEFAULT_COLS;
171 SetUnsignedIntAttr(nsGkAtoms::cols, cols, DEFAULT_COLS, aError);
173 void GetDirName(nsAString& aValue) {
174 GetHTMLAttr(nsGkAtoms::dirname, aValue);
176 void SetDirName(const nsAString& aValue, ErrorResult& aError) {
177 SetHTMLAttr(nsGkAtoms::dirname, aValue, aError);
179 bool Disabled() { return GetBoolAttr(nsGkAtoms::disabled); }
180 void SetDisabled(bool aDisabled, ErrorResult& aError) {
181 SetHTMLBoolAttr(nsGkAtoms::disabled, aDisabled, aError);
183 // nsGenericHTMLFormControlElementWithState::GetForm is fine
184 using nsGenericHTMLFormControlElementWithState::GetForm;
185 int32_t MaxLength() const { return GetIntAttr(nsGkAtoms::maxlength, -1); }
186 int32_t UsedMaxLength() const final { return MaxLength(); }
187 void SetMaxLength(int32_t aMaxLength, ErrorResult& aError) {
188 int32_t minLength = MinLength();
189 if (aMaxLength < 0 || (minLength >= 0 && aMaxLength < minLength)) {
190 aError.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
191 } else {
192 SetHTMLIntAttr(nsGkAtoms::maxlength, aMaxLength, aError);
195 int32_t MinLength() const { return GetIntAttr(nsGkAtoms::minlength, -1); }
196 void SetMinLength(int32_t aMinLength, ErrorResult& aError) {
197 int32_t maxLength = MaxLength();
198 if (aMinLength < 0 || (maxLength >= 0 && aMinLength > maxLength)) {
199 aError.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
200 } else {
201 SetHTMLIntAttr(nsGkAtoms::minlength, aMinLength, aError);
204 void GetName(nsAString& aName) { GetHTMLAttr(nsGkAtoms::name, aName); }
205 void SetName(const nsAString& aName, ErrorResult& aError) {
206 SetHTMLAttr(nsGkAtoms::name, aName, aError);
208 void GetPlaceholder(nsAString& aPlaceholder) {
209 GetHTMLAttr(nsGkAtoms::placeholder, aPlaceholder);
211 void SetPlaceholder(const nsAString& aPlaceholder, ErrorResult& aError) {
212 SetHTMLAttr(nsGkAtoms::placeholder, aPlaceholder, aError);
214 bool ReadOnly() { return GetBoolAttr(nsGkAtoms::readonly); }
215 void SetReadOnly(bool aReadOnly, ErrorResult& aError) {
216 SetHTMLBoolAttr(nsGkAtoms::readonly, aReadOnly, aError);
218 bool Required() const { return State().HasState(ElementState::REQUIRED); }
220 MOZ_CAN_RUN_SCRIPT void SetRangeText(const nsAString& aReplacement,
221 ErrorResult& aRv);
223 MOZ_CAN_RUN_SCRIPT void SetRangeText(const nsAString& aReplacement,
224 uint32_t aStart, uint32_t aEnd,
225 SelectionMode aSelectMode,
226 ErrorResult& aRv);
228 void SetRequired(bool aRequired, ErrorResult& aError) {
229 SetHTMLBoolAttr(nsGkAtoms::required, aRequired, aError);
231 uint32_t Rows() {
232 return GetUnsignedIntAttr(nsGkAtoms::rows, DEFAULT_ROWS_TEXTAREA);
234 void SetRows(uint32_t aRows, ErrorResult& aError) {
235 uint32_t rows = aRows ? aRows : DEFAULT_ROWS_TEXTAREA;
236 SetUnsignedIntAttr(nsGkAtoms::rows, rows, DEFAULT_ROWS_TEXTAREA, aError);
238 void GetWrap(nsAString& aWrap) { GetHTMLAttr(nsGkAtoms::wrap, aWrap); }
239 void SetWrap(const nsAString& aWrap, ErrorResult& aError) {
240 SetHTMLAttr(nsGkAtoms::wrap, aWrap, aError);
242 void GetType(nsAString& aType);
243 void GetDefaultValue(nsAString& aDefaultValue, ErrorResult& aError) const;
244 void SetDefaultValue(const nsAString& aDefaultValue, ErrorResult& aError);
245 void GetValue(nsAString& aValue);
247 * ValueEquals() is designed for internal use so that aValue shouldn't
248 * include \r character. It should be handled before calling this with
249 * nsContentUtils::PlatformToDOMLineBreaks().
251 bool ValueEquals(const nsAString& aValue) const;
252 MOZ_CAN_RUN_SCRIPT void SetValue(const nsAString&, ErrorResult&);
254 uint32_t GetTextLength();
256 // Override SetCustomValidity so we update our state properly when it's called
257 // via bindings.
258 void SetCustomValidity(const nsAString& aError);
260 MOZ_CAN_RUN_SCRIPT void Select();
261 Nullable<uint32_t> GetSelectionStart(ErrorResult& aError);
262 MOZ_CAN_RUN_SCRIPT void SetSelectionStart(
263 const Nullable<uint32_t>& aSelectionStart, ErrorResult& aError);
264 Nullable<uint32_t> GetSelectionEnd(ErrorResult& aError);
265 MOZ_CAN_RUN_SCRIPT void SetSelectionEnd(
266 const Nullable<uint32_t>& aSelectionEnd, ErrorResult& aError);
267 void GetSelectionDirection(nsAString& aDirection, ErrorResult& aError);
268 MOZ_CAN_RUN_SCRIPT void SetSelectionDirection(const nsAString& aDirection,
269 ErrorResult& aError);
270 MOZ_CAN_RUN_SCRIPT void SetSelectionRange(
271 uint32_t aSelectionStart, uint32_t aSelectionEnd,
272 const Optional<nsAString>& aDirecton, ErrorResult& aError);
273 nsIControllers* GetControllers(ErrorResult& aError);
274 // XPCOM adapter function widely used throughout code, leaving it as is.
275 nsresult GetControllers(nsIControllers** aResult);
277 MOZ_CAN_RUN_SCRIPT nsIEditor* GetEditorForBindings();
278 bool HasEditor() {
279 MOZ_ASSERT(mState);
280 return !!mState->GetTextEditorWithoutCreation();
283 bool IsInputEventTarget() const { return true; }
285 MOZ_CAN_RUN_SCRIPT_BOUNDARY void SetUserInput(
286 const nsAString& aValue, nsIPrincipal& aSubjectPrincipal);
288 protected:
289 MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual ~HTMLTextAreaElement();
291 // get rid of the compiler warning
292 using nsGenericHTMLFormControlElementWithState::IsSingleLineTextControl;
294 JSObject* WrapNode(JSContext*, JS::Handle<JSObject*> aGivenProto) override;
296 nsCOMPtr<nsIControllers> mControllers;
297 /** Whether or not the value has changed since its default value was given. */
298 bool mValueChanged;
299 /** Whether or not the last change to the value was made interactively by the
300 * user. */
301 bool mLastValueChangeWasInteractive;
302 /** Whether or not we are already handling select event. */
303 bool mHandlingSelect;
304 /** Whether or not we are done adding children (always true if not
305 created by a parser */
306 bool mDoneAddingChildren;
307 /** Whether state restoration should be inhibited in DoneAddingChildren. */
308 bool mInhibitStateRestoration;
309 /** Whether our disabled state has changed from the default **/
310 bool mDisabledChanged;
311 /** Whether we should make :-moz-ui-invalid apply on the element. **/
312 bool mCanShowInvalidUI;
313 /** Whether we should make :-moz-ui-valid apply on the element. **/
314 bool mCanShowValidUI;
315 bool mIsPreviewEnabled;
317 nsContentUtils::AutocompleteAttrState mAutocompleteAttrState;
319 void FireChangeEventIfNeeded();
321 nsString mFocusedValue;
323 /** The state of the text editor (selection controller and the editor) **/
324 TextControlState* mState;
326 NS_IMETHOD SelectAll(nsPresContext* aPresContext);
328 * Get the value, whether it is from the content or the frame.
329 * @param aValue the value [out]
330 * @param aIgnoreWrap whether to ignore the wrap attribute when getting the
331 * value. If this is true, linebreaks will not be inserted even if
332 * wrap=hard.
334 void GetValueInternal(nsAString& aValue, bool aIgnoreWrap) const;
337 * Setting the value.
339 * @param aValue String to set.
340 * @param aOptions See TextControlState::ValueSetterOption.
342 MOZ_CAN_RUN_SCRIPT nsresult
343 SetValueInternal(const nsAString& aValue, const ValueSetterOptions& aOptions);
346 * Common method to call from the various mutation observer methods.
347 * aContent is a content node that's either the one that changed or its
348 * parent; we should only respond to the change if aContent is non-anonymous.
350 void ContentChanged(nsIContent* aContent);
352 void AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
353 const nsAttrValue* aValue, const nsAttrValue* aOldValue,
354 nsIPrincipal* aSubjectPrincipal, bool aNotify) override;
356 void SetDirectionFromValue(bool aNotify,
357 const nsAString* aKnownValue = nullptr);
360 * Return if an element should have a specific validity UI
361 * (with :-moz-ui-invalid and :-moz-ui-valid pseudo-classes).
363 * @return Whether the element should have a validity UI.
365 bool ShouldShowValidityUI() const {
367 * Always show the validity UI if the form has already tried to be submitted
368 * but was invalid.
370 * Otherwise, show the validity UI if the element's value has been changed.
373 if (mForm && mForm->HasEverTriedInvalidSubmit()) {
374 return true;
377 return mValueChanged;
381 * Get the mutable state of the element.
383 bool IsMutable() const;
386 * Returns whether the current value is the empty string.
388 * @return whether the current value is the empty string.
390 bool IsValueEmpty() const {
391 return State().HasState(ElementState::VALUE_EMPTY);
395 * A helper to get the current selection range. Will throw on the ErrorResult
396 * if we have no editor state.
398 void GetSelectionRange(uint32_t* aSelectionStart, uint32_t* aSelectionEnd,
399 ErrorResult& aRv);
401 private:
402 static void MapAttributesIntoRule(const nsMappedAttributes* aAttributes,
403 MappedDeclarations&);
406 } // namespace dom
407 } // namespace mozilla
409 #endif