DXR will be turned off on Tuesday, December 29th. It will redirect to Searchfox.
See the announcement on Discourse.

DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Implementation

Mercurial (c68fe15a81fc)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_HTMLTextAreaElement_h
#define mozilla_dom_HTMLTextAreaElement_h

#include "mozilla/Attributes.h"
#include "mozilla/TextControlElement.h"
#include "mozilla/TextControlElement.h"
#include "mozilla/TextControlState.h"
#include "mozilla/TextEditor.h"
#include "mozilla/dom/HTMLFormElement.h"
#include "mozilla/dom/HTMLInputElementBinding.h"
#include "nsIControllers.h"
#include "nsIControllers.h"
#include "nsCOMPtr.h"
#include "nsGenericHTMLElement.h"
#include "nsStubMutationObserver.h"
#include "nsIConstraintValidation.h"
#include "nsGkAtoms.h"
#include "nsGkAtoms.h"

class nsIControllers;
class nsPresContext;

namespace mozilla {
namespace mozilla {


class EventChainPostVisitor;
class EventChainPreVisitor;
class EventStates;
class PresState;


namespace dom {

class HTMLFormSubmission;

class HTMLTextAreaElement final : public TextControlElement,
class HTMLTextAreaElement final : public TextControlElement,
                                  public nsStubMutationObserver,
                                  public nsIConstraintValidation {
 public:
  using nsIConstraintValidation::GetValidationMessage;


  explicit HTMLTextAreaElement(
      already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
      FromParser aFromParser = NOT_FROM_PARSER);


  // nsISupports
  NS_DECL_ISUPPORTS_INHERITED

  NS_IMPL_FROMNODE_HTML_WITH_TAG(HTMLTextAreaElement, textarea)


  virtual int32_t TabIndexDefault() override;

  // Element
  virtual bool IsInteractiveHTMLContent() const override { return true; }


  // nsIFormControl
  MOZ_CAN_RUN_SCRIPT_BOUNDARY
  NS_IMETHOD Reset() override;
  NS_IMETHOD SubmitNamesValues(HTMLFormSubmission* aFormSubmission) override;
  NS_IMETHOD SaveState() override;
  NS_IMETHOD SaveState() override;
  virtual bool RestoreState(PresState* aState) override;
  virtual bool IsDisabledForEvents(WidgetEvent* aEvent) override;

  virtual void FieldSetDisabledChanged(bool aNotify) override;
  virtual void FieldSetDisabledChanged(bool aNotify) override;

  virtual EventStates IntrinsicState() const override;

  // TextControlElement
  virtual nsresult SetValueChanged(bool aValueChanged) override;
  virtual nsresult SetValueChanged(bool aValueChanged) override;
  virtual bool IsSingleLineTextControl() const override;
  virtual bool IsTextArea() const override;
  virtual bool IsPasswordTextControl() const override;
  virtual int32_t GetCols() override;
  virtual int32_t GetWrapCols() override;
  virtual int32_t GetWrapCols() override;
  virtual int32_t GetRows() override;
  virtual void GetDefaultValueFromContent(nsAString& aValue) override;
  virtual bool ValueChanged() const override;
  virtual void GetTextEditorValue(nsAString& aValue,
                                  bool aIgnoreWrap) const override;
                                  bool aIgnoreWrap) const override;
  MOZ_CAN_RUN_SCRIPT virtual TextEditor* GetTextEditor() override;
  virtual TextEditor* GetTextEditorWithoutCreation() override;
  virtual nsISelectionController* GetSelectionController() override;
  virtual nsFrameSelection* GetConstFrameSelection() override;
  virtual TextControlState* GetTextControlState() const override {
    return mState;
  }
  virtual nsresult BindToFrame(nsTextControlFrame* aFrame) override;
  MOZ_CAN_RUN_SCRIPT virtual void UnbindFromFrame(
      nsTextControlFrame* aFrame) override;
  MOZ_CAN_RUN_SCRIPT virtual nsresult CreateEditor() override;
  virtual void UpdateOverlayTextVisibility(bool aNotify) override;
  virtual void UpdateOverlayTextVisibility(bool aNotify) override;
  virtual bool GetPlaceholderVisibility() override;
  virtual bool GetPreviewVisibility() override;
  virtual void SetPreviewValue(const nsAString& aValue) override;
  virtual void GetPreviewValue(nsAString& aValue) override;
  virtual void EnablePreview() override;
  virtual void EnablePreview() override;
  virtual bool IsPreviewEnabled() override;
  virtual void InitializeKeyboardEventListeners() override;
  virtual void OnValueChanged(ValueChangeKind) override;
  virtual void GetValueFromSetRangeText(nsAString& aValue) override;
  MOZ_CAN_RUN_SCRIPT virtual nsresult SetValueFromSetRangeText(
  MOZ_CAN_RUN_SCRIPT virtual nsresult SetValueFromSetRangeText(
      const nsAString& aValue) override;
  virtual bool HasCachedSelection() override;

  // nsIContent
  virtual nsresult BindToTree(BindContext&, nsINode& aParent) override;
  virtual nsresult BindToTree(BindContext&, nsINode& aParent) override;
  virtual void UnbindFromTree(bool aNullParent = true) override;
  virtual bool ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
                              const nsAString& aValue,
                              nsIPrincipal* aMaybeScriptedPrincipal,
                              nsIPrincipal* aMaybeScriptedPrincipal,
                              nsAttrValue& aResult) override;
  virtual nsMapRuleToAttributesFunc GetAttributeMappingFunction()
      const override;
  virtual nsChangeHint GetAttributeChangeHint(const nsAtom* aAttribute,
  virtual nsChangeHint GetAttributeChangeHint(const nsAtom* aAttribute,
                                              int32_t aModType) const override;
  NS_IMETHOD_(bool) IsAttributeMapped(const nsAtom* aAttribute) const override;

  void GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
  virtual nsresult PreHandleEvent(EventChainVisitor& aVisitor) override;
  virtual nsresult PreHandleEvent(EventChainVisitor& aVisitor) override;
  virtual nsresult PostHandleEvent(EventChainPostVisitor& aVisitor) override;

  virtual bool IsHTMLFocusable(bool aWithMouse, bool* aIsFocusable,
                               int32_t* aTabIndex) override;


  virtual void DoneAddingChildren(bool aHaveNotified) override;
  virtual bool IsDoneAddingChildren() override;

  MOZ_CAN_RUN_SCRIPT_BOUNDARY
  virtual nsresult Clone(dom::NodeInfo*, nsINode** aResult) const override;
  virtual nsresult Clone(dom::NodeInfo*, nsINode** aResult) const override;

  nsresult CopyInnerTo(Element* aDest);

  /**
  /**
   * Called when an attribute is about to be changed
   */
  virtual nsresult BeforeSetAttr(int32_t aNameSpaceID, nsAtom* aName,
                                 const nsAttrValueOrString* aValue,
                                 bool aNotify) override;
                                 bool aNotify) override;

  // nsIMutationObserver
  NS_DECL_NSIMUTATIONOBSERVER_CHARACTERDATACHANGED
  NS_DECL_NSIMUTATIONOBSERVER_CONTENTAPPENDED
  NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED
  NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED
  NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED

  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLTextAreaElement,
                                           TextControlElement)


  // nsIConstraintValidation
  bool IsTooLong();
  bool IsTooShort();
  bool IsValueMissing() const;
  void UpdateTooLongValidityState();
  void UpdateTooLongValidityState();
  void UpdateTooShortValidityState();
  void UpdateValueMissingValidityState();
  void UpdateBarredFromConstraintValidation();
  nsresult GetValidationMessage(nsAString& aValidationMessage,
  nsresult GetValidationMessage(nsAString& aValidationMessage,
                                ValidityStateType aType) override;

  // Web IDL binding methods
  // Web IDL binding methods
  void GetAutocomplete(DOMString& aValue);
  void SetAutocomplete(const nsAString& aValue, ErrorResult& aRv) {
    SetHTMLAttr(nsGkAtoms::autocomplete, aValue, aRv);
  }
  bool Autofocus() { return GetBoolAttr(nsGkAtoms::autofocus); }
  bool Autofocus() { return GetBoolAttr(nsGkAtoms::autofocus); }
  void SetAutofocus(bool aAutoFocus, ErrorResult& aError) {
    SetHTMLBoolAttr(nsGkAtoms::autofocus, aAutoFocus, aError);
  }
  uint32_t Cols() { return GetUnsignedIntAttr(nsGkAtoms::cols, DEFAULT_COLS); }
  void SetCols(uint32_t aCols, ErrorResult& aError) {
  void SetCols(uint32_t aCols, ErrorResult& aError) {
    uint32_t cols = aCols ? aCols : DEFAULT_COLS;
    SetUnsignedIntAttr(nsGkAtoms::cols, cols, DEFAULT_COLS, aError);
  }
  bool Disabled() { return GetBoolAttr(nsGkAtoms::disabled); }
  void SetDisabled(bool aDisabled, ErrorResult& aError) {
  void SetDisabled(bool aDisabled, ErrorResult& aError) {
    SetHTMLBoolAttr(nsGkAtoms::disabled, aDisabled, aError);
  }
  // nsGenericHTMLFormElementWithState::GetForm is fine
  using nsGenericHTMLFormElementWithState::GetForm;
  int32_t MaxLength() const { return GetIntAttr(nsGkAtoms::maxlength, -1); }
  int32_t MaxLength() const { return GetIntAttr(nsGkAtoms::maxlength, -1); }
  int32_t UsedMaxLength() const final { return MaxLength(); }
  void SetMaxLength(int32_t aMaxLength, ErrorResult& aError) {
    int32_t minLength = MinLength();
    if (aMaxLength < 0 || (minLength >= 0 && aMaxLength < minLength)) {
      aError.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
      aError.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
    } else {
      SetHTMLIntAttr(nsGkAtoms::maxlength, aMaxLength, aError);
    }
  }
  int32_t MinLength() const { return GetIntAttr(nsGkAtoms::minlength, -1); }
  int32_t MinLength() const { return GetIntAttr(nsGkAtoms::minlength, -1); }
  void SetMinLength(int32_t aMinLength, ErrorResult& aError) {
    int32_t maxLength = MaxLength();
    if (aMinLength < 0 || (maxLength >= 0 && aMinLength > maxLength)) {
      aError.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
      aError.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
    } else {
      SetHTMLIntAttr(nsGkAtoms::minlength, aMinLength, aError);
    }
  }
  void GetName(nsAString& aName) { GetHTMLAttr(nsGkAtoms::name, aName); }
  void GetName(nsAString& aName) { GetHTMLAttr(nsGkAtoms::name, aName); }
  void SetName(const nsAString& aName, ErrorResult& aError) {
    SetHTMLAttr(nsGkAtoms::name, aName, aError);
  }
  void GetPlaceholder(nsAString& aPlaceholder) {
    GetHTMLAttr(nsGkAtoms::placeholder, aPlaceholder);
    GetHTMLAttr(nsGkAtoms::placeholder, aPlaceholder);
  }
  void SetPlaceholder(const nsAString& aPlaceholder, ErrorResult& aError) {
    SetHTMLAttr(nsGkAtoms::placeholder, aPlaceholder, aError);
  }
  bool ReadOnly() { return GetBoolAttr(nsGkAtoms::readonly); }
  void SetReadOnly(bool aReadOnly, ErrorResult& aError) {
  void SetReadOnly(bool aReadOnly, ErrorResult& aError) {
    SetHTMLBoolAttr(nsGkAtoms::readonly, aReadOnly, aError);
  }
  bool Required() const { return State().HasState(NS_EVENT_STATE_REQUIRED); }

  MOZ_CAN_RUN_SCRIPT void SetRangeText(const nsAString& aReplacement,
  MOZ_CAN_RUN_SCRIPT void SetRangeText(const nsAString& aReplacement,
                                       ErrorResult& aRv);

  MOZ_CAN_RUN_SCRIPT void SetRangeText(const nsAString& aReplacement,
                                       uint32_t aStart, uint32_t aEnd,
                                       SelectionMode aSelectMode,
                                       SelectionMode aSelectMode,
                                       ErrorResult& aRv);

  void SetRequired(bool aRequired, ErrorResult& aError) {
    SetHTMLBoolAttr(nsGkAtoms::required, aRequired, aError);
    SetHTMLBoolAttr(nsGkAtoms::required, aRequired, aError);
  }
  uint32_t Rows() {
    return GetUnsignedIntAttr(nsGkAtoms::rows, DEFAULT_ROWS_TEXTAREA);
  }
  void SetRows(uint32_t aRows, ErrorResult& aError) {
  void SetRows(uint32_t aRows, ErrorResult& aError) {
    uint32_t rows = aRows ? aRows : DEFAULT_ROWS_TEXTAREA;
    SetUnsignedIntAttr(nsGkAtoms::rows, rows, DEFAULT_ROWS_TEXTAREA, aError);
  }
  void GetWrap(nsAString& aWrap) { GetHTMLAttr(nsGkAtoms::wrap, aWrap); }
  void SetWrap(const nsAString& aWrap, ErrorResult& aError) {
  void SetWrap(const nsAString& aWrap, ErrorResult& aError) {
    SetHTMLAttr(nsGkAtoms::wrap, aWrap, aError);
  }
  void GetType(nsAString& aType);
  void GetDefaultValue(nsAString& aDefaultValue, ErrorResult& aError);
  void SetDefaultValue(const nsAString& aDefaultValue, ErrorResult& aError);
  void SetDefaultValue(const nsAString& aDefaultValue, ErrorResult& aError);
  void GetValue(nsAString& aValue);
  /**
   * ValueEquals() is designed for internal use so that aValue shouldn't
   * include \r character.  It should be handled before calling this with
   * include \r character.  It should be handled before calling this with
   * nsContentUtils::PlatformToDOMLineBreaks().
   */
  bool ValueEquals(const nsAString& aValue) const;
  MOZ_CAN_RUN_SCRIPT_BOUNDARY void SetValue(const nsAString& aValue,
                                            ErrorResult& aError);
                                            ErrorResult& aError);

  uint32_t GetTextLength();

  // Override SetCustomValidity so we update our state properly when it's called
  // via bindings.
  // via bindings.
  void SetCustomValidity(const nsAString& aError);

  void Select();
  Nullable<uint32_t> GetSelectionStart(ErrorResult& aError);
  MOZ_CAN_RUN_SCRIPT void SetSelectionStart(
  MOZ_CAN_RUN_SCRIPT void SetSelectionStart(
      const Nullable<uint32_t>& aSelectionStart, ErrorResult& aError);
  Nullable<uint32_t> GetSelectionEnd(ErrorResult& aError);
  MOZ_CAN_RUN_SCRIPT void SetSelectionEnd(
  MOZ_CAN_RUN_SCRIPT void SetSelectionEnd(
      const Nullable<uint32_t>& aSelectionEnd, ErrorResult& aError);
      const Nullable<uint32_t>& aSelectionEnd, ErrorResult& aError);
  void GetSelectionDirection(nsAString& aDirection, ErrorResult& aError);
  MOZ_CAN_RUN_SCRIPT void SetSelectionDirection(const nsAString& aDirection,
                                                ErrorResult& aError);
                                                ErrorResult& aError);
  MOZ_CAN_RUN_SCRIPT void SetSelectionRange(
      uint32_t aSelectionStart, uint32_t aSelectionEnd,
      const Optional<nsAString>& aDirecton, ErrorResult& aError);
  nsIControllers* GetControllers(ErrorResult& aError);
  // XPCOM adapter function widely used throughout code, leaving it as is.
  nsresult GetControllers(nsIControllers** aResult);
  nsresult GetControllers(nsIControllers** aResult);

  MOZ_CAN_RUN_SCRIPT nsIEditor* GetEditorForBindings();
  bool HasEditor() {
  bool HasEditor() {
    MOZ_ASSERT(mState);
    MOZ_ASSERT(mState);
    return !!mState->GetTextEditorWithoutCreation();
  }


  bool IsInputEventTarget() const { return true; }

  MOZ_CAN_RUN_SCRIPT_BOUNDARY void SetUserInput(
      const nsAString& aValue, nsIPrincipal& aSubjectPrincipal);


 protected:
  MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual ~HTMLTextAreaElement();

  // get rid of the compiler warning
  using nsGenericHTMLFormElementWithState::IsSingleLineTextControl;
  using nsGenericHTMLFormElementWithState::IsSingleLineTextControl;

  virtual JSObject* WrapNode(JSContext* aCx,
                             JS::Handle<JSObject*> aGivenProto) override;

  nsCOMPtr<nsIControllers> mControllers;
  nsCOMPtr<nsIControllers> mControllers;
  /** Whether or not the value has changed since its default value was given. */
  bool mValueChanged;
  /** Whether or not the last change to the value was made interactively by the
   * user. */
  bool mLastValueChangeWasInteractive;
  bool mLastValueChangeWasInteractive;
  /** Whether or not we are already handling select event. */
  bool mHandlingSelect;
  /** Whether or not we are done adding children (always true if not
      created by a parser */
      created by a parser */
  bool mDoneAddingChildren;
  /** Whether state restoration should be inhibited in DoneAddingChildren. */
  bool mInhibitStateRestoration;
  /** Whether our disabled state has changed from the default **/
  bool mDisabledChanged;
  bool mDisabledChanged;
  /** Whether we should make :-moz-ui-invalid apply on the element. **/
  bool mCanShowInvalidUI;
  /** Whether we should make :-moz-ui-valid apply on the element. **/
  bool mCanShowValidUI;
  bool mIsPreviewEnabled;
  bool mIsPreviewEnabled;

  nsContentUtils::AutocompleteAttrState mAutocompleteAttrState;

  void FireChangeEventIfNeeded();


  nsString mFocusedValue;

  /** The state of the text editor (selection controller and the editor) **/
  TextControlState* mState;


  NS_IMETHOD SelectAll(nsPresContext* aPresContext);
  /**
   * Get the value, whether it is from the content or the frame.
   * @param aValue the value [out]
   * @param aIgnoreWrap whether to ignore the wrap attribute when getting the
   * @param aIgnoreWrap whether to ignore the wrap attribute when getting the
   *        value.  If this is true, linebreaks will not be inserted even if
   *        wrap=hard.
   */
  void GetValueInternal(nsAString& aValue, bool aIgnoreWrap) const;
  void GetValueInternal(nsAString& aValue, bool aIgnoreWrap) const;

  /**
   * Setting the value.
   *
   * @param aValue      String to set.
   * @param aValue      String to set.
   * @param aFlags      See TextControlState::SetValueFlags.
   */
   */
  MOZ_CAN_RUN_SCRIPT
  nsresult SetValueInternal(const nsAString& aValue, uint32_t aFlags);

  /**
   * Common method to call from the various mutation observer methods.
   * Common method to call from the various mutation observer methods.
   * aContent is a content node that's either the one that changed or its
   * parent; we should only respond to the change if aContent is non-anonymous.
   */
  void ContentChanged(nsIContent* aContent);


  virtual nsresult AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
                                const nsAttrValue* aValue,
                                const nsAttrValue* aOldValue,
                                nsIPrincipal* aSubjectPrincipal,
                                bool aNotify) override;
                                bool aNotify) override;

  /**
   * Return if an element should have a specific validity UI
   * (with :-moz-ui-invalid and :-moz-ui-valid pseudo-classes).
   * (with :-moz-ui-invalid and :-moz-ui-valid pseudo-classes).
   *
   * @return Whether the element should have a validity UI.
   */
  bool ShouldShowValidityUI() const {
    /**
    /**
     * Always show the validity UI if the form has already tried to be submitted
     * but was invalid.
     *
     * Otherwise, show the validity UI if the element's value has been changed.
     */
     */

    if (mForm && mForm->HasEverTriedInvalidSubmit()) {
      return true;
      return true;
    }

    return mValueChanged;
  }


  /**
   * Get the mutable state of the element.
   */
  bool IsMutable() const;
  bool IsMutable() const;

  /**
   * Returns whether the current value is the empty string.
   *
   * @return whether the current value is the empty string.
   * @return whether the current value is the empty string.
   */
  bool IsValueEmpty() const;

  /**
   * A helper to get the current selection range.  Will throw on the ErrorResult
   * A helper to get the current selection range.  Will throw on the ErrorResult
   * if we have no editor state.
   */
  void GetSelectionRange(uint32_t* aSelectionStart, uint32_t* aSelectionEnd,
                         ErrorResult& aRv);


 private:
  static void MapAttributesIntoRule(const nsMappedAttributes* aAttributes,
                                    MappedDeclarations&);
};


}  // namespace dom
}  // namespace mozilla

#endif