HTMLInputElement.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Adam Hodgen <ant1441@gmail.com>
  4. * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/DOM/Event.h>
  10. #include <LibWeb/DOM/ShadowRoot.h>
  11. #include <LibWeb/DOM/Text.h>
  12. #include <LibWeb/HTML/BrowsingContext.h>
  13. #include <LibWeb/HTML/EventNames.h>
  14. #include <LibWeb/HTML/HTMLFormElement.h>
  15. #include <LibWeb/HTML/HTMLInputElement.h>
  16. #include <LibWeb/HTML/Scripting/Environments.h>
  17. #include <LibWeb/Infra/CharacterTypes.h>
  18. #include <LibWeb/Layout/BlockContainer.h>
  19. #include <LibWeb/Layout/ButtonBox.h>
  20. #include <LibWeb/Layout/CheckBox.h>
  21. #include <LibWeb/Layout/RadioButton.h>
  22. #include <LibWeb/WebIDL/DOMException.h>
  23. #include <LibWeb/WebIDL/ExceptionOr.h>
  24. namespace Web::HTML {
  25. HTMLInputElement::HTMLInputElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  26. : HTMLElement(document, move(qualified_name))
  27. , m_value(String::empty())
  28. {
  29. set_prototype(&Bindings::cached_web_prototype(realm(), "HTMLInputElement"));
  30. activation_behavior = [this](auto&) {
  31. // The activation behavior for input elements are these steps:
  32. // FIXME: 1. If this element is not mutable and is not in the Checkbox state and is not in the Radio state, then return.
  33. // 2. Run this element's input activation behavior, if any, and do nothing otherwise.
  34. run_input_activation_behavior();
  35. };
  36. }
  37. HTMLInputElement::~HTMLInputElement() = default;
  38. void HTMLInputElement::visit_edges(Cell::Visitor& visitor)
  39. {
  40. Base::visit_edges(visitor);
  41. visitor.visit(m_text_node.ptr());
  42. visitor.visit(m_legacy_pre_activation_behavior_checked_element_in_group.ptr());
  43. visitor.visit(m_selected_files);
  44. }
  45. RefPtr<Layout::Node> HTMLInputElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  46. {
  47. if (type_state() == TypeAttributeState::Hidden)
  48. return nullptr;
  49. if (type_state() == TypeAttributeState::SubmitButton || type_state() == TypeAttributeState::Button || type_state() == TypeAttributeState::ResetButton || type_state() == TypeAttributeState::FileUpload)
  50. return adopt_ref(*new Layout::ButtonBox(document(), *this, move(style)));
  51. if (type_state() == TypeAttributeState::Checkbox)
  52. return adopt_ref(*new Layout::CheckBox(document(), *this, move(style)));
  53. if (type_state() == TypeAttributeState::RadioButton)
  54. return adopt_ref(*new Layout::RadioButton(document(), *this, move(style)));
  55. auto layout_node = adopt_ref(*new Layout::BlockContainer(document(), this, move(style)));
  56. layout_node->set_inline(true);
  57. return layout_node;
  58. }
  59. void HTMLInputElement::set_checked(bool checked, ChangeSource change_source)
  60. {
  61. if (m_checked == checked)
  62. return;
  63. // The dirty checkedness flag must be initially set to false when the element is created,
  64. // and must be set to true whenever the user interacts with the control in a way that changes the checkedness.
  65. if (change_source == ChangeSource::User)
  66. m_dirty_checkedness = true;
  67. m_checked = checked;
  68. set_needs_style_update(true);
  69. }
  70. void HTMLInputElement::set_checked_binding(bool checked)
  71. {
  72. if (type_state() == TypeAttributeState::RadioButton) {
  73. if (checked)
  74. set_checked_within_group();
  75. else
  76. set_checked(false, ChangeSource::Programmatic);
  77. } else {
  78. set_checked(checked, ChangeSource::Programmatic);
  79. }
  80. }
  81. // https://html.spec.whatwg.org/multipage/input.html#dom-input-files
  82. JS::GCPtr<FileAPI::FileList> HTMLInputElement::files()
  83. {
  84. // On getting, if the IDL attribute applies, it must return a FileList object that represents the current selected files.
  85. // The same object must be returned until the list of selected files changes.
  86. // If the IDL attribute does not apply, then it must instead return null.
  87. if (m_type != TypeAttributeState::FileUpload)
  88. return nullptr;
  89. if (!m_selected_files)
  90. m_selected_files = FileAPI::FileList::create(realm(), {});
  91. return m_selected_files;
  92. }
  93. // https://html.spec.whatwg.org/multipage/input.html#dom-input-files
  94. void HTMLInputElement::set_files(JS::GCPtr<FileAPI::FileList> files)
  95. {
  96. // 1. If the IDL attribute does not apply or the given value is null, then return.
  97. if (m_type != TypeAttributeState::FileUpload || files == nullptr)
  98. return;
  99. // 2. Replace the element's selected files with the given value.
  100. m_selected_files = files;
  101. }
  102. // https://html.spec.whatwg.org/multipage/input.html#update-the-file-selection
  103. void HTMLInputElement::update_the_file_selection(JS::NonnullGCPtr<FileAPI::FileList> files)
  104. {
  105. // 1. Queue an element task on the user interaction task source given element and the following steps:
  106. queue_an_element_task(Task::Source::UserInteraction, [this, files]() mutable {
  107. // 1. Update element's selected files so that it represents the user's selection.
  108. this->set_files(files.ptr());
  109. // 2. Fire an event named input at the input element, with the bubbles and composed attributes initialized to true.
  110. auto input_event = DOM::Event::create(this->realm(), EventNames::input, { .bubbles = true, .composed = true });
  111. this->dispatch_event(*input_event);
  112. // 3. Fire an event named change at the input element, with the bubbles attribute initialized to true.
  113. auto change_event = DOM::Event::create(this->realm(), EventNames::change, { .bubbles = true });
  114. this->dispatch_event(*change_event);
  115. });
  116. }
  117. // https://html.spec.whatwg.org/multipage/input.html#show-the-picker,-if-applicable
  118. static void show_the_picker_if_applicable(HTMLInputElement& element)
  119. {
  120. // To show the picker, if applicable for an input element element:
  121. // 1. If element's relevant global object does not have transient activation, then return.
  122. auto& global_object = relevant_global_object(element);
  123. if (!is<HTML::Window>(global_object) || !static_cast<HTML::Window&>(global_object).has_transient_activation())
  124. return;
  125. // FIXME: 2. If element is not mutable, then return.
  126. // 3. If element's type attribute is in the File Upload state, then run these steps in parallel:
  127. if (element.type_state() == HTMLInputElement::TypeAttributeState::FileUpload) {
  128. // NOTE: These steps cannot be fully implemented here, and must be done in the PageClient when the response comes back from the PageHost
  129. // 1. Optionally, wait until any prior execution of this algorithm has terminated.
  130. // 2. Display a prompt to the user requesting that the user specify some files.
  131. // If the multiple attribute is not set on element, there must be no more than one file selected; otherwise, any number may be selected.
  132. // 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.
  133. // 3. Wait for the user to have made their selection.
  134. // 4. If the user dismissed the prompt without changing their selection,
  135. // then queue an element task on the user interaction task source given element to fire an event named cancel at element,
  136. // with the bubbles attribute initialized to true.
  137. // 5. Otherwise, update the file selection for element.
  138. bool const multiple = element.has_attribute(HTML::AttributeNames::multiple);
  139. auto weak_element = element.make_weak_ptr<DOM::EventTarget>();
  140. // FIXME: Pass along accept attribute information https://html.spec.whatwg.org/multipage/input.html#attr-input-accept
  141. // The accept attribute may be specified to provide user agents with a hint of what file types will be accepted.
  142. element.document().browsing_context()->top_level_browsing_context().page()->client().page_did_request_file_picker(weak_element, multiple);
  143. return;
  144. }
  145. // FIXME: show "any relevant user interface" for other type attribute states "in the way [the user agent] normally would"
  146. // 4. Otherwise, the user agent should show any relevant user interface for selecting a value for element,
  147. // 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.)
  148. // If such a user interface is shown, it must respect the requirements stated in the relevant parts of the specification for how element
  149. // behaves given its type attribute state. (For example, various sections describe restrictions on the resulting value string.)
  150. // This step can have side effects, such as closing other pickers that were previously shown by this algorithm.
  151. // (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.)
  152. }
  153. // https://html.spec.whatwg.org/multipage/input.html#dom-input-showpicker
  154. WebIDL::ExceptionOr<void> HTMLInputElement::show_picker()
  155. {
  156. // The showPicker() method steps are:
  157. // FIXME: 1. If this is not mutable, then throw an "InvalidStateError" DOMException.
  158. // 2. If this's relevant settings object's origin is not same origin with this's relevant settings object's top-level origin,
  159. // and this's type attribute is not in the File Upload state or Color state, then throw a "SecurityError" DOMException.
  160. // NOTE: File and Color inputs are exempted from this check for historical reason: their input activation behavior also shows their pickers,
  161. // and has never been guarded by an origin check.
  162. if (!relevant_settings_object(*this).origin().is_same_origin(relevant_settings_object(*this).top_level_origin)
  163. && m_type != TypeAttributeState::FileUpload && m_type != TypeAttributeState::Color) {
  164. return WebIDL::SecurityError::create(realm(), "Cross origin pickers are not allowed"sv);
  165. }
  166. // 3. If this's relevant global object does not have transient activation, then throw a "NotAllowedError" DOMException.
  167. // FIXME: The global object we get here should probably not need casted to Window to check for transient activation
  168. auto& global_object = relevant_global_object(*this);
  169. if (!is<HTML::Window>(global_object) || !static_cast<HTML::Window&>(global_object).has_transient_activation()) {
  170. return WebIDL::NotAllowedError::create(realm(), "Too long since user activation to show picker"sv);
  171. }
  172. // 4. Show the picker, if applicable, for this.
  173. show_the_picker_if_applicable(*this);
  174. return {};
  175. }
  176. // https://html.spec.whatwg.org/multipage/input.html#input-activation-behavior
  177. void HTMLInputElement::run_input_activation_behavior()
  178. {
  179. if (type_state() == TypeAttributeState::Checkbox || type_state() == TypeAttributeState::RadioButton) {
  180. // 1. If the element is not connected, then return.
  181. if (!is_connected())
  182. return;
  183. // 2. Fire an event named input at the element with the bubbles and composed attributes initialized to true.
  184. auto input_event = DOM::Event::create(realm(), HTML::EventNames::input);
  185. input_event->set_bubbles(true);
  186. input_event->set_composed(true);
  187. dispatch_event(*input_event);
  188. // 3. Fire an event named change at the element with the bubbles attribute initialized to true.
  189. auto change_event = DOM::Event::create(realm(), HTML::EventNames::change);
  190. change_event->set_bubbles(true);
  191. dispatch_event(*change_event);
  192. } else if (type_state() == TypeAttributeState::SubmitButton) {
  193. JS::GCPtr<HTMLFormElement> form;
  194. // 1. If the element does not have a form owner, then return.
  195. if (!(form = this->form()))
  196. return;
  197. // 2. If the element's node document is not fully active, then return.
  198. if (!document().is_fully_active())
  199. return;
  200. // 3. Submit the form owner from the element.
  201. form->submit_form(this);
  202. } else if (type_state() == TypeAttributeState::FileUpload) {
  203. show_the_picker_if_applicable(*this);
  204. } else {
  205. dispatch_event(*DOM::Event::create(realm(), EventNames::change));
  206. }
  207. }
  208. void HTMLInputElement::did_edit_text_node(Badge<BrowsingContext>)
  209. {
  210. // 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.
  211. m_value = value_sanitization_algorithm(m_text_node->data());
  212. m_dirty_value = true;
  213. // NOTE: This is a bit ad-hoc, but basically implements part of "4.10.5.5 Common event behaviors"
  214. // https://html.spec.whatwg.org/multipage/input.html#common-input-element-events
  215. queue_an_element_task(HTML::Task::Source::UserInteraction, [this] {
  216. auto input_event = DOM::Event::create(realm(), HTML::EventNames::input);
  217. input_event->set_bubbles(true);
  218. input_event->set_composed(true);
  219. dispatch_event(*input_event);
  220. // FIXME: This should only fire when the input is "committed", whatever that means.
  221. auto change_event = DOM::Event::create(realm(), HTML::EventNames::change);
  222. change_event->set_bubbles(true);
  223. dispatch_event(*change_event);
  224. });
  225. }
  226. String HTMLInputElement::value() const
  227. {
  228. // https://html.spec.whatwg.org/multipage/input.html#dom-input-value-filename
  229. if (type_state() == TypeAttributeState::FileUpload) {
  230. // NOTE: This "fakepath" requirement is a sad accident of history. See the example in the File Upload state section for more information.
  231. // NOTE: Since path components are not permitted in filenames in the list of selected files, the "\fakepath\" cannot be mistaken for a path component.
  232. if (m_selected_files && m_selected_files->item(0))
  233. return String::formatted("C:\\fakepath\\{}", m_selected_files->item(0)->name());
  234. return "C:\\fakepath\\"sv;
  235. }
  236. // https://html.spec.whatwg.org/multipage/input.html#dom-input-value-value
  237. // Return the current value of the element.
  238. return m_value;
  239. }
  240. WebIDL::ExceptionOr<void> HTMLInputElement::set_value(String value)
  241. {
  242. // https://html.spec.whatwg.org/multipage/input.html#dom-input-value-filename
  243. if (type_state() == TypeAttributeState::FileUpload) {
  244. // On setting, if the new value is the empty string, empty the list of selected files; otherwise, throw an "InvalidStateError" DOMException.
  245. if (value != String::empty())
  246. return WebIDL::InvalidStateError::create(realm(), "Setting value of input type file to non-empty string"sv);
  247. m_selected_files = nullptr;
  248. return {};
  249. }
  250. // https://html.spec.whatwg.org/multipage/input.html#dom-input-value-value
  251. // 1. Let oldValue be the element's value.
  252. auto old_value = move(m_value);
  253. // 2. Set the element's value to the new value.
  254. // NOTE: This is done as part of step 4 below.
  255. // 3. Set the element's dirty value flag to true.
  256. m_dirty_value = true;
  257. // 4. Invoke the value sanitization algorithm, if the element's type attribute's current state defines one.
  258. m_value = value_sanitization_algorithm(move(value));
  259. // 5. If the element's value (after applying the value sanitization algorithm) is different from oldValue,
  260. // and the element has a text entry cursor position, move the text entry cursor position to the end of the
  261. // text control, unselecting any selected text and resetting the selection direction to "none".
  262. if (m_text_node && (m_value != old_value))
  263. m_text_node->set_data(m_value);
  264. return {};
  265. }
  266. void HTMLInputElement::create_shadow_tree_if_needed()
  267. {
  268. if (shadow_root())
  269. return;
  270. // FIXME: This could be better factored. Everything except the below types becomes a text input.
  271. switch (type_state()) {
  272. case TypeAttributeState::RadioButton:
  273. case TypeAttributeState::Checkbox:
  274. case TypeAttributeState::Button:
  275. case TypeAttributeState::SubmitButton:
  276. case TypeAttributeState::ResetButton:
  277. case TypeAttributeState::ImageButton:
  278. return;
  279. default:
  280. break;
  281. }
  282. auto* shadow_root = heap().allocate<DOM::ShadowRoot>(realm(), document(), *this);
  283. auto initial_value = m_value;
  284. if (initial_value.is_null())
  285. initial_value = String::empty();
  286. auto element = document().create_element(HTML::TagNames::div).release_value();
  287. element->set_attribute(HTML::AttributeNames::style, "white-space: pre; padding-top: 1px; padding-bottom: 1px; padding-left: 2px; padding-right: 2px");
  288. m_text_node = heap().allocate<DOM::Text>(realm(), document(), initial_value);
  289. m_text_node->set_always_editable(m_type != TypeAttributeState::FileUpload);
  290. m_text_node->set_owner_input_element({}, *this);
  291. element->append_child(*m_text_node);
  292. shadow_root->append_child(move(element));
  293. set_shadow_root(move(shadow_root));
  294. }
  295. void HTMLInputElement::did_receive_focus()
  296. {
  297. auto* browsing_context = document().browsing_context();
  298. if (!browsing_context)
  299. return;
  300. if (!m_text_node)
  301. return;
  302. browsing_context->set_cursor_position(DOM::Position { *m_text_node, 0 });
  303. }
  304. void HTMLInputElement::parse_attribute(FlyString const& name, String const& value)
  305. {
  306. HTMLElement::parse_attribute(name, value);
  307. if (name == HTML::AttributeNames::checked) {
  308. // When the checked content attribute is added, if the control does not have dirty checkedness,
  309. // the user agent must set the checkedness of the element to true
  310. if (!m_dirty_checkedness)
  311. set_checked(true, ChangeSource::Programmatic);
  312. } else if (name == HTML::AttributeNames::type) {
  313. m_type = parse_type_attribute(value);
  314. } else if (name == HTML::AttributeNames::value) {
  315. if (!m_dirty_value)
  316. m_value = value_sanitization_algorithm(value);
  317. }
  318. }
  319. HTMLInputElement::TypeAttributeState HTMLInputElement::parse_type_attribute(StringView type)
  320. {
  321. #define __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(keyword, state) \
  322. if (type.equals_ignoring_case(#keyword##sv)) \
  323. return HTMLInputElement::TypeAttributeState::state;
  324. ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTES
  325. #undef __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE
  326. // The missing value default and the invalid value default are the Text state.
  327. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:missing-value-default
  328. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:invalid-value-default
  329. return HTMLInputElement::TypeAttributeState::Text;
  330. }
  331. void HTMLInputElement::did_remove_attribute(FlyString const& name)
  332. {
  333. HTMLElement::did_remove_attribute(name);
  334. if (name == HTML::AttributeNames::checked) {
  335. // When the checked content attribute is removed, if the control does not have dirty checkedness,
  336. // the user agent must set the checkedness of the element to false.
  337. if (!m_dirty_checkedness)
  338. set_checked(false, ChangeSource::Programmatic);
  339. } else if (name == HTML::AttributeNames::value) {
  340. if (!m_dirty_value)
  341. m_value = String::empty();
  342. }
  343. }
  344. String HTMLInputElement::type() const
  345. {
  346. switch (m_type) {
  347. #define __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(keyword, state) \
  348. case TypeAttributeState::state: \
  349. return #keyword##sv;
  350. ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTES
  351. #undef __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE
  352. }
  353. VERIFY_NOT_REACHED();
  354. }
  355. void HTMLInputElement::set_type(String const& type)
  356. {
  357. set_attribute(HTML::AttributeNames::type, type);
  358. }
  359. // https://html.spec.whatwg.org/multipage/input.html#value-sanitization-algorithm
  360. String HTMLInputElement::value_sanitization_algorithm(String value) const
  361. {
  362. if (type_state() == HTMLInputElement::TypeAttributeState::Text || type_state() == HTMLInputElement::TypeAttributeState::Search || type_state() == HTMLInputElement::TypeAttributeState::Telephone || type_state() == HTMLInputElement::TypeAttributeState::Password) {
  363. // Strip newlines from the value.
  364. if (value.contains('\r') || value.contains('\n')) {
  365. StringBuilder builder;
  366. for (auto c : value) {
  367. if (!(c == '\r' || c == '\n'))
  368. builder.append(c);
  369. }
  370. return builder.to_string();
  371. }
  372. } else if (type_state() == HTMLInputElement::TypeAttributeState::URL) {
  373. // Strip newlines from the value, then strip leading and trailing ASCII whitespace from the value.
  374. if (value.contains('\r') || value.contains('\n')) {
  375. StringBuilder builder;
  376. for (auto c : value) {
  377. if (!(c == '\r' || c == '\n'))
  378. builder.append(c);
  379. }
  380. return builder.string_view().trim(Infra::ASCII_WHITESPACE);
  381. }
  382. } else if (type_state() == HTMLInputElement::TypeAttributeState::Number) {
  383. // If the value of the element is not a valid floating-point number, then set it to the empty string instead.
  384. char* end_ptr;
  385. auto val = strtod(value.characters(), &end_ptr);
  386. if (!isfinite(val) || *end_ptr)
  387. return "";
  388. }
  389. // FIXME: Implement remaining value sanitation algorithms
  390. return value;
  391. }
  392. void HTMLInputElement::form_associated_element_was_inserted()
  393. {
  394. create_shadow_tree_if_needed();
  395. }
  396. void HTMLInputElement::set_checked_within_group()
  397. {
  398. if (checked())
  399. return;
  400. set_checked(true, ChangeSource::User);
  401. String name = this->name();
  402. document().for_each_in_inclusive_subtree_of_type<HTML::HTMLInputElement>([&](auto& element) {
  403. if (element.checked() && &element != this && element.name() == name)
  404. element.set_checked(false, ChangeSource::User);
  405. return IterationDecision::Continue;
  406. });
  407. }
  408. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:legacy-pre-activation-behavior
  409. void HTMLInputElement::legacy_pre_activation_behavior()
  410. {
  411. m_before_legacy_pre_activation_behavior_checked = checked();
  412. // 1. If this element's type attribute is in the Checkbox state, then set
  413. // this element's checkedness to its opposite value (i.e. true if it is
  414. // false, false if it is true) and set this element's indeterminate IDL
  415. // attribute to false.
  416. // FIXME: Set indeterminate to false when that exists.
  417. if (type_state() == TypeAttributeState::Checkbox) {
  418. set_checked(!checked(), ChangeSource::User);
  419. }
  420. // 2. If this element's type attribute is in the Radio Button state, then
  421. // get a reference to the element in this element's radio button group that
  422. // has its checkedness set to true, if any, and then set this element's
  423. // checkedness to true.
  424. if (type_state() == TypeAttributeState::RadioButton) {
  425. String name = this->name();
  426. document().for_each_in_inclusive_subtree_of_type<HTML::HTMLInputElement>([&](auto& element) {
  427. if (element.checked() && element.name() == name) {
  428. m_legacy_pre_activation_behavior_checked_element_in_group = &element;
  429. return IterationDecision::Break;
  430. }
  431. return IterationDecision::Continue;
  432. });
  433. set_checked_within_group();
  434. }
  435. }
  436. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:legacy-canceled-activation-behavior
  437. void HTMLInputElement::legacy_cancelled_activation_behavior()
  438. {
  439. // 1. If the element's type attribute is in the Checkbox state, then set the
  440. // element's checkedness and the element's indeterminate IDL attribute back
  441. // to the values they had before the legacy-pre-activation behavior was run.
  442. if (type_state() == TypeAttributeState::Checkbox) {
  443. set_checked(m_before_legacy_pre_activation_behavior_checked, ChangeSource::Programmatic);
  444. }
  445. // 2. If this element 's type attribute is in the Radio Button state, then
  446. // if the element to which a reference was obtained in the
  447. // legacy-pre-activation behavior, if any, is still in what is now this
  448. // element' s radio button group, if it still has one, and if so, setting
  449. // that element 's checkedness to true; or else, if there was no such
  450. // element, or that element is no longer in this element' s radio button
  451. // group, or if this element no longer has a radio button group, setting
  452. // this element's checkedness to false.
  453. if (type_state() == TypeAttributeState::RadioButton) {
  454. String name = this->name();
  455. bool did_reselect_previous_element = false;
  456. if (m_legacy_pre_activation_behavior_checked_element_in_group) {
  457. auto& element_in_group = *m_legacy_pre_activation_behavior_checked_element_in_group;
  458. if (name == element_in_group.name()) {
  459. element_in_group.set_checked_within_group();
  460. did_reselect_previous_element = true;
  461. }
  462. m_legacy_pre_activation_behavior_checked_element_in_group = nullptr;
  463. }
  464. if (!did_reselect_previous_element)
  465. set_checked(false, ChangeSource::User);
  466. }
  467. }
  468. void HTMLInputElement::legacy_cancelled_activation_behavior_was_not_called()
  469. {
  470. m_legacy_pre_activation_behavior_checked_element_in_group = nullptr;
  471. }
  472. }