HTMLFormElement.cpp 6.0 KB

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