HTMLFormElement.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/StringBuilder.h>
  27. #include <LibWeb/HTML/EventNames.h>
  28. #include <LibWeb/HTML/HTMLFormElement.h>
  29. #include <LibWeb/HTML/HTMLInputElement.h>
  30. #include <LibWeb/HTML/SubmitEvent.h>
  31. #include <LibWeb/InProcessWebView.h>
  32. #include <LibWeb/Page/Frame.h>
  33. #include <LibWeb/URLEncoder.h>
  34. namespace Web::HTML {
  35. HTMLFormElement::HTMLFormElement(DOM::Document& document, const QualifiedName& qualified_name)
  36. : HTMLElement(document, qualified_name)
  37. {
  38. }
  39. HTMLFormElement::~HTMLFormElement()
  40. {
  41. }
  42. void HTMLFormElement::submit_form(RefPtr<HTMLElement> submitter, bool from_submit_binding)
  43. {
  44. if (cannot_navigate())
  45. return;
  46. if (action().is_null()) {
  47. dbg() << "Unsupported form action ''";
  48. return;
  49. }
  50. auto effective_method = method().to_lowercase();
  51. if (effective_method == "dialog") {
  52. dbg() << "Failed to submit form: Unsupported form method '" << method() << "'";
  53. return;
  54. }
  55. if (effective_method != "get" && effective_method != "post") {
  56. effective_method = "get";
  57. }
  58. if (!from_submit_binding) {
  59. if (m_firing_submission_events)
  60. return;
  61. m_firing_submission_events = true;
  62. // FIXME: If the submitter element's no-validate state is false...
  63. RefPtr<HTMLElement> submitter_button;
  64. if (submitter != this)
  65. submitter_button = submitter;
  66. auto submit_event = SubmitEvent::create(EventNames::submit, submitter_button);
  67. submit_event->set_bubbles(true);
  68. submit_event->set_cancelable(true);
  69. bool continue_ = dispatch_event(submit_event);
  70. m_firing_submission_events = false;
  71. if (!continue_)
  72. return;
  73. // This is checked again because arbitrary JS may have run when handling submit,
  74. // which may have changed the result.
  75. if (cannot_navigate())
  76. return;
  77. }
  78. URL url(document().complete_url(action()));
  79. if (!url.is_valid()) {
  80. dbg() << "Failed to submit form: Invalid URL: " << action();
  81. return;
  82. }
  83. if (url.protocol() == "file") {
  84. if (document().url().protocol() != "file") {
  85. dbg() << "Failed to submit form: Security violation: " << document().url() << " may not submit to " << url;
  86. return;
  87. }
  88. if (effective_method != "get") {
  89. dbg() << "Failed to submit form: Unsupported form method '" << method() << "' for URL: " << url;
  90. return;
  91. }
  92. } else if (url.protocol() != "http" && url.protocol() != "https") {
  93. dbg() << "Failed to submit form: Unsupported protocol for URL: " << url;
  94. return;
  95. }
  96. Vector<URLQueryParam> parameters;
  97. for_each_in_subtree_of_type<HTMLInputElement>([&](auto& node) {
  98. auto& input = downcast<HTMLInputElement>(node);
  99. if (!input.name().is_null() && (input.type() != "submit" || &input == submitter))
  100. parameters.append({ input.name(), input.value() });
  101. return IterationDecision::Continue;
  102. });
  103. if (effective_method == "get") {
  104. url.set_query(urlencode(parameters));
  105. }
  106. LoadRequest request;
  107. request.set_url(url);
  108. if (effective_method == "post") {
  109. auto body = urlencode(parameters).to_byte_buffer();
  110. request.set_method("POST");
  111. request.set_header("Content-Type", "application/x-www-form-urlencoded");
  112. request.set_header("Content-Length", String::number(body.size()));
  113. request.set_body(body);
  114. }
  115. if (auto* page = document().page())
  116. page->load(request);
  117. }
  118. void HTMLFormElement::submit()
  119. {
  120. submit_form(this, true);
  121. }
  122. }