HTMLInputElement.cpp 49 KB

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