HTMLInputElement.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. activation_behavior = [this](auto&) {
  24. // The activation behavior for input elements are these steps:
  25. // FIXME: 1. If this element is not mutable and is not in the Checkbox state and is not in the Radio state, then return.
  26. // 2. Run this element's input activation behavior, if any, and do nothing otherwise.
  27. run_input_activation_behavior();
  28. };
  29. }
  30. HTMLInputElement::~HTMLInputElement() = default;
  31. RefPtr<Layout::Node> HTMLInputElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  32. {
  33. if (type_state() == TypeAttributeState::Hidden)
  34. return nullptr;
  35. if (type_state() == TypeAttributeState::SubmitButton || type_state() == TypeAttributeState::Button || type_state() == TypeAttributeState::ResetButton)
  36. return adopt_ref(*new Layout::ButtonBox(document(), *this, move(style)));
  37. if (type_state() == TypeAttributeState::Checkbox)
  38. return adopt_ref(*new Layout::CheckBox(document(), *this, move(style)));
  39. if (type_state() == TypeAttributeState::RadioButton)
  40. return adopt_ref(*new Layout::RadioButton(document(), *this, move(style)));
  41. auto layout_node = adopt_ref(*new Layout::BlockContainer(document(), this, move(style)));
  42. layout_node->set_inline(true);
  43. return layout_node;
  44. }
  45. void HTMLInputElement::set_checked(bool checked, ChangeSource change_source)
  46. {
  47. if (m_checked == checked)
  48. return;
  49. // The dirty checkedness flag must be initially set to false when the element is created,
  50. // and must be set to true whenever the user interacts with the control in a way that changes the checkedness.
  51. if (change_source == ChangeSource::User)
  52. m_dirty_checkedness = true;
  53. m_checked = checked;
  54. set_needs_style_update(true);
  55. }
  56. void HTMLInputElement::set_checked_binding(bool checked)
  57. {
  58. if (type_state() == TypeAttributeState::RadioButton) {
  59. if (checked)
  60. set_checked_within_group();
  61. else
  62. set_checked(false, ChangeSource::Programmatic);
  63. } else {
  64. set_checked(checked, ChangeSource::Programmatic);
  65. }
  66. }
  67. // https://html.spec.whatwg.org/multipage/input.html#input-activation-behavior
  68. void HTMLInputElement::run_input_activation_behavior()
  69. {
  70. if (type_state() == TypeAttributeState::Checkbox || type_state() == TypeAttributeState::RadioButton) {
  71. // 1. If the element is not connected, then return.
  72. if (!is_connected())
  73. return;
  74. // 2. Fire an event named input at the element with the bubbles and composed attributes initialized to true.
  75. auto input_event = DOM::Event::create(HTML::EventNames::input);
  76. input_event->set_bubbles(true);
  77. input_event->set_composed(true);
  78. dispatch_event(move(input_event));
  79. // 3. Fire an event named change at the element with the bubbles attribute initialized to true.
  80. auto change_event = DOM::Event::create(HTML::EventNames::change);
  81. change_event->set_bubbles(true);
  82. dispatch_event(move(change_event));
  83. } else if (type_state() == TypeAttributeState::SubmitButton) {
  84. RefPtr<HTMLFormElement> form;
  85. // 1. If the element does not have a form owner, then return.
  86. if (!(form = this->form()))
  87. return;
  88. // 2. If the element's node document is not fully active, then return.
  89. if (!document().is_fully_active())
  90. return;
  91. // 3. Submit the form owner from the element.
  92. form->submit_form(this);
  93. } else {
  94. dispatch_event(DOM::Event::create(EventNames::change));
  95. }
  96. }
  97. void HTMLInputElement::did_edit_text_node(Badge<BrowsingContext>)
  98. {
  99. // NOTE: This is a bit ad-hoc, but basically implements part of "4.10.5.5 Common event behaviors"
  100. // https://html.spec.whatwg.org/multipage/input.html#common-input-element-events
  101. queue_an_element_task(HTML::Task::Source::UserInteraction, [this] {
  102. auto input_event = DOM::Event::create(HTML::EventNames::input);
  103. input_event->set_bubbles(true);
  104. input_event->set_composed(true);
  105. dispatch_event(move(input_event));
  106. // FIXME: This should only fire when the input is "committed", whatever that means.
  107. auto change_event = DOM::Event::create(HTML::EventNames::change);
  108. change_event->set_bubbles(true);
  109. dispatch_event(move(change_event));
  110. });
  111. }
  112. String HTMLInputElement::value() const
  113. {
  114. if (m_text_node)
  115. return m_text_node->data();
  116. return default_value();
  117. }
  118. void HTMLInputElement::set_value(String value)
  119. {
  120. auto sanitised_value = value_sanitization_algorithm(move(value));
  121. if (m_text_node) {
  122. m_text_node->set_data(sanitised_value);
  123. return;
  124. }
  125. set_attribute(HTML::AttributeNames::value, sanitised_value);
  126. }
  127. void HTMLInputElement::create_shadow_tree_if_needed()
  128. {
  129. if (shadow_root())
  130. return;
  131. // FIXME: This could be better factored. Everything except the below types becomes a text input.
  132. switch (type_state()) {
  133. case TypeAttributeState::RadioButton:
  134. case TypeAttributeState::Checkbox:
  135. case TypeAttributeState::Button:
  136. case TypeAttributeState::SubmitButton:
  137. case TypeAttributeState::ResetButton:
  138. case TypeAttributeState::ImageButton:
  139. return;
  140. default:
  141. break;
  142. }
  143. auto shadow_root = adopt_ref(*new DOM::ShadowRoot(document(), *this));
  144. auto initial_value = attribute(HTML::AttributeNames::value);
  145. if (initial_value.is_null())
  146. initial_value = String::empty();
  147. auto element = document().create_element(HTML::TagNames::div).release_value();
  148. element->set_attribute(HTML::AttributeNames::style, "white-space: pre; padding-top: 1px; padding-bottom: 1px; padding-left: 2px; padding-right: 2px");
  149. m_text_node = adopt_ref(*new DOM::Text(document(), initial_value));
  150. m_text_node->set_always_editable(true);
  151. m_text_node->set_owner_input_element({}, *this);
  152. element->append_child(*m_text_node);
  153. shadow_root->append_child(move(element));
  154. set_shadow_root(move(shadow_root));
  155. }
  156. void HTMLInputElement::did_receive_focus()
  157. {
  158. auto* browsing_context = document().browsing_context();
  159. if (!browsing_context)
  160. return;
  161. if (!m_text_node)
  162. return;
  163. browsing_context->set_cursor_position(DOM::Position { *m_text_node, 0 });
  164. }
  165. bool HTMLInputElement::is_focusable() const
  166. {
  167. return m_text_node;
  168. }
  169. void HTMLInputElement::parse_attribute(FlyString const& name, String const& value)
  170. {
  171. FormAssociatedElement::parse_attribute(name, value);
  172. if (name == HTML::AttributeNames::checked) {
  173. // When the checked content attribute is added, if the control does not have dirty checkedness,
  174. // the user agent must set the checkedness of the element to true
  175. if (!m_dirty_checkedness)
  176. set_checked(true, ChangeSource::Programmatic);
  177. } else if (name == HTML::AttributeNames::type) {
  178. m_type = parse_type_attribute(value);
  179. }
  180. }
  181. HTMLInputElement::TypeAttributeState HTMLInputElement::parse_type_attribute(StringView type)
  182. {
  183. #define __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(keyword, state) \
  184. if (type.equals_ignoring_case(#keyword)) \
  185. return HTMLInputElement::TypeAttributeState::state;
  186. ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTES
  187. #undef __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE
  188. // The missing value default and the invalid value default are the Text state.
  189. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:missing-value-default
  190. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:invalid-value-default
  191. return HTMLInputElement::TypeAttributeState::Text;
  192. }
  193. void HTMLInputElement::did_remove_attribute(FlyString const& name)
  194. {
  195. FormAssociatedElement::did_remove_attribute(name);
  196. if (name == HTML::AttributeNames::checked) {
  197. // When the checked content attribute is removed, if the control does not have dirty checkedness,
  198. // the user agent must set the checkedness of the element to false.
  199. if (!m_dirty_checkedness)
  200. set_checked(false, ChangeSource::Programmatic);
  201. }
  202. }
  203. String HTMLInputElement::type() const
  204. {
  205. switch (m_type) {
  206. #define __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE(keyword, state) \
  207. case TypeAttributeState::state: \
  208. return #keyword##sv;
  209. ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTES
  210. #undef __ENUMERATE_HTML_INPUT_TYPE_ATTRIBUTE
  211. }
  212. VERIFY_NOT_REACHED();
  213. }
  214. void HTMLInputElement::set_type(String const& type)
  215. {
  216. set_attribute(HTML::AttributeNames::type, type);
  217. }
  218. // https://html.spec.whatwg.org/multipage/input.html#value-sanitization-algorithm
  219. String HTMLInputElement::value_sanitization_algorithm(String value) const
  220. {
  221. if (type_state() == HTMLInputElement::TypeAttributeState::Text || type_state() == HTMLInputElement::TypeAttributeState::Search || type_state() == HTMLInputElement::TypeAttributeState::Telephone || type_state() == HTMLInputElement::TypeAttributeState::Password) {
  222. // Strip newlines from the value.
  223. if (value.contains('\r') || value.contains('\n')) {
  224. StringBuilder builder;
  225. for (auto c : value) {
  226. if (!(c == '\r' || c == '\n'))
  227. builder.append(c);
  228. }
  229. return builder.to_string();
  230. }
  231. } else if (type_state() == HTMLInputElement::TypeAttributeState::URL) {
  232. // Strip newlines from the value, then strip leading and trailing ASCII whitespace from the value.
  233. if (value.contains('\r') || value.contains('\n')) {
  234. StringBuilder builder;
  235. for (auto c : value) {
  236. if (!(c == '\r' || c == '\n'))
  237. builder.append(c);
  238. }
  239. return builder.to_string().trim_whitespace();
  240. }
  241. } else if (type_state() == HTMLInputElement::TypeAttributeState::Number) {
  242. // If the value of the element is not a valid floating-point number, then set it to the empty string instead.
  243. char* end_ptr;
  244. auto val = strtod(value.characters(), &end_ptr);
  245. if (!isfinite(val) || *end_ptr)
  246. return "";
  247. }
  248. // FIXME: Implement remaining value sanitation algorithms
  249. return value;
  250. }
  251. void HTMLInputElement::inserted()
  252. {
  253. create_shadow_tree_if_needed();
  254. }
  255. void HTMLInputElement::set_checked_within_group()
  256. {
  257. if (checked())
  258. return;
  259. set_checked(true, ChangeSource::User);
  260. String name = this->name();
  261. document().for_each_in_inclusive_subtree_of_type<HTML::HTMLInputElement>([&](auto& element) {
  262. if (element.checked() && &element != this && element.name() == name)
  263. element.set_checked(false, ChangeSource::User);
  264. return IterationDecision::Continue;
  265. });
  266. }
  267. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:legacy-pre-activation-behavior
  268. void HTMLInputElement::legacy_pre_activation_behavior()
  269. {
  270. m_before_legacy_pre_activation_behavior_checked = checked();
  271. // 1. If this element's type attribute is in the Checkbox state, then set
  272. // this element's checkedness to its opposite value (i.e. true if it is
  273. // false, false if it is true) and set this element's indeterminate IDL
  274. // attribute to false.
  275. // FIXME: Set indeterminate to false when that exists.
  276. if (type_state() == TypeAttributeState::Checkbox) {
  277. set_checked(!checked(), ChangeSource::User);
  278. }
  279. // 2. If this element's type attribute is in the Radio Button state, then
  280. // get a reference to the element in this element's radio button group that
  281. // has its checkedness set to true, if any, and then set this element's
  282. // checkedness to true.
  283. if (type_state() == TypeAttributeState::RadioButton) {
  284. String name = this->name();
  285. document().for_each_in_inclusive_subtree_of_type<HTML::HTMLInputElement>([&](auto& element) {
  286. if (element.checked() && element.name() == name) {
  287. m_legacy_pre_activation_behavior_checked_element_in_group = element;
  288. return IterationDecision::Break;
  289. }
  290. return IterationDecision::Continue;
  291. });
  292. set_checked_within_group();
  293. }
  294. }
  295. // https://html.spec.whatwg.org/multipage/input.html#the-input-element:legacy-canceled-activation-behavior
  296. void HTMLInputElement::legacy_cancelled_activation_behavior()
  297. {
  298. // 1. If the element's type attribute is in the Checkbox state, then set the
  299. // element's checkedness and the element's indeterminate IDL attribute back
  300. // to the values they had before the legacy-pre-activation behavior was run.
  301. if (type_state() == TypeAttributeState::Checkbox) {
  302. set_checked(m_before_legacy_pre_activation_behavior_checked, ChangeSource::Programmatic);
  303. }
  304. // 2. If this element 's type attribute is in the Radio Button state, then
  305. // if the element to which a reference was obtained in the
  306. // legacy-pre-activation behavior, if any, is still in what is now this
  307. // element' s radio button group, if it still has one, and if so, setting
  308. // that element 's checkedness to true; or else, if there was no such
  309. // element, or that element is no longer in this element' s radio button
  310. // group, or if this element no longer has a radio button group, setting
  311. // this element's checkedness to false.
  312. if (type_state() == TypeAttributeState::RadioButton) {
  313. String name = this->name();
  314. bool did_reselect_previous_element = false;
  315. if (m_legacy_pre_activation_behavior_checked_element_in_group) {
  316. auto& element_in_group = *m_legacy_pre_activation_behavior_checked_element_in_group;
  317. if (name == element_in_group.name()) {
  318. element_in_group.set_checked_within_group();
  319. did_reselect_previous_element = true;
  320. }
  321. m_legacy_pre_activation_behavior_checked_element_in_group = nullptr;
  322. }
  323. if (!did_reselect_previous_element)
  324. set_checked(false, ChangeSource::User);
  325. }
  326. }
  327. void HTMLInputElement::legacy_cancelled_activation_behavior_was_not_called()
  328. {
  329. m_legacy_pre_activation_behavior_checked_element_in_group = nullptr;
  330. }
  331. }