HTMLFormElement.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/HTMLFormElement.h>
  11. #include <LibWeb/HTML/HTMLInputElement.h>
  12. #include <LibWeb/HTML/SubmitEvent.h>
  13. #include <LibWeb/Page/Page.h>
  14. #include <LibWeb/URL/URL.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. SubmitEventInit event_init {};
  48. event_init.submitter = submitter_button;
  49. auto submit_event = SubmitEvent::create(EventNames::submit, event_init);
  50. submit_event->set_bubbles(true);
  51. submit_event->set_cancelable(true);
  52. bool continue_ = dispatch_event(submit_event);
  53. m_firing_submission_events = false;
  54. if (!continue_)
  55. return;
  56. // This is checked again because arbitrary JS may have run when handling submit,
  57. // which may have changed the result.
  58. if (cannot_navigate())
  59. return;
  60. }
  61. AK::URL url(document().parse_url(action()));
  62. if (!url.is_valid()) {
  63. dbgln("Failed to submit form: Invalid URL: {}", action());
  64. return;
  65. }
  66. if (url.protocol() == "file") {
  67. if (document().url().protocol() != "file") {
  68. dbgln("Failed to submit form: Security violation: {} may not submit to {}", document().url(), url);
  69. return;
  70. }
  71. if (effective_method != "get") {
  72. dbgln("Failed to submit form: Unsupported form method '{}' for URL: {}", method(), url);
  73. return;
  74. }
  75. } else if (url.protocol() != "http" && url.protocol() != "https") {
  76. dbgln("Failed to submit form: Unsupported protocol for URL: {}", url);
  77. return;
  78. }
  79. Vector<URL::QueryParam> parameters;
  80. for_each_in_inclusive_subtree_of_type<HTMLInputElement>([&](auto& input) {
  81. if (!input.name().is_null() && (input.type() != "submit" || &input == submitter))
  82. parameters.append({ input.name(), input.value() });
  83. return IterationDecision::Continue;
  84. });
  85. if (effective_method == "get") {
  86. url.set_query(url_encode(parameters, AK::URL::PercentEncodeSet::ApplicationXWWWFormUrlencoded));
  87. }
  88. LoadRequest request;
  89. request.set_url(url);
  90. if (effective_method == "post") {
  91. auto body = url_encode(parameters, AK::URL::PercentEncodeSet::ApplicationXWWWFormUrlencoded).to_byte_buffer();
  92. request.set_method("POST");
  93. request.set_header("Content-Type", "application/x-www-form-urlencoded");
  94. request.set_header("Content-Length", String::number(body.size()));
  95. request.set_body(body);
  96. }
  97. if (auto* page = document().page())
  98. page->load(request);
  99. }
  100. void HTMLFormElement::submit()
  101. {
  102. submit_form(this, true);
  103. }
  104. void HTMLFormElement::add_associated_element(Badge<FormAssociatedElement>, HTMLElement& element)
  105. {
  106. m_associated_elements.append(element);
  107. }
  108. void HTMLFormElement::remove_associated_element(Badge<FormAssociatedElement>, HTMLElement& element)
  109. {
  110. m_associated_elements.remove_first_matching([&](auto& entry) { return entry.ptr() == &element; });
  111. }
  112. }