HTMLInputElement.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/Document.h>
  7. #include <LibWeb/DOM/Event.h>
  8. #include <LibWeb/DOM/ShadowRoot.h>
  9. #include <LibWeb/DOM/Text.h>
  10. #include <LibWeb/HTML/BrowsingContext.h>
  11. #include <LibWeb/HTML/EventNames.h>
  12. #include <LibWeb/HTML/HTMLFormElement.h>
  13. #include <LibWeb/HTML/HTMLInputElement.h>
  14. #include <LibWeb/Layout/BlockContainer.h>
  15. #include <LibWeb/Layout/ButtonBox.h>
  16. #include <LibWeb/Layout/CheckBox.h>
  17. #include <LibWeb/Layout/RadioButton.h>
  18. namespace Web::HTML {
  19. HTMLInputElement::HTMLInputElement(DOM::Document& document, QualifiedName qualified_name)
  20. : FormAssociatedElement(document, move(qualified_name))
  21. {
  22. }
  23. HTMLInputElement::~HTMLInputElement()
  24. {
  25. }
  26. void HTMLInputElement::did_click_button(Badge<Layout::ButtonBox>)
  27. {
  28. // FIXME: This should be a PointerEvent.
  29. dispatch_event(DOM::Event::create(EventNames::click));
  30. if (type().equals_ignoring_case("submit")) {
  31. if (auto* form = first_ancestor_of_type<HTMLFormElement>()) {
  32. form->submit_form(this);
  33. }
  34. return;
  35. }
  36. }
  37. void HTMLInputElement::did_click_checkbox(Badge<Layout::CheckBox>)
  38. {
  39. // FIXME: This should be a PointerEvent.
  40. auto click_event = DOM::Event::create(EventNames::click);
  41. click_event->set_bubbles(true);
  42. dispatch_event(move(click_event));
  43. }
  44. RefPtr<Layout::Node> HTMLInputElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  45. {
  46. if (type() == "hidden")
  47. return nullptr;
  48. if (type().equals_ignoring_case("submit") || type().equals_ignoring_case("button"))
  49. return adopt_ref(*new Layout::ButtonBox(document(), *this, move(style)));
  50. if (type() == "checkbox")
  51. return adopt_ref(*new Layout::CheckBox(document(), *this, move(style)));
  52. if (type() == "radio")
  53. return adopt_ref(*new Layout::RadioButton(document(), *this, move(style)));
  54. create_shadow_tree_if_needed();
  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, ShouldRunActivationBehavior should_run_activation_behavior)
  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. if (layout_node())
  69. layout_node()->set_needs_display();
  70. if (should_run_activation_behavior == ShouldRunActivationBehavior::Yes)
  71. run_activation_behavior();
  72. }
  73. void HTMLInputElement::run_activation_behavior()
  74. {
  75. // The activation behavior for input elements are these steps:
  76. // FIXME: 1. If this element is not mutable and is not in the Checkbox state and is not in the Radio state, then return.
  77. // 2. Run this element's input activation behavior, if any, and do nothing otherwise.
  78. run_input_activation_behavior();
  79. }
  80. // https://html.spec.whatwg.org/multipage/input.html#input-activation-behavior
  81. void HTMLInputElement::run_input_activation_behavior()
  82. {
  83. if (type().equals_ignoring_case("checkbox")) {
  84. // 1. If the element is not connected, then return.
  85. if (!is_connected())
  86. return;
  87. // 2. Fire an event named input at the element with the bubbles and composed attributes initialized to true.
  88. auto input_event = DOM::Event::create(HTML::EventNames::input);
  89. input_event->set_bubbles(true);
  90. input_event->set_composed(true);
  91. dispatch_event(move(input_event));
  92. // 3. Fire an event named change at the element with the bubbles attribute initialized to true.
  93. auto change_event = DOM::Event::create(HTML::EventNames::change);
  94. change_event->set_bubbles(true);
  95. dispatch_event(move(change_event));
  96. } else {
  97. dispatch_event(DOM::Event::create(EventNames::change));
  98. }
  99. }
  100. void HTMLInputElement::did_edit_text_node(Badge<BrowsingContext>)
  101. {
  102. // NOTE: This is a bit ad-hoc, but basically implements part of "4.10.5.5 Common event behaviors"
  103. // https://html.spec.whatwg.org/multipage/input.html#common-input-element-events
  104. queue_an_element_task(HTML::Task::Source::UserInteraction, [this] {
  105. auto input_event = DOM::Event::create(HTML::EventNames::input);
  106. input_event->set_bubbles(true);
  107. input_event->set_composed(true);
  108. dispatch_event(move(input_event));
  109. // FIXME: This should only fire when the input is "committed", whatever that means.
  110. auto change_event = DOM::Event::create(HTML::EventNames::change);
  111. change_event->set_bubbles(true);
  112. dispatch_event(move(change_event));
  113. });
  114. }
  115. bool HTMLInputElement::enabled() const
  116. {
  117. return !has_attribute(HTML::AttributeNames::disabled);
  118. }
  119. String HTMLInputElement::value() const
  120. {
  121. if (m_text_node)
  122. return m_text_node->data();
  123. return default_value();
  124. }
  125. void HTMLInputElement::set_value(String value)
  126. {
  127. if (m_text_node) {
  128. m_text_node->set_data(move(value));
  129. return;
  130. }
  131. set_attribute(HTML::AttributeNames::value, move(value));
  132. }
  133. void HTMLInputElement::create_shadow_tree_if_needed()
  134. {
  135. if (shadow_root())
  136. return;
  137. // FIXME: This assumes that we want a text box. Is that always true?
  138. auto shadow_root = adopt_ref(*new DOM::ShadowRoot(document(), *this));
  139. auto initial_value = attribute(HTML::AttributeNames::value);
  140. if (initial_value.is_null())
  141. initial_value = String::empty();
  142. auto element = document().create_element(HTML::TagNames::div);
  143. element->set_attribute(HTML::AttributeNames::style, "white-space: pre");
  144. m_text_node = adopt_ref(*new DOM::Text(document(), initial_value));
  145. m_text_node->set_always_editable(true);
  146. m_text_node->set_owner_input_element({}, *this);
  147. element->append_child(*m_text_node);
  148. shadow_root->append_child(move(element));
  149. set_shadow_root(move(shadow_root));
  150. }
  151. void HTMLInputElement::did_receive_focus()
  152. {
  153. auto* browsing_context = document().browsing_context();
  154. if (!browsing_context)
  155. return;
  156. if (!m_text_node)
  157. return;
  158. browsing_context->set_cursor_position(DOM::Position { *m_text_node, 0 });
  159. }
  160. bool HTMLInputElement::is_focusable() const
  161. {
  162. return m_text_node;
  163. }
  164. void HTMLInputElement::inserted()
  165. {
  166. HTMLElement::inserted();
  167. set_form(first_ancestor_of_type<HTMLFormElement>());
  168. }
  169. void HTMLInputElement::removed_from(DOM::Node* old_parent)
  170. {
  171. HTMLElement::removed_from(old_parent);
  172. set_form(nullptr);
  173. }
  174. void HTMLInputElement::parse_attribute(FlyString const& name, String const& value)
  175. {
  176. FormAssociatedElement::parse_attribute(name, value);
  177. if (name == HTML::AttributeNames::checked) {
  178. // When the checked content attribute is added, if the control does not have dirty checkedness,
  179. // the user agent must set the checkedness of the element to true
  180. if (!m_dirty_checkedness)
  181. set_checked(true, ChangeSource::Programmatic, ShouldRunActivationBehavior::No);
  182. }
  183. }
  184. void HTMLInputElement::did_remove_attribute(FlyString const& name)
  185. {
  186. FormAssociatedElement::did_remove_attribute(name);
  187. if (name == HTML::AttributeNames::checked) {
  188. // When the checked content attribute is removed, if the control does not have dirty checkedness,
  189. // the user agent must set the checkedness of the element to false.
  190. if (!m_dirty_checkedness)
  191. set_checked(false, ChangeSource::Programmatic, ShouldRunActivationBehavior::No);
  192. }
  193. }
  194. String HTMLInputElement::type() const
  195. {
  196. // FIXME: This should only reflect known values.
  197. auto value = attribute(HTML::AttributeNames::type);
  198. if (value.is_null())
  199. return "text";
  200. return value;
  201. }
  202. void HTMLInputElement::set_type(String const& type)
  203. {
  204. set_attribute(HTML::AttributeNames::type, type);
  205. }
  206. }