HTMLFormElement.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StringBuilder.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/DOM/Event.h>
  9. #include <LibWeb/HTML/BrowsingContext.h>
  10. #include <LibWeb/HTML/EventNames.h>
  11. #include <LibWeb/HTML/HTMLButtonElement.h>
  12. #include <LibWeb/HTML/HTMLFieldSetElement.h>
  13. #include <LibWeb/HTML/HTMLFormElement.h>
  14. #include <LibWeb/HTML/HTMLInputElement.h>
  15. #include <LibWeb/HTML/HTMLObjectElement.h>
  16. #include <LibWeb/HTML/HTMLOutputElement.h>
  17. #include <LibWeb/HTML/HTMLSelectElement.h>
  18. #include <LibWeb/HTML/HTMLTextAreaElement.h>
  19. #include <LibWeb/HTML/SubmitEvent.h>
  20. #include <LibWeb/Page/Page.h>
  21. #include <LibWeb/URL/URL.h>
  22. namespace Web::HTML {
  23. HTMLFormElement::HTMLFormElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  24. : HTMLElement(document, move(qualified_name))
  25. {
  26. set_prototype(&Bindings::cached_web_prototype(realm(), "HTMLFormElement"));
  27. }
  28. HTMLFormElement::~HTMLFormElement() = default;
  29. void HTMLFormElement::visit_edges(Cell::Visitor& visitor)
  30. {
  31. Base::visit_edges(visitor);
  32. visitor.visit(m_elements);
  33. for (auto& element : m_associated_elements)
  34. visitor.visit(element.ptr());
  35. }
  36. void HTMLFormElement::submit_form(JS::GCPtr<HTMLElement> submitter, bool from_submit_binding)
  37. {
  38. if (cannot_navigate())
  39. return;
  40. if (action().is_null()) {
  41. dbgln("Unsupported form action ''");
  42. return;
  43. }
  44. auto effective_method = method().to_lowercase();
  45. if (effective_method == "dialog") {
  46. dbgln("Failed to submit form: Unsupported form method '{}'", method());
  47. return;
  48. }
  49. if (effective_method != "get" && effective_method != "post") {
  50. effective_method = "get";
  51. }
  52. if (!from_submit_binding) {
  53. if (m_firing_submission_events)
  54. return;
  55. m_firing_submission_events = true;
  56. // FIXME: If the submitter element's no-validate state is false...
  57. JS::GCPtr<HTMLElement> submitter_button;
  58. if (submitter != this)
  59. submitter_button = submitter;
  60. SubmitEventInit event_init {};
  61. event_init.submitter = submitter_button;
  62. auto submit_event = SubmitEvent::create(realm(), EventNames::submit, event_init);
  63. submit_event->set_bubbles(true);
  64. submit_event->set_cancelable(true);
  65. bool continue_ = dispatch_event(*submit_event);
  66. m_firing_submission_events = false;
  67. if (!continue_)
  68. return;
  69. // This is checked again because arbitrary JS may have run when handling submit,
  70. // which may have changed the result.
  71. if (cannot_navigate())
  72. return;
  73. }
  74. AK::URL url(document().parse_url(action()));
  75. if (!url.is_valid()) {
  76. dbgln("Failed to submit form: Invalid URL: {}", action());
  77. return;
  78. }
  79. if (url.scheme() == "file") {
  80. if (document().url().scheme() != "file") {
  81. dbgln("Failed to submit form: Security violation: {} may not submit to {}", document().url(), url);
  82. return;
  83. }
  84. if (effective_method != "get") {
  85. dbgln("Failed to submit form: Unsupported form method '{}' for URL: {}", method(), url);
  86. return;
  87. }
  88. } else if (url.scheme() != "http" && url.scheme() != "https") {
  89. dbgln("Failed to submit form: Unsupported protocol for URL: {}", url);
  90. return;
  91. }
  92. Vector<URL::QueryParam> parameters;
  93. for_each_in_inclusive_subtree_of_type<HTMLInputElement>([&](auto& input) {
  94. if (!input.name().is_null() && (input.type() != "submit" || &input == submitter))
  95. parameters.append({ input.name(), input.value() });
  96. return IterationDecision::Continue;
  97. });
  98. if (effective_method == "get") {
  99. url.set_query(url_encode(parameters, AK::URL::PercentEncodeSet::ApplicationXWWWFormUrlencoded));
  100. }
  101. LoadRequest request = LoadRequest::create_for_url_on_page(url, document().page());
  102. if (effective_method == "post") {
  103. auto body = url_encode(parameters, AK::URL::PercentEncodeSet::ApplicationXWWWFormUrlencoded).to_byte_buffer();
  104. request.set_method("POST");
  105. request.set_header("Content-Type", "application/x-www-form-urlencoded");
  106. request.set_body(move(body));
  107. }
  108. if (auto* page = document().page())
  109. page->load(request);
  110. }
  111. // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#resetting-a-form
  112. void HTMLFormElement::reset_form()
  113. {
  114. // 1. Let reset be the result of firing an event named reset at form, with the bubbles and cancelable attributes initialized to true.
  115. auto reset_event = DOM::Event::create(realm(), HTML::EventNames::reset);
  116. reset_event->set_bubbles(true);
  117. reset_event->set_cancelable(true);
  118. bool reset = dispatch_event(*reset_event);
  119. // 2. If reset is true, then invoke the reset algorithm of each resettable element whose form owner is form.
  120. if (reset) {
  121. for (auto element : m_associated_elements) {
  122. VERIFY(is<FormAssociatedElement>(*element));
  123. auto& form_associated_element = dynamic_cast<FormAssociatedElement&>(*element);
  124. if (form_associated_element.is_resettable())
  125. form_associated_element.reset_algorithm();
  126. }
  127. }
  128. }
  129. void HTMLFormElement::submit()
  130. {
  131. submit_form(this, true);
  132. }
  133. // https://html.spec.whatwg.org/multipage/forms.html#dom-form-reset
  134. void HTMLFormElement::reset()
  135. {
  136. // 1. If the form element is marked as locked for reset, then return.
  137. if (m_locked_for_reset)
  138. return;
  139. // 2. Mark the form element as locked for reset.
  140. m_locked_for_reset = true;
  141. // 3. Reset the form element.
  142. reset_form();
  143. // 4. Unmark the form element as locked for reset.
  144. m_locked_for_reset = false;
  145. }
  146. void HTMLFormElement::add_associated_element(Badge<FormAssociatedElement>, HTMLElement& element)
  147. {
  148. m_associated_elements.append(element);
  149. }
  150. void HTMLFormElement::remove_associated_element(Badge<FormAssociatedElement>, HTMLElement& element)
  151. {
  152. m_associated_elements.remove_first_matching([&](auto& entry) { return entry.ptr() == &element; });
  153. }
  154. // https://html.spec.whatwg.org/#dom-fs-action
  155. DeprecatedString HTMLFormElement::action() const
  156. {
  157. auto value = attribute(HTML::AttributeNames::action);
  158. // Return the current URL if the action attribute is null or an empty string
  159. if (value.is_null() || value.is_empty()) {
  160. return document().url().to_deprecated_string();
  161. }
  162. return value;
  163. }
  164. static bool is_form_control(DOM::Element const& element)
  165. {
  166. if (is<HTMLButtonElement>(element)
  167. || is<HTMLFieldSetElement>(element)
  168. || is<HTMLObjectElement>(element)
  169. || is<HTMLOutputElement>(element)
  170. || is<HTMLSelectElement>(element)
  171. || is<HTMLTextAreaElement>(element)) {
  172. return true;
  173. }
  174. if (is<HTMLInputElement>(element)
  175. && !element.get_attribute(HTML::AttributeNames::type).equals_ignoring_case("image"sv)) {
  176. return true;
  177. }
  178. return false;
  179. }
  180. // https://html.spec.whatwg.org/multipage/forms.html#dom-form-elements
  181. JS::NonnullGCPtr<DOM::HTMLCollection> HTMLFormElement::elements() const
  182. {
  183. if (!m_elements) {
  184. m_elements = DOM::HTMLCollection::create(const_cast<HTMLFormElement&>(*this), [](Element const& element) {
  185. return is_form_control(element);
  186. });
  187. }
  188. return *m_elements;
  189. }
  190. // https://html.spec.whatwg.org/multipage/forms.html#dom-form-length
  191. unsigned HTMLFormElement::length() const
  192. {
  193. // The length IDL attribute must return the number of nodes represented by the elements collection.
  194. return elements()->length();
  195. }
  196. }