HTMLInputElement.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Adam Hodgen <ant1441@gmail.com>
  4. * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
  9. #include <LibWeb/CSS/StyleValues/IdentifierStyleValue.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/DOM/ElementFactory.h>
  12. #include <LibWeb/DOM/Event.h>
  13. #include <LibWeb/DOM/ShadowRoot.h>
  14. #include <LibWeb/DOM/Text.h>
  15. #include <LibWeb/HTML/BrowsingContext.h>
  16. #include <LibWeb/HTML/EventNames.h>
  17. #include <LibWeb/HTML/HTMLFormElement.h>
  18. #include <LibWeb/HTML/HTMLInputElement.h>
  19. #include <LibWeb/HTML/Scripting/Environments.h>
  20. #include <LibWeb/Infra/CharacterTypes.h>
  21. #include <LibWeb/Layout/BlockContainer.h>
  22. #include <LibWeb/Layout/ButtonBox.h>
  23. #include <LibWeb/Layout/CheckBox.h>
  24. #include <LibWeb/Layout/RadioButton.h>
  25. #include <LibWeb/Namespace.h>
  26. #include <LibWeb/WebIDL/DOMException.h>
  27. #include <LibWeb/WebIDL/ExceptionOr.h>
  28. namespace Web::HTML {
  29. HTMLInputElement::HTMLInputElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  30. : HTMLElement(document, move(qualified_name))
  31. , m_value(DeprecatedString::empty())
  32. {
  33. activation_behavior = [this](auto&) {
  34. // The activation behavior for input elements are these steps:
  35. // FIXME: 1. If this element is not mutable and is not in the Checkbox state and is not in the Radio state, then return.
  36. // 2. Run this element's input activation behavior, if any, and do nothing otherwise.
  37. run_input_activation_behavior().release_value_but_fixme_should_propagate_errors();
  38. };
  39. }
  40. HTMLInputElement::~HTMLInputElement() = default;
  41. JS::ThrowCompletionOr<void> HTMLInputElement::initialize(JS::Realm& realm)
  42. {
  43. MUST_OR_THROW_OOM(Base::initialize(realm));
  44. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLInputElementPrototype>(realm, "HTMLInputElement"));
  45. return {};
  46. }
  47. void HTMLInputElement::visit_edges(Cell::Visitor& visitor)
  48. {
  49. Base::visit_edges(visitor);
  50. visitor.visit(m_text_node.ptr());
  51. visitor.visit(m_legacy_pre_activation_behavior_checked_element_in_group.ptr());
  52. visitor.visit(m_selected_files);
  53. }
  54. JS::GCPtr<Layout::Node> HTMLInputElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  55. {
  56. if (type_state() == TypeAttributeState::Hidden)
  57. return nullptr;
  58. if (type_state() == TypeAttributeState::SubmitButton || type_state() == TypeAttributeState::Button || type_state() == TypeAttributeState::ResetButton || type_state() == TypeAttributeState::FileUpload)
  59. return heap().allocate_without_realm<Layout::ButtonBox>(document(), *this, move(style));
  60. if (type_state() == TypeAttributeState::Checkbox)
  61. return heap().allocate_without_realm<Layout::CheckBox>(document(), *this, move(style));
  62. if (type_state() == TypeAttributeState::RadioButton)
  63. return heap().allocate_without_realm<Layout::RadioButton>(document(), *this, move(style));
  64. // AD-HOC: We rewrite `display: inline` to `display: inline-block`.
  65. // This is required for the internal shadow tree to work correctly in layout.
  66. if (style->display().is_inline_outside() && style->display().is_flow_inside())
  67. style->set_property(CSS::PropertyID::Display, CSS::DisplayStyleValue::create(CSS::Display::from_short(CSS::Display::Short::InlineBlock)).release_value_but_fixme_should_propagate_errors());
  68. return Element::create_layout_node_for_display_type(document(), style->display(), style, this);
  69. }
  70. void HTMLInputElement::set_checked(bool checked, ChangeSource change_source)
  71. {
  72. if (m_checked == checked)
  73. return;
  74. // The dirty checkedness flag must be initially set to false when the element is created,
  75. // and must be set to true whenever the user interacts with the control in a way that changes the checkedness.
  76. if (change_source == ChangeSource::User)
  77. m_dirty_checkedness = true;
  78. m_checked = checked;
  79. // This element's :checked pseudo-class could be used in a sibling's sibling-selector,
  80. // so we need to invalidate the style of all siblings.
  81. if (parent()) {
  82. parent()->for_each_child([&](auto& child) {
  83. child.invalidate_style();
  84. });
  85. }
  86. }
  87. void HTMLInputElement::set_checked_binding(bool checked)
  88. {
  89. if (type_state() == TypeAttributeState::RadioButton) {
  90. if (checked)
  91. set_checked_within_group();
  92. else
  93. set_checked(false, ChangeSource::Programmatic);
  94. } else {
  95. set_checked(checked, ChangeSource::Programmatic);
  96. }
  97. }
  98. // https://html.spec.whatwg.org/multipage/input.html#dom-input-indeterminate
  99. void HTMLInputElement::set_indeterminate(bool value)
  100. {
  101. // On setting, it must be set to the new value. It has no effect except for changing the appearance of checkbox controls.
  102. m_indeterminate = value;
  103. }
  104. // https://html.spec.whatwg.org/multipage/input.html#dom-input-files
  105. JS::GCPtr<FileAPI::FileList> HTMLInputElement::files()
  106. {
  107. // On getting, if the IDL attribute applies, it must return a FileList object that represents the current selected files.
  108. // The same object must be returned until the list of selected files changes.
  109. // If the IDL attribute does not apply, then it must instead return null.
  110. if (m_type != TypeAttributeState::FileUpload)
  111. return nullptr;
  112. if (!m_selected_files)
  113. m_selected_files = FileAPI::FileList::create(realm(), {}).release_value_but_fixme_should_propagate_errors();
  114. return m_selected_files;
  115. }
  116. // https://html.spec.whatwg.org/multipage/input.html#dom-input-files
  117. void HTMLInputElement::set_files(JS::GCPtr<FileAPI::FileList> files)
  118. {
  119. // 1. If the IDL attribute does not apply or the given value is null, then return.
  120. if (m_type != TypeAttributeState::FileUpload || files == nullptr)
  121. return;
  122. // 2. Replace the element's selected files with the given value.
  123. m_selected_files = files;
  124. }
  125. // https://html.spec.whatwg.org/multipage/input.html#update-the-file-selection
  126. void HTMLInputElement::update_the_file_selection(JS::NonnullGCPtr<FileAPI::FileList> files)
  127. {
  128. // 1. Queue an element task on the user interaction task source given element and the following steps:
  129. queue_an_element_task(Task::Source::UserInteraction, [this, files] {
  130. // 1. Update element's selected files so that it represents the user's selection.
  131. this->set_files(files.ptr());
  132. // 2. Fire an event named input at the input element, with the bubbles and composed attributes initialized to true.
  133. auto input_event = DOM::Event::create(this->realm(), EventNames::input, { .bubbles = true, .composed = true }).release_value_but_fixme_should_propagate_errors();
  134. this->dispatch_event(input_event);
  135. // 3. Fire an event named change at the input element, with the bubbles attribute initialized to true.
  136. auto change_event = DOM::Event::create(this->realm(), EventNames::change, { .bubbles = true }).release_value_but_fixme_should_propagate_errors();
  137. this->dispatch_event(change_event);
  138. });
  139. }
  140. // https://html.spec.whatwg.org/multipage/input.html#show-the-picker,-if-applicable
  141. static void show_the_picker_if_applicable(HTMLInputElement& element)
  142. {
  143. // To show the picker, if applicable for an input element element:
  144. // 1. If element's relevant global object does not have transient activation, then return.
  145. auto& global_object = relevant_global_object(element);
  146. if (!is<HTML::Window>(global_object) || !static_cast<HTML::Window&>(global_object).has_transient_activation())
  147. return;
  148. // FIXME: 2. If element is not mutable, then return.
  149. // 3. If element's type attribute is in the File Upload state, then run these steps in parallel:
  150. if (element.type_state() == HTMLInputElement::TypeAttributeState::FileUpload) {
  151. // NOTE: These steps cannot be fully implemented here, and must be done in the PageClient when the response comes back from the PageHost
  152. // 1. Optionally, wait until any prior execution of this algorithm has terminated.
  153. // 2. Display a prompt to the user requesting that the user specify some files.
  154. // If the multiple attribute is not set on element, there must be no more than one file selected; otherwise, any number may be selected.
  155. // Files can be from the filesystem or created on the fly, e.g., a picture taken from a camera connected to the user's device.
  156. // 3. Wait for the user to have made their selection.
  157. // 4. If the user dismissed the prompt without changing their selection,
  158. // then queue an element task on the user interaction task source given element to fire an event named cancel at element,
  159. // with the bubbles attribute initialized to true.
  160. // 5. Otherwise, update the file selection for element.
  161. bool const multiple = element.has_attribute(HTML::AttributeNames::multiple);
  162. auto weak_element = element.make_weak_ptr<DOM::EventTarget>();
  163. // FIXME: Pass along accept attribute information https://html.spec.whatwg.org/multipage/input.html#attr-input-accept
  164. // The accept attribute may be specified to provide user agents with a hint of what file types will be accepted.
  165. element.document().browsing_context()->top_level_browsing_context().page()->client().page_did_request_file_picker(weak_element, multiple);
  166. return;
  167. }
  168. // FIXME: show "any relevant user interface" for other type attribute states "in the way [the user agent] normally would"
  169. // 4. Otherwise, the user agent should show any relevant user interface for selecting a value for element,
  170. // in the way it normally would when the user interacts with the control. (If no such UI applies to element, then this step does nothing.)
  171. // If such a user interface is shown, it must respect the requirements stated in the relevant parts of the specification for how element
  172. // behaves given its type attribute state. (For example, various sections describe restrictions on the resulting value string.)
  173. // This step can have side effects, such as closing other pickers that were previously shown by this algorithm.
  174. // (If this closes a file selection picker, then per the above that will lead to firing either input and change events, or a cancel event.)
  175. }
  176. // https://html.spec.whatwg.org/multipage/input.html#dom-input-showpicker
  177. WebIDL::ExceptionOr<void> HTMLInputElement::show_picker()
  178. {
  179. // The showPicker() method steps are:
  180. // FIXME: 1. If this is not mutable, then throw an "InvalidStateError" DOMException.
  181. // 2. If this's relevant settings object's origin is not same origin with this's relevant settings object's top-level origin,
  182. // and this's type attribute is not in the File Upload state or Color state, then throw a "SecurityError" DOMException.
  183. // NOTE: File and Color inputs are exempted from this check for historical reason: their input activation behavior also shows their pickers,
  184. // and has never been guarded by an origin check.
  185. if (!relevant_settings_object(*this).origin().is_same_origin(relevant_settings_object(*this).top_level_origin)
  186. && m_type != TypeAttributeState::FileUpload && m_type != TypeAttributeState::Color) {
  187. return WebIDL::SecurityError::create(realm(), "Cross origin pickers are not allowed"sv);
  188. }
  189. // 3. If this's relevant global object does not have transient activation, then throw a "NotAllowedError" DOMException.
  190. // FIXME: The global object we get here should probably not need casted to Window to check for transient activation
  191. auto& global_object = relevant_global_object(*this);
  192. if (!is<HTML::Window>(global_object) || !static_cast<HTML::Window&>(global_object).has_transient_activation()) {
  193. return WebIDL::NotAllowedError::create(realm(), "Too long since user activation to show picker"sv);
  194. }
  195. // 4. Show the picker, if applicable, for this.
  196. show_the_picker_if_applicable(*this);
  197. return {};
  198. }
  199. // https://html.spec.whatwg.org/multipage/input.html#input-activation-behavior
  200. ErrorOr<void> HTMLInputElement::run_input_activation_behavior()
  201. {
  202. if (type_state() == TypeAttributeState::Checkbox || type_state() == TypeAttributeState::RadioButton) {
  203. // 1. If the element is not connected, then return.
  204. if (!is_connected())
  205. return {};
  206. // 2. Fire an event named input at the element with the bubbles and composed attributes initialized to true.
  207. auto input_event = DOM::Event::create(realm(), HTML::EventNames::input).release_value_but_fixme_should_propagate_errors();
  208. input_event->set_bubbles(true);
  209. input_event->set_composed(true);
  210. dispatch_event(input_event);
  211. // 3. Fire an event named change at the element with the bubbles attribute initialized to true.
  212. auto change_event = DOM::Event::create(realm(), HTML::EventNames::change).release_value_but_fixme_should_propagate_errors();
  213. change_event->set_bubbles(true);
  214. dispatch_event(*change_event);
  215. } else if (type_state() == TypeAttributeState::SubmitButton) {
  216. JS::GCPtr<HTMLFormElement> form;
  217. // 1. If the element does not have a form owner, then return.
  218. if (!(form = this->form()))
  219. return {};
  220. // 2. If the element's node document is not fully active, then return.
  221. if (!document().is_fully_active())
  222. return {};
  223. // 3. Submit the form owner from the element.
  224. TRY(form->submit_form(this));
  225. } else if (type_state() == TypeAttributeState::FileUpload) {
  226. show_the_picker_if_applicable(*this);
  227. } else {
  228. dispatch_event(DOM::Event::create(realm(), EventNames::change).release_value_but_fixme_should_propagate_errors());
  229. }
  230. return {};
  231. }
  232. void HTMLInputElement::did_edit_text_node(Badge<BrowsingContext>)
  233. {
  234. // An input element's dirty value flag must be set to true whenever the user interacts with the control in a way that changes the value.
  235. m_value = value_sanitization_algorithm(m_text_node->data());
  236. m_dirty_value = true;
  237. // NOTE: This is a bit ad-hoc, but basically implements part of "4.10.5.5 Common event behaviors"
  238. // https://html.spec.whatwg.org/multipage/input.html#common-input-element-events
  239. queue_an_element_task(HTML::Task::Source::UserInteraction, [this] {
  240. auto input_event = DOM::Event::create(realm(), HTML::EventNames::input).release_value_but_fixme_should_propagate_errors();
  241. input_event->set_bubbles(true);
  242. input_event->set_composed(true);
  243. dispatch_event(*input_event);
  244. // FIXME: This should only fire when the input is "committed", whatever that means.
  245. auto change_event = DOM::Event::create(realm(), HTML::EventNames::change).release_value_but_fixme_should_propagate_errors();
  246. change_event->set_bubbles(true);
  247. dispatch_event(change_event);
  248. });
  249. }
  250. DeprecatedString HTMLInputElement::value() const
  251. {
  252. // https://html.spec.whatwg.org/multipage/input.html#dom-input-value-filename
  253. if (type_state() == TypeAttributeState::FileUpload) {
  254. // NOTE: This "fakepath" requirement is a sad accident of history. See the example in the File Upload state section for more information.
  255. // NOTE: Since path components are not permitted in filenames in the list of selected files, the "\fakepath\" cannot be mistaken for a path component.
  256. if (m_selected_files && m_selected_files->item(0))
  257. return DeprecatedString::formatted("C:\\fakepath\\{}", m_selected_files->item(0)->name());
  258. return "C:\\fakepath\\"sv;
  259. }
  260. // https://html.spec.whatwg.org/multipage/input.html#dom-input-value-value
  261. // Return the current value of the element.
  262. return m_value;
  263. }
  264. WebIDL::ExceptionOr<void> HTMLInputElement::set_value(DeprecatedString value)
  265. {
  266. // https://html.spec.whatwg.org/multipage/input.html#dom-input-value-filename
  267. if (type_state() == TypeAttributeState::FileUpload) {
  268. // On setting, if the new value is the empty string, empty the list of selected files; otherwise, throw an "InvalidStateError" DOMException.
  269. if (value != DeprecatedString::empty())
  270. return WebIDL::InvalidStateError::create(realm(), "Setting value of input type file to non-empty string"sv);
  271. m_selected_files = nullptr;
  272. return {};
  273. }
  274. // https://html.spec.whatwg.org/multipage/input.html#dom-input-value-value
  275. // 1. Let oldValue be the element's value.
  276. auto old_value = move(m_value);
  277. // 2. Set the element's value to the new value.
  278. // NOTE: This is done as part of step 4 below.
  279. // 3. Set the element's dirty value flag to true.
  280. m_dirty_value = true;
  281. // 4. Invoke the value sanitization algorithm, if the element's type attribute's current state defines one.
  282. m_value = value_sanitization_algorithm(move(value));
  283. // 5. If the element's value (after applying the value sanitization algorithm) is different from oldValue,
  284. // and the element has a text entry cursor position, move the text entry cursor position to the end of the
  285. // text control, unselecting any selected text and resetting the selection direction to "none".
  286. if (m_text_node && (m_value != old_value))
  287. m_text_node->set_data(m_value);
  288. return {};
  289. }
  290. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:attr-input-placeholder-3
  291. static bool is_allowed_to_have_placeholder(HTML::HTMLInputElement::TypeAttributeState state)
  292. {
  293. switch (state) {
  294. case HTML::HTMLInputElement::TypeAttributeState::Text:
  295. case HTML::HTMLInputElement::TypeAttributeState::Search:
  296. case HTML::HTMLInputElement::TypeAttributeState::URL:
  297. case HTML::HTMLInputElement::TypeAttributeState::Telephone:
  298. case HTML::HTMLInputElement::TypeAttributeState::Email:
  299. case HTML::HTMLInputElement::TypeAttributeState::Password:
  300. case HTML::HTMLInputElement::TypeAttributeState::Number:
  301. return true;
  302. default:
  303. return false;
  304. }
  305. }
  306. // https://html.spec.whatwg.org/multipage/input.html#attr-input-placeholder
  307. Optional<DeprecatedString> HTMLInputElement::placeholder_value() const
  308. {
  309. if (!m_text_node || !m_text_node->data().is_empty())
  310. return {};
  311. if (!is_allowed_to_have_placeholder(type_state()))
  312. return {};
  313. if (!has_attribute(HTML::AttributeNames::placeholder))
  314. return {};
  315. auto placeholder = attribute(HTML::AttributeNames::placeholder);
  316. if (placeholder.contains('\r') || placeholder.contains('\n')) {
  317. StringBuilder builder;
  318. for (auto ch : placeholder) {
  319. if (ch != '\r' && ch != '\n')
  320. builder.append(ch);
  321. }
  322. placeholder = builder.to_deprecated_string();
  323. }
  324. return placeholder;
  325. }
  326. void HTMLInputElement::create_shadow_tree_if_needed()
  327. {
  328. if (shadow_root_internal())
  329. return;
  330. // FIXME: This could be better factored. Everything except the below types becomes a text input.
  331. switch (type_state()) {
  332. case TypeAttributeState::RadioButton:
  333. case TypeAttributeState::Checkbox:
  334. case TypeAttributeState::Button:
  335. case TypeAttributeState::SubmitButton:
  336. case TypeAttributeState::ResetButton:
  337. case TypeAttributeState::ImageButton:
  338. return;
  339. default:
  340. break;
  341. }
  342. auto shadow_root = heap().allocate<DOM::ShadowRoot>(realm(), document(), *this, Bindings::ShadowRootMode::Closed).release_allocated_value_but_fixme_should_propagate_errors();
  343. auto initial_value = m_value;
  344. if (initial_value.is_null())
  345. initial_value = DeprecatedString::empty();
  346. auto element = DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
  347. MUST(element->set_attribute(HTML::AttributeNames::style, "white-space: pre; padding-top: 1px; padding-bottom: 1px; padding-left: 2px; padding-right: 2px; height: 1lh;"));
  348. m_text_node = heap().allocate<DOM::Text>(realm(), document(), initial_value).release_allocated_value_but_fixme_should_propagate_errors();
  349. m_text_node->set_always_editable(m_type != TypeAttributeState::FileUpload);
  350. m_text_node->set_owner_input_element({}, *this);
  351. if (m_type == TypeAttributeState::Password)
  352. m_text_node->set_is_password_input({}, true);
  353. MUST(element->append_child(*m_text_node));
  354. MUST(shadow_root->append_child(element));
  355. set_shadow_root(shadow_root);
  356. }
  357. void HTMLInputElement::did_receive_focus()
  358. {
  359. auto* browsing_context = document().browsing_context();
  360. if (!browsing_context)
  361. return;
  362. if (!m_text_node)
  363. return;
  364. browsing_context->set_cursor_position(DOM::Position { *m_text_node, 0 });
  365. }
  366. void HTMLInputElement::parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value)
  367. {
  368. HTMLElement::parse_attribute(name, value);
  369. if (name == HTML::AttributeNames::checked) {
  370. // When the checked content attribute is added, if the control does not have dirty checkedness,
  371. // the user agent must set the checkedness of the element to true
  372. if (!m_dirty_checkedness)
  373. set_checked(true, ChangeSource::Programmatic);
  374. } else if (name == HTML::AttributeNames::type) {
  375. m_type = parse_type_attribute(value);
  376. } else if (name == HTML::AttributeNames::value) {
  377. if (!m_dirty_value)
  378. m_value = value_sanitization_algorithm(value);
  379. }
  380. }
  381. HTMLInputElement::TypeAttributeState HTMLInputElement::parse_type_attribute(StringView type)
  382. {
  383. #define __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(keyword, state) \
  384. if (type.equals_ignoring_ascii_case(#keyword##sv)) \
  385. return HTMLInputElement::TypeAttributeState::state;
  386. ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTES
  387. #undef __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE
  388. // The missing value default and the invalid value default are the Text state.
  389. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:missing-value-default
  390. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:invalid-value-default
  391. return HTMLInputElement::TypeAttributeState::Text;
  392. }
  393. void HTMLInputElement::did_remove_attribute(DeprecatedFlyString const& name)
  394. {
  395. HTMLElement::did_remove_attribute(name);
  396. if (name == HTML::AttributeNames::checked) {
  397. // When the checked content attribute is removed, if the control does not have dirty checkedness,
  398. // the user agent must set the checkedness of the element to false.
  399. if (!m_dirty_checkedness)
  400. set_checked(false, ChangeSource::Programmatic);
  401. } else if (name == HTML::AttributeNames::value) {
  402. if (!m_dirty_value)
  403. m_value = DeprecatedString::empty();
  404. }
  405. }
  406. DeprecatedString HTMLInputElement::type() const
  407. {
  408. switch (m_type) {
  409. #define __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(keyword, state) \
  410. case TypeAttributeState::state: \
  411. return #keyword##sv;
  412. ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTES
  413. #undef __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE
  414. }
  415. VERIFY_NOT_REACHED();
  416. }
  417. void HTMLInputElement::set_type(DeprecatedString const& type)
  418. {
  419. MUST(set_attribute(HTML::AttributeNames::type, type));
  420. }
  421. // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-simple-colour
  422. static bool is_valid_simple_color(DeprecatedString const& value)
  423. {
  424. // if it is exactly seven characters long,
  425. if (value.length() != 7)
  426. return false;
  427. // and the first character is a U+0023 NUMBER SIGN character (#),
  428. if (!value.starts_with('#'))
  429. return false;
  430. // and the remaining six characters are all ASCII hex digits
  431. for (size_t i = 1; i < value.length(); i++)
  432. if (!is_ascii_hex_digit(value[i]))
  433. return false;
  434. return true;
  435. }
  436. // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-time-string
  437. static bool is_valid_time_string(DeprecatedString const& value)
  438. {
  439. // A string is a valid time string representing an hour hour, a minute minute, and a second second if it consists of the following components in the given order:
  440. // 1. Two ASCII digits, representing hour, in the range 0 ≤ hour ≤ 23
  441. // 2. A U+003A COLON character (:)
  442. // 3. Two ASCII digits, representing minute, in the range 0 ≤ minute ≤ 59
  443. // 4. If second is nonzero, or optionally if second is zero:
  444. // 1. A U+003A COLON character (:)
  445. // 2. Two ASCII digits, representing the integer part of second, in the range 0 ≤ s ≤ 59
  446. // 3. If second is not an integer, or optionally if second is an integer:
  447. // 1. A U+002E FULL STOP character (.)
  448. // 2. One, two, or three ASCII digits, representing the fractional part of second
  449. auto parts = value.split(':');
  450. if (parts.size() != 2 || parts.size() != 3)
  451. return false;
  452. if (parts[0].length() != 2)
  453. return false;
  454. auto hour = (parse_ascii_digit(parts[0][0]) * 10) + parse_ascii_digit(parts[0][1]);
  455. if (hour > 23)
  456. return false;
  457. if (parts[1].length() != 2)
  458. return false;
  459. auto minute = (parse_ascii_digit(parts[1][0]) * 10) + parse_ascii_digit(parts[1][1]);
  460. if (minute > 59)
  461. return false;
  462. if (parts.size() == 2)
  463. return true;
  464. if (parts[2].length() < 2)
  465. return false;
  466. auto second = (parse_ascii_digit(parts[2][0]) * 10) + parse_ascii_digit(parts[2][1]);
  467. if (second > 59)
  468. return false;
  469. if (parts[2].length() == 2)
  470. return true;
  471. auto second_parts = parts[2].split('.');
  472. if (second_parts.size() != 2)
  473. return false;
  474. if (second_parts[1].length() < 1 || second_parts[1].length() > 3)
  475. return false;
  476. for (auto digit : second_parts[1])
  477. if (!is_ascii_digit(digit))
  478. return false;
  479. return true;
  480. }
  481. // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#week-number-of-the-last-day
  482. static u32 week_number_of_the_last_day(u64)
  483. {
  484. // FIXME: sometimes return 53 (!)
  485. // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#weeks
  486. return 52;
  487. }
  488. // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-week-string
  489. static bool is_valid_week_string(DeprecatedString const& value)
  490. {
  491. // A string is a valid week string representing a week-year year and week week if it consists of the following components in the given order:
  492. // 1. Four or more ASCII digits, representing year, where year > 0
  493. // 2. A U+002D HYPHEN-MINUS character (-)
  494. // 3. A U+0057 LATIN CAPITAL LETTER W character (W)
  495. // 4. Two ASCII digits, representing the week week, in the range 1 ≤ week ≤ maxweek, where maxweek is the week number of the last day of week-year year
  496. auto parts = value.split('-');
  497. if (parts.size() != 2)
  498. return false;
  499. if (parts[0].length() < 4)
  500. return false;
  501. for (auto digit : parts[0])
  502. if (!is_ascii_digit(digit))
  503. return false;
  504. if (parts[1].length() != 3)
  505. return false;
  506. if (!parts[1].starts_with('W'))
  507. return false;
  508. if (!is_ascii_digit(parts[1][1]))
  509. return false;
  510. if (!is_ascii_digit(parts[1][2]))
  511. return false;
  512. u64 year = 0;
  513. for (auto d : parts[0]) {
  514. year *= 10;
  515. year += parse_ascii_digit(d);
  516. }
  517. auto week = (parse_ascii_digit(parts[1][1]) * 10) + parse_ascii_digit(parts[1][2]);
  518. return week >= 1 && week <= week_number_of_the_last_day(year);
  519. }
  520. // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-month-string
  521. static bool is_valid_month_string(DeprecatedString const& value)
  522. {
  523. // A string is a valid month string representing a year year and month month if it consists of the following components in the given order:
  524. // 1. Four or more ASCII digits, representing year, where year > 0
  525. // 2. A U+002D HYPHEN-MINUS character (-)
  526. // 3. Two ASCII digits, representing the month month, in the range 1 ≤ month ≤ 12
  527. auto parts = value.split('-');
  528. if (parts.size() != 2)
  529. return false;
  530. if (parts[0].length() < 4)
  531. return false;
  532. for (auto digit : parts[0])
  533. if (!is_ascii_digit(digit))
  534. return false;
  535. if (parts[1].length() != 2)
  536. return false;
  537. if (!is_ascii_digit(parts[1][0]))
  538. return false;
  539. if (!is_ascii_digit(parts[1][1]))
  540. return false;
  541. auto month = (parse_ascii_digit(parts[1][0]) * 10) + parse_ascii_digit(parts[1][1]);
  542. return month >= 1 && month <= 12;
  543. }
  544. // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-date-string
  545. static bool is_valid_date_string(DeprecatedString const& value)
  546. {
  547. // A string is a valid date string representing a year year, month month, and day day if it consists of the following components in the given order:
  548. // 1. A valid month string, representing year and month
  549. // 2. A U+002D HYPHEN-MINUS character (-)
  550. // 3. Two ASCII digits, representing day, in the range 1 ≤ day ≤ maxday where maxday is the number of days in the month month and year year
  551. auto parts = value.split('-');
  552. if (parts.size() != 3)
  553. return false;
  554. if (!is_valid_month_string(DeprecatedString::formatted("{}-{}", parts[0], parts[1])))
  555. return false;
  556. if (parts[2].length() != 2)
  557. return false;
  558. i64 year = 0;
  559. for (auto d : parts[0]) {
  560. year *= 10;
  561. year += parse_ascii_digit(d);
  562. }
  563. auto month = (parse_ascii_digit(parts[1][0]) * 10) + parse_ascii_digit(parts[1][1]);
  564. i64 day = (parse_ascii_digit(parts[2][0]) * 10) + parse_ascii_digit(parts[2][1]);
  565. return day >= 1 && day <= AK::days_in_month(year, month);
  566. }
  567. // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-local-date-and-time-string
  568. static bool is_valid_local_date_and_time_string(DeprecatedString const& value)
  569. {
  570. auto parts_split_by_T = value.split('T');
  571. if (parts_split_by_T.size() == 2)
  572. return is_valid_date_string(parts_split_by_T[0]) && is_valid_time_string(parts_split_by_T[1]);
  573. auto parts_split_by_space = value.split(' ');
  574. if (parts_split_by_space.size() == 2)
  575. return is_valid_date_string(parts_split_by_space[0]) && is_valid_time_string(parts_split_by_space[1]);
  576. return false;
  577. }
  578. // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-normalised-local-date-and-time-string
  579. static DeprecatedString normalize_local_date_and_time_string(DeprecatedString const& value)
  580. {
  581. VERIFY(value.count(" "sv) == 1);
  582. return value.replace(" "sv, "T"sv, ReplaceMode::FirstOnly);
  583. }
  584. // https://html.spec.whatwg.org/multipage/input.html#value-sanitization-algorithm
  585. DeprecatedString HTMLInputElement::value_sanitization_algorithm(DeprecatedString value) const
  586. {
  587. if (type_state() == HTMLInputElement::TypeAttributeState::Text || type_state() == HTMLInputElement::TypeAttributeState::Search || type_state() == HTMLInputElement::TypeAttributeState::Telephone || type_state() == HTMLInputElement::TypeAttributeState::Password) {
  588. // Strip newlines from the value.
  589. if (value.contains('\r') || value.contains('\n')) {
  590. StringBuilder builder;
  591. for (auto c : value) {
  592. if (!(c == '\r' || c == '\n'))
  593. builder.append(c);
  594. }
  595. return builder.to_deprecated_string();
  596. }
  597. } else if (type_state() == HTMLInputElement::TypeAttributeState::URL) {
  598. // Strip newlines from the value, then strip leading and trailing ASCII whitespace from the value.
  599. if (value.contains('\r') || value.contains('\n')) {
  600. StringBuilder builder;
  601. for (auto c : value) {
  602. if (!(c == '\r' || c == '\n'))
  603. builder.append(c);
  604. }
  605. return builder.string_view().trim(Infra::ASCII_WHITESPACE);
  606. }
  607. } else if (type_state() == HTMLInputElement::TypeAttributeState::Email) {
  608. // https://html.spec.whatwg.org/multipage/input.html#email-state-(type=email):value-sanitization-algorithm
  609. // FIXME: handle the `multiple` attribute
  610. // Strip newlines from the value, then strip leading and trailing ASCII whitespace from the value.
  611. if (value.contains('\r') || value.contains('\n')) {
  612. StringBuilder builder;
  613. for (auto c : value) {
  614. if (!(c == '\r' || c == '\n'))
  615. builder.append(c);
  616. }
  617. return builder.string_view().trim(Infra::ASCII_WHITESPACE);
  618. }
  619. } else if (type_state() == HTMLInputElement::TypeAttributeState::Number) {
  620. // If the value of the element is not a valid floating-point number, then set it to the empty string instead.
  621. // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#rules-for-parsing-floating-point-number-values
  622. // 6. Skip ASCII whitespace within input given position.
  623. auto maybe_double = value.to_double(TrimWhitespace::Yes);
  624. if (!maybe_double.has_value() || !isfinite(maybe_double.value()))
  625. return "";
  626. } else if (type_state() == HTMLInputElement::TypeAttributeState::Date) {
  627. // https://html.spec.whatwg.org/multipage/input.html#date-state-(type=date):value-sanitization-algorithm
  628. if (!is_valid_date_string(value))
  629. return "";
  630. } else if (type_state() == HTMLInputElement::TypeAttributeState::Month) {
  631. // https://html.spec.whatwg.org/multipage/input.html#month-state-(type=month):value-sanitization-algorithm
  632. if (!is_valid_month_string(value))
  633. return "";
  634. } else if (type_state() == HTMLInputElement::TypeAttributeState::Week) {
  635. // https://html.spec.whatwg.org/multipage/input.html#week-state-(type=week):value-sanitization-algorithm
  636. if (!is_valid_week_string(value))
  637. return "";
  638. } else if (type_state() == HTMLInputElement::TypeAttributeState::Time) {
  639. // https://html.spec.whatwg.org/multipage/input.html#time-state-(type=time):value-sanitization-algorithm
  640. if (!is_valid_time_string(value))
  641. return "";
  642. } else if (type_state() == HTMLInputElement::TypeAttributeState::LocalDateAndTime) {
  643. // https://html.spec.whatwg.org/multipage/input.html#local-date-and-time-state-(type=datetime-local):value-sanitization-algorithm
  644. if (is_valid_local_date_and_time_string(value))
  645. return normalize_local_date_and_time_string(value);
  646. return "";
  647. } else if (type_state() == HTMLInputElement::TypeAttributeState::Range) {
  648. // https://html.spec.whatwg.org/multipage/input.html#range-state-(type=range):value-sanitization-algorithm
  649. auto maybe_double = value.to_double(TrimWhitespace::Yes);
  650. if (!maybe_double.has_value() || !isfinite(maybe_double.value()))
  651. return JS::number_to_deprecated_string(maybe_double.value_or(0));
  652. } else if (type_state() == HTMLInputElement::TypeAttributeState::Color) {
  653. // https://html.spec.whatwg.org/multipage/input.html#color-state-(type=color):value-sanitization-algorithm
  654. // If the value of the element is a valid simple color, then set it to the value of the element converted to ASCII lowercase;
  655. if (is_valid_simple_color(value))
  656. return value.to_lowercase();
  657. // otherwise, set it to the string "#000000".
  658. return "#000000";
  659. }
  660. return value;
  661. }
  662. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:concept-form-reset-control
  663. void HTMLInputElement::reset_algorithm()
  664. {
  665. // The reset algorithm for input elements is to set the dirty value flag and dirty checkedness flag back to false,
  666. m_dirty_value = false;
  667. m_dirty_checkedness = false;
  668. // set the value of the element to the value of the value content attribute, if there is one, or the empty string otherwise,
  669. m_value = has_attribute(AttributeNames::value) ? get_attribute(AttributeNames::value) : DeprecatedString::empty();
  670. // set the checkedness of the element to true if the element has a checked content attribute and false if it does not,
  671. m_checked = has_attribute(AttributeNames::checked);
  672. // empty the list of selected files,
  673. m_selected_files = FileAPI::FileList::create(realm(), {}).release_value_but_fixme_should_propagate_errors();
  674. // and then invoke the value sanitization algorithm, if the type attribute's current state defines one.
  675. m_value = value_sanitization_algorithm(m_value);
  676. if (m_text_node)
  677. m_text_node->set_data(m_value);
  678. }
  679. void HTMLInputElement::form_associated_element_was_inserted()
  680. {
  681. create_shadow_tree_if_needed();
  682. }
  683. // https://html.spec.whatwg.org/multipage/input.html#radio-button-group
  684. static bool is_in_same_radio_button_group(HTML::HTMLInputElement const& a, HTML::HTMLInputElement const& b)
  685. {
  686. auto non_empty_equals = [](auto const& value_a, auto const& value_b) {
  687. return !value_a.is_empty() && value_a == value_b;
  688. };
  689. // The radio button group that contains an input element a also contains all the
  690. // other input elements b that fulfill all of the following conditions:
  691. return (
  692. // - Both a and b are in the same tree.
  693. // - The input element b's type attribute is in the Radio Button state.
  694. a.type_state() == b.type_state()
  695. && b.type_state() == HTMLInputElement::TypeAttributeState::RadioButton
  696. // - Either a and b have the same form owner, or they both have no form owner.
  697. && a.form() == b.form()
  698. // - They both have a name attribute, their name attributes are not empty, and the
  699. // value of a's name attribute equals the value of b's name attribute.
  700. && a.has_attribute(HTML::AttributeNames::name)
  701. && b.has_attribute(HTML::AttributeNames::name)
  702. && non_empty_equals(a.name(), b.name()));
  703. }
  704. // https://html.spec.whatwg.org/multipage/input.html#radio-button-state-(type=radio)
  705. void HTMLInputElement::set_checked_within_group()
  706. {
  707. if (checked())
  708. return;
  709. set_checked(true, ChangeSource::User);
  710. // No point iterating the tree if we have an empty name.
  711. auto name = this->name();
  712. if (name.is_empty())
  713. return;
  714. document().for_each_in_inclusive_subtree_of_type<HTML::HTMLInputElement>([&](auto& element) {
  715. if (element.checked() && &element != this && is_in_same_radio_button_group(*this, element))
  716. element.set_checked(false, ChangeSource::User);
  717. return IterationDecision::Continue;
  718. });
  719. }
  720. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:legacy-pre-activation-behavior
  721. void HTMLInputElement::legacy_pre_activation_behavior()
  722. {
  723. m_before_legacy_pre_activation_behavior_checked = checked();
  724. m_before_legacy_pre_activation_behavior_indeterminate = indeterminate();
  725. // 1. If this element's type attribute is in the Checkbox state, then set
  726. // this element's checkedness to its opposite value (i.e. true if it is
  727. // false, false if it is true) and set this element's indeterminate IDL
  728. // attribute to false.
  729. if (type_state() == TypeAttributeState::Checkbox) {
  730. set_checked(!checked(), ChangeSource::User);
  731. set_indeterminate(false);
  732. }
  733. // 2. If this element's type attribute is in the Radio Button state, then
  734. // get a reference to the element in this element's radio button group that
  735. // has its checkedness set to true, if any, and then set this element's
  736. // checkedness to true.
  737. if (type_state() == TypeAttributeState::RadioButton) {
  738. DeprecatedString name = this->name();
  739. document().for_each_in_inclusive_subtree_of_type<HTML::HTMLInputElement>([&](auto& element) {
  740. if (element.checked() && is_in_same_radio_button_group(*this, element)) {
  741. m_legacy_pre_activation_behavior_checked_element_in_group = &element;
  742. return IterationDecision::Break;
  743. }
  744. return IterationDecision::Continue;
  745. });
  746. set_checked_within_group();
  747. }
  748. }
  749. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:legacy-canceled-activation-behavior
  750. void HTMLInputElement::legacy_cancelled_activation_behavior()
  751. {
  752. // 1. If the element's type attribute is in the Checkbox state, then set the
  753. // element's checkedness and the element's indeterminate IDL attribute back
  754. // to the values they had before the legacy-pre-activation behavior was run.
  755. if (type_state() == TypeAttributeState::Checkbox) {
  756. set_checked(m_before_legacy_pre_activation_behavior_checked, ChangeSource::Programmatic);
  757. set_indeterminate(m_before_legacy_pre_activation_behavior_indeterminate);
  758. }
  759. // 2. If this element 's type attribute is in the Radio Button state, then
  760. // if the element to which a reference was obtained in the
  761. // legacy-pre-activation behavior, if any, is still in what is now this
  762. // element' s radio button group, if it still has one, and if so, setting
  763. // that element 's checkedness to true; or else, if there was no such
  764. // element, or that element is no longer in this element' s radio button
  765. // group, or if this element no longer has a radio button group, setting
  766. // this element's checkedness to false.
  767. if (type_state() == TypeAttributeState::RadioButton) {
  768. bool did_reselect_previous_element = false;
  769. if (m_legacy_pre_activation_behavior_checked_element_in_group) {
  770. auto& element_in_group = *m_legacy_pre_activation_behavior_checked_element_in_group;
  771. if (is_in_same_radio_button_group(*this, element_in_group)) {
  772. element_in_group.set_checked_within_group();
  773. did_reselect_previous_element = true;
  774. }
  775. m_legacy_pre_activation_behavior_checked_element_in_group = nullptr;
  776. }
  777. if (!did_reselect_previous_element)
  778. set_checked(false, ChangeSource::User);
  779. }
  780. }
  781. void HTMLInputElement::legacy_cancelled_activation_behavior_was_not_called()
  782. {
  783. m_legacy_pre_activation_behavior_checked_element_in_group = nullptr;
  784. }
  785. // https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
  786. i32 HTMLInputElement::default_tab_index_value() const
  787. {
  788. // See the base function for the spec comments.
  789. return 0;
  790. }
  791. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-checkvalidity
  792. WebIDL::ExceptionOr<bool> HTMLInputElement::check_validity()
  793. {
  794. dbgln("(STUBBED) HTMLInputElement::check_validity(). Called on: {}", debug_description());
  795. return true;
  796. }
  797. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-reportvalidity
  798. WebIDL::ExceptionOr<bool> HTMLInputElement::report_validity()
  799. {
  800. dbgln("(STUBBED) HTMLInputElement::report_validity(). Called on: {}", debug_description());
  801. return true;
  802. }
  803. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-setcustomvalidity
  804. void HTMLInputElement::set_custom_validity(DeprecatedString const& error)
  805. {
  806. dbgln("(STUBBED) HTMLInputElement::set_custom_validity(error={}). Called on: {}", error, debug_description());
  807. return;
  808. }
  809. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-select
  810. WebIDL::ExceptionOr<void> HTMLInputElement::select()
  811. {
  812. dbgln("(STUBBED) HTMLInputElement::select(). Called on: {}", debug_description());
  813. return {};
  814. }
  815. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-setselectionrange
  816. WebIDL::ExceptionOr<void> HTMLInputElement::set_selection_range(u32 start, u32 end, DeprecatedString const& direction)
  817. {
  818. dbgln("(STUBBED) HTMLInputElement::set_selection_range(start={}, end={}, direction='{}'). Called on: {}", start, end, direction, debug_description());
  819. return {};
  820. }
  821. Optional<ARIA::Role> HTMLInputElement::default_role() const
  822. {
  823. // https://www.w3.org/TR/html-aria/#el-input-button
  824. if (type_state() == TypeAttributeState::Button)
  825. return ARIA::Role::button;
  826. // https://www.w3.org/TR/html-aria/#el-input-checkbox
  827. if (type_state() == TypeAttributeState::Checkbox)
  828. return ARIA::Role::checkbox;
  829. // https://www.w3.org/TR/html-aria/#el-input-email
  830. if (type_state() == TypeAttributeState::Email && attribute("list").is_null())
  831. return ARIA::Role::textbox;
  832. // https://www.w3.org/TR/html-aria/#el-input-image
  833. if (type_state() == TypeAttributeState::ImageButton)
  834. return ARIA::Role::button;
  835. // https://www.w3.org/TR/html-aria/#el-input-number
  836. if (type_state() == TypeAttributeState::Number)
  837. return ARIA::Role::spinbutton;
  838. // https://www.w3.org/TR/html-aria/#el-input-radio
  839. if (type_state() == TypeAttributeState::RadioButton)
  840. return ARIA::Role::radio;
  841. // https://www.w3.org/TR/html-aria/#el-input-range
  842. if (type_state() == TypeAttributeState::Range)
  843. return ARIA::Role::slider;
  844. // https://www.w3.org/TR/html-aria/#el-input-reset
  845. if (type_state() == TypeAttributeState::ResetButton)
  846. return ARIA::Role::button;
  847. // https://www.w3.org/TR/html-aria/#el-input-text-list
  848. if ((type_state() == TypeAttributeState::Text
  849. || type_state() == TypeAttributeState::Search
  850. || type_state() == TypeAttributeState::Telephone
  851. || type_state() == TypeAttributeState::URL
  852. || type_state() == TypeAttributeState::Email)
  853. && !attribute("list").is_null())
  854. return ARIA::Role::combobox;
  855. // https://www.w3.org/TR/html-aria/#el-input-search
  856. if (type_state() == TypeAttributeState::Search && attribute("list").is_null())
  857. return ARIA::Role::textbox;
  858. // https://www.w3.org/TR/html-aria/#el-input-submit
  859. if (type_state() == TypeAttributeState::SubmitButton)
  860. return ARIA::Role::button;
  861. // https://www.w3.org/TR/html-aria/#el-input-tel
  862. if (type_state() == TypeAttributeState::Telephone)
  863. return ARIA::Role::textbox;
  864. // https://www.w3.org/TR/html-aria/#el-input-text
  865. if (type_state() == TypeAttributeState::Text && attribute("list").is_null())
  866. return ARIA::Role::textbox;
  867. // https://www.w3.org/TR/html-aria/#el-input-url
  868. if (type_state() == TypeAttributeState::URL && attribute("list").is_null())
  869. return ARIA::Role::textbox;
  870. // https://www.w3.org/TR/html-aria/#el-input-color
  871. // https://www.w3.org/TR/html-aria/#el-input-date
  872. // https://www.w3.org/TR/html-aria/#el-input-datetime-local
  873. // https://www.w3.org/TR/html-aria/#el-input-file
  874. // https://www.w3.org/TR/html-aria/#el-input-hidden
  875. // https://www.w3.org/TR/html-aria/#el-input-month
  876. // https://www.w3.org/TR/html-aria/#el-input-password
  877. // https://www.w3.org/TR/html-aria/#el-input-time
  878. // https://www.w3.org/TR/html-aria/#el-input-week
  879. return {};
  880. }
  881. }