Bug 1880216 - Migrate Fenix docs into Sphinx. r=owlish,geckoview-reviewers,android...
[gecko.git] / dom / html / input / InputType.h
bloba3599feaddc8df62d9d7580b36c17e4283904cdc
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/. */
7 #ifndef mozilla_dom_InputType_h__
8 #define mozilla_dom_InputType_h__
10 #include <stdint.h>
11 #include "mozilla/Decimal.h"
12 #include "mozilla/Maybe.h"
13 #include "mozilla/TextControlState.h"
14 #include "mozilla/UniquePtr.h"
15 #include "nsIConstraintValidation.h"
16 #include "nsString.h"
17 #include "nsError.h"
19 // This must come outside of any namespace, or else it won't overload with the
20 // double based version in nsMathUtils.h
21 inline mozilla::Decimal NS_floorModulo(mozilla::Decimal x, mozilla::Decimal y) {
22 return (x - y * (x / y).floor());
25 class nsIFrame;
27 namespace mozilla::dom {
28 class HTMLInputElement;
30 /**
31 * A common superclass for different types of a HTMLInputElement.
33 class InputType {
34 public:
35 using ValueSetterOption = TextControlState::ValueSetterOption;
36 using ValueSetterOptions = TextControlState::ValueSetterOptions;
38 // Custom deleter for UniquePtr<InputType> to avoid freeing memory
39 // pre-allocated for InputType, but we still need to call the destructor
40 // explictly.
41 struct DoNotDelete {
42 void operator()(InputType* p) { p->~InputType(); }
45 static UniquePtr<InputType, DoNotDelete> Create(
46 HTMLInputElement* aInputElement, FormControlType, void* aMemory);
48 virtual ~InputType() = default;
50 // Float value returned by GetStep() when the step attribute is set to 'any'.
51 static constexpr Decimal kStepAny = Decimal(0_d);
53 /**
54 * Drop the reference to the input element.
56 void DropReference();
58 virtual bool MinAndMaxLengthApply() const { return false; }
59 virtual bool IsTooLong() const;
60 virtual bool IsTooShort() const;
61 virtual bool IsValueMissing() const;
62 virtual bool HasTypeMismatch() const;
63 // May return Nothing() if the JS engine failed to evaluate the regex.
64 virtual Maybe<bool> HasPatternMismatch() const;
65 virtual bool IsRangeOverflow() const;
66 virtual bool IsRangeUnderflow() const;
67 virtual bool HasStepMismatch() const;
68 virtual bool HasBadInput() const;
70 nsresult GetValidationMessage(
71 nsAString& aValidationMessage,
72 nsIConstraintValidation::ValidityStateType aType);
73 virtual nsresult GetValueMissingMessage(nsAString& aMessage);
74 virtual nsresult GetTypeMismatchMessage(nsAString& aMessage);
75 virtual nsresult GetRangeOverflowMessage(nsAString& aMessage);
76 virtual nsresult GetRangeUnderflowMessage(nsAString& aMessage);
77 virtual nsresult GetBadInputMessage(nsAString& aMessage);
79 MOZ_CAN_RUN_SCRIPT virtual void MinMaxStepAttrChanged() {}
81 /**
82 * Convert a string to a Decimal number in a type specific way,
83 * http://www.whatwg.org/specs/web-apps/current-work/multipage/the-input-element.html#concept-input-value-string-number
84 * ie parse a date string to a timestamp if type=date,
85 * or parse a number string to its value if type=number.
86 * @param aValue the string to be parsed.
88 struct StringToNumberResult {
89 // The result decimal. Successfully parsed if it's finite.
90 Decimal mResult = Decimal::nan();
91 // Whether the result required reading locale-dependent data (for input
92 // type=number), or the value parses using the regular HTML rules.
93 bool mLocalized = false;
95 virtual StringToNumberResult ConvertStringToNumber(
96 const nsAString& aValue) const;
98 /**
99 * Convert a Decimal to a string in a type specific way, ie convert a
100 * timestamp to a date string if type=date or append the number string
101 * representing the value if type=number.
103 * @param aValue the Decimal to be converted
104 * @param aResultString [out] the string representing the Decimal
105 * @return whether the function succeeded, it will fail if the current input's
106 * type is not supported or the number can't be converted to a string
107 * as expected by the type.
109 virtual bool ConvertNumberToString(Decimal aValue,
110 nsAString& aResultString) const;
112 protected:
113 explicit InputType(HTMLInputElement* aInputElement)
114 : mInputElement(aInputElement) {}
117 * Get the mutable state of the element.
118 * When the element isn't mutable (immutable), the value or checkedness
119 * should not be changed by the user.
121 * See:
122 * https://html.spec.whatwg.org/multipage/forms.html#the-input-element:concept-fe-mutable
124 virtual bool IsMutable() const;
127 * Returns whether the input element's current value is the empty string.
128 * This only makes sense for some input types; does NOT make sense for file
129 * inputs.
131 * @return whether the input element's current value is the empty string.
133 bool IsValueEmpty() const;
135 // A getter for callers that know we're not dealing with a file input, so they
136 // don't have to think about the caller type.
137 void GetNonFileValueInternal(nsAString& aValue) const;
140 * Setting the input element's value.
142 * @param aValue String to set.
143 * @param aOptions See TextControlState::ValueSetterOption.
145 MOZ_CAN_RUN_SCRIPT nsresult
146 SetValueInternal(const nsAString& aValue, const ValueSetterOptions& aOptions);
149 * Get the primary frame for the input element.
151 nsIFrame* GetPrimaryFrame() const;
154 * Parse a date string of the form yyyy-mm-dd
156 * @param aValue the string to be parsed.
157 * @return the date in aYear, aMonth, aDay.
158 * @return whether the parsing was successful.
160 bool ParseDate(const nsAString& aValue, uint32_t* aYear, uint32_t* aMonth,
161 uint32_t* aDay) const;
164 * Returns the time expressed in milliseconds of |aValue| being parsed as a
165 * time following the HTML specifications:
166 * https://html.spec.whatwg.org/multipage/infrastructure.html#parse-a-time-string
168 * Note: |aResult| can be null.
170 * @param aValue the string to be parsed.
171 * @param aResult the time expressed in milliseconds representing the time
172 * [out]
173 * @return whether the parsing was successful.
175 bool ParseTime(const nsAString& aValue, uint32_t* aResult) const;
178 * Parse a month string of the form yyyy-mm
180 * @param the string to be parsed.
181 * @return the year and month in aYear and aMonth.
182 * @return whether the parsing was successful.
184 bool ParseMonth(const nsAString& aValue, uint32_t* aYear,
185 uint32_t* aMonth) const;
188 * Parse a week string of the form yyyy-Www
190 * @param the string to be parsed.
191 * @return the year and week in aYear and aWeek.
192 * @return whether the parsing was successful.
194 bool ParseWeek(const nsAString& aValue, uint32_t* aYear,
195 uint32_t* aWeek) const;
198 * Parse a datetime-local string of the form yyyy-mm-ddThh:mm[:ss.s] or
199 * yyyy-mm-dd hh:mm[:ss.s], where fractions of seconds can be 1 to 3 digits.
201 * @param the string to be parsed.
202 * @return the date in aYear, aMonth, aDay and time expressed in milliseconds
203 * in aTime.
204 * @return whether the parsing was successful.
206 bool ParseDateTimeLocal(const nsAString& aValue, uint32_t* aYear,
207 uint32_t* aMonth, uint32_t* aDay,
208 uint32_t* aTime) const;
211 * This methods returns the number of months between January 1970 and the
212 * given year and month.
214 int32_t MonthsSinceJan1970(uint32_t aYear, uint32_t aMonth) const;
217 * This methods returns the number of days since epoch for a given year and
218 * week.
220 double DaysSinceEpochFromWeek(uint32_t aYear, uint32_t aWeek) const;
223 * This methods returns the day of the week given a date. If @isoWeek is true,
224 * 7=Sunday, otherwise, 0=Sunday.
226 uint32_t DayOfWeek(uint32_t aYear, uint32_t aMonth, uint32_t aDay,
227 bool isoWeek) const;
230 * This methods returns the maximum number of week in a given year, the
231 * result is either 52 or 53.
233 uint32_t MaximumWeekInYear(uint32_t aYear) const;
235 HTMLInputElement* mInputElement;
238 } // namespace mozilla::dom
240 #endif /* mozilla_dom_InputType_h__ */