HTMLFormElement.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <AK/StringBuilder.h>
  2. #include <LibHTML/DOM/HTMLFormElement.h>
  3. #include <LibHTML/DOM/HTMLInputElement.h>
  4. #include <LibHTML/Frame.h>
  5. #include <LibHTML/HtmlView.h>
  6. HTMLFormElement::HTMLFormElement(Document& document, const String& tag_name)
  7. : HTMLElement(document, tag_name)
  8. {
  9. }
  10. HTMLFormElement::~HTMLFormElement()
  11. {
  12. }
  13. void HTMLFormElement::submit()
  14. {
  15. if (action().is_null()) {
  16. dbg() << "Unsupported form action ''";
  17. return;
  18. }
  19. if (method().to_lowercase() != "get") {
  20. dbg() << "Unsupported form method '" << method() << "'";
  21. return;
  22. }
  23. URL url(document().complete_url(action()));
  24. struct NameAndValue {
  25. String name;
  26. String value;
  27. };
  28. Vector<NameAndValue> parameters;
  29. for_each_in_subtree([&](auto& node) {
  30. if (is<HTMLInputElement>(node)) {
  31. auto& input = to<HTMLInputElement>(node);
  32. if (!input.name().is_null())
  33. parameters.append({ input.name(), input.value() });
  34. }
  35. return IterationDecision::Continue;
  36. });
  37. StringBuilder builder;
  38. for (int i = 0; i < parameters.size(); ++i) {
  39. builder.append(parameters[i].name);
  40. builder.append('=');
  41. builder.append(parameters[i].value);
  42. if (i != parameters.size() - 1)
  43. builder.append('&');
  44. }
  45. url.set_query(builder.to_string());
  46. // FIXME: We shouldn't let the form just do this willy-nilly.
  47. document().frame()->html_view()->load(url);
  48. }