HTMLFormElement.cpp 7.7 KB

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