Line data Source code
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 DateTimeInputTypes_h__
8 : #define DateTimeInputTypes_h__
9 :
10 : #include "InputType.h"
11 :
12 : class DateTimeInputTypeBase : public ::InputType
13 : {
14 : public:
15 0 : ~DateTimeInputTypeBase() override {}
16 :
17 : bool IsValueMissing() const override;
18 : bool IsRangeOverflow() const override;
19 : bool IsRangeUnderflow() const override;
20 : bool HasStepMismatch(bool aUseZeroIfValueNaN) const override;
21 : bool HasBadInput() const override;
22 :
23 : nsresult GetRangeOverflowMessage(nsXPIDLString& aMessage) override;
24 : nsresult GetRangeUnderflowMessage(nsXPIDLString& aMessage) override;
25 :
26 : nsresult MinMaxStepAttrChanged() override;
27 :
28 : protected:
29 0 : explicit DateTimeInputTypeBase(mozilla::dom::HTMLInputElement* aInputElement)
30 0 : : InputType(aInputElement)
31 0 : {}
32 :
33 : /**
34 : * Checks preference "dom.forms.datetime" to determine if input date and time
35 : * should be supported.
36 : */
37 : static bool IsInputDateTimeEnabled();
38 :
39 : bool IsMutable() const override;
40 :
41 : /**
42 : * This method converts aValue (milliseconds within a day) to hours, minutes,
43 : * seconds and milliseconds.
44 : */
45 : bool GetTimeFromMs(double aValue, uint16_t* aHours, uint16_t* aMinutes,
46 : uint16_t* aSeconds, uint16_t* aMilliseconds) const;
47 :
48 : // Minimum year limited by HTML standard, year >= 1.
49 : static const double kMinimumYear;
50 : // Maximum year limited by ECMAScript date object range, year <= 275760.
51 : static const double kMaximumYear;
52 : // Maximum valid month is 275760-09.
53 : static const double kMaximumMonthInMaximumYear;
54 : // Maximum valid week is 275760-W37.
55 : static const double kMaximumWeekInMaximumYear;
56 : // Milliseconds in a day.
57 : static const double kMsPerDay;
58 : };
59 :
60 : // input type=date
61 0 : class DateInputType : public DateTimeInputTypeBase
62 : {
63 : public:
64 : static InputType*
65 0 : Create(mozilla::dom::HTMLInputElement* aInputElement, void* aMemory)
66 : {
67 0 : return new (aMemory) DateInputType(aInputElement);
68 : }
69 :
70 : // Currently, for input date and time, only date can have an invalid value, as
71 : // we forbid or autocorrect values that are not in the valid range for time.
72 : // For example, in 12hr format, if user enters '2' in the hour field, it will
73 : // be treated as '02' and automatically advance to the next field.
74 : nsresult GetBadInputMessage(nsXPIDLString& aMessage) override;
75 :
76 : bool ConvertStringToNumber(nsAString& aValue,
77 : mozilla::Decimal& aResultValue) const override;
78 : bool ConvertNumberToString(mozilla::Decimal aValue,
79 : nsAString& aResultString) const override;
80 :
81 : private:
82 0 : explicit DateInputType(mozilla::dom::HTMLInputElement* aInputElement)
83 0 : : DateTimeInputTypeBase(aInputElement)
84 0 : {}
85 : };
86 :
87 : // input type=time
88 0 : class TimeInputType : public DateTimeInputTypeBase
89 : {
90 : public:
91 : static InputType*
92 0 : Create(mozilla::dom::HTMLInputElement* aInputElement, void* aMemory)
93 : {
94 0 : return new (aMemory) TimeInputType(aInputElement);
95 : }
96 :
97 : bool ConvertStringToNumber(nsAString& aValue,
98 : mozilla::Decimal& aResultValue) const override;
99 : bool ConvertNumberToString(mozilla::Decimal aValue,
100 : nsAString& aResultString) const override;
101 :
102 : private:
103 0 : explicit TimeInputType(mozilla::dom::HTMLInputElement* aInputElement)
104 0 : : DateTimeInputTypeBase(aInputElement)
105 0 : {}
106 : };
107 :
108 : // input type=week
109 0 : class WeekInputType : public DateTimeInputTypeBase
110 : {
111 : public:
112 : static InputType*
113 0 : Create(mozilla::dom::HTMLInputElement* aInputElement, void* aMemory)
114 : {
115 0 : return new (aMemory) WeekInputType(aInputElement);
116 : }
117 :
118 : bool ConvertStringToNumber(nsAString& aValue,
119 : mozilla::Decimal& aResultValue) const override;
120 : bool ConvertNumberToString(mozilla::Decimal aValue,
121 : nsAString& aResultString) const override;
122 :
123 : private:
124 0 : explicit WeekInputType(mozilla::dom::HTMLInputElement* aInputElement)
125 0 : : DateTimeInputTypeBase(aInputElement)
126 0 : {}
127 : };
128 :
129 : // input type=month
130 0 : class MonthInputType : public DateTimeInputTypeBase
131 : {
132 : public:
133 : static InputType*
134 0 : Create(mozilla::dom::HTMLInputElement* aInputElement, void* aMemory)
135 : {
136 0 : return new (aMemory) MonthInputType(aInputElement);
137 : }
138 :
139 : bool ConvertStringToNumber(nsAString& aValue,
140 : mozilla::Decimal& aResultValue) const override;
141 : bool ConvertNumberToString(mozilla::Decimal aValue,
142 : nsAString& aResultString) const override;
143 :
144 : private:
145 0 : explicit MonthInputType(mozilla::dom::HTMLInputElement* aInputElement)
146 0 : : DateTimeInputTypeBase(aInputElement)
147 0 : {}
148 : };
149 :
150 : // input type=datetime-local
151 0 : class DateTimeLocalInputType : public DateTimeInputTypeBase
152 : {
153 : public:
154 : static InputType*
155 0 : Create(mozilla::dom::HTMLInputElement* aInputElement, void* aMemory)
156 : {
157 0 : return new (aMemory) DateTimeLocalInputType(aInputElement);
158 : }
159 :
160 : bool ConvertStringToNumber(nsAString& aValue,
161 : mozilla::Decimal& aResultValue) const override;
162 : bool ConvertNumberToString(mozilla::Decimal aValue,
163 : nsAString& aResultString) const override;
164 :
165 : private:
166 0 : explicit DateTimeLocalInputType(mozilla::dom::HTMLInputElement* aInputElement)
167 0 : : DateTimeInputTypeBase(aInputElement)
168 0 : {}
169 : };
170 :
171 :
172 : #endif /* DateTimeInputTypes_h__ */
|