HTMLFormElement.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 = LoadRequest::create_for_url_on_page(url, document().page());
  95. if (effective_method == "post") {
  96. auto body = url_encode(parameters, AK::URL::PercentEncodeSet::ApplicationXWWWFormUrlencoded).to_byte_buffer();
  97. request.set_method("POST");
  98. request.set_header("Content-Type", "application/x-www-form-urlencoded");
  99. request.set_body(body);
  100. }
  101. if (auto* page = document().page())
  102. page->load(request);
  103. }
  104. void HTMLFormElement::submit()
  105. {
  106. submit_form(this, true);
  107. }
  108. void HTMLFormElement::add_associated_element(Badge<FormAssociatedElement>, HTMLElement& element)
  109. {
  110. m_associated_elements.append(element);
  111. }
  112. void HTMLFormElement::remove_associated_element(Badge<FormAssociatedElement>, HTMLElement& element)
  113. {
  114. m_associated_elements.remove_first_matching([&](auto& entry) { return entry.ptr() == &element; });
  115. }
  116. // https://html.spec.whatwg.org/#dom-fs-action
  117. String HTMLFormElement::action() const
  118. {
  119. auto value = attribute(HTML::AttributeNames::action);
  120. // Return the current URL if the action attribute is null or an empty string
  121. if (value.is_null() || value.is_empty()) {
  122. return document().url().to_string();
  123. }
  124. return value;
  125. }
  126. static bool is_form_control(DOM::Element const& element)
  127. {
  128. if (is<HTMLButtonElement>(element)
  129. || is<HTMLFieldSetElement>(element)
  130. || is<HTMLObjectElement>(element)
  131. || is<HTMLOutputElement>(element)
  132. || is<HTMLSelectElement>(element)
  133. || is<HTMLTextAreaElement>(element)) {
  134. return true;
  135. }
  136. if (is<HTMLInputElement>(element)
  137. && !element.get_attribute(HTML::AttributeNames::type).equals_ignoring_case("image")) {
  138. return true;
  139. }
  140. return false;
  141. }
  142. // https://html.spec.whatwg.org/multipage/forms.html#dom-form-elements
  143. NonnullRefPtr<DOM::HTMLCollection> HTMLFormElement::elements() const
  144. {
  145. // FIXME: This should return the same HTMLFormControlsCollection object every time,
  146. // but that would cause a reference cycle since HTMLCollection refs the root.
  147. return DOM::HTMLCollection::create(const_cast<HTMLFormElement&>(*this), [](Element const& element) {
  148. return is_form_control(element);
  149. });
  150. }
  151. // https://html.spec.whatwg.org/multipage/forms.html#dom-form-length
  152. unsigned HTMLFormElement::length() const
  153. {
  154. // The length IDL attribute must return the number of nodes represented by the elements collection.
  155. return elements()->length();
  156. }
  157. }