HTMLInputElement.cpp 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960
  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. * Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
  6. * Copyright (c) 2023, Bastiaan van der Plaat <bastiaan.v.d.plaat@gmail.com>
  7. *
  8. * SPDX-License-Identifier: BSD-2-Clause
  9. */
  10. #include <LibJS/Runtime/Date.h>
  11. #include <LibJS/Runtime/NativeFunction.h>
  12. #include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
  13. #include <LibWeb/CSS/StyleValues/IdentifierStyleValue.h>
  14. #include <LibWeb/DOM/Document.h>
  15. #include <LibWeb/DOM/ElementFactory.h>
  16. #include <LibWeb/DOM/Event.h>
  17. #include <LibWeb/DOM/IDLEventListener.h>
  18. #include <LibWeb/DOM/ShadowRoot.h>
  19. #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
  20. #include <LibWeb/HTML/BrowsingContext.h>
  21. #include <LibWeb/HTML/Dates.h>
  22. #include <LibWeb/HTML/DecodedImageData.h>
  23. #include <LibWeb/HTML/EventNames.h>
  24. #include <LibWeb/HTML/HTMLDivElement.h>
  25. #include <LibWeb/HTML/HTMLFormElement.h>
  26. #include <LibWeb/HTML/HTMLInputElement.h>
  27. #include <LibWeb/HTML/Numbers.h>
  28. #include <LibWeb/HTML/Scripting/Environments.h>
  29. #include <LibWeb/HTML/SharedImageRequest.h>
  30. #include <LibWeb/HTML/Window.h>
  31. #include <LibWeb/Infra/CharacterTypes.h>
  32. #include <LibWeb/Infra/Strings.h>
  33. #include <LibWeb/Layout/BlockContainer.h>
  34. #include <LibWeb/Layout/ButtonBox.h>
  35. #include <LibWeb/Layout/CheckBox.h>
  36. #include <LibWeb/Layout/ImageBox.h>
  37. #include <LibWeb/Layout/RadioButton.h>
  38. #include <LibWeb/Namespace.h>
  39. #include <LibWeb/Page/Page.h>
  40. #include <LibWeb/UIEvents/EventNames.h>
  41. #include <LibWeb/UIEvents/MouseEvent.h>
  42. #include <LibWeb/WebIDL/DOMException.h>
  43. #include <LibWeb/WebIDL/ExceptionOr.h>
  44. namespace Web::HTML {
  45. JS_DEFINE_ALLOCATOR(HTMLInputElement);
  46. HTMLInputElement::HTMLInputElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  47. : HTMLElement(document, move(qualified_name))
  48. {
  49. }
  50. HTMLInputElement::~HTMLInputElement() = default;
  51. void HTMLInputElement::initialize(JS::Realm& realm)
  52. {
  53. Base::initialize(realm);
  54. set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLInputElementPrototype>(realm, "HTMLInputElement"_fly_string));
  55. }
  56. void HTMLInputElement::visit_edges(Cell::Visitor& visitor)
  57. {
  58. Base::visit_edges(visitor);
  59. visitor.visit(m_inner_text_element);
  60. visitor.visit(m_text_node);
  61. visitor.visit(m_placeholder_element);
  62. visitor.visit(m_placeholder_text_node);
  63. visitor.visit(m_color_well_element);
  64. visitor.visit(m_legacy_pre_activation_behavior_checked_element_in_group);
  65. visitor.visit(m_selected_files);
  66. visitor.visit(m_slider_thumb);
  67. visitor.visit(m_image_request);
  68. }
  69. JS::GCPtr<Layout::Node> HTMLInputElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  70. {
  71. if (type_state() == TypeAttributeState::Hidden)
  72. return nullptr;
  73. if (type_state() == TypeAttributeState::SubmitButton || type_state() == TypeAttributeState::Button || type_state() == TypeAttributeState::ResetButton || type_state() == TypeAttributeState::FileUpload)
  74. return heap().allocate_without_realm<Layout::ButtonBox>(document(), *this, move(style));
  75. if (type_state() == TypeAttributeState::ImageButton)
  76. return heap().allocate_without_realm<Layout::ImageBox>(document(), *this, move(style), *this);
  77. if (type_state() == TypeAttributeState::Checkbox)
  78. return heap().allocate_without_realm<Layout::CheckBox>(document(), *this, move(style));
  79. if (type_state() == TypeAttributeState::RadioButton)
  80. return heap().allocate_without_realm<Layout::RadioButton>(document(), *this, move(style));
  81. // AD-HOC: We rewrite `display: inline` to `display: inline-block`.
  82. // This is required for the internal shadow tree to work correctly in layout.
  83. if (style->display().is_inline_outside() && style->display().is_flow_inside())
  84. style->set_property(CSS::PropertyID::Display, CSS::DisplayStyleValue::create(CSS::Display::from_short(CSS::Display::Short::InlineBlock)));
  85. return Element::create_layout_node_for_display_type(document(), style->display(), style, this);
  86. }
  87. void HTMLInputElement::set_checked(bool checked, ChangeSource change_source)
  88. {
  89. if (m_checked == checked)
  90. return;
  91. // The dirty checkedness flag must be initially set to false when the element is created,
  92. // and must be set to true whenever the user interacts with the control in a way that changes the checkedness.
  93. if (change_source == ChangeSource::User)
  94. m_dirty_checkedness = true;
  95. m_checked = checked;
  96. // This element's :checked pseudo-class could be used in a sibling's sibling-selector,
  97. // so we need to invalidate the style of all siblings.
  98. if (parent()) {
  99. parent()->for_each_child([&](auto& child) {
  100. child.invalidate_style();
  101. });
  102. }
  103. }
  104. void HTMLInputElement::set_checked_binding(bool checked)
  105. {
  106. if (type_state() == TypeAttributeState::RadioButton) {
  107. if (checked)
  108. set_checked_within_group();
  109. else
  110. set_checked(false, ChangeSource::Programmatic);
  111. } else {
  112. set_checked(checked, ChangeSource::Programmatic);
  113. }
  114. }
  115. // https://html.spec.whatwg.org/multipage/input.html#dom-input-indeterminate
  116. void HTMLInputElement::set_indeterminate(bool value)
  117. {
  118. // On setting, it must be set to the new value. It has no effect except for changing the appearance of checkbox controls.
  119. m_indeterminate = value;
  120. }
  121. // https://html.spec.whatwg.org/multipage/input.html#dom-input-files
  122. JS::GCPtr<FileAPI::FileList> HTMLInputElement::files()
  123. {
  124. // On getting, if the IDL attribute applies, it must return a FileList object that represents the current selected files.
  125. // The same object must be returned until the list of selected files changes.
  126. // If the IDL attribute does not apply, then it must instead return null.
  127. if (m_type != TypeAttributeState::FileUpload)
  128. return nullptr;
  129. if (!m_selected_files)
  130. m_selected_files = FileAPI::FileList::create(realm(), {});
  131. return m_selected_files;
  132. }
  133. // https://html.spec.whatwg.org/multipage/input.html#dom-input-files
  134. void HTMLInputElement::set_files(JS::GCPtr<FileAPI::FileList> files)
  135. {
  136. // 1. If the IDL attribute does not apply or the given value is null, then return.
  137. if (m_type != TypeAttributeState::FileUpload || files == nullptr)
  138. return;
  139. // 2. Replace the element's selected files with the given value.
  140. m_selected_files = files;
  141. }
  142. // https://html.spec.whatwg.org/multipage/input.html#update-the-file-selection
  143. void HTMLInputElement::update_the_file_selection(JS::NonnullGCPtr<FileAPI::FileList> files)
  144. {
  145. // 1. Queue an element task on the user interaction task source given element and the following steps:
  146. queue_an_element_task(Task::Source::UserInteraction, [this, files] {
  147. // 1. Update element's selected files so that it represents the user's selection.
  148. this->set_files(files.ptr());
  149. // 2. Fire an event named input at the input element, with the bubbles and composed attributes initialized to true.
  150. auto input_event = DOM::Event::create(this->realm(), EventNames::input, { .bubbles = true, .composed = true });
  151. this->dispatch_event(input_event);
  152. // 3. Fire an event named change at the input element, with the bubbles attribute initialized to true.
  153. auto change_event = DOM::Event::create(this->realm(), EventNames::change, { .bubbles = true });
  154. this->dispatch_event(change_event);
  155. });
  156. }
  157. // https://html.spec.whatwg.org/multipage/input.html#show-the-picker,-if-applicable
  158. static void show_the_picker_if_applicable(HTMLInputElement& element)
  159. {
  160. // To show the picker, if applicable for an input element element:
  161. // 1. If element's relevant global object does not have transient activation, then return.
  162. auto& global_object = relevant_global_object(element);
  163. if (!is<HTML::Window>(global_object) || !static_cast<HTML::Window&>(global_object).has_transient_activation())
  164. return;
  165. // 2. If element is not mutable, then return.
  166. if (!element.is_mutable())
  167. return;
  168. // 3. If element's type attribute is in the File Upload state, then run these steps in parallel:
  169. if (element.type_state() == HTMLInputElement::TypeAttributeState::FileUpload) {
  170. // NOTE: These steps cannot be fully implemented here, and must be done in the PageClient when the response comes back from the PageHost
  171. // 1. Optionally, wait until any prior execution of this algorithm has terminated.
  172. // 2. Display a prompt to the user requesting that the user specify some files.
  173. // If the multiple attribute is not set on element, there must be no more than one file selected; otherwise, any number may be selected.
  174. // 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.
  175. // 3. Wait for the user to have made their selection.
  176. // 4. If the user dismissed the prompt without changing their selection,
  177. // then queue an element task on the user interaction task source given element to fire an event named cancel at element,
  178. // with the bubbles attribute initialized to true.
  179. // 5. Otherwise, update the file selection for element.
  180. bool const multiple = element.has_attribute(HTML::AttributeNames::multiple);
  181. auto weak_element = element.make_weak_ptr<DOM::EventTarget>();
  182. // FIXME: Pass along accept attribute information https://html.spec.whatwg.org/multipage/input.html#attr-input-accept
  183. // The accept attribute may be specified to provide user agents with a hint of what file types will be accepted.
  184. element.document().browsing_context()->top_level_browsing_context()->page().client().page_did_request_file_picker(weak_element, multiple);
  185. return;
  186. }
  187. // 4. Otherwise, the user agent should show any relevant user interface for selecting a value for element,
  188. // 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.)
  189. // If such a user interface is shown, it must respect the requirements stated in the relevant parts of the specification for how element
  190. // behaves given its type attribute state. (For example, various sections describe restrictions on the resulting value string.)
  191. // This step can have side effects, such as closing other pickers that were previously shown by this algorithm.
  192. // (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.)
  193. if (element.type_state() == HTMLInputElement::TypeAttributeState::Color) {
  194. auto weak_element = element.make_weak_ptr<HTMLInputElement>();
  195. element.document().browsing_context()->top_level_browsing_context()->page().did_request_color_picker(weak_element, Color::from_string(element.value()).value_or(Color(0, 0, 0)));
  196. }
  197. }
  198. // https://html.spec.whatwg.org/multipage/input.html#dom-input-showpicker
  199. WebIDL::ExceptionOr<void> HTMLInputElement::show_picker()
  200. {
  201. // The showPicker() method steps are:
  202. // 1. If this is not mutable, then throw an "InvalidStateError" DOMException.
  203. if (!m_is_mutable)
  204. return WebIDL::InvalidStateError::create(realm(), "Element is not mutable"_fly_string);
  205. // 2. If this's relevant settings object's origin is not same origin with this's relevant settings object's top-level origin,
  206. // and this's type attribute is not in the File Upload state or Color state, then throw a "SecurityError" DOMException.
  207. // NOTE: File and Color inputs are exempted from this check for historical reason: their input activation behavior also shows their pickers,
  208. // and has never been guarded by an origin check.
  209. if (!relevant_settings_object(*this).origin().is_same_origin(relevant_settings_object(*this).top_level_origin)
  210. && m_type != TypeAttributeState::FileUpload && m_type != TypeAttributeState::Color) {
  211. return WebIDL::SecurityError::create(realm(), "Cross origin pickers are not allowed"_fly_string);
  212. }
  213. // 3. If this's relevant global object does not have transient activation, then throw a "NotAllowedError" DOMException.
  214. // FIXME: The global object we get here should probably not need casted to Window to check for transient activation
  215. auto& global_object = relevant_global_object(*this);
  216. if (!is<HTML::Window>(global_object) || !static_cast<HTML::Window&>(global_object).has_transient_activation()) {
  217. return WebIDL::NotAllowedError::create(realm(), "Too long since user activation to show picker"_fly_string);
  218. }
  219. // 4. Show the picker, if applicable, for this.
  220. show_the_picker_if_applicable(*this);
  221. return {};
  222. }
  223. // https://html.spec.whatwg.org/multipage/input.html#input-activation-behavior
  224. WebIDL::ExceptionOr<void> HTMLInputElement::run_input_activation_behavior(DOM::Event const& event)
  225. {
  226. if (type_state() == TypeAttributeState::Checkbox || type_state() == TypeAttributeState::RadioButton) {
  227. // 1. If the element is not connected, then return.
  228. if (!is_connected())
  229. return {};
  230. // 2. Fire an event named input at the element with the bubbles and composed attributes initialized to true.
  231. auto input_event = DOM::Event::create(realm(), HTML::EventNames::input);
  232. input_event->set_bubbles(true);
  233. input_event->set_composed(true);
  234. dispatch_event(input_event);
  235. // 3. Fire an event named change at the element with the bubbles attribute initialized to true.
  236. auto change_event = DOM::Event::create(realm(), HTML::EventNames::change);
  237. change_event->set_bubbles(true);
  238. dispatch_event(*change_event);
  239. } else if (type_state() == TypeAttributeState::SubmitButton) {
  240. JS::GCPtr<HTMLFormElement> form;
  241. // 1. If the element does not have a form owner, then return.
  242. if (!(form = this->form()))
  243. return {};
  244. // 2. If the element's node document is not fully active, then return.
  245. if (!document().is_fully_active())
  246. return {};
  247. // 3. Submit the element's form owner from the element with userInvolvement set to event's user navigation involvement.
  248. TRY(form->submit_form(*this, { .user_involvement = user_navigation_involvement(event) }));
  249. } else if (type_state() == TypeAttributeState::FileUpload || type_state() == TypeAttributeState::Color) {
  250. show_the_picker_if_applicable(*this);
  251. }
  252. // https://html.spec.whatwg.org/multipage/input.html#image-button-state-(type=image):input-activation-behavior
  253. else if (type_state() == TypeAttributeState::ImageButton) {
  254. // 1. If the element does not have a form owner, then return.
  255. auto* form = this->form();
  256. if (!form)
  257. return {};
  258. // 2. If the element's node document is not fully active, then return.
  259. if (!document().is_fully_active())
  260. return {};
  261. // 3. If the user activated the control while explicitly selecting a coordinate, then set the element's selected
  262. // coordinate to that coordinate.
  263. if (event.is_trusted() && is<UIEvents::MouseEvent>(event)) {
  264. auto const& mouse_event = static_cast<UIEvents::MouseEvent const&>(event);
  265. m_selected_coordinate = { mouse_event.offset_x(), mouse_event.offset_y() };
  266. }
  267. // 4. Submit the element's form owner from the element with userInvolvement set to event's user navigation involvement.
  268. TRY(form->submit_form(*this, { .user_involvement = user_navigation_involvement(event) }));
  269. }
  270. return {};
  271. }
  272. void HTMLInputElement::did_edit_text_node(Badge<BrowsingContext>)
  273. {
  274. // 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.
  275. m_value = value_sanitization_algorithm(m_text_node->data());
  276. m_dirty_value = true;
  277. m_has_uncommitted_changes = true;
  278. update_placeholder_visibility();
  279. // NOTE: This is a bit ad-hoc, but basically implements part of "4.10.5.5 Common event behaviors"
  280. // https://html.spec.whatwg.org/multipage/input.html#common-input-element-events
  281. queue_an_element_task(HTML::Task::Source::UserInteraction, [this] {
  282. auto input_event = DOM::Event::create(realm(), HTML::EventNames::input);
  283. input_event->set_bubbles(true);
  284. input_event->set_composed(true);
  285. dispatch_event(*input_event);
  286. });
  287. }
  288. void HTMLInputElement::did_pick_color(Optional<Color> picked_color)
  289. {
  290. // https://html.spec.whatwg.org/multipage/input.html#common-input-element-events
  291. // For input elements without a defined input activation behavior, but to which these events apply
  292. // and for which the user interface involves both interactive manipulation and an explicit commit action
  293. if (type_state() == TypeAttributeState::Color && picked_color.has_value()) {
  294. // then when the user changes the element's value
  295. m_value = value_sanitization_algorithm(picked_color.value().to_string_without_alpha());
  296. m_dirty_value = true;
  297. if (m_color_well_element)
  298. update_color_well_element();
  299. // the user agent must queue an element task on the user interaction task source
  300. queue_an_element_task(HTML::Task::Source::UserInteraction, [this] {
  301. // given the input element to fire an event named input at the input element, with the bubbles and composed attributes initialized to true
  302. auto input_event = DOM::Event::create(realm(), HTML::EventNames::input);
  303. input_event->set_bubbles(true);
  304. input_event->set_composed(true);
  305. dispatch_event(*input_event);
  306. });
  307. // and any time the user commits the change, the user agent must queue an element task on the user interaction task source
  308. queue_an_element_task(HTML::Task::Source::UserInteraction, [this] {
  309. // given the input element
  310. // FIXME: to set its user interacted to true
  311. // and fire an event named change at the input element, with the bubbles attribute initialized to true.
  312. auto change_event = DOM::Event::create(realm(), HTML::EventNames::change);
  313. change_event->set_bubbles(true);
  314. dispatch_event(*change_event);
  315. });
  316. }
  317. }
  318. String HTMLInputElement::value() const
  319. {
  320. switch (value_attribute_mode()) {
  321. // https://html.spec.whatwg.org/multipage/input.html#dom-input-value-value
  322. case ValueAttributeMode::Value:
  323. // Return the current value of the element.
  324. return m_value;
  325. // https://html.spec.whatwg.org/multipage/input.html#dom-input-value-default
  326. case ValueAttributeMode::Default:
  327. // On getting, if the element has a value content attribute, return that attribute's value; otherwise, return
  328. // the empty string.
  329. return get_attribute_value(AttributeNames::value);
  330. // https://html.spec.whatwg.org/multipage/input.html#dom-input-value-default-on
  331. case ValueAttributeMode::DefaultOn:
  332. // On getting, if the element has a value content attribute, return that attribute's value; otherwise, return
  333. // the string "on".
  334. return get_attribute(AttributeNames::value).value_or("on"_string);
  335. // https://html.spec.whatwg.org/multipage/input.html#dom-input-value-filename
  336. case ValueAttributeMode::Filename:
  337. // On getting, return the string "C:\fakepath\" followed by the name of the first file in the list of selected
  338. // files, if any, or the empty string if the list is empty.
  339. if (m_selected_files && m_selected_files->item(0))
  340. return MUST(String::formatted("C:\\fakepath\\{}", m_selected_files->item(0)->name()));
  341. return String {};
  342. }
  343. VERIFY_NOT_REACHED();
  344. }
  345. WebIDL::ExceptionOr<void> HTMLInputElement::set_value(String const& value)
  346. {
  347. auto& realm = this->realm();
  348. switch (value_attribute_mode()) {
  349. // https://html.spec.whatwg.org/multipage/input.html#dom-input-value-value
  350. case ValueAttributeMode::Value: {
  351. // 1. Let oldValue be the element's value.
  352. auto old_value = move(m_value);
  353. // 2. Set the element's value to the new value.
  354. // NOTE: For the TextNode this is done as part of step 4 below.
  355. // 3. Set the element's dirty value flag to true.
  356. m_dirty_value = true;
  357. // 4. Invoke the value sanitization algorithm, if the element's type attribute's current state defines one.
  358. m_value = value_sanitization_algorithm(value);
  359. // 5. If the element's value (after applying the value sanitization algorithm) is different from oldValue,
  360. // and the element has a text entry cursor position, move the text entry cursor position to the end of the
  361. // text control, unselecting any selected text and resetting the selection direction to "none".
  362. if (m_value != old_value) {
  363. if (m_text_node) {
  364. m_text_node->set_data(m_value);
  365. update_placeholder_visibility();
  366. if (auto* browsing_context = document().browsing_context())
  367. browsing_context->set_cursor_position(DOM::Position::create(realm, *m_text_node, m_text_node->data().bytes().size()));
  368. }
  369. if (type_state() == TypeAttributeState::Color && m_color_well_element)
  370. update_color_well_element();
  371. if (type_state() == TypeAttributeState::Range && m_slider_thumb)
  372. update_slider_thumb_element();
  373. }
  374. break;
  375. }
  376. // https://html.spec.whatwg.org/multipage/input.html#dom-input-value-default
  377. // https://html.spec.whatwg.org/multipage/input.html#dom-input-value-default-on
  378. case ValueAttributeMode::Default:
  379. case ValueAttributeMode::DefaultOn:
  380. // On setting, set the value of the element's value content attribute to the new value.
  381. TRY(set_attribute(HTML::AttributeNames::value, value));
  382. break;
  383. // https://html.spec.whatwg.org/multipage/input.html#dom-input-value-filename
  384. case ValueAttributeMode::Filename:
  385. // On setting, if the new value is the empty string, empty the list of selected files; otherwise, throw an "InvalidStateError" DOMException.
  386. if (!value.is_empty())
  387. return WebIDL::InvalidStateError::create(realm, "Setting value of input type file to non-empty string"_fly_string);
  388. m_selected_files = nullptr;
  389. break;
  390. }
  391. return {};
  392. }
  393. void HTMLInputElement::commit_pending_changes()
  394. {
  395. // The change event fires when the value is committed, if that makes sense for the control,
  396. // or else when the control loses focus
  397. switch (type_state()) {
  398. case TypeAttributeState::Email:
  399. case TypeAttributeState::Password:
  400. case TypeAttributeState::Search:
  401. case TypeAttributeState::Telephone:
  402. case TypeAttributeState::Text:
  403. case TypeAttributeState::URL:
  404. if (!m_has_uncommitted_changes)
  405. return;
  406. break;
  407. default:
  408. break;
  409. }
  410. m_has_uncommitted_changes = false;
  411. auto change_event = DOM::Event::create(realm(), HTML::EventNames::change, { .bubbles = true });
  412. dispatch_event(change_event);
  413. }
  414. void HTMLInputElement::update_placeholder_visibility()
  415. {
  416. if (!m_placeholder_element)
  417. return;
  418. auto placeholder_text = this->placeholder_value();
  419. if (placeholder_text.has_value()) {
  420. MUST(m_placeholder_element->style_for_bindings()->set_property(CSS::PropertyID::Display, "block"sv));
  421. } else {
  422. MUST(m_placeholder_element->style_for_bindings()->set_property(CSS::PropertyID::Display, "none"sv));
  423. }
  424. }
  425. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:attr-input-readonly-3
  426. static bool is_allowed_to_be_readonly(HTML::HTMLInputElement::TypeAttributeState state)
  427. {
  428. switch (state) {
  429. case HTML::HTMLInputElement::TypeAttributeState::Text:
  430. case HTML::HTMLInputElement::TypeAttributeState::Search:
  431. case HTML::HTMLInputElement::TypeAttributeState::Telephone:
  432. case HTML::HTMLInputElement::TypeAttributeState::URL:
  433. case HTML::HTMLInputElement::TypeAttributeState::Email:
  434. case HTML::HTMLInputElement::TypeAttributeState::Password:
  435. case HTML::HTMLInputElement::TypeAttributeState::Date:
  436. case HTML::HTMLInputElement::TypeAttributeState::Month:
  437. case HTML::HTMLInputElement::TypeAttributeState::Week:
  438. case HTML::HTMLInputElement::TypeAttributeState::Time:
  439. case HTML::HTMLInputElement::TypeAttributeState::LocalDateAndTime:
  440. case HTML::HTMLInputElement::TypeAttributeState::Number:
  441. return true;
  442. default:
  443. return false;
  444. }
  445. }
  446. // https://html.spec.whatwg.org/multipage/input.html#attr-input-readonly
  447. void HTMLInputElement::handle_readonly_attribute(Optional<String> const& maybe_value)
  448. {
  449. // The readonly attribute is a boolean attribute that controls whether or not the user can edit the form control. When specified, the element is not mutable.
  450. m_is_mutable = !maybe_value.has_value() || !is_allowed_to_be_readonly(m_type);
  451. if (m_text_node)
  452. m_text_node->set_always_editable(m_is_mutable);
  453. }
  454. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:attr-input-placeholder-3
  455. static bool is_allowed_to_have_placeholder(HTML::HTMLInputElement::TypeAttributeState state)
  456. {
  457. switch (state) {
  458. case HTML::HTMLInputElement::TypeAttributeState::Text:
  459. case HTML::HTMLInputElement::TypeAttributeState::Search:
  460. case HTML::HTMLInputElement::TypeAttributeState::URL:
  461. case HTML::HTMLInputElement::TypeAttributeState::Telephone:
  462. case HTML::HTMLInputElement::TypeAttributeState::Email:
  463. case HTML::HTMLInputElement::TypeAttributeState::Password:
  464. case HTML::HTMLInputElement::TypeAttributeState::Number:
  465. return true;
  466. default:
  467. return false;
  468. }
  469. }
  470. // https://html.spec.whatwg.org/multipage/input.html#attr-input-placeholder
  471. String HTMLInputElement::placeholder() const
  472. {
  473. auto maybe_placeholder = get_attribute(HTML::AttributeNames::placeholder);
  474. if (!maybe_placeholder.has_value())
  475. return String {};
  476. auto placeholder = *maybe_placeholder;
  477. // The attribute, if specified, must have a value that contains no U+000A LINE FEED (LF) or U+000D CARRIAGE RETURN (CR) characters.
  478. StringBuilder builder;
  479. for (auto c : placeholder.bytes_as_string_view()) {
  480. if (c != '\r' && c != '\n')
  481. builder.append(c);
  482. }
  483. return MUST(builder.to_string());
  484. }
  485. // https://html.spec.whatwg.org/multipage/input.html#attr-input-placeholder
  486. Optional<ByteString> HTMLInputElement::placeholder_value() const
  487. {
  488. if (!m_text_node || !m_text_node->data().is_empty())
  489. return {};
  490. if (!is_allowed_to_have_placeholder(type_state()))
  491. return {};
  492. if (!has_attribute(HTML::AttributeNames::placeholder))
  493. return {};
  494. return placeholder().to_byte_string();
  495. }
  496. void HTMLInputElement::create_shadow_tree_if_needed()
  497. {
  498. if (shadow_root_internal())
  499. return;
  500. switch (type_state()) {
  501. case TypeAttributeState::Hidden:
  502. case TypeAttributeState::RadioButton:
  503. case TypeAttributeState::Checkbox:
  504. case TypeAttributeState::Button:
  505. case TypeAttributeState::SubmitButton:
  506. case TypeAttributeState::ResetButton:
  507. case TypeAttributeState::ImageButton:
  508. break;
  509. case TypeAttributeState::Color:
  510. create_color_input_shadow_tree();
  511. break;
  512. case TypeAttributeState::Range:
  513. create_range_input_shadow_tree();
  514. break;
  515. // FIXME: This could be better factored. Everything except the above types becomes a text input.
  516. default:
  517. create_text_input_shadow_tree();
  518. break;
  519. }
  520. }
  521. void HTMLInputElement::create_text_input_shadow_tree()
  522. {
  523. auto shadow_root = heap().allocate<DOM::ShadowRoot>(realm(), document(), *this, Bindings::ShadowRootMode::Closed);
  524. set_shadow_root(shadow_root);
  525. auto initial_value = m_value;
  526. auto element = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
  527. MUST(element->set_attribute(HTML::AttributeNames::style, R"~~~(
  528. display: flex;
  529. height: 100%;
  530. align-items: center;
  531. white-space: pre;
  532. border: none;
  533. padding: 1px 2px;
  534. )~~~"_string));
  535. MUST(shadow_root->append_child(element));
  536. m_placeholder_element = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
  537. m_placeholder_element->set_use_pseudo_element(CSS::Selector::PseudoElement::Type::Placeholder);
  538. MUST(m_placeholder_element->set_attribute(HTML::AttributeNames::style, R"~~~(
  539. flex: 1;
  540. height: 1lh;
  541. )~~~"_string));
  542. MUST(element->append_child(*m_placeholder_element));
  543. m_placeholder_text_node = heap().allocate<DOM::Text>(realm(), document(), String {});
  544. m_placeholder_text_node->set_data(placeholder());
  545. m_placeholder_text_node->set_editable_text_node_owner(Badge<HTMLInputElement> {}, *this);
  546. MUST(m_placeholder_element->append_child(*m_placeholder_text_node));
  547. m_inner_text_element = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
  548. MUST(m_inner_text_element->set_attribute(HTML::AttributeNames::style, R"~~~(
  549. flex: 1;
  550. height: 1lh;
  551. )~~~"_string));
  552. MUST(element->append_child(*m_inner_text_element));
  553. m_text_node = heap().allocate<DOM::Text>(realm(), document(), move(initial_value));
  554. if (type_state() == TypeAttributeState::FileUpload) {
  555. // NOTE: file upload state is mutable, but we don't allow the text node to be modifed
  556. m_text_node->set_always_editable(false);
  557. } else {
  558. handle_readonly_attribute(attribute(HTML::AttributeNames::readonly));
  559. }
  560. m_text_node->set_editable_text_node_owner(Badge<HTMLInputElement> {}, *this);
  561. if (type_state() == TypeAttributeState::Password)
  562. m_text_node->set_is_password_input({}, true);
  563. MUST(m_inner_text_element->append_child(*m_text_node));
  564. update_placeholder_visibility();
  565. if (type_state() == TypeAttributeState::Number) {
  566. // Up button
  567. auto up_button = MUST(DOM::create_element(document(), HTML::TagNames::button, Namespace::HTML));
  568. // FIXME: This cursor property doesn't work
  569. MUST(up_button->set_attribute(HTML::AttributeNames::style, R"~~~(
  570. padding: 0;
  571. cursor: default;
  572. )~~~"_string));
  573. MUST(up_button->set_inner_html("<svg style=\"width: 1em; height: 1em;\" xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><path fill=\"currentColor\" d=\"M7.41,15.41L12,10.83L16.59,15.41L18,14L12,8L6,14L7.41,15.41Z\" /></svg>"sv));
  574. MUST(element->append_child(up_button));
  575. auto up_callback_function = JS::NativeFunction::create(
  576. realm(), [this](JS::VM&) {
  577. MUST(step_up());
  578. return JS::js_undefined();
  579. },
  580. 0, "", &realm());
  581. auto up_callback = realm().heap().allocate_without_realm<WebIDL::CallbackType>(*up_callback_function, Bindings::host_defined_environment_settings_object(realm()));
  582. up_button->add_event_listener_without_options(UIEvents::EventNames::click, DOM::IDLEventListener::create(realm(), up_callback));
  583. // Down button
  584. auto down_button = MUST(DOM::create_element(document(), HTML::TagNames::button, Namespace::HTML));
  585. MUST(down_button->set_attribute(HTML::AttributeNames::style, R"~~~(
  586. padding: 0;
  587. cursor: default;
  588. )~~~"_string));
  589. MUST(down_button->set_inner_html("<svg style=\"width: 1em; height: 1em;\" xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><path fill=\"currentColor\" d=\"M7.41,8.58L12,13.17L16.59,8.58L18,10L12,16L6,10L7.41,8.58Z\" /></svg>"sv));
  590. MUST(element->append_child(down_button));
  591. auto down_callback_function = JS::NativeFunction::create(
  592. realm(), [this](JS::VM&) {
  593. MUST(step_down());
  594. return JS::js_undefined();
  595. },
  596. 0, "", &realm());
  597. auto down_callback = realm().heap().allocate_without_realm<WebIDL::CallbackType>(*down_callback_function, Bindings::host_defined_environment_settings_object(realm()));
  598. down_button->add_event_listener_without_options(UIEvents::EventNames::click, DOM::IDLEventListener::create(realm(), down_callback));
  599. }
  600. }
  601. void HTMLInputElement::create_color_input_shadow_tree()
  602. {
  603. auto shadow_root = heap().allocate<DOM::ShadowRoot>(realm(), document(), *this, Bindings::ShadowRootMode::Closed);
  604. auto color = value_sanitization_algorithm(m_value);
  605. auto border = DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
  606. MUST(border->set_attribute(HTML::AttributeNames::style, R"~~~(
  607. width: fit-content;
  608. height: fit-content;
  609. padding: 4px;
  610. border: 1px solid ButtonBorder;
  611. background-color: ButtonFace;
  612. )~~~"_string));
  613. m_color_well_element = DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
  614. MUST(m_color_well_element->set_attribute(HTML::AttributeNames::style, R"~~~(
  615. width: 24px;
  616. height: 24px;
  617. border: 1px solid ButtonBorder;
  618. box-sizing: border-box;
  619. )~~~"_string));
  620. MUST(m_color_well_element->style_for_bindings()->set_property(CSS::PropertyID::BackgroundColor, color));
  621. MUST(border->append_child(*m_color_well_element));
  622. MUST(shadow_root->append_child(border));
  623. set_shadow_root(shadow_root);
  624. }
  625. void HTMLInputElement::update_color_well_element()
  626. {
  627. MUST(m_color_well_element->style_for_bindings()->set_property(CSS::PropertyID::BackgroundColor, m_value));
  628. }
  629. void HTMLInputElement::create_range_input_shadow_tree()
  630. {
  631. auto shadow_root = heap().allocate<DOM::ShadowRoot>(realm(), document(), *this, Bindings::ShadowRootMode::Closed);
  632. set_shadow_root(shadow_root);
  633. auto slider_runnable_track = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
  634. slider_runnable_track->set_use_pseudo_element(CSS::Selector::PseudoElement::Type::SliderRunnableTrack);
  635. MUST(shadow_root->append_child(slider_runnable_track));
  636. m_slider_thumb = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
  637. m_slider_thumb->set_use_pseudo_element(CSS::Selector::PseudoElement::Type::SliderThumb);
  638. MUST(slider_runnable_track->append_child(*m_slider_thumb));
  639. update_slider_thumb_element();
  640. //  User event listeners
  641. auto dispatch_input_event = [this]() {
  642. queue_an_element_task(HTML::Task::Source::UserInteraction, [&] {
  643. auto input_event = DOM::Event::create(realm(), HTML::EventNames::input);
  644. input_event->set_bubbles(true);
  645. input_event->set_composed(true);
  646. dispatch_event(*input_event);
  647. });
  648. };
  649. auto keydown_callback_function = JS::NativeFunction::create(
  650. realm(), [this, dispatch_input_event](JS::VM& vm) {
  651. auto key = MUST(vm.argument(0).get(vm, "key")).as_string().utf8_string();
  652. if (key == "ArrowLeft" || key == "ArrowDown")
  653. MUST(step_down());
  654. if (key == "PageDown")
  655. MUST(step_down(10));
  656. if (key == "ArrowRight" || key == "ArrowUp")
  657. MUST(step_up());
  658. if (key == "PageUp")
  659. MUST(step_up(10));
  660. dispatch_input_event();
  661. return JS::js_undefined();
  662. },
  663. 0, "", &realm());
  664. auto keydown_callback = realm().heap().allocate_without_realm<WebIDL::CallbackType>(*keydown_callback_function, Bindings::host_defined_environment_settings_object(realm()));
  665. add_event_listener_without_options(UIEvents::EventNames::keydown, DOM::IDLEventListener::create(realm(), keydown_callback));
  666. auto wheel_callback_function = JS::NativeFunction::create(
  667. realm(), [this, dispatch_input_event](JS::VM& vm) {
  668. auto deltaY = MUST(vm.argument(0).get(vm, "deltaY")).as_i32();
  669. if (deltaY > 0) {
  670. MUST(step_down());
  671. } else {
  672. MUST(step_up());
  673. }
  674. dispatch_input_event();
  675. return JS::js_undefined();
  676. },
  677. 0, "", &realm());
  678. auto wheel_callback = realm().heap().allocate_without_realm<WebIDL::CallbackType>(*wheel_callback_function, Bindings::host_defined_environment_settings_object(realm()));
  679. add_event_listener_without_options(UIEvents::EventNames::wheel, DOM::IDLEventListener::create(realm(), wheel_callback));
  680. auto update_slider_by_mouse = [this, dispatch_input_event](JS::VM& vm) {
  681. auto client_x = MUST(vm.argument(0).get(vm, "clientX")).as_double();
  682. auto rect = get_bounding_client_rect();
  683. double minimum = *min();
  684. double maximum = *max();
  685. // FIXME: Snap new value to input steps
  686. MUST(set_value_as_number(clamp(round(((client_x - rect->left()) / rect->width()) * (maximum - minimum) + minimum), minimum, maximum)));
  687. dispatch_input_event();
  688. };
  689. auto mousedown_callback_function = JS::NativeFunction::create(
  690. realm(), [this, update_slider_by_mouse](JS::VM& vm) {
  691. update_slider_by_mouse(vm);
  692. auto mousemove_callback_function = JS::NativeFunction::create(
  693. realm(), [update_slider_by_mouse](JS::VM& vm) {
  694. update_slider_by_mouse(vm);
  695. return JS::js_undefined();
  696. },
  697. 0, "", &realm());
  698. auto mousemove_callback = realm().heap().allocate_without_realm<WebIDL::CallbackType>(*mousemove_callback_function, Bindings::host_defined_environment_settings_object(realm()));
  699. auto mousemove_listener = DOM::IDLEventListener::create(realm(), mousemove_callback);
  700. auto& window = static_cast<HTML::Window&>(relevant_global_object(*this));
  701. window.add_event_listener_without_options(UIEvents::EventNames::mousemove, mousemove_listener);
  702. auto mouseup_callback_function = JS::NativeFunction::create(
  703. realm(), [this, mousemove_listener](JS::VM&) {
  704. auto& window = static_cast<HTML::Window&>(relevant_global_object(*this));
  705. window.remove_event_listener_without_options(UIEvents::EventNames::mousemove, mousemove_listener);
  706. return JS::js_undefined();
  707. },
  708. 0, "", &realm());
  709. auto mouseup_callback = realm().heap().allocate_without_realm<WebIDL::CallbackType>(*mouseup_callback_function, Bindings::host_defined_environment_settings_object(realm()));
  710. DOM::AddEventListenerOptions mouseup_listener_options;
  711. mouseup_listener_options.once = true;
  712. window.add_event_listener(UIEvents::EventNames::mouseup, DOM::IDLEventListener::create(realm(), mouseup_callback), mouseup_listener_options);
  713. return JS::js_undefined();
  714. },
  715. 0, "", &realm());
  716. auto mousedown_callback = realm().heap().allocate_without_realm<WebIDL::CallbackType>(*mousedown_callback_function, Bindings::host_defined_environment_settings_object(realm()));
  717. add_event_listener_without_options(UIEvents::EventNames::mousedown, DOM::IDLEventListener::create(realm(), mousedown_callback));
  718. }
  719. void HTMLInputElement::update_slider_thumb_element()
  720. {
  721. double value = value_as_number();
  722. double minimum = *min();
  723. double maximum = *max();
  724. double position = (value - minimum) / (maximum - minimum) * 100;
  725. MUST(m_slider_thumb->style_for_bindings()->set_property(CSS::PropertyID::MarginLeft, MUST(String::formatted("{}%", position))));
  726. }
  727. void HTMLInputElement::did_receive_focus()
  728. {
  729. auto* browsing_context = document().browsing_context();
  730. if (!browsing_context)
  731. return;
  732. if (!m_text_node)
  733. return;
  734. browsing_context->set_cursor_position(DOM::Position::create(realm(), *m_text_node, 0));
  735. }
  736. void HTMLInputElement::did_lose_focus()
  737. {
  738. commit_pending_changes();
  739. }
  740. void HTMLInputElement::form_associated_element_attribute_changed(FlyString const& name, Optional<String> const& value)
  741. {
  742. if (name == HTML::AttributeNames::checked) {
  743. if (!value.has_value()) {
  744. // When the checked content attribute is removed, if the control does not have dirty checkedness,
  745. // the user agent must set the checkedness of the element to false.
  746. if (!m_dirty_checkedness)
  747. set_checked(false, ChangeSource::Programmatic);
  748. } else {
  749. // When the checked content attribute is added, if the control does not have dirty checkedness,
  750. // the user agent must set the checkedness of the element to true
  751. if (!m_dirty_checkedness)
  752. set_checked(true, ChangeSource::Programmatic);
  753. }
  754. } else if (name == HTML::AttributeNames::type) {
  755. m_type = parse_type_attribute(value.value_or(String {}));
  756. } else if (name == HTML::AttributeNames::value) {
  757. if (!m_dirty_value) {
  758. if (!value.has_value()) {
  759. m_value = String {};
  760. } else {
  761. m_value = value_sanitization_algorithm(*value);
  762. }
  763. update_placeholder_visibility();
  764. if (type_state() == TypeAttributeState::Color && m_color_well_element)
  765. update_color_well_element();
  766. if (type_state() == TypeAttributeState::Range && m_slider_thumb)
  767. update_slider_thumb_element();
  768. }
  769. } else if (name == HTML::AttributeNames::placeholder) {
  770. if (m_placeholder_text_node)
  771. m_placeholder_text_node->set_data(placeholder());
  772. } else if (name == HTML::AttributeNames::readonly) {
  773. handle_readonly_attribute(value);
  774. } else if (name == HTML::AttributeNames::src) {
  775. handle_src_attribute(value.value_or({})).release_value_but_fixme_should_propagate_errors();
  776. } else if (name == HTML::AttributeNames::alt) {
  777. if (layout_node() && type_state() == TypeAttributeState::ImageButton)
  778. did_update_alt_text(verify_cast<Layout::ImageBox>(*layout_node()));
  779. }
  780. }
  781. // https://html.spec.whatwg.org/multipage/input.html#attr-input-src
  782. WebIDL::ExceptionOr<void> HTMLInputElement::handle_src_attribute(StringView value)
  783. {
  784. auto& realm = this->realm();
  785. auto& vm = realm.vm();
  786. if (type_state() != TypeAttributeState::ImageButton)
  787. return {};
  788. // 1. Let url be the result of encoding-parsing a URL given the src attribute's value, relative to the element's
  789. // node document.
  790. auto url = document().parse_url(value);
  791. // 2. If url is failure, then return.
  792. if (!url.is_valid())
  793. return {};
  794. // 3. Let request be a new request whose URL is url, client is the element's node document's relevant settings
  795. // object, destination is "image", initiator type is "input", credentials mode is "include", and whose
  796. // use-URL-credentials flag is set.
  797. auto request = Fetch::Infrastructure::Request::create(vm);
  798. request->set_url(move(url));
  799. request->set_client(&document().relevant_settings_object());
  800. request->set_destination(Fetch::Infrastructure::Request::Destination::Image);
  801. request->set_initiator_type(Fetch::Infrastructure::Request::InitiatorType::Input);
  802. request->set_credentials_mode(Fetch::Infrastructure::Request::CredentialsMode::Include);
  803. request->set_use_url_credentials(true);
  804. // 4. Fetch request, with processResponseEndOfBody set to the following step given response response:
  805. m_image_request = SharedImageRequest::get_or_create(realm, document().page(), request->url());
  806. m_image_request->add_callbacks(
  807. [this, &realm]() {
  808. // 1. If the download was successful and the image is available, queue an element task on the user interaction
  809. // task source given the input element to fire an event named load at the input element.
  810. queue_an_element_task(HTML::Task::Source::UserInteraction, [this, &realm]() {
  811. dispatch_event(DOM::Event::create(realm, HTML::EventNames::load));
  812. });
  813. m_load_event_delayer.clear();
  814. document().invalidate_layout();
  815. },
  816. [this, &realm]() {
  817. // 2. Otherwise, if the fetching process fails without a response from the remote server, or completes but the
  818. // image is not a valid or supported image, then queue an element task on the user interaction task source
  819. // given the input element to fire an event named error on the input element.
  820. queue_an_element_task(HTML::Task::Source::UserInteraction, [this, &realm]() {
  821. dispatch_event(DOM::Event::create(realm, HTML::EventNames::error));
  822. });
  823. m_load_event_delayer.clear();
  824. });
  825. if (m_image_request->needs_fetching()) {
  826. m_image_request->fetch_image(realm, request);
  827. }
  828. // Fetching the image must delay the load event of the element's node document until the task that is queued by the
  829. // networking task source once the resource has been fetched (defined below) has been run.
  830. m_load_event_delayer.emplace(document());
  831. return {};
  832. }
  833. HTMLInputElement::TypeAttributeState HTMLInputElement::parse_type_attribute(StringView type)
  834. {
  835. #define __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(keyword, state) \
  836. if (type.equals_ignoring_ascii_case(#keyword##sv)) \
  837. return HTMLInputElement::TypeAttributeState::state;
  838. ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTES
  839. #undef __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE
  840. // The missing value default and the invalid value default are the Text state.
  841. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:missing-value-default
  842. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:invalid-value-default
  843. return HTMLInputElement::TypeAttributeState::Text;
  844. }
  845. StringView HTMLInputElement::type() const
  846. {
  847. // FIXME: This should probably be `Reflect` in the IDL.
  848. switch (m_type) {
  849. #define __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(keyword, state) \
  850. case TypeAttributeState::state: \
  851. return #keyword##sv;
  852. ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTES
  853. #undef __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE
  854. }
  855. VERIFY_NOT_REACHED();
  856. }
  857. WebIDL::ExceptionOr<void> HTMLInputElement::set_type(String const& type)
  858. {
  859. return set_attribute(HTML::AttributeNames::type, type);
  860. }
  861. // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-simple-colour
  862. static bool is_valid_simple_color(StringView value)
  863. {
  864. // if it is exactly seven characters long,
  865. if (value.length() != 7)
  866. return false;
  867. // and the first character is a U+0023 NUMBER SIGN character (#),
  868. if (!value.starts_with('#'))
  869. return false;
  870. // and the remaining six characters are all ASCII hex digits
  871. for (size_t i = 1; i < value.length(); i++)
  872. if (!is_ascii_hex_digit(value[i]))
  873. return false;
  874. return true;
  875. }
  876. // https://html.spec.whatwg.org/multipage/input.html#value-sanitization-algorithm
  877. String HTMLInputElement::value_sanitization_algorithm(String const& value) const
  878. {
  879. if (type_state() == HTMLInputElement::TypeAttributeState::Text || type_state() == HTMLInputElement::TypeAttributeState::Search || type_state() == HTMLInputElement::TypeAttributeState::Telephone || type_state() == HTMLInputElement::TypeAttributeState::Password) {
  880. // Strip newlines from the value.
  881. if (value.bytes_as_string_view().contains('\r') || value.bytes_as_string_view().contains('\n')) {
  882. StringBuilder builder;
  883. for (auto c : value.bytes_as_string_view()) {
  884. if (c != '\r' && c != '\n')
  885. builder.append(c);
  886. }
  887. return MUST(builder.to_string());
  888. }
  889. } else if (type_state() == HTMLInputElement::TypeAttributeState::URL) {
  890. // Strip newlines from the value, then strip leading and trailing ASCII whitespace from the value.
  891. if (value.bytes_as_string_view().contains('\r') || value.bytes_as_string_view().contains('\n')) {
  892. StringBuilder builder;
  893. for (auto c : value.bytes_as_string_view()) {
  894. if (c != '\r' && c != '\n')
  895. builder.append(c);
  896. }
  897. return MUST(String::from_utf8(builder.string_view().trim(Infra::ASCII_WHITESPACE)));
  898. }
  899. } else if (type_state() == HTMLInputElement::TypeAttributeState::Email) {
  900. // https://html.spec.whatwg.org/multipage/input.html#email-state-(type=email):value-sanitization-algorithm
  901. // FIXME: handle the `multiple` attribute
  902. // Strip newlines from the value, then strip leading and trailing ASCII whitespace from the value.
  903. if (value.bytes_as_string_view().contains('\r') || value.bytes_as_string_view().contains('\n')) {
  904. StringBuilder builder;
  905. for (auto c : value.bytes_as_string_view()) {
  906. if (c != '\r' && c != '\n')
  907. builder.append(c);
  908. }
  909. return MUST(String::from_utf8(builder.string_view().trim(Infra::ASCII_WHITESPACE)));
  910. }
  911. } else if (type_state() == HTMLInputElement::TypeAttributeState::Number) {
  912. // If the value of the element is not a valid floating-point number, then set it to the empty string instead.
  913. auto maybe_value = parse_floating_point_number(value);
  914. if (!maybe_value.has_value() || !isfinite(maybe_value.value()))
  915. return String {};
  916. } else if (type_state() == HTMLInputElement::TypeAttributeState::Date) {
  917. // https://html.spec.whatwg.org/multipage/input.html#date-state-(type=date):value-sanitization-algorithm
  918. if (!is_valid_date_string(value))
  919. return String {};
  920. } else if (type_state() == HTMLInputElement::TypeAttributeState::Month) {
  921. // https://html.spec.whatwg.org/multipage/input.html#month-state-(type=month):value-sanitization-algorithm
  922. if (!is_valid_month_string(value))
  923. return String {};
  924. } else if (type_state() == HTMLInputElement::TypeAttributeState::Week) {
  925. // https://html.spec.whatwg.org/multipage/input.html#week-state-(type=week):value-sanitization-algorithm
  926. if (!is_valid_week_string(value))
  927. return String {};
  928. } else if (type_state() == HTMLInputElement::TypeAttributeState::Time) {
  929. // https://html.spec.whatwg.org/multipage/input.html#time-state-(type=time):value-sanitization-algorithm
  930. if (!is_valid_time_string(value))
  931. return String {};
  932. } else if (type_state() == HTMLInputElement::TypeAttributeState::LocalDateAndTime) {
  933. // https://html.spec.whatwg.org/multipage/input.html#local-date-and-time-state-(type=datetime-local):value-sanitization-algorithm
  934. if (is_valid_local_date_and_time_string(value))
  935. return normalize_local_date_and_time_string(value);
  936. return String {};
  937. } else if (type_state() == HTMLInputElement::TypeAttributeState::Range) {
  938. // https://html.spec.whatwg.org/multipage/input.html#range-state-(type=range):value-sanitization-algorithm
  939. // If the value of the element is not a valid floating-point number, then set it to the best representation, as a floating-point number, of the default value.
  940. auto maybe_value = parse_floating_point_number(value);
  941. if (!maybe_value.has_value() || !isfinite(maybe_value.value())) {
  942. // The default value is the minimum plus half the difference between the minimum and the maximum, unless the maximum is less than the minimum, in which case the default value is the minimum.
  943. auto min = parse_floating_point_number(get_attribute(HTML::AttributeNames::min).value_or("0"_string)).value_or(0);
  944. auto max = parse_floating_point_number(get_attribute(HTML::AttributeNames::max).value_or("1"_string)).value_or(1);
  945. if (max > min)
  946. return JS::number_to_string(min);
  947. return JS::number_to_string(min + (max - min) / 2);
  948. }
  949. } else if (type_state() == HTMLInputElement::TypeAttributeState::Color) {
  950. // https://html.spec.whatwg.org/multipage/input.html#color-state-(type=color):value-sanitization-algorithm
  951. // If the value of the element is a valid simple color, then set it to the value of the element converted to ASCII lowercase;
  952. if (is_valid_simple_color(value))
  953. return MUST(Infra::to_ascii_lowercase(value));
  954. // otherwise, set it to the string "#000000".
  955. return "#000000"_string;
  956. }
  957. return value;
  958. }
  959. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:concept-form-reset-control
  960. void HTMLInputElement::reset_algorithm()
  961. {
  962. // The reset algorithm for input elements is to set the dirty value flag and dirty checkedness flag back to false,
  963. m_dirty_value = false;
  964. m_dirty_checkedness = false;
  965. // set the value of the element to the value of the value content attribute, if there is one, or the empty string otherwise,
  966. m_value = get_attribute_value(AttributeNames::value);
  967. // set the checkedness of the element to true if the element has a checked content attribute and false if it does not,
  968. m_checked = has_attribute(AttributeNames::checked);
  969. // empty the list of selected files,
  970. m_selected_files = FileAPI::FileList::create(realm(), {});
  971. // and then invoke the value sanitization algorithm, if the type attribute's current state defines one.
  972. m_value = value_sanitization_algorithm(m_value);
  973. if (m_text_node) {
  974. m_text_node->set_data(m_value);
  975. update_placeholder_visibility();
  976. }
  977. if (type_state() == TypeAttributeState::Color && m_color_well_element)
  978. update_color_well_element();
  979. if (type_state() == TypeAttributeState::Range && m_slider_thumb)
  980. update_slider_thumb_element();
  981. }
  982. void HTMLInputElement::form_associated_element_was_inserted()
  983. {
  984. create_shadow_tree_if_needed();
  985. }
  986. void HTMLInputElement::form_associated_element_was_removed(DOM::Node*)
  987. {
  988. set_shadow_root(nullptr);
  989. }
  990. // https://html.spec.whatwg.org/multipage/input.html#radio-button-group
  991. static bool is_in_same_radio_button_group(HTML::HTMLInputElement const& a, HTML::HTMLInputElement const& b)
  992. {
  993. auto non_empty_equals = [](auto const& value_a, auto const& value_b) {
  994. return !value_a.is_empty() && value_a == value_b;
  995. };
  996. // The radio button group that contains an input element a also contains all the
  997. // other input elements b that fulfill all of the following conditions:
  998. return (
  999. // - Both a and b are in the same tree.
  1000. // - The input element b's type attribute is in the Radio Button state.
  1001. a.type_state() == b.type_state()
  1002. && b.type_state() == HTMLInputElement::TypeAttributeState::RadioButton
  1003. // - Either a and b have the same form owner, or they both have no form owner.
  1004. && a.form() == b.form()
  1005. // - They both have a name attribute, their name attributes are not empty, and the
  1006. // value of a's name attribute equals the value of b's name attribute.
  1007. && a.name().has_value()
  1008. && b.name().has_value()
  1009. && non_empty_equals(a.name().value(), b.name().value()));
  1010. }
  1011. // https://html.spec.whatwg.org/multipage/input.html#radio-button-state-(type=radio)
  1012. void HTMLInputElement::set_checked_within_group()
  1013. {
  1014. if (checked())
  1015. return;
  1016. set_checked(true, ChangeSource::User);
  1017. // No point iterating the tree if we have an empty name.
  1018. if (!name().has_value() || name()->is_empty())
  1019. return;
  1020. document().for_each_in_inclusive_subtree_of_type<HTML::HTMLInputElement>([&](auto& element) {
  1021. if (element.checked() && &element != this && is_in_same_radio_button_group(*this, element))
  1022. element.set_checked(false, ChangeSource::User);
  1023. return IterationDecision::Continue;
  1024. });
  1025. }
  1026. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:legacy-pre-activation-behavior
  1027. void HTMLInputElement::legacy_pre_activation_behavior()
  1028. {
  1029. m_before_legacy_pre_activation_behavior_checked = checked();
  1030. m_before_legacy_pre_activation_behavior_indeterminate = indeterminate();
  1031. // 1. If this element's type attribute is in the Checkbox state, then set
  1032. // this element's checkedness to its opposite value (i.e. true if it is
  1033. // false, false if it is true) and set this element's indeterminate IDL
  1034. // attribute to false.
  1035. if (type_state() == TypeAttributeState::Checkbox) {
  1036. set_checked(!checked(), ChangeSource::User);
  1037. set_indeterminate(false);
  1038. }
  1039. // 2. If this element's type attribute is in the Radio Button state, then
  1040. // get a reference to the element in this element's radio button group that
  1041. // has its checkedness set to true, if any, and then set this element's
  1042. // checkedness to true.
  1043. if (type_state() == TypeAttributeState::RadioButton) {
  1044. document().for_each_in_inclusive_subtree_of_type<HTML::HTMLInputElement>([&](auto& element) {
  1045. if (element.checked() && is_in_same_radio_button_group(*this, element)) {
  1046. m_legacy_pre_activation_behavior_checked_element_in_group = &element;
  1047. return IterationDecision::Break;
  1048. }
  1049. return IterationDecision::Continue;
  1050. });
  1051. set_checked_within_group();
  1052. }
  1053. }
  1054. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:legacy-canceled-activation-behavior
  1055. void HTMLInputElement::legacy_cancelled_activation_behavior()
  1056. {
  1057. // 1. If the element's type attribute is in the Checkbox state, then set the
  1058. // element's checkedness and the element's indeterminate IDL attribute back
  1059. // to the values they had before the legacy-pre-activation behavior was run.
  1060. if (type_state() == TypeAttributeState::Checkbox) {
  1061. set_checked(m_before_legacy_pre_activation_behavior_checked, ChangeSource::Programmatic);
  1062. set_indeterminate(m_before_legacy_pre_activation_behavior_indeterminate);
  1063. }
  1064. // 2. If this element 's type attribute is in the Radio Button state, then
  1065. // if the element to which a reference was obtained in the
  1066. // legacy-pre-activation behavior, if any, is still in what is now this
  1067. // element' s radio button group, if it still has one, and if so, setting
  1068. // that element 's checkedness to true; or else, if there was no such
  1069. // element, or that element is no longer in this element' s radio button
  1070. // group, or if this element no longer has a radio button group, setting
  1071. // this element's checkedness to false.
  1072. if (type_state() == TypeAttributeState::RadioButton) {
  1073. bool did_reselect_previous_element = false;
  1074. if (m_legacy_pre_activation_behavior_checked_element_in_group) {
  1075. auto& element_in_group = *m_legacy_pre_activation_behavior_checked_element_in_group;
  1076. if (is_in_same_radio_button_group(*this, element_in_group)) {
  1077. element_in_group.set_checked_within_group();
  1078. did_reselect_previous_element = true;
  1079. }
  1080. m_legacy_pre_activation_behavior_checked_element_in_group = nullptr;
  1081. }
  1082. if (!did_reselect_previous_element)
  1083. set_checked(false, ChangeSource::User);
  1084. }
  1085. }
  1086. void HTMLInputElement::legacy_cancelled_activation_behavior_was_not_called()
  1087. {
  1088. m_legacy_pre_activation_behavior_checked_element_in_group = nullptr;
  1089. }
  1090. JS::GCPtr<DecodedImageData> HTMLInputElement::image_data() const
  1091. {
  1092. if (m_image_request)
  1093. return m_image_request->image_data();
  1094. return nullptr;
  1095. }
  1096. bool HTMLInputElement::is_image_available() const
  1097. {
  1098. return image_data() != nullptr;
  1099. }
  1100. Optional<CSSPixels> HTMLInputElement::intrinsic_width() const
  1101. {
  1102. if (auto image_data = this->image_data())
  1103. return image_data->intrinsic_width();
  1104. return {};
  1105. }
  1106. Optional<CSSPixels> HTMLInputElement::intrinsic_height() const
  1107. {
  1108. if (auto image_data = this->image_data())
  1109. return image_data->intrinsic_height();
  1110. return {};
  1111. }
  1112. Optional<CSSPixelFraction> HTMLInputElement::intrinsic_aspect_ratio() const
  1113. {
  1114. if (auto image_data = this->image_data())
  1115. return image_data->intrinsic_aspect_ratio();
  1116. return {};
  1117. }
  1118. RefPtr<Gfx::ImmutableBitmap> HTMLInputElement::current_image_bitmap(Gfx::IntSize size) const
  1119. {
  1120. if (auto image_data = this->image_data())
  1121. return image_data->bitmap(0, size);
  1122. return nullptr;
  1123. }
  1124. void HTMLInputElement::set_visible_in_viewport(bool)
  1125. {
  1126. // FIXME: Loosen grip on image data when it's not visible, e.g via volatile memory.
  1127. }
  1128. // https://html.spec.whatwg.org/multipage/interaction.html#dom-tabindex
  1129. i32 HTMLInputElement::default_tab_index_value() const
  1130. {
  1131. // See the base function for the spec comments.
  1132. return 0;
  1133. }
  1134. // https://html.spec.whatwg.org/multipage/input.html#the-size-attribute
  1135. unsigned HTMLInputElement::size() const
  1136. {
  1137. // The size IDL attribute is limited to only positive numbers and has a default value of 20.
  1138. if (auto size_string = get_attribute(HTML::AttributeNames::size); size_string.has_value()) {
  1139. if (auto size = parse_non_negative_integer(*size_string); size.has_value())
  1140. return *size;
  1141. }
  1142. return 20;
  1143. }
  1144. WebIDL::ExceptionOr<void> HTMLInputElement::set_size(unsigned value)
  1145. {
  1146. return set_attribute(HTML::AttributeNames::size, MUST(String::number(value)));
  1147. }
  1148. // https://html.spec.whatwg.org/multipage/input.html#concept-input-value-string-number
  1149. Optional<double> HTMLInputElement::convert_string_to_number(StringView input) const
  1150. {
  1151. // https://html.spec.whatwg.org/multipage/input.html#number-state-(type=number):concept-input-value-string-number
  1152. if (type_state() == TypeAttributeState::Number)
  1153. return parse_floating_point_number(input);
  1154. // https://html.spec.whatwg.org/multipage/input.html#range-state-(type=range):concept-input-value-string-number
  1155. if (type_state() == TypeAttributeState::Range)
  1156. return parse_floating_point_number(input);
  1157. dbgln("HTMLInputElement::convert_string_to_number() not implemented for input type {}", type());
  1158. return {};
  1159. }
  1160. // https://html.spec.whatwg.org/multipage/input.html#concept-input-value-string-number
  1161. String HTMLInputElement::covert_number_to_string(double input) const
  1162. {
  1163. // https://html.spec.whatwg.org/multipage/input.html#number-state-(type=number):concept-input-value-number-string
  1164. if (type_state() == TypeAttributeState::Number)
  1165. return MUST(String::number(input));
  1166. // https://html.spec.whatwg.org/multipage/input.html#range-state-(type=range):concept-input-value-number-string
  1167. if (type_state() == TypeAttributeState::Range)
  1168. return MUST(String::number(input));
  1169. dbgln("HTMLInputElement::covert_number_to_string() not implemented for input type {}", type());
  1170. return {};
  1171. }
  1172. // https://html.spec.whatwg.org/multipage/input.html#concept-input-value-string-date
  1173. WebIDL::ExceptionOr<JS::GCPtr<JS::Date>> HTMLInputElement::convert_string_to_date(StringView input) const
  1174. {
  1175. // https://html.spec.whatwg.org/multipage/input.html#date-state-(type=date):concept-input-value-string-date
  1176. if (type_state() == TypeAttributeState::Date) {
  1177. // If parsing a date from input results in an error, then return an error;
  1178. auto maybe_date = parse_date_string(realm(), input);
  1179. if (maybe_date.is_exception())
  1180. return maybe_date.exception();
  1181. // otherwise, return a new Date object representing midnight UTC on the morning of the parsed date.
  1182. return maybe_date.value();
  1183. }
  1184. // https://html.spec.whatwg.org/multipage/input.html#time-state-(type=time):concept-input-value-string-date
  1185. if (type_state() == TypeAttributeState::Time) {
  1186. // If parsing a time from input results in an error, then return an error;
  1187. auto maybe_time = parse_time_string(realm(), input);
  1188. if (maybe_time.is_exception())
  1189. return maybe_time.exception();
  1190. // otherwise, return a new Date object representing the parsed time in UTC on 1970-01-01.
  1191. return maybe_time.value();
  1192. }
  1193. dbgln("HTMLInputElement::convert_string_to_date() not implemented for input type {}", type());
  1194. return nullptr;
  1195. }
  1196. // https://html.spec.whatwg.org/multipage/input.html#concept-input-value-date-string
  1197. String HTMLInputElement::covert_date_to_string(JS::NonnullGCPtr<JS::Date> input) const
  1198. {
  1199. // https://html.spec.whatwg.org/multipage/input.html#date-state-(type=date):concept-input-value-date-string
  1200. if (type_state() == TypeAttributeState::Date) {
  1201. // Return a valid date string that represents the date current at the time represented by input in the UTC time zone.
  1202. // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-date-string
  1203. return MUST(String::formatted("{:04d}-{:02d}-{:02d}", JS::year_from_time(input->date_value()), JS::month_from_time(input->date_value()) + 1, JS::date_from_time(input->date_value())));
  1204. }
  1205. // https://html.spec.whatwg.org/multipage/input.html#time-state-(type=time):concept-input-value-string-date
  1206. if (type_state() == TypeAttributeState::Time) {
  1207. // Return a valid time string that represents the UTC time component that is represented by input.
  1208. // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-time-string
  1209. auto seconds = JS::sec_from_time(input->date_value());
  1210. auto milliseconds = JS::ms_from_time(input->date_value());
  1211. if (seconds > 0) {
  1212. if (milliseconds > 0)
  1213. return MUST(String::formatted("{:02d}:{:02d}:{:02d}.{:3d}", JS::hour_from_time(input->date_value()), JS::min_from_time(input->date_value()), seconds, milliseconds));
  1214. return MUST(String::formatted("{:02d}:{:02d}:{:02d}", JS::hour_from_time(input->date_value()), JS::min_from_time(input->date_value()), seconds));
  1215. }
  1216. return MUST(String::formatted("{:02d}:{:02d}", JS::hour_from_time(input->date_value()), JS::min_from_time(input->date_value())));
  1217. }
  1218. dbgln("HTMLInputElement::covert_date_to_string() not implemented for input type {}", type());
  1219. return {};
  1220. }
  1221. // https://html.spec.whatwg.org/multipage/input.html#attr-input-min
  1222. Optional<double> HTMLInputElement::min() const
  1223. {
  1224. // If the element has a min attribute, and the result of applying the algorithm to convert a string to a number to
  1225. // the value of the min attribute is a number, then that number is the element's minimum; otherwise, if the type
  1226. // attribute's current state defines a default minimum, then that is the minimum; otherwise, the element has no minimum.
  1227. if (auto min_string = get_attribute(HTML::AttributeNames::min); min_string.has_value()) {
  1228. if (auto min = convert_string_to_number(*min_string); min.has_value())
  1229. return *min;
  1230. }
  1231. // https://html.spec.whatwg.org/multipage/input.html#range-state-(type=range):concept-input-min-default
  1232. if (type_state() == TypeAttributeState::Range)
  1233. return 0;
  1234. return {};
  1235. }
  1236. // https://html.spec.whatwg.org/multipage/input.html#attr-input-max
  1237. Optional<double> HTMLInputElement::max() const
  1238. {
  1239. // If the element has a max attribute, and the result of applying the algorithm to convert a string to a number to the
  1240. // value of the max attribute is a number, then that number is the element's maximum; otherwise, if the type attribute's
  1241. // current state defines a default maximum, then that is the maximum; otherwise, the element has no maximum.
  1242. if (auto max_string = get_attribute(HTML::AttributeNames::max); max_string.has_value()) {
  1243. if (auto max = convert_string_to_number(*max_string); max.has_value())
  1244. return *max;
  1245. }
  1246. // https://html.spec.whatwg.org/multipage/input.html#range-state-(type=range):concept-input-max-default
  1247. if (type_state() == TypeAttributeState::Range)
  1248. return 100;
  1249. return {};
  1250. }
  1251. // https://html.spec.whatwg.org/multipage/input.html#concept-input-step-default
  1252. double HTMLInputElement::default_step() const
  1253. {
  1254. // https://html.spec.whatwg.org/multipage/input.html#number-state-(type=number):concept-input-step-default
  1255. if (type_state() == TypeAttributeState::Number)
  1256. return 1;
  1257. // https://html.spec.whatwg.org/multipage/input.html#range-state-(type=range):concept-input-step-default
  1258. if (type_state() == TypeAttributeState::Range)
  1259. return 1;
  1260. dbgln("HTMLInputElement::default_step() not implemented for input type {}", type());
  1261. return 0;
  1262. }
  1263. // https://html.spec.whatwg.org/multipage/input.html#concept-input-step-scale
  1264. double HTMLInputElement::step_scale_factor() const
  1265. {
  1266. // https://html.spec.whatwg.org/multipage/input.html#number-state-(type=number):concept-input-step-scale
  1267. if (type_state() == TypeAttributeState::Number)
  1268. return 1;
  1269. // https://html.spec.whatwg.org/multipage/input.html#range-state-(type=range):concept-input-step-scale
  1270. if (type_state() == TypeAttributeState::Range)
  1271. return 1;
  1272. dbgln("HTMLInputElement::step_scale_factor() not implemented for input type {}", type());
  1273. return 0;
  1274. }
  1275. // https://html.spec.whatwg.org/multipage/input.html#concept-input-step
  1276. Optional<double> HTMLInputElement::allowed_value_step() const
  1277. {
  1278. // 1. If the attribute does not apply, then there is no allowed value step.
  1279. if (!step_applies())
  1280. return {};
  1281. // 2. Otherwise, if the attribute is absent, then the allowed value step is the default step multiplied by the step scale factor.
  1282. auto maybe_step_string = get_attribute(AttributeNames::step);
  1283. if (!maybe_step_string.has_value())
  1284. return default_step() * step_scale_factor();
  1285. auto step_string = *maybe_step_string;
  1286. // 3. Otherwise, if the attribute's value is an ASCII case-insensitive match for the string "any", then there is no allowed value step.
  1287. if (Infra::is_ascii_case_insensitive_match(step_string, "any"_string))
  1288. return {};
  1289. // 4. Otherwise, if the rules for parsing floating-point number values, when they are applied to the attribute's value, return an error,
  1290. // zero, or a number less than zero, then the allowed value step is the default step multiplied by the step scale factor.
  1291. auto maybe_step = parse_floating_point_number(step_string);
  1292. if (!maybe_step.has_value() || *maybe_step == 0 || *maybe_step < 0)
  1293. return default_step() * step_scale_factor();
  1294. // 5. Otherwise, the allowed value step is the number returned by the rules for parsing floating-point number values when they are applied
  1295. // to the attribute's value, multiplied by the step scale factor.
  1296. return *maybe_step * step_scale_factor();
  1297. }
  1298. // https://html.spec.whatwg.org/multipage/input.html#concept-input-min-zero
  1299. double HTMLInputElement::step_base() const
  1300. {
  1301. // 1. If the element has a min content attribute, and the result of applying the algorithm to convert a string to a number to the value of
  1302. // the min content attribute is not an error, then return that result.
  1303. if (auto min = this->min(); min.has_value())
  1304. return *min;
  1305. // 2. If the element has a value content attribute, and the result of applying the algorithm to convert a string to a number to the value of
  1306. // the value content attribute is not an error, then return that result.
  1307. if (auto value = convert_string_to_number(this->value()); value.has_value())
  1308. return *value;
  1309. // 3. If a default step base is defined for this element given its type attribute's state, then return it.
  1310. if (type_state() == TypeAttributeState::Week) {
  1311. // The default step base is −259,200,000 (the start of week 1970-W01).
  1312. return -259'200'000;
  1313. }
  1314. // 4. Return zero.
  1315. return 0;
  1316. }
  1317. // https://html.spec.whatwg.org/multipage/input.html#dom-input-valueasdate
  1318. JS::Object* HTMLInputElement::value_as_date() const
  1319. {
  1320. // On getting, if the valueAsDate attribute does not apply, as defined for the input element's type attribute's current state, then return null.
  1321. if (!value_as_date_applies())
  1322. return nullptr;
  1323. // Otherwise, run the algorithm to convert a string to a Date object defined for that state to the element's value;
  1324. // if the algorithm returned a Date object, then return it, otherwise, return null.
  1325. auto maybe_date = convert_string_to_date(value());
  1326. if (!maybe_date.is_exception())
  1327. return maybe_date.value().ptr();
  1328. return nullptr;
  1329. }
  1330. // https://html.spec.whatwg.org/multipage/input.html#dom-input-valueasdate
  1331. WebIDL::ExceptionOr<void> HTMLInputElement::set_value_as_date(Optional<JS::Handle<JS::Object>> const& value)
  1332. {
  1333. // On setting, if the valueAsDate attribute does not apply, as defined for the input element's type attribute's current state, then throw an "InvalidStateError" DOMException;
  1334. if (!value_as_date_applies())
  1335. return WebIDL::InvalidStateError::create(realm(), "valueAsDate: Invalid input type used"_fly_string);
  1336. // otherwise, if the new value is not null and not a Date object throw a TypeError exception;
  1337. if (value.has_value() && !is<JS::Date>(**value))
  1338. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "valueAsDate: input is not a Date"sv };
  1339. // otherwise if the new value is null or a Date object representing the NaN time value, then set the value of the element to the empty string;
  1340. if (!value.has_value()) {
  1341. TRY(set_value(String {}));
  1342. return {};
  1343. }
  1344. auto& date = static_cast<JS::Date&>(**value);
  1345. if (!isfinite(date.date_value())) {
  1346. TRY(set_value(String {}));
  1347. return {};
  1348. }
  1349. // otherwise, run the algorithm to convert a Date object to a string, as defined for that state, on the new value, and set the value of the element to the resulting string.
  1350. TRY(set_value(covert_date_to_string(date)));
  1351. return {};
  1352. }
  1353. // https://html.spec.whatwg.org/multipage/input.html#dom-input-valueasnumber
  1354. double HTMLInputElement::value_as_number() const
  1355. {
  1356. // On getting, if the valueAsNumber attribute does not apply, as defined for the input element's type attribute's current state, then return a Not-a-Number (NaN) value.
  1357. if (!value_as_number_applies())
  1358. return NAN;
  1359. // Otherwise, run the algorithm to convert a string to a number defined for that state to the element's value;
  1360. // if the algorithm returned a number, then return it, otherwise, return a Not-a-Number (NaN) value.
  1361. return convert_string_to_number(value()).value_or(NAN);
  1362. }
  1363. // https://html.spec.whatwg.org/multipage/input.html#dom-input-valueasnumber
  1364. WebIDL::ExceptionOr<void> HTMLInputElement::set_value_as_number(double value)
  1365. {
  1366. // On setting, if the new value is infinite, then throw a TypeError exception.
  1367. if (!isfinite(value))
  1368. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "valueAsNumber: Value is infinite"sv };
  1369. // Otherwise, if the valueAsNumber attribute does not apply, as defined for the input element's type attribute's current state, then throw an "InvalidStateError" DOMException.
  1370. if (!value_as_number_applies())
  1371. return WebIDL::InvalidStateError::create(realm(), "valueAsNumber: Invalid input type used"_fly_string);
  1372. // Otherwise, if the new value is a Not-a-Number (NaN) value, then set the value of the element to the empty string.
  1373. if (value == NAN) {
  1374. TRY(set_value(String {}));
  1375. return {};
  1376. }
  1377. // Otherwise, run the algorithm to convert a number to a string, as defined for that state, on the new value, and set the value of the element to the resulting string.
  1378. TRY(set_value(covert_number_to_string(value)));
  1379. return {};
  1380. }
  1381. // https://html.spec.whatwg.org/multipage/input.html#dom-input-stepup
  1382. WebIDL::ExceptionOr<void> HTMLInputElement::step_up(long n)
  1383. {
  1384. return step_up_or_down(false, n);
  1385. }
  1386. // https://html.spec.whatwg.org/multipage/input.html#dom-input-stepdown
  1387. WebIDL::ExceptionOr<void> HTMLInputElement::step_down(long n)
  1388. {
  1389. return step_up_or_down(true, n);
  1390. }
  1391. // https://html.spec.whatwg.org/multipage/input.html#dom-input-stepup
  1392. WebIDL::ExceptionOr<void> HTMLInputElement::step_up_or_down(bool is_down, long n)
  1393. {
  1394. // 1. If the stepDown() and stepUp() methods do not apply, as defined for the input element's type attribute's current state, then throw an "InvalidStateError" DOMException.
  1395. if (!step_up_or_down_applies())
  1396. return WebIDL::InvalidStateError::create(realm(), MUST(String::formatted("{}: Invalid input type used", is_down ? "stepDown()" : "stepUp()")));
  1397. // 2. If the element has no allowed value step, then throw an "InvalidStateError" DOMException.
  1398. auto maybe_allowed_value_step = allowed_value_step();
  1399. if (!maybe_allowed_value_step.has_value())
  1400. return WebIDL::InvalidStateError::create(realm(), "element has no allowed value step"_fly_string);
  1401. double allowed_value_step = *maybe_allowed_value_step;
  1402. // 3. If the element has a minimum and a maximum and the minimum is greater than the maximum, then return.
  1403. auto maybe_minimum = min();
  1404. auto maybe_maximum = max();
  1405. if (maybe_minimum.has_value() && maybe_maximum.has_value() && *maybe_minimum > *maybe_maximum)
  1406. return {};
  1407. // FIXME: 4. If the element has a minimum and a maximum and there is no value greater than or equal to the element's minimum and less than
  1408. // or equal to the element's maximum that, when subtracted from the step base, is an integral multiple of the allowed value step, then return.
  1409. // 5. If applying the algorithm to convert a string to a number to the string given by the element's value does not result in an error,
  1410. // then let value be the result of that algorithm. Otherwise, let value be zero.
  1411. double value = convert_string_to_number(this->value()).value_or(0);
  1412. // 6. Let valueBeforeStepping be value.
  1413. double value_before_stepping = value;
  1414. // 7. If value subtracted from the step base is not an integral multiple of the allowed value step, then set value to the nearest value that,
  1415. // when subtracted from the step base, is an integral multiple of the allowed value step, and that is less than value if the method invoked was the stepDown() method, and more than value otherwise.
  1416. if (fmod(step_base() - value, allowed_value_step) != 0) {
  1417. double diff = step_base() - value;
  1418. if (is_down) {
  1419. value = diff - fmod(diff, allowed_value_step);
  1420. } else {
  1421. value = diff + fmod(diff, allowed_value_step);
  1422. }
  1423. } else {
  1424. // 1. Let n be the argument.
  1425. // 2. Let delta be the allowed value step multiplied by n.
  1426. double delta = allowed_value_step * n;
  1427. // 3. If the method invoked was the stepDown() method, negate delta.
  1428. if (is_down)
  1429. delta = -delta;
  1430. // 4. Let value be the result of adding delta to value.
  1431. value += delta;
  1432. }
  1433. // 8. If the element has a minimum, and value is less than that minimum, then set value to the smallest value that,
  1434. // when subtracted from the step base, is an integral multiple of the allowed value step, and that is more than or equal to minimum.
  1435. if (maybe_minimum.has_value() && value < *maybe_minimum) {
  1436. value = AK::max(value, *maybe_minimum);
  1437. }
  1438. // 9. If the element has a maximum, and value is greater than that maximum, then set value to the largest value that,
  1439. // when subtracted from the step base, is an integral multiple of the allowed value step, and that is less than or equal to maximum.
  1440. if (maybe_maximum.has_value() && value > *maybe_maximum) {
  1441. value = AK::min(value, *maybe_maximum);
  1442. }
  1443. // 10. If either the method invoked was the stepDown() method and value is greater than valueBeforeStepping,
  1444. // or the method invoked was the stepUp() method and value is less than valueBeforeStepping, then return.
  1445. if (is_down) {
  1446. if (value > value_before_stepping)
  1447. return {};
  1448. } else {
  1449. if (value < value_before_stepping)
  1450. return {};
  1451. }
  1452. // 11. Let value as string be the result of running the algorithm to convert a number to a string,
  1453. // as defined for the input element's type attribute's current state, on value.
  1454. auto value_as_string = covert_number_to_string(value);
  1455. // 12. Set the value of the element to value as string.
  1456. TRY(set_value(value_as_string));
  1457. return {};
  1458. }
  1459. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-checkvalidity
  1460. WebIDL::ExceptionOr<bool> HTMLInputElement::check_validity()
  1461. {
  1462. dbgln("(STUBBED) HTMLInputElement::check_validity(). Called on: {}", debug_description());
  1463. return true;
  1464. }
  1465. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-reportvalidity
  1466. WebIDL::ExceptionOr<bool> HTMLInputElement::report_validity()
  1467. {
  1468. dbgln("(STUBBED) HTMLInputElement::report_validity(). Called on: {}", debug_description());
  1469. return true;
  1470. }
  1471. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-setcustomvalidity
  1472. void HTMLInputElement::set_custom_validity(String const& error)
  1473. {
  1474. dbgln("(STUBBED) HTMLInputElement::set_custom_validity(error={}). Called on: {}", error, debug_description());
  1475. return;
  1476. }
  1477. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-select
  1478. WebIDL::ExceptionOr<void> HTMLInputElement::select()
  1479. {
  1480. dbgln("(STUBBED) HTMLInputElement::select(). Called on: {}", debug_description());
  1481. return {};
  1482. }
  1483. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-textarea/input-setselectionrange
  1484. WebIDL::ExceptionOr<void> HTMLInputElement::set_selection_range(u32 start, u32 end, Optional<String> const& direction)
  1485. {
  1486. dbgln("(STUBBED) HTMLInputElement::set_selection_range(start={}, end={}, direction='{}'). Called on: {}", start, end, direction, debug_description());
  1487. return {};
  1488. }
  1489. Optional<ARIA::Role> HTMLInputElement::default_role() const
  1490. {
  1491. // https://www.w3.org/TR/html-aria/#el-input-button
  1492. if (type_state() == TypeAttributeState::Button)
  1493. return ARIA::Role::button;
  1494. // https://www.w3.org/TR/html-aria/#el-input-checkbox
  1495. if (type_state() == TypeAttributeState::Checkbox)
  1496. return ARIA::Role::checkbox;
  1497. // https://www.w3.org/TR/html-aria/#el-input-email
  1498. if (type_state() == TypeAttributeState::Email && !has_attribute(AttributeNames::list))
  1499. return ARIA::Role::textbox;
  1500. // https://www.w3.org/TR/html-aria/#el-input-image
  1501. if (type_state() == TypeAttributeState::ImageButton)
  1502. return ARIA::Role::button;
  1503. // https://www.w3.org/TR/html-aria/#el-input-number
  1504. if (type_state() == TypeAttributeState::Number)
  1505. return ARIA::Role::spinbutton;
  1506. // https://www.w3.org/TR/html-aria/#el-input-radio
  1507. if (type_state() == TypeAttributeState::RadioButton)
  1508. return ARIA::Role::radio;
  1509. // https://www.w3.org/TR/html-aria/#el-input-range
  1510. if (type_state() == TypeAttributeState::Range)
  1511. return ARIA::Role::slider;
  1512. // https://www.w3.org/TR/html-aria/#el-input-reset
  1513. if (type_state() == TypeAttributeState::ResetButton)
  1514. return ARIA::Role::button;
  1515. // https://www.w3.org/TR/html-aria/#el-input-text-list
  1516. if ((type_state() == TypeAttributeState::Text
  1517. || type_state() == TypeAttributeState::Search
  1518. || type_state() == TypeAttributeState::Telephone
  1519. || type_state() == TypeAttributeState::URL
  1520. || type_state() == TypeAttributeState::Email)
  1521. && has_attribute(AttributeNames::list))
  1522. return ARIA::Role::combobox;
  1523. // https://www.w3.org/TR/html-aria/#el-input-search
  1524. if (type_state() == TypeAttributeState::Search && !has_attribute(AttributeNames::list))
  1525. return ARIA::Role::textbox;
  1526. // https://www.w3.org/TR/html-aria/#el-input-submit
  1527. if (type_state() == TypeAttributeState::SubmitButton)
  1528. return ARIA::Role::button;
  1529. // https://www.w3.org/TR/html-aria/#el-input-tel
  1530. if (type_state() == TypeAttributeState::Telephone)
  1531. return ARIA::Role::textbox;
  1532. // https://www.w3.org/TR/html-aria/#el-input-text
  1533. if (type_state() == TypeAttributeState::Text && !has_attribute(AttributeNames::list))
  1534. return ARIA::Role::textbox;
  1535. // https://www.w3.org/TR/html-aria/#el-input-url
  1536. if (type_state() == TypeAttributeState::URL && !has_attribute(AttributeNames::list))
  1537. return ARIA::Role::textbox;
  1538. // https://www.w3.org/TR/html-aria/#el-input-color
  1539. // https://www.w3.org/TR/html-aria/#el-input-date
  1540. // https://www.w3.org/TR/html-aria/#el-input-datetime-local
  1541. // https://www.w3.org/TR/html-aria/#el-input-file
  1542. // https://www.w3.org/TR/html-aria/#el-input-hidden
  1543. // https://www.w3.org/TR/html-aria/#el-input-month
  1544. // https://www.w3.org/TR/html-aria/#el-input-password
  1545. // https://www.w3.org/TR/html-aria/#el-input-time
  1546. // https://www.w3.org/TR/html-aria/#el-input-week
  1547. return {};
  1548. }
  1549. bool HTMLInputElement::is_button() const
  1550. {
  1551. // https://html.spec.whatwg.org/multipage/input.html#submit-button-state-(type=submit):concept-button
  1552. // https://html.spec.whatwg.org/multipage/input.html#image-button-state-(type=image):concept-button
  1553. // https://html.spec.whatwg.org/multipage/input.html#reset-button-state-(type=reset):concept-button
  1554. // https://html.spec.whatwg.org/multipage/input.html#button-state-(type=button):concept-button
  1555. return type_state() == TypeAttributeState::SubmitButton
  1556. || type_state() == TypeAttributeState::ImageButton
  1557. || type_state() == TypeAttributeState::ResetButton
  1558. || type_state() == TypeAttributeState::Button;
  1559. }
  1560. bool HTMLInputElement::is_submit_button() const
  1561. {
  1562. // https://html.spec.whatwg.org/multipage/input.html#submit-button-state-(type=submit):concept-submit-button
  1563. // https://html.spec.whatwg.org/multipage/input.html#image-button-state-(type=image):concept-submit-button
  1564. return type_state() == TypeAttributeState::SubmitButton
  1565. || type_state() == TypeAttributeState::ImageButton;
  1566. }
  1567. bool HTMLInputElement::has_activation_behavior() const
  1568. {
  1569. return true;
  1570. }
  1571. void HTMLInputElement::activation_behavior(DOM::Event const& event)
  1572. {
  1573. // The activation behavior for input elements are these steps:
  1574. // FIXME: 1. If this element is not mutable and is not in the Checkbox state and is not in the Radio state, then return.
  1575. // 2. Run this element's input activation behavior, if any, and do nothing otherwise.
  1576. run_input_activation_behavior(event).release_value_but_fixme_should_propagate_errors();
  1577. }
  1578. bool HTMLInputElement::has_input_activation_behavior() const
  1579. {
  1580. switch (type_state()) {
  1581. case TypeAttributeState::Checkbox:
  1582. case TypeAttributeState::Color:
  1583. case TypeAttributeState::FileUpload:
  1584. case TypeAttributeState::ImageButton:
  1585. case TypeAttributeState::RadioButton:
  1586. case TypeAttributeState::ResetButton:
  1587. case TypeAttributeState::SubmitButton:
  1588. return true;
  1589. default:
  1590. return false;
  1591. }
  1592. }
  1593. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:event-change-2
  1594. bool HTMLInputElement::change_event_applies() const
  1595. {
  1596. switch (type_state()) {
  1597. case TypeAttributeState::Checkbox:
  1598. case TypeAttributeState::Color:
  1599. case TypeAttributeState::Date:
  1600. case TypeAttributeState::Email:
  1601. case TypeAttributeState::FileUpload:
  1602. case TypeAttributeState::LocalDateAndTime:
  1603. case TypeAttributeState::Month:
  1604. case TypeAttributeState::Number:
  1605. case TypeAttributeState::Password:
  1606. case TypeAttributeState::RadioButton:
  1607. case TypeAttributeState::Range:
  1608. case TypeAttributeState::Search:
  1609. case TypeAttributeState::Telephone:
  1610. case TypeAttributeState::Text:
  1611. case TypeAttributeState::Time:
  1612. case TypeAttributeState::URL:
  1613. case TypeAttributeState::Week:
  1614. return true;
  1615. default:
  1616. return false;
  1617. }
  1618. }
  1619. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:dom-input-valueasdate-3
  1620. bool HTMLInputElement::value_as_date_applies() const
  1621. {
  1622. switch (type_state()) {
  1623. case TypeAttributeState::Date:
  1624. case TypeAttributeState::Month:
  1625. case TypeAttributeState::Week:
  1626. case TypeAttributeState::Time:
  1627. return true;
  1628. default:
  1629. return false;
  1630. }
  1631. }
  1632. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:dom-input-valueasnumber-3
  1633. bool HTMLInputElement::value_as_number_applies() const
  1634. {
  1635. switch (type_state()) {
  1636. case TypeAttributeState::Date:
  1637. case TypeAttributeState::Month:
  1638. case TypeAttributeState::Week:
  1639. case TypeAttributeState::Time:
  1640. case TypeAttributeState::LocalDateAndTime:
  1641. case TypeAttributeState::Number:
  1642. case TypeAttributeState::Range:
  1643. return true;
  1644. default:
  1645. return false;
  1646. }
  1647. }
  1648. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:attr-input-step-3
  1649. bool HTMLInputElement::step_applies() const
  1650. {
  1651. return value_as_number_applies();
  1652. }
  1653. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:dom-input-stepup-3
  1654. bool HTMLInputElement::step_up_or_down_applies() const
  1655. {
  1656. return value_as_number_applies();
  1657. }
  1658. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:dom-input-value-2
  1659. HTMLInputElement::ValueAttributeMode HTMLInputElement::value_attribute_mode() const
  1660. {
  1661. switch (type_state()) {
  1662. case TypeAttributeState::Text:
  1663. case TypeAttributeState::Search:
  1664. case TypeAttributeState::Telephone:
  1665. case TypeAttributeState::URL:
  1666. case TypeAttributeState::Email:
  1667. case TypeAttributeState::Password:
  1668. case TypeAttributeState::Date:
  1669. case TypeAttributeState::Month:
  1670. case TypeAttributeState::Week:
  1671. case TypeAttributeState::Time:
  1672. case TypeAttributeState::LocalDateAndTime:
  1673. case TypeAttributeState::Number:
  1674. case TypeAttributeState::Range:
  1675. case TypeAttributeState::Color:
  1676. return ValueAttributeMode::Value;
  1677. case TypeAttributeState::Hidden:
  1678. case TypeAttributeState::SubmitButton:
  1679. case TypeAttributeState::ImageButton:
  1680. case TypeAttributeState::ResetButton:
  1681. case TypeAttributeState::Button:
  1682. return ValueAttributeMode::Default;
  1683. case TypeAttributeState::Checkbox:
  1684. case TypeAttributeState::RadioButton:
  1685. return ValueAttributeMode::DefaultOn;
  1686. case TypeAttributeState::FileUpload:
  1687. return ValueAttributeMode::Filename;
  1688. }
  1689. VERIFY_NOT_REACHED();
  1690. }
  1691. }