1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_HTML_FORMS_BASE_TEMPORAL_INPUT_TYPE_H_
32 #define THIRD_PARTY_BLINK_RENDERER_CORE_HTML_FORMS_BASE_TEMPORAL_INPUT_TYPE_H_
33 
34 #include "third_party/blink/renderer/core/html/forms/date_time_edit_element.h"
35 #include "third_party/blink/renderer/core/html/forms/input_type.h"
36 #include "third_party/blink/renderer/platform/text/date_components.h"
37 
38 namespace blink {
39 
40 class ExceptionState;
41 
42 // A super class of date, datetime, datetime-local, month, time, and week types.
43 // TODO(tkent): A single temporal input type creates two InputTypeView instances
44 // unnecessarily.  One is ChooserOnlyTemporalInputTypeView or
45 // MultipleFieldsTemporalInputType, and another is BaseTemporalInputType, which
46 // inherits from InputTypeView through InputType.  The latter is not used.
47 class BaseTemporalInputType : public InputType {
48  public:
49   String VisibleValue() const override;
50   String SanitizeValue(const String&) const override;
51   // Parses the specified string for this InputType, and returns true if it
52   // is successfully parsed. An instance pointed by the DateComponents*
53   // parameter will have parsed values and be modified even if the parsing
54   // fails. The DateComponents* parameter may be 0.
55   bool ParseToDateComponents(const String&, DateComponents*) const;
56   virtual bool SetMillisecondToDateComponents(double,
57                                               DateComponents*) const = 0;
58 
59   // Provide some helpers for MultipleFieldsTemporalInputTypeView.
60   virtual String FormatDateTimeFieldsState(
61       const DateTimeFieldsState&) const = 0;
62   virtual void SetupLayoutParameters(DateTimeEditElement::LayoutParameters&,
63                                      const DateComponents&) const = 0;
64   virtual bool IsValidFormat(bool has_year,
65                              bool has_month,
66                              bool has_week,
67                              bool has_day,
68                              bool has_ampm,
69                              bool has_hour,
70                              bool has_minute,
71                              bool has_second) const = 0;
72   virtual String AriaRoleForPickerIndicator() const = 0;
73 
74  protected:
BaseTemporalInputType(HTMLInputElement & element)75   BaseTemporalInputType(HTMLInputElement& element) : InputType(element) {}
76   Decimal ParseToNumber(const String&, const Decimal&) const override;
77   String Serialize(const Decimal&) const override;
78   String SerializeWithComponents(const DateComponents&) const;
79   bool ShouldHaveSecondField(const DateComponents&) const;
80 
81  private:
82   virtual bool ParseToDateComponentsInternal(const String&,
83                                              DateComponents*) const = 0;
84 
85   String BadInputText() const override;
86   InputTypeView* CreateView() override;
87   ValueMode GetValueMode() const override;
88   double ValueAsDate() const override;
89   void SetValueAsDate(const base::Optional<base::Time>&,
90                       ExceptionState&) const override;
91   double ValueAsDouble() const override;
92   void SetValueAsDouble(double,
93                         TextFieldEventBehavior,
94                         ExceptionState&) const override;
95   bool TypeMismatchFor(const String&) const override;
96   bool TypeMismatch() const override;
97   bool ValueMissing(const String&) const override;
98   String ValueNotEqualText(const Decimal& value) const override;
99   String RangeOverflowText(const Decimal& maximum) const override;
100   String RangeUnderflowText(const Decimal& minimum) const override;
101   String RangeInvalidText(const Decimal& minimum,
102                           const Decimal& maximum) const override;
103   Decimal DefaultValueForStepUp() const override;
104   bool IsSteppable() const override;
105   virtual String SerializeWithDate(const base::Optional<base::Time>&) const;
106   String LocalizeValue(const String&) const override;
107   bool SupportsReadOnly() const override;
108   bool ShouldRespectListAttribute() override;
109   bool MayTriggerVirtualKeyboard() const override;
110 };
111 
112 }  // namespace blink
113 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_HTML_FORMS_BASE_TEMPORAL_INPUT_TYPE_H_
114