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/HTML/EventNames.h>
  8. #include <LibWeb/HTML/HTMLFormElement.h>
  9. #include <LibWeb/HTML/HTMLInputElement.h>
  10. #include <LibWeb/HTML/SubmitEvent.h>
  11. #include <LibWeb/InProcessWebView.h>
  12. #include <LibWeb/Page/BrowsingContext.h>
  13. #include <LibWeb/URLEncoder.h>
  14. namespace Web::HTML {
  15. HTMLFormElement::HTMLFormElement(DOM::Document& document, QualifiedName qualified_name)
  16. : HTMLElement(document, move(qualified_name))
  17. {
  18. }
  19. HTMLFormElement::~HTMLFormElement()
  20. {
  21. }
  22. void HTMLFormElement::submit_form(RefPtr<HTMLElement> submitter, bool from_submit_binding)
  23. {
  24. if (cannot_navigate())
  25. return;
  26. if (action().is_null()) {
  27. dbgln("Unsupported form action ''");
  28. return;
  29. }
  30. auto effective_method = method().to_lowercase();
  31. if (effective_method == "dialog") {
  32. dbgln("Failed to submit form: Unsupported form method '{}'", method());
  33. return;
  34. }
  35. if (effective_method != "get" && effective_method != "post") {
  36. effective_method = "get";
  37. }
  38. if (!from_submit_binding) {
  39. if (m_firing_submission_events)
  40. return;
  41. m_firing_submission_events = true;
  42. // FIXME: If the submitter element's no-validate state is false...
  43. RefPtr<HTMLElement> submitter_button;
  44. if (submitter != this)
  45. submitter_button = submitter;
  46. auto submit_event = SubmitEvent::create(EventNames::submit, submitter_button);
  47. submit_event->set_bubbles(true);
  48. submit_event->set_cancelable(true);
  49. bool continue_ = dispatch_event(submit_event);
  50. m_firing_submission_events = false;
  51. if (!continue_)
  52. return;
  53. // This is checked again because arbitrary JS may have run when handling submit,
  54. // which may have changed the result.
  55. if (cannot_navigate())
  56. return;
  57. }
  58. URL url(document().complete_url(action()));
  59. if (!url.is_valid()) {
  60. dbgln("Failed to submit form: Invalid URL: {}", action());
  61. return;
  62. }
  63. if (url.protocol() == "file") {
  64. if (document().url().protocol() != "file") {
  65. dbgln("Failed to submit form: Security violation: {} may not submit to {}", document().url(), url);
  66. return;
  67. }
  68. if (effective_method != "get") {
  69. dbgln("Failed to submit form: Unsupported form method '{}' for URL: {}", method(), url);
  70. return;
  71. }
  72. } else if (url.protocol() != "http" && url.protocol() != "https") {
  73. dbgln("Failed to submit form: Unsupported protocol for URL: {}", url);
  74. return;
  75. }
  76. Vector<URLQueryParam> parameters;
  77. for_each_in_inclusive_subtree_of_type<HTMLInputElement>([&](auto& node) {
  78. auto& input = verify_cast<HTMLInputElement>(node);
  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. }