Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/dom/HTMLTextAreaElement.h
Line
Count
Source (jump to first uncovered line)
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_dom_HTMLTextAreaElement_h
8
#define mozilla_dom_HTMLTextAreaElement_h
9
10
#include "mozilla/Attributes.h"
11
#include "nsITextControlElement.h"
12
#include "nsIControllers.h"
13
#include "nsCOMPtr.h"
14
#include "nsGenericHTMLElement.h"
15
#include "nsStubMutationObserver.h"
16
#include "nsIConstraintValidation.h"
17
#include "mozilla/dom/HTMLFormElement.h"
18
#include "mozilla/dom/HTMLInputElementBinding.h"
19
#include "nsGkAtoms.h"
20
21
#include "mozilla/TextEditor.h"
22
#include "nsTextEditorState.h"
23
24
class nsIControllers;
25
class nsIDocument;
26
class nsPresContext;
27
28
namespace mozilla {
29
30
class EventChainPostVisitor;
31
class EventChainPreVisitor;
32
class EventStates;
33
class PresState;
34
35
namespace dom {
36
37
class HTMLFormSubmission;
38
39
class HTMLTextAreaElement final : public nsGenericHTMLFormElementWithState,
40
                                  public nsITextControlElement,
41
                                  public nsStubMutationObserver,
42
                                  public nsIConstraintValidation
43
{
44
public:
45
  using nsIConstraintValidation::GetValidationMessage;
46
47
  explicit HTMLTextAreaElement(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
48
                               FromParser aFromParser = NOT_FROM_PARSER);
49
50
  // nsISupports
51
  NS_DECL_ISUPPORTS_INHERITED
52
53
  NS_IMPL_FROMNODE_HTML_WITH_TAG(HTMLTextAreaElement, textarea)
54
55
  virtual int32_t TabIndexDefault() override;
56
57
  // Element
58
  virtual bool IsInteractiveHTMLContent(bool aIgnoreTabindex) const override
59
0
  {
60
0
    return true;
61
0
  }
62
63
  // nsIFormControl
64
  NS_IMETHOD Reset() override;
65
  NS_IMETHOD SubmitNamesValues(HTMLFormSubmission* aFormSubmission) override;
66
  NS_IMETHOD SaveState() override;
67
  virtual bool RestoreState(PresState* aState) override;
68
  virtual bool IsDisabledForEvents(EventMessage aMessage) override;
69
70
  virtual void FieldSetDisabledChanged(bool aNotify) override;
71
72
  virtual EventStates IntrinsicState() const override;
73
74
  // nsITextControlElemet
75
  NS_IMETHOD SetValueChanged(bool aValueChanged) override;
76
  NS_IMETHOD_(bool) IsSingleLineTextControl() const override;
77
  NS_IMETHOD_(bool) IsTextArea() const override;
78
  NS_IMETHOD_(bool) IsPasswordTextControl() const override;
79
  NS_IMETHOD_(int32_t) GetCols() override;
80
  NS_IMETHOD_(int32_t) GetWrapCols() override;
81
  NS_IMETHOD_(int32_t) GetRows() override;
82
  NS_IMETHOD_(void) GetDefaultValueFromContent(nsAString& aValue) override;
83
  NS_IMETHOD_(bool) ValueChanged() const override;
84
  NS_IMETHOD_(void) GetTextEditorValue(nsAString& aValue, bool aIgnoreWrap) const override;
85
  NS_IMETHOD_(mozilla::TextEditor*) GetTextEditor() override;
86
  NS_IMETHOD_(nsISelectionController*) GetSelectionController() override;
87
  NS_IMETHOD_(nsFrameSelection*) GetConstFrameSelection() override;
88
  NS_IMETHOD BindToFrame(nsTextControlFrame* aFrame) override;
89
  NS_IMETHOD_(void) UnbindFromFrame(nsTextControlFrame* aFrame) override;
90
  NS_IMETHOD CreateEditor() override;
91
  NS_IMETHOD_(void) UpdateOverlayTextVisibility(bool aNotify) override;
92
  NS_IMETHOD_(bool) GetPlaceholderVisibility() override;
93
  NS_IMETHOD_(bool) GetPreviewVisibility() override;
94
  NS_IMETHOD_(void) SetPreviewValue(const nsAString& aValue) override;
95
  NS_IMETHOD_(void) GetPreviewValue(nsAString& aValue) override;
96
  NS_IMETHOD_(void) EnablePreview() override;
97
  NS_IMETHOD_(bool) IsPreviewEnabled() override;
98
  NS_IMETHOD_(void) InitializeKeyboardEventListeners() override;
99
  NS_IMETHOD_(void) OnValueChanged(bool aNotify, bool aWasInteractiveUserChange) override;
100
  virtual void GetValueFromSetRangeText(nsAString& aValue) override;
101
  virtual nsresult SetValueFromSetRangeText(const nsAString& aValue) override;
102
  NS_IMETHOD_(bool) HasCachedSelection() override;
103
104
105
  // nsIContent
106
  virtual nsresult BindToTree(nsIDocument* aDocument, nsIContent* aParent,
107
                               nsIContent* aBindingParent) override;
108
  virtual void UnbindFromTree(bool aDeep = true,
109
                              bool aNullParent = true) override;
110
  virtual bool ParseAttribute(int32_t aNamespaceID,
111
                                nsAtom* aAttribute,
112
                                const nsAString& aValue,
113
                                nsIPrincipal* aMaybeScriptedPrincipal,
114
                                nsAttrValue& aResult) override;
115
  virtual nsMapRuleToAttributesFunc GetAttributeMappingFunction() const override;
116
  virtual nsChangeHint GetAttributeChangeHint(const nsAtom* aAttribute,
117
                                              int32_t aModType) const override;
118
  NS_IMETHOD_(bool) IsAttributeMapped(const nsAtom* aAttribute) const override;
119
120
  void GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
121
  virtual nsresult PreHandleEvent(EventChainVisitor& aVisitor) override;
122
  virtual nsresult PostHandleEvent(
123
                     EventChainPostVisitor& aVisitor) override;
124
125
  virtual bool IsHTMLFocusable(bool aWithMouse, bool *aIsFocusable, int32_t *aTabIndex) override;
126
127
  virtual void DoneAddingChildren(bool aHaveNotified) override;
128
  virtual bool IsDoneAddingChildren() override;
129
130
  virtual nsresult Clone(dom::NodeInfo*, nsINode** aResult) const override;
131
132
  nsresult CopyInnerTo(Element* aDest);
133
134
  /**
135
   * Called when an attribute is about to be changed
136
   */
137
  virtual nsresult BeforeSetAttr(int32_t aNameSpaceID, nsAtom* aName,
138
                                 const nsAttrValueOrString* aValue,
139
                                 bool aNotify) override;
140
141
  // nsIMutationObserver
142
  NS_DECL_NSIMUTATIONOBSERVER_CHARACTERDATACHANGED
143
  NS_DECL_NSIMUTATIONOBSERVER_CONTENTAPPENDED
144
  NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED
145
  NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED
146
147
  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLTextAreaElement,
148
                                           nsGenericHTMLFormElementWithState)
149
150
  // nsIConstraintValidation
151
  bool     IsTooLong();
152
  bool     IsTooShort();
153
  bool     IsValueMissing() const;
154
  void     UpdateTooLongValidityState();
155
  void     UpdateTooShortValidityState();
156
  void     UpdateValueMissingValidityState();
157
  void     UpdateBarredFromConstraintValidation();
158
  nsresult GetValidationMessage(nsAString& aValidationMessage,
159
                                ValidityStateType aType) override;
160
161
  // Web IDL binding methods
162
  void GetAutocomplete(DOMString& aValue);
163
  void SetAutocomplete(const nsAString& aValue, ErrorResult& aRv)
164
0
  {
165
0
    SetHTMLAttr(nsGkAtoms::autocomplete, aValue, aRv);
166
0
  }
167
  bool Autofocus()
168
0
  {
169
0
    return GetBoolAttr(nsGkAtoms::autofocus);
170
0
  }
171
  void SetAutofocus(bool aAutoFocus, ErrorResult& aError)
172
0
  {
173
0
    SetHTMLBoolAttr(nsGkAtoms::autofocus, aAutoFocus, aError);
174
0
  }
175
  uint32_t Cols()
176
0
  {
177
0
    return GetIntAttr(nsGkAtoms::cols, DEFAULT_COLS);
178
0
  }
179
  void SetCols(uint32_t aCols, ErrorResult& aError)
180
0
  {
181
0
    uint32_t cols = aCols ? aCols : DEFAULT_COLS;
182
0
    SetUnsignedIntAttr(nsGkAtoms::cols, cols, DEFAULT_COLS, aError);
183
0
  }
184
  bool Disabled()
185
0
  {
186
0
    return GetBoolAttr(nsGkAtoms::disabled);
187
0
  }
188
  void SetDisabled(bool aDisabled, ErrorResult& aError)
189
0
  {
190
0
    SetHTMLBoolAttr(nsGkAtoms::disabled, aDisabled, aError);
191
0
  }
192
  // nsGenericHTMLFormElementWithState::GetForm is fine
193
  using nsGenericHTMLFormElementWithState::GetForm;
194
  int32_t MaxLength()
195
0
  {
196
0
    return GetIntAttr(nsGkAtoms::maxlength, -1);
197
0
  }
198
  void SetMaxLength(int32_t aMaxLength, ErrorResult& aError)
199
0
  {
200
0
    int32_t minLength = MinLength();
201
0
    if (aMaxLength < 0 || (minLength >= 0 && aMaxLength < minLength)) {
202
0
      aError.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
203
0
    } else {
204
0
      SetHTMLIntAttr(nsGkAtoms::maxlength, aMaxLength, aError);
205
0
    }
206
0
  }
207
  int32_t MinLength()
208
0
  {
209
0
    return GetIntAttr(nsGkAtoms::minlength, -1);
210
0
  }
211
  void SetMinLength(int32_t aMinLength, ErrorResult& aError)
212
0
  {
213
0
    int32_t maxLength = MaxLength();
214
0
    if (aMinLength < 0 || (maxLength >= 0 && aMinLength > maxLength)) {
215
0
      aError.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
216
0
    } else {
217
0
      SetHTMLIntAttr(nsGkAtoms::minlength, aMinLength, aError);
218
0
    }
219
0
  }
220
  void GetName(nsAString& aName)
221
0
  {
222
0
    GetHTMLAttr(nsGkAtoms::name, aName);
223
0
  }
224
  void SetName(const nsAString& aName, ErrorResult& aError)
225
0
  {
226
0
    SetHTMLAttr(nsGkAtoms::name, aName, aError);
227
0
  }
228
  void GetPlaceholder(nsAString& aPlaceholder)
229
0
  {
230
0
    GetHTMLAttr(nsGkAtoms::placeholder, aPlaceholder);
231
0
  }
232
  void SetPlaceholder(const nsAString& aPlaceholder, ErrorResult& aError)
233
0
  {
234
0
    SetHTMLAttr(nsGkAtoms::placeholder, aPlaceholder, aError);
235
0
  }
236
  bool ReadOnly()
237
0
  {
238
0
    return GetBoolAttr(nsGkAtoms::readonly);
239
0
  }
240
  void SetReadOnly(bool aReadOnly, ErrorResult& aError)
241
0
  {
242
0
    SetHTMLBoolAttr(nsGkAtoms::readonly, aReadOnly, aError);
243
0
  }
244
  bool Required() const
245
0
  {
246
0
    return State().HasState(NS_EVENT_STATE_REQUIRED);
247
0
  }
248
249
  void SetRangeText(const nsAString& aReplacement, ErrorResult& aRv);
250
251
  void SetRangeText(const nsAString& aReplacement, uint32_t aStart,
252
                    uint32_t aEnd, SelectionMode aSelectMode,
253
                    ErrorResult& aRv);
254
255
  void SetRequired(bool aRequired, ErrorResult& aError)
256
0
  {
257
0
    SetHTMLBoolAttr(nsGkAtoms::required, aRequired, aError);
258
0
  }
259
  uint32_t Rows()
260
0
  {
261
0
    return GetIntAttr(nsGkAtoms::rows, DEFAULT_ROWS_TEXTAREA);
262
0
  }
263
  void SetRows(uint32_t aRows, ErrorResult& aError)
264
0
  {
265
0
    uint32_t rows = aRows ? aRows : DEFAULT_ROWS_TEXTAREA;
266
0
    SetUnsignedIntAttr(nsGkAtoms::rows, rows, DEFAULT_ROWS_TEXTAREA, aError);
267
0
  }
268
  void GetWrap(nsAString& aWrap)
269
0
  {
270
0
    GetHTMLAttr(nsGkAtoms::wrap, aWrap);
271
0
  }
272
  void SetWrap(const nsAString& aWrap, ErrorResult& aError)
273
0
  {
274
0
    SetHTMLAttr(nsGkAtoms::wrap, aWrap, aError);
275
0
  }
276
  void GetType(nsAString& aType);
277
  void GetDefaultValue(nsAString& aDefaultValue, ErrorResult& aError);
278
  void SetDefaultValue(const nsAString& aDefaultValue, ErrorResult& aError);
279
  void GetValue(nsAString& aValue);
280
  void SetValue(const nsAString& aValue, ErrorResult& aError);
281
282
  uint32_t GetTextLength();
283
284
  // Override SetCustomValidity so we update our state properly when it's called
285
  // via bindings.
286
  void SetCustomValidity(const nsAString& aError);
287
288
  void Select();
289
  Nullable<uint32_t> GetSelectionStart(ErrorResult& aError);
290
  void SetSelectionStart(const Nullable<uint32_t>& aSelectionStart, ErrorResult& aError);
291
  Nullable<uint32_t> GetSelectionEnd(ErrorResult& aError);
292
  void SetSelectionEnd(const Nullable<uint32_t>& aSelectionEnd, ErrorResult& aError);
293
  void GetSelectionDirection(nsAString& aDirection, ErrorResult& aError);
294
  void SetSelectionDirection(const nsAString& aDirection, ErrorResult& aError);
295
  void SetSelectionRange(uint32_t aSelectionStart, uint32_t aSelectionEnd, const Optional<nsAString>& aDirecton, ErrorResult& aError);
296
  nsIControllers* GetControllers(ErrorResult& aError);
297
  // XPCOM adapter function widely used throughout code, leaving it as is.
298
  nsresult GetControllers(nsIControllers** aResult);
299
300
  nsIEditor* GetEditor()
301
0
  {
302
0
    return mState.GetTextEditor();
303
0
  }
304
305
  void SetUserInput(const nsAString& aValue,
306
                    nsIPrincipal& aSubjectPrincipal);
307
308
protected:
309
0
  virtual ~HTMLTextAreaElement() {}
310
311
  // get rid of the compiler warning
312
  using nsGenericHTMLFormElementWithState::IsSingleLineTextControl;
313
314
  virtual JSObject* WrapNode(JSContext *aCx, JS::Handle<JSObject*> aGivenProto) override;
315
316
  nsCOMPtr<nsIControllers> mControllers;
317
  /** Whether or not the value has changed since its default value was given. */
318
  bool                     mValueChanged;
319
  /** Whether or not the last change to the value was made interactively by the user. */
320
  bool                     mLastValueChangeWasInteractive;
321
  /** Whether or not we are already handling select event. */
322
  bool                     mHandlingSelect;
323
  /** Whether or not we are done adding children (always true if not
324
      created by a parser */
325
  bool                     mDoneAddingChildren;
326
  /** Whether state restoration should be inhibited in DoneAddingChildren. */
327
  bool                     mInhibitStateRestoration;
328
  /** Whether our disabled state has changed from the default **/
329
  bool                     mDisabledChanged;
330
  /** Whether we should make :-moz-ui-invalid apply on the element. **/
331
  bool                     mCanShowInvalidUI;
332
  /** Whether we should make :-moz-ui-valid apply on the element. **/
333
  bool                     mCanShowValidUI;
334
  bool                     mIsPreviewEnabled;
335
336
  nsContentUtils::AutocompleteAttrState mAutocompleteAttrState;
337
338
  void FireChangeEventIfNeeded();
339
340
  nsString mFocusedValue;
341
342
  /** The state of the text editor (selection controller and the editor) **/
343
  nsTextEditorState mState;
344
345
  NS_IMETHOD SelectAll(nsPresContext* aPresContext);
346
  /**
347
   * Get the value, whether it is from the content or the frame.
348
   * @param aValue the value [out]
349
   * @param aIgnoreWrap whether to ignore the wrap attribute when getting the
350
   *        value.  If this is true, linebreaks will not be inserted even if
351
   *        wrap=hard.
352
   */
353
  void GetValueInternal(nsAString& aValue, bool aIgnoreWrap) const;
354
355
  /**
356
   * Setting the value.
357
   *
358
   * @param aValue      String to set.
359
   * @param aFlags      See nsTextEditorState::SetValueFlags.
360
   */
361
  nsresult SetValueInternal(const nsAString& aValue, uint32_t aFlags);
362
363
  /**
364
   * Common method to call from the various mutation observer methods.
365
   * aContent is a content node that's either the one that changed or its
366
   * parent; we should only respond to the change if aContent is non-anonymous.
367
   */
368
  void ContentChanged(nsIContent* aContent);
369
370
  virtual nsresult AfterSetAttr(int32_t aNamespaceID, nsAtom *aName,
371
                                const nsAttrValue* aValue,
372
                                const nsAttrValue* aOldValue,
373
                                nsIPrincipal* aSubjectPrincipal,
374
                                bool aNotify) override;
375
376
  /**
377
   * Return if an element should have a specific validity UI
378
   * (with :-moz-ui-invalid and :-moz-ui-valid pseudo-classes).
379
   *
380
   * @return Whether the element should have a validity UI.
381
   */
382
0
  bool ShouldShowValidityUI() const {
383
0
    /**
384
0
     * Always show the validity UI if the form has already tried to be submitted
385
0
     * but was invalid.
386
0
     *
387
0
     * Otherwise, show the validity UI if the element's value has been changed.
388
0
     */
389
0
390
0
    if (mForm && mForm->HasEverTriedInvalidSubmit()) {
391
0
      return true;
392
0
    }
393
0
394
0
    return mValueChanged;
395
0
  }
396
397
  /**
398
   * Get the mutable state of the element.
399
   */
400
  bool IsMutable() const;
401
402
  /**
403
   * Returns whether the current value is the empty string.
404
   *
405
   * @return whether the current value is the empty string.
406
   */
407
  bool IsValueEmpty() const;
408
409
  /**
410
   * A helper to get the current selection range.  Will throw on the ErrorResult
411
   * if we have no editor state.
412
   */
413
  void GetSelectionRange(uint32_t* aSelectionStart,
414
                         uint32_t* aSelectionEnd,
415
                         ErrorResult& aRv);
416
417
private:
418
  static void MapAttributesIntoRule(const nsMappedAttributes* aAttributes,
419
                                    MappedDeclarations&);
420
};
421
422
} // namespace dom
423
} // namespace mozilla
424
425
#endif
426