HTMLInputElement.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Adam Hodgen <ant1441@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/DOM/Event.h>
  9. #include <LibWeb/DOM/ShadowRoot.h>
  10. #include <LibWeb/DOM/Text.h>
  11. #include <LibWeb/HTML/BrowsingContext.h>
  12. #include <LibWeb/HTML/EventNames.h>
  13. #include <LibWeb/HTML/HTMLFormElement.h>
  14. #include <LibWeb/HTML/HTMLInputElement.h>
  15. #include <LibWeb/Layout/BlockContainer.h>
  16. #include <LibWeb/Layout/ButtonBox.h>
  17. #include <LibWeb/Layout/CheckBox.h>
  18. #include <LibWeb/Layout/RadioButton.h>
  19. namespace Web::HTML {
  20. HTMLInputElement::HTMLInputElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  21. : FormAssociatedElement(document, move(qualified_name))
  22. {
  23. }
  24. HTMLInputElement::~HTMLInputElement()
  25. {
  26. }
  27. void HTMLInputElement::did_click_button(Badge<Layout::ButtonBox>)
  28. {
  29. // FIXME: This should be a PointerEvent.
  30. dispatch_event(DOM::Event::create(EventNames::click));
  31. if (type() == "submit") {
  32. if (auto* form = first_ancestor_of_type<HTMLFormElement>()) {
  33. form->submit_form(this);
  34. }
  35. return;
  36. }
  37. }
  38. void HTMLInputElement::did_click_checkbox(Badge<Layout::CheckBox>)
  39. {
  40. // FIXME: This should be a PointerEvent.
  41. auto click_event = DOM::Event::create(EventNames::click);
  42. click_event->set_bubbles(true);
  43. dispatch_event(move(click_event));
  44. }
  45. RefPtr<Layout::Node> HTMLInputElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  46. {
  47. if (type() == "hidden")
  48. return nullptr;
  49. if (type() == "submit" || type() == "button" || type() == "reset")
  50. return adopt_ref(*new Layout::ButtonBox(document(), *this, move(style)));
  51. if (type() == "checkbox")
  52. return adopt_ref(*new Layout::CheckBox(document(), *this, move(style)));
  53. if (type() == "radio")
  54. return adopt_ref(*new Layout::RadioButton(document(), *this, move(style)));
  55. create_shadow_tree_if_needed();
  56. auto layout_node = adopt_ref(*new Layout::BlockContainer(document(), this, move(style)));
  57. layout_node->set_inline(true);
  58. return layout_node;
  59. }
  60. void HTMLInputElement::set_checked(bool checked, ChangeSource change_source, ShouldRunActivationBehavior should_run_activation_behavior)
  61. {
  62. if (m_checked == checked)
  63. return;
  64. // The dirty checkedness flag must be initially set to false when the element is created,
  65. // and must be set to true whenever the user interacts with the control in a way that changes the checkedness.
  66. if (change_source == ChangeSource::User)
  67. m_dirty_checkedness = true;
  68. m_checked = checked;
  69. if (layout_node())
  70. layout_node()->set_needs_display();
  71. if (should_run_activation_behavior == ShouldRunActivationBehavior::Yes)
  72. run_activation_behavior();
  73. }
  74. void HTMLInputElement::run_activation_behavior()
  75. {
  76. // The activation behavior for input elements are these steps:
  77. // FIXME: 1. If this element is not mutable and is not in the Checkbox state and is not in the Radio state, then return.
  78. // 2. Run this element's input activation behavior, if any, and do nothing otherwise.
  79. run_input_activation_behavior();
  80. }
  81. // https://html.spec.whatwg.org/multipage/input.html#input-activation-behavior
  82. void HTMLInputElement::run_input_activation_behavior()
  83. {
  84. if (type() == "checkbox") {
  85. // 1. If the element is not connected, then return.
  86. if (!is_connected())
  87. return;
  88. // 2. Fire an event named input at the element with the bubbles and composed attributes initialized to true.
  89. auto input_event = DOM::Event::create(HTML::EventNames::input);
  90. input_event->set_bubbles(true);
  91. input_event->set_composed(true);
  92. dispatch_event(move(input_event));
  93. // 3. Fire an event named change at the element with the bubbles attribute initialized to true.
  94. auto change_event = DOM::Event::create(HTML::EventNames::change);
  95. change_event->set_bubbles(true);
  96. dispatch_event(move(change_event));
  97. } else {
  98. dispatch_event(DOM::Event::create(EventNames::change));
  99. }
  100. }
  101. void HTMLInputElement::did_edit_text_node(Badge<BrowsingContext>)
  102. {
  103. // NOTE: This is a bit ad-hoc, but basically implements part of "4.10.5.5 Common event behaviors"
  104. // https://html.spec.whatwg.org/multipage/input.html#common-input-element-events
  105. queue_an_element_task(HTML::Task::Source::UserInteraction, [this] {
  106. auto input_event = DOM::Event::create(HTML::EventNames::input);
  107. input_event->set_bubbles(true);
  108. input_event->set_composed(true);
  109. dispatch_event(move(input_event));
  110. // FIXME: This should only fire when the input is "committed", whatever that means.
  111. auto change_event = DOM::Event::create(HTML::EventNames::change);
  112. change_event->set_bubbles(true);
  113. dispatch_event(move(change_event));
  114. });
  115. }
  116. bool HTMLInputElement::enabled() const
  117. {
  118. return !has_attribute(HTML::AttributeNames::disabled);
  119. }
  120. String HTMLInputElement::value() const
  121. {
  122. if (m_text_node)
  123. return m_text_node->data();
  124. return default_value();
  125. }
  126. void HTMLInputElement::set_value(String value)
  127. {
  128. auto sanitised_value = value_sanitization_algorithm(move(value));
  129. if (m_text_node) {
  130. m_text_node->set_data(sanitised_value);
  131. return;
  132. }
  133. set_attribute(HTML::AttributeNames::value, sanitised_value);
  134. }
  135. void HTMLInputElement::create_shadow_tree_if_needed()
  136. {
  137. if (shadow_root())
  138. return;
  139. // FIXME: This assumes that we want a text box. Is that always true?
  140. auto shadow_root = adopt_ref(*new DOM::ShadowRoot(document(), *this));
  141. auto initial_value = attribute(HTML::AttributeNames::value);
  142. if (initial_value.is_null())
  143. initial_value = String::empty();
  144. auto element = document().create_element(HTML::TagNames::div);
  145. element->set_attribute(HTML::AttributeNames::style, "white-space: pre; padding-top: 1px; padding-bottom: 1px; padding-left: 2px; padding-right: 2px");
  146. m_text_node = adopt_ref(*new DOM::Text(document(), initial_value));
  147. m_text_node->set_always_editable(true);
  148. m_text_node->set_owner_input_element({}, *this);
  149. element->append_child(*m_text_node);
  150. shadow_root->append_child(move(element));
  151. set_shadow_root(move(shadow_root));
  152. }
  153. void HTMLInputElement::did_receive_focus()
  154. {
  155. auto* browsing_context = document().browsing_context();
  156. if (!browsing_context)
  157. return;
  158. if (!m_text_node)
  159. return;
  160. browsing_context->set_cursor_position(DOM::Position { *m_text_node, 0 });
  161. }
  162. bool HTMLInputElement::is_focusable() const
  163. {
  164. return m_text_node;
  165. }
  166. void HTMLInputElement::inserted()
  167. {
  168. HTMLElement::inserted();
  169. set_form(first_ancestor_of_type<HTMLFormElement>());
  170. }
  171. void HTMLInputElement::removed_from(DOM::Node* old_parent)
  172. {
  173. HTMLElement::removed_from(old_parent);
  174. set_form(nullptr);
  175. }
  176. void HTMLInputElement::parse_attribute(FlyString const& name, String const& value)
  177. {
  178. FormAssociatedElement::parse_attribute(name, value);
  179. if (name == HTML::AttributeNames::checked) {
  180. // When the checked content attribute is added, if the control does not have dirty checkedness,
  181. // the user agent must set the checkedness of the element to true
  182. if (!m_dirty_checkedness)
  183. set_checked(true, ChangeSource::Programmatic, ShouldRunActivationBehavior::No);
  184. }
  185. }
  186. void HTMLInputElement::did_remove_attribute(FlyString const& name)
  187. {
  188. FormAssociatedElement::did_remove_attribute(name);
  189. if (name == HTML::AttributeNames::checked) {
  190. // When the checked content attribute is removed, if the control does not have dirty checkedness,
  191. // the user agent must set the checkedness of the element to false.
  192. if (!m_dirty_checkedness)
  193. set_checked(false, ChangeSource::Programmatic, ShouldRunActivationBehavior::No);
  194. }
  195. }
  196. String HTMLInputElement::type() const
  197. {
  198. auto value = attribute(HTML::AttributeNames::type);
  199. #define __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(keyword, _) \
  200. if (value.equals_ignoring_case(#keyword)) \
  201. return #keyword;
  202. ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTES
  203. #undef __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE
  204. // The missing value default and the invalid value default are the Text state.
  205. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:missing-value-default
  206. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:invalid-value-default
  207. return "text";
  208. }
  209. HTMLInputElement::TypeAttributeState HTMLInputElement::type_state() const
  210. {
  211. auto value = attribute(HTML::AttributeNames::type);
  212. #define __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(keyword, state) \
  213. if (value.equals_ignoring_case(#keyword)) \
  214. return HTMLInputElement::TypeAttributeState::state;
  215. ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTES
  216. #undef __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE
  217. // The missing value default and the invalid value default are the Text state.
  218. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:missing-value-default
  219. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:invalid-value-default
  220. return HTMLInputElement::TypeAttributeState::Text;
  221. }
  222. void HTMLInputElement::set_type(String const& type)
  223. {
  224. set_attribute(HTML::AttributeNames::type, type);
  225. }
  226. // https://html.spec.whatwg.org/multipage/input.html#value-sanitization-algorithm
  227. String HTMLInputElement::value_sanitization_algorithm(String value) const
  228. {
  229. if (type_state() == HTMLInputElement::TypeAttributeState::Text || type_state() == HTMLInputElement::TypeAttributeState::Search || type_state() == HTMLInputElement::TypeAttributeState::Telephone || type_state() == HTMLInputElement::TypeAttributeState::Password) {
  230. // Strip newlines from the value.
  231. if (value.contains('\r') || value.contains('\n')) {
  232. StringBuilder builder;
  233. for (auto c : value) {
  234. if (!(c == '\r' || c == '\n'))
  235. builder.append(c);
  236. }
  237. return builder.to_string();
  238. }
  239. } else if (type_state() == HTMLInputElement::TypeAttributeState::URL) {
  240. // Strip newlines from the value, then strip leading and trailing ASCII whitespace from the value.
  241. if (value.contains('\r') || value.contains('\n')) {
  242. StringBuilder builder;
  243. for (auto c : value) {
  244. if (!(c == '\r' || c == '\n'))
  245. builder.append(c);
  246. }
  247. return builder.to_string().trim_whitespace();
  248. }
  249. } else if (type_state() == HTMLInputElement::TypeAttributeState::Number) {
  250. // If the value of the element is not a valid floating-point number, then set it to the empty string instead.
  251. char* end_ptr;
  252. auto val = strtod(value.characters(), &end_ptr);
  253. if (!isfinite(val) || *end_ptr)
  254. return "";
  255. }
  256. // FIXME: Implement remaining value sanitation algorithms
  257. return value;
  258. }
  259. }