HTMLFormElement.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/EventNames.h>
  9. #include <LibWeb/HTML/HTMLFormElement.h>
  10. #include <LibWeb/HTML/HTMLInputElement.h>
  11. #include <LibWeb/HTML/SubmitEvent.h>
  12. #include <LibWeb/Page/BrowsingContext.h>
  13. #include <LibWeb/Page/Page.h>
  14. #include <LibWeb/URLEncoder.h>
  15. namespace Web::HTML {
  16. HTMLFormElement::HTMLFormElement(DOM::Document& document, QualifiedName qualified_name)
  17. : HTMLElement(document, move(qualified_name))
  18. {
  19. }
  20. HTMLFormElement::~HTMLFormElement()
  21. {
  22. }
  23. void HTMLFormElement::submit_form(RefPtr<HTMLElement> submitter, bool from_submit_binding)
  24. {
  25. if (cannot_navigate())
  26. return;
  27. if (action().is_null()) {
  28. dbgln("Unsupported form action ''");
  29. return;
  30. }
  31. auto effective_method = method().to_lowercase();
  32. if (effective_method == "dialog") {
  33. dbgln("Failed to submit form: Unsupported form method '{}'", method());
  34. return;
  35. }
  36. if (effective_method != "get" && effective_method != "post") {
  37. effective_method = "get";
  38. }
  39. if (!from_submit_binding) {
  40. if (m_firing_submission_events)
  41. return;
  42. m_firing_submission_events = true;
  43. // FIXME: If the submitter element's no-validate state is false...
  44. RefPtr<HTMLElement> submitter_button;
  45. if (submitter != this)
  46. submitter_button = submitter;
  47. auto submit_event = SubmitEvent::create(EventNames::submit, submitter_button);
  48. submit_event->set_bubbles(true);
  49. submit_event->set_cancelable(true);
  50. bool continue_ = dispatch_event(submit_event);
  51. m_firing_submission_events = false;
  52. if (!continue_)
  53. return;
  54. // This is checked again because arbitrary JS may have run when handling submit,
  55. // which may have changed the result.
  56. if (cannot_navigate())
  57. return;
  58. }
  59. URL url(document().parse_url(action()));
  60. if (!url.is_valid()) {
  61. dbgln("Failed to submit form: Invalid URL: {}", action());
  62. return;
  63. }
  64. if (url.protocol() == "file") {
  65. if (document().url().protocol() != "file") {
  66. dbgln("Failed to submit form: Security violation: {} may not submit to {}", document().url(), url);
  67. return;
  68. }
  69. if (effective_method != "get") {
  70. dbgln("Failed to submit form: Unsupported form method '{}' for URL: {}", method(), url);
  71. return;
  72. }
  73. } else if (url.protocol() != "http" && url.protocol() != "https") {
  74. dbgln("Failed to submit form: Unsupported protocol for URL: {}", url);
  75. return;
  76. }
  77. Vector<URLQueryParam> parameters;
  78. for_each_in_inclusive_subtree_of_type<HTMLInputElement>([&](auto& input) {
  79. if (!input.name().is_null() && (input.type() != "submit" || &input == submitter))
  80. parameters.append({ input.name(), input.value() });
  81. return IterationDecision::Continue;
  82. });
  83. if (effective_method == "get") {
  84. url.set_query(urlencode(parameters, URL::PercentEncodeSet::ApplicationXWWWFormUrlencoded));
  85. }
  86. LoadRequest request;
  87. request.set_url(url);
  88. if (effective_method == "post") {
  89. auto body = urlencode(parameters, URL::PercentEncodeSet::ApplicationXWWWFormUrlencoded).to_byte_buffer();
  90. request.set_method("POST");
  91. request.set_header("Content-Type", "application/x-www-form-urlencoded");
  92. request.set_header("Content-Length", String::number(body.size()));
  93. request.set_body(body);
  94. }
  95. if (auto* page = document().page())
  96. page->load(request);
  97. }
  98. void HTMLFormElement::submit()
  99. {
  100. submit_form(this, true);
  101. }
  102. void HTMLFormElement::add_associated_element(Badge<FormAssociatedElement>, HTMLElement& element)
  103. {
  104. m_associated_elements.append(element);
  105. }
  106. void HTMLFormElement::remove_associated_element(Badge<FormAssociatedElement>, HTMLElement& element)
  107. {
  108. m_associated_elements.remove_first_matching([&](auto& entry) { return entry.ptr() == &element; });
  109. }
  110. }