DateTimeInputTypes.h (5291B)
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 mozilla_dom_DateTimeInputTypes_h__ 8 #define mozilla_dom_DateTimeInputTypes_h__ 9 10 #include "mozilla/dom/InputType.h" 11 12 namespace mozilla::dom { 13 14 class DateTimeInputTypeBase : public InputType { 15 public: 16 ~DateTimeInputTypeBase() override = default; 17 18 bool IsValueMissing() const override; 19 bool IsRangeOverflow() const override; 20 bool IsRangeUnderflow() const override; 21 bool HasStepMismatch() const override; 22 bool HasBadInput() const override; 23 24 nsresult GetRangeOverflowMessage(nsAString& aMessage) override; 25 nsresult GetRangeUnderflowMessage(nsAString& aMessage) override; 26 27 void MinMaxStepAttrChanged() override; 28 29 protected: 30 explicit DateTimeInputTypeBase(HTMLInputElement* aInputElement) 31 : InputType(aInputElement) {} 32 33 bool IsMutable() const override; 34 35 nsresult GetBadInputMessage(nsAString& aMessage) override = 0; 36 37 /** 38 * This method converts aValue (milliseconds within a day) to hours, minutes, 39 * seconds and milliseconds. 40 */ 41 bool GetTimeFromMs(double aValue, uint16_t* aHours, uint16_t* aMinutes, 42 uint16_t* aSeconds, uint16_t* aMilliseconds) const; 43 44 // Minimum year limited by HTML standard, year >= 1. 45 static const double kMinimumYear; 46 // Maximum year limited by ECMAScript date object range, year <= 275760. 47 static const double kMaximumYear; 48 // Maximum valid month is 275760-09. 49 static const double kMaximumMonthInMaximumYear; 50 // Maximum valid week is 275760-W37. 51 static const double kMaximumWeekInMaximumYear; 52 // Milliseconds in a day. 53 static const double kMsPerDay; 54 }; 55 56 // input type=date 57 class DateInputType : public DateTimeInputTypeBase { 58 public: 59 static InputType* Create(HTMLInputElement* aInputElement, void* aMemory) { 60 return new (aMemory) DateInputType(aInputElement); 61 } 62 63 nsresult GetBadInputMessage(nsAString& aMessage) override; 64 65 StringToNumberResult ConvertStringToNumber(const nsAString&) const override; 66 bool ConvertNumberToString(Decimal, Localized, 67 nsAString& aResultString) const override; 68 69 private: 70 explicit DateInputType(HTMLInputElement* aInputElement) 71 : DateTimeInputTypeBase(aInputElement) {} 72 }; 73 74 // input type=time 75 class TimeInputType : public DateTimeInputTypeBase { 76 public: 77 static InputType* Create(HTMLInputElement* aInputElement, void* aMemory) { 78 return new (aMemory) TimeInputType(aInputElement); 79 } 80 81 nsresult GetBadInputMessage(nsAString& aMessage) override; 82 83 StringToNumberResult ConvertStringToNumber(const nsAString&) const override; 84 bool ConvertNumberToString(Decimal, Localized, nsAString&) const override; 85 bool IsRangeOverflow() const override; 86 bool IsRangeUnderflow() const override; 87 nsresult GetRangeOverflowMessage(nsAString& aMessage) override; 88 nsresult GetRangeUnderflowMessage(nsAString& aMessage) override; 89 90 private: 91 explicit TimeInputType(HTMLInputElement* aInputElement) 92 : DateTimeInputTypeBase(aInputElement) {} 93 94 // https://html.spec.whatwg.org/multipage/input.html#has-a-reversed-range 95 bool HasReversedRange() const; 96 bool IsReversedRangeUnderflowAndOverflow() const; 97 nsresult GetReversedRangeUnderflowAndOverflowMessage(nsAString& aMessage); 98 }; 99 100 // input type=week 101 class WeekInputType : public DateTimeInputTypeBase { 102 public: 103 static InputType* Create(HTMLInputElement* aInputElement, void* aMemory) { 104 return new (aMemory) WeekInputType(aInputElement); 105 } 106 107 nsresult GetBadInputMessage(nsAString& aMessage) override; 108 StringToNumberResult ConvertStringToNumber(const nsAString&) const override; 109 bool ConvertNumberToString(Decimal, Localized, nsAString&) const override; 110 111 private: 112 explicit WeekInputType(HTMLInputElement* aInputElement) 113 : DateTimeInputTypeBase(aInputElement) {} 114 }; 115 116 // input type=month 117 class MonthInputType : public DateTimeInputTypeBase { 118 public: 119 static InputType* Create(HTMLInputElement* aInputElement, void* aMemory) { 120 return new (aMemory) MonthInputType(aInputElement); 121 } 122 123 nsresult GetBadInputMessage(nsAString& aMessage) override; 124 StringToNumberResult ConvertStringToNumber(const nsAString&) const override; 125 bool ConvertNumberToString(Decimal, Localized, nsAString&) const override; 126 127 private: 128 explicit MonthInputType(HTMLInputElement* aInputElement) 129 : DateTimeInputTypeBase(aInputElement) {} 130 }; 131 132 // input type=datetime-local 133 class DateTimeLocalInputType : public DateTimeInputTypeBase { 134 public: 135 static InputType* Create(HTMLInputElement* aInputElement, void* aMemory) { 136 return new (aMemory) DateTimeLocalInputType(aInputElement); 137 } 138 139 nsresult GetBadInputMessage(nsAString& aMessage) override; 140 StringToNumberResult ConvertStringToNumber(const nsAString&) const override; 141 bool ConvertNumberToString(Decimal, Localized, nsAString&) const override; 142 143 private: 144 explicit DateTimeLocalInputType(HTMLInputElement* aInputElement) 145 : DateTimeInputTypeBase(aInputElement) {} 146 }; 147 148 } // namespace mozilla::dom 149 150 #endif /* mozilla_dom_DateTimeInputTypes_h__ */