1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  * Copyright (C) 2011 Apple Inc. All rights reserved.
4  * Copyright (C) 2012 Samsung Electronics. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  *     * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_HTML_FORMS_INPUT_TYPE_H_
34 #define THIRD_PARTY_BLINK_RENDERER_CORE_HTML_FORMS_INPUT_TYPE_H_
35 
36 #include "base/macros.h"
37 #include "third_party/blink/renderer/core/core_export.h"
38 #include "third_party/blink/renderer/core/frame/web_feature_forward.h"
39 #include "third_party/blink/renderer/core/html/forms/color_chooser_client.h"
40 #include "third_party/blink/renderer/core/html/forms/step_range.h"
41 #include "third_party/blink/renderer/core/html/forms/text_control_element.h"
42 
43 namespace blink {
44 
45 class ChromeClient;
46 class DragData;
47 class ExceptionState;
48 class FileList;
49 class FormData;
50 class InputTypeView;
51 
52 // An InputType object represents the type-specific part of an HTMLInputElement.
53 // Do not expose instances of InputType and classes derived from it to classes
54 // other than HTMLInputElement.
55 class CORE_EXPORT InputType : public GarbageCollected<InputType> {
56  public:
57   static InputType* Create(HTMLInputElement&, const AtomicString&);
58   static const AtomicString& NormalizeTypeName(const AtomicString&);
59   virtual ~InputType();
60   virtual void Trace(Visitor*) const;
61 
62   virtual InputTypeView* CreateView() = 0;
63   virtual const AtomicString& FormControlType() const = 0;
64 
65   // Type query functions
66 
67   // Any time we are using one of these functions it's best to refactor
68   // to add a virtual function to allow the input type object to do the
69   // work instead, or at least make a query function that asks a higher
70   // level question. These functions make the HTMLInputElement class
71   // inflexible because it's harder to add new input types if there is
72   // scattered code with special cases for various types.
73 
74   virtual bool IsInteractiveContent() const;
75   virtual bool IsTextButton() const;
76   virtual bool IsTextField() const;
77 
78   // Form value functions
79 
80   virtual bool ShouldSaveAndRestoreFormControlState() const;
81   virtual bool IsFormDataAppendable() const;
82   virtual void AppendToFormData(FormData&) const;
83   virtual String ResultForDialogSubmit() const;
84 
85   // DOM property functions
86 
87   // Returns a string value in ValueMode::kFilename.
88   virtual String ValueInFilenameValueMode() const;
89   // Default string to be used for showing button and form submission if |value|
90   // is missing.
91   virtual String DefaultLabel() const;
92 
93   // https://html.spec.whatwg.org/C/#dom-input-value
94   enum class ValueMode { kValue, kDefault, kDefaultOn, kFilename };
95   virtual ValueMode GetValueMode() const = 0;
96 
97   virtual double ValueAsDate() const;
98   virtual void SetValueAsDate(const base::Optional<base::Time>&,
99                               ExceptionState&) const;
100   virtual double ValueAsDouble() const;
101   virtual void SetValueAsDouble(double,
102                                 TextFieldEventBehavior,
103                                 ExceptionState&) const;
104   virtual void SetValueAsDecimal(const Decimal&,
105                                  TextFieldEventBehavior,
106                                  ExceptionState&) const;
107 
108   // Functions related to 'checked'
109 
110   virtual void ReadingChecked() const;
111   // The function is called just before updating checkedness.
112   virtual void WillUpdateCheckedness(bool new_checked);
113 
114   // Validation functions
115 
116   // Returns a validation message as .first, and title attribute value as
117   // .second if patternMismatch.
118   std::pair<String, String> ValidationMessage(const InputTypeView&) const;
119   virtual bool SupportsValidation() const;
120   virtual bool TypeMismatchFor(const String&) const;
121   // Type check for the current input value. We do nothing for some types
122   // though typeMismatchFor() does something for them because of value
123   // sanitization.
124   virtual bool TypeMismatch() const;
125   virtual bool SupportsRequired() const;
126   virtual bool ValueMissing(const String&) const;
127   virtual bool PatternMismatch(const String&) const;
128   virtual bool TooLong(const String&,
129                        TextControlElement::NeedsToCheckDirtyFlag) const;
130   virtual bool TooShort(const String&,
131                         TextControlElement::NeedsToCheckDirtyFlag) const;
132   bool RangeUnderflow(const String&) const;
133   bool RangeOverflow(const String&) const;
134   bool IsInRange(const String&) const;
135   bool IsOutOfRange(const String&) const;
136   void InRangeChanged() const;
137   virtual Decimal DefaultValueForStepUp() const;
138   double Minimum() const;
139   double Maximum() const;
140   bool StepMismatch(const String&) const;
141   bool GetAllowedValueStep(Decimal*) const;
142   virtual StepRange CreateStepRange(AnyStepHandling) const;
143   void StepUp(double, ExceptionState&);
144   void StepUpFromLayoutObject(int);
145   virtual String BadInputText() const;
146   virtual String ValueNotEqualText(const Decimal& value) const;
147   virtual String RangeOverflowText(const Decimal& maximum) const;
148   virtual String RangeUnderflowText(const Decimal& minimum) const;
149   virtual String ReversedRangeOutOfRangeText(const Decimal& minimum,
150                                              const Decimal& maximum) const;
151   virtual String RangeInvalidText(const Decimal& minimum,
152                                   const Decimal& maximum) const;
153   virtual String TypeMismatchText() const;
154   virtual String ValueMissingText() const;
155   virtual bool CanSetStringValue() const;
156   virtual String LocalizeValue(const String&) const;
157   virtual String VisibleValue() const;
158   // Returing the null string means "use the default value."
159   // This function must be called only by HTMLInputElement::sanitizeValue().
160   virtual String SanitizeValue(const String&) const;
161   virtual String SanitizeUserInputValue(const String&) const;
162   virtual void WarnIfValueIsInvalid(const String&) const;
163   void WarnIfValueIsInvalidAndElementIsVisible(const String&) const;
164 
165   virtual bool IsKeyboardFocusable() const;
166   virtual bool MayTriggerVirtualKeyboard() const;
167   virtual bool CanBeSuccessfulSubmitButton();
168   virtual bool MatchesDefaultPseudoClass();
169 
170   // Miscellaneous functions
171 
172   virtual bool LayoutObjectIsNeeded();
173   virtual void CountUsage();
174   virtual void SanitizeValueInResponseToMinOrMaxAttributeChange();
175   virtual bool ShouldRespectAlignAttribute();
176   virtual FileList* Files();
177   // Should return true if the file list was were changed.
178   virtual bool SetFiles(FileList*);
179   virtual void SetFilesAndDispatchEvents(FileList*);
180   virtual void SetFilesFromPaths(const Vector<String>&);
181   // Should return true if the given DragData has more than one dropped files.
182   virtual bool ReceiveDroppedFiles(const DragData*);
183   virtual String DroppedFileSystemId();
184   // Should return true if the corresponding layoutObject for a type can display
185   // a suggested value.
186   virtual bool CanSetSuggestedValue();
187   virtual bool ShouldSendChangeEventAfterCheckedChanged();
188   virtual bool CanSetValue(const String&);
189   virtual void SetValue(const String&,
190                         bool value_changed,
191                         TextFieldEventBehavior,
192                         TextControlSetValueSelection);
193   virtual bool ShouldRespectListAttribute();
194   virtual bool IsEnumeratable();
195   virtual bool IsCheckable();
196   virtual bool IsSteppable() const;
197   virtual bool ShouldRespectHeightAndWidthAttributes();
198   virtual int MaxLength() const;
199   virtual int MinLength() const;
200   virtual bool SupportsPlaceholder() const;
201   virtual bool SupportsReadOnly() const;
202   virtual String DefaultToolTip(const InputTypeView&) const;
203   virtual Decimal FindClosestTickMarkValue(const Decimal&);
204   virtual bool HasLegalLinkAttribute(const QualifiedName&) const;
205   virtual const QualifiedName& SubResourceAttributeName() const;
206   virtual void CopyNonAttributeProperties(const HTMLInputElement&);
207   virtual void OnAttachWithLayoutObject();
208 
209   // Parses the specified string for the type, and return
210   // the Decimal value for the parsing result if the parsing
211   // succeeds; Returns defaultValue otherwise. This function can
212   // return NaN or Infinity only if defaultValue is NaN or Infinity.
213   virtual Decimal ParseToNumber(const String&,
214                                 const Decimal& default_value) const;
215 
216   // Create a string representation of the specified Decimal value for the
217   // input type. If NaN or Infinity is specified, this returns an empty
218   // string. This should not be called for types without valueAsNumber.
219   virtual String Serialize(const Decimal&) const;
220 
221   virtual bool ShouldAppearIndeterminate() const;
222 
223   virtual bool SupportsInputModeAttribute() const;
224 
225   virtual bool SupportsSelectionAPI() const;
226 
227   // Gets width and height of the input element if the type of the
228   // element is image. It returns 0 if the element is not image type.
229   virtual unsigned Height() const;
230   virtual unsigned Width() const;
231 
232   virtual void DispatchSearchEvent();
233 
234   // For test purpose
235   virtual ColorChooserClient* GetColorChooserClient();
236 
237  protected:
InputType(HTMLInputElement & element)238   InputType(HTMLInputElement& element) : element_(element) {}
GetElement()239   HTMLInputElement& GetElement() const { return *element_; }
240   ChromeClient* GetChromeClient() const;
241   Locale& GetLocale() const;
242   Decimal ParseToNumberOrNaN(const String&) const;
243   void CountUsageIfVisible(WebFeature) const;
244 
245   // Derive the step base, following the HTML algorithm steps.
246   Decimal FindStepBase(const Decimal&) const;
247 
248   StepRange CreateStepRange(AnyStepHandling,
249                             const Decimal& step_base_default,
250                             const Decimal& minimum_default,
251                             const Decimal& maximum_default,
252                             const StepRange::StepDescription&) const;
253   StepRange CreateReversibleStepRange(AnyStepHandling,
254                                       const Decimal& step_base_default,
255                                       const Decimal& minimum_default,
256                                       const Decimal& maximum_default,
257                                       const StepRange::StepDescription&) const;
258   void AddWarningToConsole(const char* message_format,
259                            const String& value) const;
260 
261  private:
262   // Helper for stepUp()/stepDown(). Adds step value * count to the current
263   // value.
264   void ApplyStep(const Decimal&,
265                  double count,
266                  AnyStepHandling,
267                  TextFieldEventBehavior,
268                  ExceptionState&);
269 
270   StepRange CreateStepRange(AnyStepHandling,
271                             const Decimal& step_base_default,
272                             const Decimal& minimum_default,
273                             const Decimal& maximum_default,
274                             const StepRange::StepDescription&,
275                             bool supports_reversed_range) const;
276 
277   Member<HTMLInputElement> element_;
278 
279   DISALLOW_COPY_AND_ASSIGN(InputType);
280 };
281 
282 }  // namespace blink
283 #endif
284