script/dom/html/input_element/
input_type.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4use embedder_traits::InputMethodType;
5use js::context::JSContext;
6use script_bindings::codegen::GenericBindings::HTMLInputElementBinding::HTMLInputElementMethods;
7use script_bindings::domstring::DOMString;
8use script_bindings::root::DomRoot;
9use script_bindings::script_runtime::CanGc;
10use stylo_atoms::Atom;
11use time::OffsetDateTime;
12
13use crate::dom::attr::Attr;
14use crate::dom::element::AttributeMutation;
15use crate::dom::event::Event;
16use crate::dom::eventtarget::EventTarget;
17use crate::dom::filelist::FileList;
18use crate::dom::htmlformelement::HTMLFormElement;
19use crate::dom::input_element::button_input_type::ButtonInputType;
20use crate::dom::input_element::checkbox_input_type::CheckboxInputType;
21use crate::dom::input_element::color_input_type::ColorInputType;
22use crate::dom::input_element::date_input_type::DateInputType;
23use crate::dom::input_element::datetime_local_input_type::DatetimeLocalInputType;
24use crate::dom::input_element::email_input_type::EmailInputType;
25use crate::dom::input_element::file_input_type::FileInputType;
26use crate::dom::input_element::hidden_input_type::HiddenInputType;
27use crate::dom::input_element::image_input_type::ImageInputType;
28use crate::dom::input_element::month_input_type::MonthInputType;
29use crate::dom::input_element::number_input_type::NumberInputType;
30use crate::dom::input_element::password_input_type::PasswordInputType;
31use crate::dom::input_element::radio_input_type::RadioInputType;
32use crate::dom::input_element::range_input_type::RangeInputType;
33use crate::dom::input_element::reset_input_type::ResetInputType;
34use crate::dom::input_element::search_input_type::SearchInputType;
35use crate::dom::input_element::submit_input_type::SubmitInputType;
36use crate::dom::input_element::tel_input_type::TelInputType;
37use crate::dom::input_element::text_input_type::TextInputType;
38use crate::dom::input_element::time_input_type::TimeInputType;
39use crate::dom::input_element::url_input_type::UrlInputType;
40use crate::dom::input_element::week_input_type::WeekInputType;
41use crate::dom::input_element::{HTMLInputElement, InputActivationState, ValueMode};
42use crate::dom::node::{BindContext, UnbindContext};
43
44/// <https://html.spec.whatwg.org/multipage/#attr-input-type>
45#[derive(JSTraceable, MallocSizeOf, PartialEq)]
46#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
47pub(crate) enum InputType {
48    /// <https://html.spec.whatwg.org/multipage/#button-state-(type=button)>
49    Button(ButtonInputType),
50
51    /// <https://html.spec.whatwg.org/multipage/#checkbox-state-(type=checkbox)>
52    Checkbox(CheckboxInputType),
53
54    /// <https://html.spec.whatwg.org/multipage/#color-state-(type=color)>
55    Color(ColorInputType),
56
57    /// <https://html.spec.whatwg.org/multipage/#date-state-(type=date)>
58    Date(DateInputType),
59
60    /// <https://html.spec.whatwg.org/multipage/#local-date-and-time-state-(type=datetime-local)>
61    DatetimeLocal(DatetimeLocalInputType),
62
63    /// <https://html.spec.whatwg.org/multipage/#email-state-(type=email)>
64    Email(EmailInputType),
65
66    /// <https://html.spec.whatwg.org/multipage/#file-upload-state-(type=file)>
67    File(FileInputType),
68
69    /// <https://html.spec.whatwg.org/multipage/#hidden-state-(type=hidden)>
70    Hidden(HiddenInputType),
71
72    /// <https://html.spec.whatwg.org/multipage/#image-button-state-(type=image)>
73    Image(ImageInputType),
74
75    /// <https://html.spec.whatwg.org/multipage/#month-state-(type=month)>
76    Month(MonthInputType),
77
78    /// <https://html.spec.whatwg.org/multipage/#number-state-(type=number)>
79    Number(NumberInputType),
80
81    /// <https://html.spec.whatwg.org/multipage/#password-state-(type=password)>
82    Password(PasswordInputType),
83
84    /// <https://html.spec.whatwg.org/multipage/#radio-button-state-(type=radio)>
85    Radio(RadioInputType),
86
87    /// <https://html.spec.whatwg.org/multipage/#range-state-(type=range)>
88    Range(RangeInputType),
89
90    /// <https://html.spec.whatwg.org/multipage/#reset-button-state-(type=reset)>
91    Reset(ResetInputType),
92
93    /// <https://html.spec.whatwg.org/multipage/#text-(type=text)-state-and-search-state-(type=search)>
94    Search(SearchInputType),
95
96    /// <https://html.spec.whatwg.org/multipage/#submit-button-state-(type=submit)>
97    Submit(SubmitInputType),
98
99    /// <https://html.spec.whatwg.org/multipage/#telephone-state-(type=tel)>
100    Tel(TelInputType),
101
102    /// <https://html.spec.whatwg.org/multipage/#text-(type=text)-state-and-search-state-(type=search)>
103    Text(TextInputType),
104
105    /// <https://html.spec.whatwg.org/multipage/#time-state-(type=time)>
106    Time(TimeInputType),
107
108    /// <https://html.spec.whatwg.org/multipage/#url-state-(type=url)>
109    Url(UrlInputType),
110
111    /// <https://html.spec.whatwg.org/multipage/#week-state-(type=week)>
112    Week(WeekInputType),
113}
114
115impl InputType {
116    pub(crate) fn new_from_atom(value: &Atom) -> Self {
117        match value.to_ascii_lowercase() {
118            atom!("button") => InputType::Button(Default::default()),
119            atom!("checkbox") => InputType::Checkbox(Default::default()),
120            atom!("color") => InputType::Color(Default::default()),
121            atom!("date") => InputType::Date(Default::default()),
122            atom!("datetime-local") => InputType::DatetimeLocal(Default::default()),
123            atom!("email") => InputType::Email(Default::default()),
124            atom!("file") => InputType::File(Default::default()),
125            atom!("hidden") => InputType::Hidden(Default::default()),
126            atom!("image") => InputType::Image(Default::default()),
127            atom!("month") => InputType::Month(Default::default()),
128            atom!("number") => InputType::Number(Default::default()),
129            atom!("password") => InputType::Password(Default::default()),
130            atom!("radio") => InputType::Radio(Default::default()),
131            atom!("range") => InputType::Range(Default::default()),
132            atom!("reset") => InputType::Reset(Default::default()),
133            atom!("search") => InputType::Search(Default::default()),
134            atom!("submit") => InputType::Submit(Default::default()),
135            atom!("tel") => InputType::Tel(Default::default()),
136            atom!("text") => InputType::Text(Default::default()),
137            atom!("time") => InputType::Time(Default::default()),
138            atom!("url") => InputType::Url(Default::default()),
139            atom!("week") => InputType::Week(Default::default()),
140            _ => InputType::Text(Default::default()),
141        }
142    }
143
144    pub(crate) fn new_text() -> Self {
145        Self::Text(TextInputType::default())
146    }
147
148    pub(crate) fn as_specific(&self) -> &dyn SpecificInputType {
149        match self {
150            Self::Button(input_type) => input_type as &dyn SpecificInputType,
151            Self::Checkbox(input_type) => input_type as &dyn SpecificInputType,
152            Self::Color(input_type) => input_type as &dyn SpecificInputType,
153            Self::Date(input_type) => input_type as &dyn SpecificInputType,
154            Self::DatetimeLocal(input_type) => input_type as &dyn SpecificInputType,
155            Self::Email(input_type) => input_type as &dyn SpecificInputType,
156            Self::File(input_type) => input_type as &dyn SpecificInputType,
157            Self::Hidden(input_type) => input_type as &dyn SpecificInputType,
158            Self::Image(input_type) => input_type as &dyn SpecificInputType,
159            Self::Month(input_type) => input_type as &dyn SpecificInputType,
160            Self::Number(input_type) => input_type as &dyn SpecificInputType,
161            Self::Password(input_type) => input_type as &dyn SpecificInputType,
162            Self::Radio(input_type) => input_type as &dyn SpecificInputType,
163            Self::Range(input_type) => input_type as &dyn SpecificInputType,
164            Self::Reset(input_type) => input_type as &dyn SpecificInputType,
165            Self::Search(input_type) => input_type as &dyn SpecificInputType,
166            Self::Submit(input_type) => input_type as &dyn SpecificInputType,
167            Self::Tel(input_type) => input_type as &dyn SpecificInputType,
168            Self::Text(input_type) => input_type as &dyn SpecificInputType,
169            Self::Time(input_type) => input_type as &dyn SpecificInputType,
170            Self::Url(input_type) => input_type as &dyn SpecificInputType,
171            Self::Week(input_type) => input_type as &dyn SpecificInputType,
172        }
173    }
174
175    /// Defines which input type that should perform like a text input,
176    /// specifically when it is interacting with JS. Note that Password
177    /// is not included here since it is handled slightly differently,
178    /// with placeholder characters shown rather than the underlying value.
179    pub(crate) fn is_textual(&self) -> bool {
180        matches!(
181            *self,
182            Self::Date(_) |
183                Self::DatetimeLocal(_) |
184                Self::Email(_) |
185                Self::Hidden(_) |
186                Self::Month(_) |
187                Self::Number(_) |
188                Self::Search(_) |
189                Self::Tel(_) |
190                Self::Text(_) |
191                Self::Time(_) |
192                Self::Url(_) |
193                Self::Week(_)
194        )
195    }
196
197    pub(crate) fn is_textual_or_password(&self) -> bool {
198        self.is_textual() || matches!(self, Self::Password(_))
199    }
200
201    /// <https://html.spec.whatwg.org/multipage/#has-a-periodic-domain>
202    pub(crate) fn has_periodic_domain(&self) -> bool {
203        matches!(self, Self::Time(_))
204    }
205
206    pub(crate) fn as_str(&self) -> &str {
207        match *self {
208            InputType::Button(_) => "button",
209            InputType::Checkbox(_) => "checkbox",
210            InputType::Color(_) => "color",
211            InputType::Date(_) => "date",
212            InputType::DatetimeLocal(_) => "datetime-local",
213            InputType::Email(_) => "email",
214            InputType::File(_) => "file",
215            InputType::Hidden(_) => "hidden",
216            InputType::Image(_) => "image",
217            InputType::Month(_) => "month",
218            InputType::Number(_) => "number",
219            InputType::Password(_) => "password",
220            InputType::Radio(_) => "radio",
221            InputType::Range(_) => "range",
222            InputType::Reset(_) => "reset",
223            InputType::Search(_) => "search",
224            InputType::Submit(_) => "submit",
225            InputType::Tel(_) => "tel",
226            InputType::Text(_) => "text",
227            InputType::Time(_) => "time",
228            InputType::Url(_) => "url",
229            InputType::Week(_) => "week",
230        }
231    }
232}
233
234impl TryFrom<&InputType> for InputMethodType {
235    type Error = &'static str;
236
237    fn try_from(input_type: &InputType) -> Result<Self, Self::Error> {
238        match input_type {
239            InputType::Color(_) => Ok(InputMethodType::Color),
240            InputType::Date(_) => Ok(InputMethodType::Date),
241            InputType::DatetimeLocal(_) => Ok(InputMethodType::DatetimeLocal),
242            InputType::Email(_) => Ok(InputMethodType::Email),
243            InputType::Month(_) => Ok(InputMethodType::Month),
244            InputType::Number(_) => Ok(InputMethodType::Number),
245            InputType::Password(_) => Ok(InputMethodType::Password),
246            InputType::Search(_) => Ok(InputMethodType::Search),
247            InputType::Tel(_) => Ok(InputMethodType::Tel),
248            InputType::Text(_) => Ok(InputMethodType::Text),
249            InputType::Time(_) => Ok(InputMethodType::Time),
250            InputType::Url(_) => Ok(InputMethodType::Url),
251            InputType::Week(_) => Ok(InputMethodType::Week),
252            _ => Err("Input does not support IME."),
253        }
254    }
255}
256
257pub(crate) trait SpecificInputType {
258    fn sanitize_value(&self, _input: &HTMLInputElement, _value: &mut DOMString) {}
259
260    fn convert_string_to_number(&self, _value: &str) -> Option<f64> {
261        None
262    }
263
264    fn convert_number_to_string(&self, _value: f64) -> Option<DOMString> {
265        unreachable!("Should not have called convert_number_to_string for non-Date types")
266    }
267
268    /// <https://html.spec.whatwg.org/multipage/#concept-input-value-string-date>
269    /// This does the safe Rust part of conversion; the unsafe JS Date part
270    /// is in GetValueAsDate
271    fn convert_string_to_naive_datetime(&self, _value: DOMString) -> Option<OffsetDateTime> {
272        None
273    }
274
275    /// <https://html.spec.whatwg.org/multipage/#concept-input-value-date-string>
276    /// This does the safe Rust part of conversion; the unsafe JS Date part
277    /// is in SetValueAsDate
278    fn convert_datetime_to_dom_string(&self, _value: OffsetDateTime) -> DOMString {
279        unreachable!("Should not have called convert_datetime_to_string for non-Date types")
280    }
281
282    /// <https://html.spec.whatwg.org/multipage/#the-required-attribute%3Asuffering-from-being-missing>
283    fn suffers_from_being_missing(&self, input: &HTMLInputElement, value: &DOMString) -> bool {
284        input.Required() &&
285            input.value_mode() == ValueMode::Value &&
286            input.is_mutable() &&
287            value.is_empty()
288    }
289
290    fn suffers_from_bad_input(&self, _value: &DOMString) -> bool {
291        false
292    }
293
294    fn suffers_from_type_mismatch(&self, _input: &HTMLInputElement, _value: &DOMString) -> bool {
295        false
296    }
297
298    fn value_for_shadow_dom(&self, _input: &HTMLInputElement) -> DOMString {
299        "".into()
300    }
301
302    /// <https://html.spec.whatwg.org/multipage/#signal-a-type-change>
303    fn signal_type_change(&self, _input: &HTMLInputElement, _can_gc: CanGc) {}
304
305    fn activation_behavior(
306        &self,
307        _input: &HTMLInputElement,
308        _event: &Event,
309        _target: &EventTarget,
310        _can_gc: CanGc,
311    ) {
312    }
313
314    fn legacy_pre_activation_behavior(
315        &self,
316        _input: &HTMLInputElement,
317        _can_gc: CanGc,
318    ) -> Option<InputActivationState> {
319        None
320    }
321
322    fn legacy_canceled_activation_behavior(
323        &self,
324        _input: &HTMLInputElement,
325        _cache: InputActivationState,
326        _can_gc: CanGc,
327    ) {
328    }
329
330    fn show_the_picker_if_applicable(&self, _input: &HTMLInputElement) {}
331
332    fn select_files(&self, _input: &HTMLInputElement, _test_paths: Option<Vec<DOMString>>) {}
333
334    fn get_files(&self) -> Option<DomRoot<FileList>> {
335        None
336    }
337
338    fn set_files(&self, _filelist: &FileList) {}
339
340    fn update_shadow_tree(&self, _cx: &mut JSContext, _input: &HTMLInputElement) {}
341
342    fn update_placeholder_contents(&self, _cx: &mut JSContext, _input: &HTMLInputElement) {}
343
344    fn attribute_mutated(
345        &self,
346        _cx: &mut JSContext,
347        _input: &HTMLInputElement,
348        _attr: &Attr,
349        _mutation: AttributeMutation,
350    ) {
351    }
352
353    fn bind_to_tree(&self, _cx: &mut JSContext, _input: &HTMLInputElement, _context: &BindContext) {
354    }
355
356    fn unbind_from_tree(
357        &self,
358        _input: &HTMLInputElement,
359        _form_owner: Option<DomRoot<HTMLFormElement>>,
360        _context: &UnbindContext,
361        _can_gc: CanGc,
362    ) {
363    }
364}