HTMLFormElement.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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/HTML/BrowsingContext.h>
  9. #include <LibWeb/HTML/EventNames.h>
  10. #include <LibWeb/HTML/HTMLButtonElement.h>
  11. #include <LibWeb/HTML/HTMLFieldSetElement.h>
  12. #include <LibWeb/HTML/HTMLFormElement.h>
  13. #include <LibWeb/HTML/HTMLInputElement.h>
  14. #include <LibWeb/HTML/HTMLObjectElement.h>
  15. #include <LibWeb/HTML/HTMLOutputElement.h>
  16. #include <LibWeb/HTML/HTMLSelectElement.h>
  17. #include <LibWeb/HTML/HTMLTextAreaElement.h>
  18. #include <LibWeb/HTML/SubmitEvent.h>
  19. #include <LibWeb/Page/Page.h>
  20. #include <LibWeb/URL/URL.h>
  21. namespace Web::HTML {
  22. HTMLFormElement::HTMLFormElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  23. : HTMLElement(document, move(qualified_name))
  24. {
  25. }
  26. HTMLFormElement::~HTMLFormElement()
  27. {
  28. }
  29. void HTMLFormElement::submit_form(RefPtr<HTMLElement> submitter, bool from_submit_binding)
  30. {
  31. if (cannot_navigate())
  32. return;
  33. if (action().is_null()) {
  34. dbgln("Unsupported form action ''");
  35. return;
  36. }
  37. auto effective_method = method().to_lowercase();
  38. if (effective_method == "dialog") {
  39. dbgln("Failed to submit form: Unsupported form method '{}'", method());
  40. return;
  41. }
  42. if (effective_method != "get" && effective_method != "post") {
  43. effective_method = "get";
  44. }
  45. if (!from_submit_binding) {
  46. if (m_firing_submission_events)
  47. return;
  48. m_firing_submission_events = true;
  49. // FIXME: If the submitter element's no-validate state is false...
  50. RefPtr<HTMLElement> submitter_button;
  51. if (submitter != this)
  52. submitter_button = submitter;
  53. SubmitEventInit event_init {};
  54. event_init.submitter = submitter_button;
  55. auto submit_event = SubmitEvent::create(EventNames::submit, event_init);
  56. submit_event->set_bubbles(true);
  57. submit_event->set_cancelable(true);
  58. bool continue_ = dispatch_event(submit_event);
  59. m_firing_submission_events = false;
  60. if (!continue_)
  61. return;
  62. // This is checked again because arbitrary JS may have run when handling submit,
  63. // which may have changed the result.
  64. if (cannot_navigate())
  65. return;
  66. }
  67. AK::URL url(document().parse_url(action()));
  68. if (!url.is_valid()) {
  69. dbgln("Failed to submit form: Invalid URL: {}", action());
  70. return;
  71. }
  72. if (url.protocol() == "file") {
  73. if (document().url().protocol() != "file") {
  74. dbgln("Failed to submit form: Security violation: {} may not submit to {}", document().url(), url);
  75. return;
  76. }
  77. if (effective_method != "get") {
  78. dbgln("Failed to submit form: Unsupported form method '{}' for URL: {}", method(), url);
  79. return;
  80. }
  81. } else if (url.protocol() != "http" && url.protocol() != "https") {
  82. dbgln("Failed to submit form: Unsupported protocol for URL: {}", url);
  83. return;
  84. }
  85. Vector<URL::QueryParam> parameters;
  86. for_each_in_inclusive_subtree_of_type<HTMLInputElement>([&](auto& input) {
  87. if (!input.name().is_null() && (input.type() != "submit" || &input == submitter))
  88. parameters.append({ input.name(), input.value() });
  89. return IterationDecision::Continue;
  90. });
  91. if (effective_method == "get") {
  92. url.set_query(url_encode(parameters, AK::URL::PercentEncodeSet::ApplicationXWWWFormUrlencoded));
  93. }
  94. LoadRequest request;
  95. request.set_url(url);
  96. if (effective_method == "post") {
  97. auto body = url_encode(parameters, AK::URL::PercentEncodeSet::ApplicationXWWWFormUrlencoded).to_byte_buffer();
  98. request.set_method("POST");
  99. request.set_header("Content-Type", "application/x-www-form-urlencoded");
  100. request.set_body(body);
  101. }
  102. if (auto* page = document().page())
  103. page->load(request);
  104. }
  105. void HTMLFormElement::submit()
  106. {
  107. submit_form(this, true);
  108. }
  109. void HTMLFormElement::add_associated_element(Badge<FormAssociatedElement>, HTMLElement& element)
  110. {
  111. m_associated_elements.append(element);
  112. }
  113. void HTMLFormElement::remove_associated_element(Badge<FormAssociatedElement>, HTMLElement& element)
  114. {
  115. m_associated_elements.remove_first_matching([&](auto& entry) { return entry.ptr() == &element; });
  116. }
  117. // https://html.spec.whatwg.org/#dom-fs-action
  118. String HTMLFormElement::action() const
  119. {
  120. auto value = attribute(HTML::AttributeNames::action);
  121. // Return the current URL if the action attribute is null or an empty string
  122. if (value.is_null() || value.is_empty()) {
  123. return document().url().to_string();
  124. }
  125. return value;
  126. }
  127. static bool is_form_control(DOM::Element const& element)
  128. {
  129. if (is<HTMLButtonElement>(element)
  130. || is<HTMLFieldSetElement>(element)
  131. || is<HTMLObjectElement>(element)
  132. || is<HTMLOutputElement>(element)
  133. || is<HTMLSelectElement>(element)
  134. || is<HTMLTextAreaElement>(element)) {
  135. return true;
  136. }
  137. if (is<HTMLInputElement>(element)
  138. && !element.get_attribute(HTML::AttributeNames::type).equals_ignoring_case("image")) {
  139. return true;
  140. }
  141. return false;
  142. }
  143. // https://html.spec.whatwg.org/multipage/forms.html#dom-form-elements
  144. NonnullRefPtr<DOM::HTMLCollection> HTMLFormElement::elements() const
  145. {
  146. // FIXME: This should return the same HTMLFormControlsCollection object every time,
  147. // but that would cause a reference cycle since HTMLCollection refs the root.
  148. return DOM::HTMLCollection::create(const_cast<HTMLFormElement&>(*this), [](Element const& element) {
  149. return is_form_control(element);
  150. });
  151. }
  152. // https://html.spec.whatwg.org/multipage/forms.html#dom-form-length
  153. unsigned HTMLFormElement::length() const
  154. {
  155. // The length IDL attribute must return the number of nodes represented by the elements collection.
  156. return elements()->length();
  157. }
  158. }