summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/html/forms/listed_element.h
blob: bd6071b5303b63a9b5e8ed471e17c245d7df47f2 (plain)
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
/*
 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
 *           (C) 2000 Dirk Mueller (mueller@kde.org)
 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights
 * reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_HTML_FORMS_LISTED_ELEMENT_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_HTML_FORMS_LISTED_ELEMENT_H_

#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/text/text_direction.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

namespace blink {

class ContainerNode;
class Document;
class Element;
class FormAttributeTargetObserver;
class FormControlState;
class FormData;
class HTMLElement;
class HTMLFormElement;
class Node;
class ValidationMessageClient;
class ValidityState;

// https://html.spec.whatwg.org/C/#category-listed
class CORE_EXPORT ListedElement : public GarbageCollectedMixin {
 public:
  virtual ~ListedElement();
  // Returns a valid ListedElement pointer if the specified element is an
  // instance of a ListedElement subclass, or a form-associated custom element.
  // Returns nullptr otherwise.
  static ListedElement* From(Element& element);

  // Cast |this| to HTMLElement, or return the target element associated
  // to ElementInternals.
  const HTMLElement& ToHTMLElement() const;
  HTMLElement& ToHTMLElement();

  static HTMLFormElement* FindAssociatedForm(const HTMLElement*,
                                             const AtomicString& form_id,
                                             HTMLFormElement* form_ancestor);
  HTMLFormElement* Form() const { return form_.Get(); }
  ValidityState* validity();

  virtual bool IsFormControlElement() const = 0;
  virtual bool IsFormControlElementWithState() const;
  virtual bool IsElementInternals() const;
  virtual bool IsEnumeratable() const = 0;

  // Returns the 'name' attribute value. If this element has no name
  // attribute, it returns an empty string instead of null string.
  // Note that the 'name' IDL attribute doesn't use this function.
  virtual const AtomicString& GetName() const;

  // Override in derived classes to get the encoded name=value pair for
  // submitting.
  virtual void AppendToFormData(FormData&) {}

  void ResetFormOwner();

  void FormRemovedFromTree(const Node& form_root);

  bool WillValidate() const;

  // ValidityState attribute implementations
  virtual bool CustomError() const;

  // Functions for ValidityState interface methods.
  // Override functions for PatterMismatch, RangeOverflow, RangerUnderflow,
  // StepMismatch, TooLong, TooShort and ValueMissing must call WillValidate
  // method.
  virtual bool HasBadInput() const;
  virtual bool PatternMismatch() const;
  virtual bool RangeOverflow() const;
  virtual bool RangeUnderflow() const;
  virtual bool StepMismatch() const;
  virtual bool TooLong() const;
  virtual bool TooShort() const;
  virtual bool TypeMismatch() const;
  virtual bool ValueMissing() const;
  bool Valid() const;

  using List = HeapVector<Member<ListedElement>>;

  virtual String validationMessage() const;
  virtual String ValidationSubMessage() const;
  virtual void setCustomValidity(const String&);
  void UpdateVisibleValidationMessage();
  void HideVisibleValidationMessage();
  bool checkValidity(List* unhandled_invalid_controls = nullptr);
  bool reportValidity();
  // This must be called only after the caller check the element is focusable.
  void ShowValidationMessage();
  bool IsValidationMessageVisible() const;
  void FindCustomValidationMessageTextDirection(const String& message,
                                                TextDirection& message_dir,
                                                String& sub_message,
                                                TextDirection& sub_message_dir);
  virtual Element& ValidationAnchor() const;
  bool ValidationAnchorOrHostIsFocusable() const;

  // For Element::IsValidElement(), which is for :valid :invalid selectors.
  bool IsValidElement();
  // Returns true if
  //  - this is not a candidate for constraint validation, or
  //    https://html.spec.whatwg.org/C/#candidate-for-constraint-validation
  //  - this satisfies its constraints
  //    https://html.spec.whatwg.org/C/#concept-fv-valid
  bool IsNotCandidateOrValid();

  // This must be called when a validation constraint or control value is
  // changed.
  void SetNeedsValidityCheck();

  // This should be called when |disabled| content attribute is changed.
  virtual void DisabledAttributeChanged();
  // Override this if you want to know 'disabled' state changes immediately.
  virtual void DisabledStateMightBeChanged() {}
  // This should be called when |form| content attribute is changed.
  void FormAttributeChanged();
  // This is for FormAttributeTargteObserver class.
  void FormAttributeTargetChanged();
  // This should be called in Node::InsertedInto().
  void InsertedInto(ContainerNode&);
  // This should be called in Node::RemovedFrom().
  void RemovedFrom(ContainerNode&);
  // This should be called in Node::DidMoveToDocument().
  void DidMoveToNewDocument(Document& old_document);
  // This is for HTMLFieldSetElement class.
  void AncestorDisabledStateWasChanged();

  // https://html.spec.whatwg.org/C/#concept-element-disabled
  bool IsActuallyDisabled() const;

  // Returns a static value of class-level support of the state restore
  // feature.  If a sub-class of ListedElement supports the state restore
  // feature, this function should return true.
  virtual bool ClassSupportsStateRestore() const;
  // Returns a flag to represent support of the state restore feature per
  // instances.
  virtual bool ShouldSaveAndRestoreFormControlState() const;
  virtual FormControlState SaveFormControlState() const;
  // The specified FormControlState must have at least one string value.
  virtual void RestoreFormControlState(const FormControlState& state);
  // This should be called whenever an element supports state restore changes
  // its state.
  void NotifyFormStateChanged();
  // This should be called in Element::FinishParsingChildren() override.
  void TakeStateAndRestore();

  void Trace(Visitor*) const override;

 protected:
  ListedElement();

  // FIXME: Remove usage of setForm. resetFormOwner should be enough, and
  // setForm is confusing.
  void SetForm(HTMLFormElement*);
  void AssociateByParser(HTMLFormElement*);

  // If you add an override of willChangeForm() or didChangeForm() to a class
  // derived from this one, you will need to add a call to setForm(0) to the
  // destructor of that class.
  virtual void WillChangeForm();
  virtual void DidChangeForm();

  // This must be called any time the result of WillValidate() has changed.
  void UpdateWillValidateCache();
  virtual bool RecalcWillValidate() const;

  String CustomValidationMessage() const;
  // This is just a setter. This doesn't set |customError| flag.
  void SetCustomValidationMessage(const String& message);

  // False; There are no FIELDSET ancestors.
  // True; There might be a FIELDSET ancestor, and thre might be no
  //       FIELDSET ancestors.
  mutable bool may_have_field_set_ancestor_ = true;

 private:
  void UpdateAncestorDisabledState() const;
  void SetFormAttributeTargetObserver(FormAttributeTargetObserver*);
  void ResetFormAttributeTargetObserver();
  // Requests validity recalc for the form owner, if one exists.
  void FormOwnerSetNeedsValidityCheck();
  // Requests validity recalc for all ancestor fieldsets, if exist.
  void FieldSetAncestorsSetNeedsValidityCheck(Node*);

  ValidationMessageClient* GetValidationMessageClient() const;

  Member<FormAttributeTargetObserver> form_attribute_target_observer_;
  Member<HTMLFormElement> form_;
  Member<ValidityState> validity_state_;
  String custom_validation_message_;
  bool has_validation_message_ : 1;
  // If form_was_set_by_parser_ is true, form_ is always non-null.
  bool form_was_set_by_parser_ : 1;

  // The initial value of will_validate_ depends on the derived class. We can't
  // initialize it with a virtual function in the constructor. will_validate_
  // is not deterministic as long as will_validate_initialized_ is false.
  mutable bool will_validate_initialized_ : 1;
  mutable bool will_validate_ : 1;

  // Cache of IsValidElement().
  bool is_valid_ : 1;
  bool validity_is_dirty_ : 1;

  enum class AncestorDisabledState { kUnknown, kEnabled, kDisabled };
  mutable AncestorDisabledState ancestor_disabled_state_ =
      AncestorDisabledState::kUnknown;

  enum class DataListAncestorState {
    kUnknown,
    kInsideDataList,
    kNotInsideDataList
  };
  mutable enum DataListAncestorState data_list_ancestor_state_ =
      DataListAncestorState::kUnknown;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_HTML_FORMS_LISTED_ELEMENT_H_