HTMLFormElement.cpp 7.7 KB

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