HTMLFormElement.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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_of_type<HTMLInputElement>([&](auto& node) {
  30. auto& input = to<HTMLInputElement>(node);
  31. if (!input.name().is_null())
  32. parameters.append({ input.name(), input.value() });
  33. return IterationDecision::Continue;
  34. });
  35. StringBuilder builder;
  36. for (int i = 0; i < parameters.size(); ++i) {
  37. builder.append(parameters[i].name);
  38. builder.append('=');
  39. builder.append(parameters[i].value);
  40. if (i != parameters.size() - 1)
  41. builder.append('&');
  42. }
  43. url.set_query(builder.to_string());
  44. // FIXME: We shouldn't let the form just do this willy-nilly.
  45. document().frame()->html_view()->load(url);
  46. }